#include using namespace std; enum HOOKIES { // PARENTHESIS = 3, // BRACKET = 57, // ACCOLADE = 1197, // POINTY_BRACKET = 25137, PARENTHESIS = 1, BRACKET = 2, ACCOLADE = 3, POINTY_BRACKET = 4, }; unsigned int line_score_corrupted(const string& s){ stack chars; unsigned int score = 0; for(auto c : s){ if(c == '(' || c == '[' || c == '{' || c == '<'){ if(c == '('){ chars.push(PARENTHESIS); } else if(c == '['){ chars.push(BRACKET); } else if(c == '{'){ chars.push(ACCOLADE); } else{ chars.push(POINTY_BRACKET); } } else if(c == ')' || c == ']' || c == '}' || c == '>'){ if(chars.empty()){ score = 0; break; } if(c == ')'){ if(chars.top() == PARENTHESIS){ chars.pop(); } else{ score = PARENTHESIS; break; } } else if(c == ']'){ if(chars.top() == BRACKET){ chars.pop(); } else{ score = BRACKET; break; } } else if(c == '}'){ if(chars.top() == ACCOLADE){ chars.pop(); } else{ score = ACCOLADE; break; } } else{ if(chars.top() == POINTY_BRACKET){ chars.pop(); } else{ score = POINTY_BRACKET; break; } } } else{ break; } } return score; } unsigned int line_score_missing(const string& s){ stack chars; bool corrupted = false; for(auto c : s){ if(c == '(' || c == '[' || c == '{' || c == '<'){ if(c == '('){ chars.push(PARENTHESIS); } else if(c == '['){ chars.push(BRACKET); } else if(c == '{'){ chars.push(ACCOLADE); } else{ chars.push(POINTY_BRACKET); } } else if(c == ')' || c == ']' || c == '}' || c == '>'){ if(chars.empty()){ corrupted = true; break; } if(c == ')'){ if(chars.top() == PARENTHESIS){ chars.pop(); } else{ corrupted = true; break; } } else if(c == ']'){ if(chars.top() == BRACKET){ chars.pop(); } else{ corrupted = true; break; } } else if(c == '}'){ if(chars.top() == ACCOLADE){ chars.pop(); } else{ corrupted = true; break; } } else{ if(chars.top() == POINTY_BRACKET){ chars.pop(); } else{ corrupted = true; break; } } } else{ corrupted = true; break; } } unsigned int score = 0; if(!corrupted){ while(!chars.empty()){ auto h = chars.top(); score = score * 5 + h; chars.pop(); } return score; } return score; } int main(){ ifstream input_file("input.txt"); string line; unsigned int total_score = 0; if(input_file.is_open()){ while(getline(input_file, line)){ // total_score += line_score_corrupted(line); total_score += line_score_missing(line); } } cout << "Total syntax score: " << total_score << endl; return 0; }