|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <sstream> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +// global vars |
| 10 | +vector<unsigned long long> operands; |
| 11 | +vector<char> operators; |
| 12 | +unsigned long long output, counter1, counter2; |
| 13 | + |
| 14 | +// get value of current expression (single check in DFS) |
| 15 | +unsigned long long evaluate(bool part2) |
| 16 | +{ |
| 17 | + unsigned long long value = operands[0], temp; |
| 18 | + |
| 19 | + for (int i = 1; i < operands.size(); i++) |
| 20 | + switch (operators[i-1]) |
| 21 | + { |
| 22 | + case '+': |
| 23 | + value += operands[i]; |
| 24 | + break; |
| 25 | + |
| 26 | + case '*': |
| 27 | + value *= operands[i]; |
| 28 | + break; |
| 29 | + |
| 30 | + // operator is only valid for part 2 |
| 31 | + case '|': |
| 32 | + if (!part2) break; |
| 33 | + |
| 34 | + temp = operands[i]; |
| 35 | + while (temp > 0) |
| 36 | + { |
| 37 | + temp /= 10; |
| 38 | + value *= 10; |
| 39 | + } |
| 40 | + value += operands[i]; |
| 41 | + break; |
| 42 | + |
| 43 | + default: |
| 44 | + break; |
| 45 | + } |
| 46 | + |
| 47 | + return value; |
| 48 | +} |
| 49 | + |
| 50 | +// there definitely exists a faster way to do this (single DFS for both parts w/ diff evaluation at end) |
| 51 | +// but will take time to implement from starting point of part 1 solution and solution still runs quickly |
| 52 | +bool dfsSearch(int index, bool part2) |
| 53 | +{ |
| 54 | + // leaf node |
| 55 | + if (index == operators.size()) |
| 56 | + { |
| 57 | + if (evaluate(part2) == output) return true; |
| 58 | + else return false; |
| 59 | + } |
| 60 | + |
| 61 | + // branch deeper (try both operators) |
| 62 | + bool answer = false; |
| 63 | + operators[index] = '+'; |
| 64 | + answer |= dfsSearch(index + 1, part2); |
| 65 | + operators[index] = '*'; |
| 66 | + answer |= dfsSearch(index + 1, part2); |
| 67 | + operators[index] = '|'; |
| 68 | + answer |= dfsSearch(index + 1, part2); |
| 69 | + |
| 70 | + // could not find a combo of operators that worked |
| 71 | + return answer; |
| 72 | +} |
| 73 | + |
| 74 | +int main() |
| 75 | +{ |
| 76 | + // setup |
| 77 | + ifstream in("day7.in"); |
| 78 | + counter1 = counter2 = 0; |
| 79 | + |
| 80 | + // read in cases |
| 81 | + string temp; |
| 82 | + unsigned long long tempNum; |
| 83 | + while (!in.eof()) |
| 84 | + { |
| 85 | + // setup |
| 86 | + operands.clear(); |
| 87 | + operators.clear(); |
| 88 | + |
| 89 | + // input |
| 90 | + getline(in, temp); |
| 91 | + if (in.eof()) break; |
| 92 | + temp.erase(temp.find(':'), 1); |
| 93 | + |
| 94 | + stringstream ss(temp); |
| 95 | + ss >> output; |
| 96 | + while (ss >> tempNum) operands.push_back(tempNum); |
| 97 | + |
| 98 | + // process |
| 99 | + for (int i = 0; i < operands.size() - 1; i++) operators.push_back(' '); |
| 100 | + |
| 101 | + if (dfsSearch(0, false)) counter1 += output; |
| 102 | + if (dfsSearch(0, true)) counter2 += output; |
| 103 | + } |
| 104 | + |
| 105 | + // output |
| 106 | + cout << "Part 1: " << counter1 << "\nPart 2: " << counter2 << endl; |
| 107 | + |
| 108 | + // close out |
| 109 | + in.close(); |
| 110 | + return 0; |
| 111 | +} |
0 commit comments