#include<iostream>
#include<fstream>
#include<vector>
#include<sstream>
#include<cstring>

using namespace std;

int loc(vector<string> v, string word) {

    for (int i = 0; i<v.size();i++) {

        if (v[i]==word) {

            return i;

        }

    }

    return -1;

}

int main(int count, char** args) {

    if (!strcmp("A",args[1])) {

            ifstream myfile;
    
      myfile.open(args[3]);

      string line;

      float gate_delay[5];

      while(getline(myfile,line)){

        stringstream ss(line);

        string word;

        ss>>word;

        if (word == "AND2") {

            ss>>word;

            gate_delay[0] = stof(word);

        }

        else if (word == "NAND2") {

            ss>>word;

            gate_delay[1] = stof(word);

        }

        else if (word == "OR2") {

            ss>>word;

            gate_delay[2] = stof(word);

        }

        else if (word == "NOR2") {

            ss>>word;

            gate_delay[3] = stof(word);

        }

        else if (word == "INV") {

            ss>>word;

            gate_delay[4] = stof(word);

        }
    
     };

     myfile.close();

     vector<string> signame;

    vector<float> sigtime;

    vector<int> sigtype;

    myfile.open(args[2]);

    while(getline(myfile,line)) {

        stringstream ss(line);

        string word;

        ss>>word;

        if (word=="PRIMARY_INPUTS") {

            while(ss>>word) {

                signame.push_back(word);

                sigtype.push_back(0);

                sigtime.push_back(0);

            }

        }

        else if (word=="PRIMARY_OUTPUTS") {

            while(ss>>word) {

                signame.push_back(word);

                sigtype.push_back(-2);

                sigtime.push_back(0);

            }

        }

        else if (word=="INTERNAL_SIGNALS") {

            while(ss>>word) {

                signame.push_back(word);

                sigtype.push_back(-1);

                sigtime.push_back(0);

            }

        }

    }

    myfile.close();

    bool done = false;

    while(!done) {

        myfile.open(args[2]);

        while(getline(myfile,line)) {

        stringstream ss(line);

        string word;

        string s1,s2,s3;

        int n1,n2,n3;

        ss>>word;

        if (word=="AND2") {

            ss>>s1;

            ss>>s2;

            ss>>s3;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            n3 = loc(signame,s3);

            if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) {

                sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[0];

                sigtype[n3] = -sigtype[n3];

            }

        }

        if (word=="NAND2") {

            ss>>s1;

            ss>>s2;

            ss>>s3;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            n3 = loc(signame,s3);

            if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) {

                sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[1];

                sigtype[n3] = -sigtype[n3];

            }

        }

        if (word=="OR2") {

            ss>>s1;

            ss>>s2;

            ss>>s3;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            n3 = loc(signame,s3);

            if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) {

                sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[2];

                sigtype[n3] = -sigtype[n3];

            }

        }

        if (word=="NOR2") {

            ss>>s1;

            ss>>s2;

            ss>>s3;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            n3 = loc(signame,s3);

            if (sigtype[n1] >= 0 && sigtype[n2] >= 0 && sigtype[n3] < 0) {

                sigtime[n3] = max(sigtime[n1],sigtime[n2]) + gate_delay[3];

                sigtype[n3] = -sigtype[n3];

            }

        }

        if (word=="INV") {

            ss>>s1;

            ss>>s2;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            if (sigtype[n1] >= 0 && sigtype[n2] < 0) {

                sigtime[n2] = (sigtime[n1]) + gate_delay[4];

                sigtype[n2] = -sigtype[n2];

            }

        }

        }

        myfile.close();

        done = true;

        for (int i = 0; i < signame.size(); i++) {

            if (sigtype[i]<0) {

                done = false;

                break;

            }

        }

    }

    ofstream outputter;

    outputter.open("output_delays.txt",ios::trunc);

    for (int i = 0; i < signame.size(); i++) {

        if (sigtype[i]==2) {

            outputter<<signame[i]<<" "<<sigtime[i]<<endl;

        }

    }

    outputter.close();

    }

    else if (!strcmp("B",args[1])) {

          vector<string> gates = {"AND2","NAND2","OR2","NOR2","INV"};

    ifstream myfile;
    
    myfile.open(args[3]);

    string line;

    float gate_delay[5];

    while(getline(myfile,line)){

        stringstream ss(line);

        string word;

        ss>>word;

        if (word == "AND2") {

            ss>>word;

            gate_delay[0] = stof(word);

        }

        else if (word == "NAND2") {

            ss>>word;

            gate_delay[1] = stof(word);

        }

        else if (word == "OR2") {

            ss>>word;

            gate_delay[2] = stof(word);

        }

        else if (word == "NOR2") {

            ss>>word;

            gate_delay[3] = stof(word);

        }

        else if (word == "INV") {

            ss>>word;

            gate_delay[4] = stof(word);

        }
    
    };

    myfile.close();

    vector<string> signame;

    vector<string> inputs;

    vector<float> sigtime;

    vector<int> sigtype;

    myfile.open(args[2]);

    while(getline(myfile,line)) {

        stringstream ss(line);

        string word;

        ss>>word;

        if (word=="PRIMARY_INPUTS") {

            while(ss>>word) {

                signame.push_back(word);

                inputs.push_back(word);

                sigtype.push_back(1);

                sigtime.push_back(0);

            }

        }

        else if (word=="PRIMARY_OUTPUTS") {

            while(ss>>word) {

                signame.push_back(word);

                sigtype.push_back(0);

                sigtime.push_back(0);

            }

        }

        else if (word=="INTERNAL_SIGNALS") {

            while(ss>>word) {

                signame.push_back(word);

                sigtype.push_back(1);

                sigtime.push_back(0);

            }

        }

    }

    myfile.close();

    myfile.open(args[4]);

    float m;

    while(getline(myfile,line)) {

        stringstream ss(line);

        string word;

        string s1;

        while(ss>>word) {

            int n1 = loc(signame,word);

            if (n1>=0) {

                ss>>s1;

                sigtime[n1] = stof(s1);

                m = max(m,stof(s1));

            }

        }

    }

    for(int i = 0; i < sigtime.size(); i++) {

        if (sigtime[i] == 0) {

        sigtime[i] = m;

        }

    }

    myfile.close();

    bool done = false;

    while(!done) {

        myfile.open(args[2]);

        while(getline(myfile,line)) {

        stringstream ss(line);

        string word;

        string s1,s2,s3;

        int n1,n2,n3;

        ss>>word;

        if (word=="AND2" || word=="NAND2" || word == "OR2" || word == "NOR2") {

            ss>>s1;

            ss>>s2;

            ss>>s3;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            n3 = loc(signame,s3);

            sigtime[n1] = min(sigtime[n1],sigtime[n3]-gate_delay[loc(gates,word)]);

            sigtime[n2] = min(sigtime[n2],sigtime[n3]-gate_delay[loc(gates,word)]);

            sigtype[n1]+=sigtype[n3];

            sigtype[n2]+=sigtype[n3];

        }

        else if (word == "INV") {

            ss>>s1;

            ss>>s2;

            n1 = loc(signame,s1);

            n2 = loc(signame,s2);

            sigtime[n1] = min(sigtime[n1],sigtime[n2]-gate_delay[loc(gates,word)]);

            sigtype[n1]+=sigtype[n2];

        }

        }

        for(int i = 0; i < signame.size(); i++) {

            if (sigtype[i]==1) {

                sigtype[i] = 0;

            }

            if (sigtype[i] > 1) {

                sigtype[i] = 1;

                sigtime[i] = m;

            }

        }

        myfile.close();

        done = true;

        for (int i = 0; i < signame.size(); i++) {

            if (sigtype[i]!=0) {

                done = false;

                break;

            }

        }

    }

    ofstream outputter;

    outputter.open("input_delays.txt", ios::trunc);

    for (string word : inputs) {

        outputter<<word<<" "<<sigtime[loc(signame,word)]<<endl;

    }

    outputter.close();

    }

} change this code in python.