-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-Some-Assembly-Required.cpp
More file actions
152 lines (120 loc) · 4.32 KB
/
07-Some-Assembly-Required.cpp
File metadata and controls
152 lines (120 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2023 Joe Baker (JoeBlakeB)
// Advent of Code 2015 - Day 07: Some Assembly Required
// Usage:
// scripts/cppRun.sh 2015/07-Some-Assembly-Required.cpp < 2015/inputs/07.txt
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include "utils.cpp"
class Circuit {
public:
void calculateWireStrengths(std::vector<std::string> unresolved) {
while (unresolved.size() > 0) {
unsigned int lastSize = unresolved.size();
for (auto it = unresolved.begin(); it != unresolved.end();) {
if (calculateInstruction(*it)) {
it = unresolved.erase(it);
} else { ++it; }
}
if (unresolved.size() == lastSize) {
std::cout << "\033[1;31mError: Could not resolve all signals\033[0m" << std::endl;
std::cout << "Unresolved Wires:" << std::endl;
for (std::string wire : unresolved)
std::cout << " " << wire << std::endl;
printValues();
exit(1);
}
}
}
bool calculateInstruction(std::string instruction) {
if (instruction.find("AND") != std::string::npos ||
instruction.find("OR") != std::string::npos ||
instruction.find("SHIFT") != std::string::npos) {
return calculateBinary(instruction);
} else {
return calculateUnary(instruction);
}
}
bool calculateBinary(std::string instruction) {
std::istringstream iss(instruction);
std::string inputName1, operation, inputName2, outputName;
iss >> inputName1 >> operation >> inputName2 >> outputName >> outputName;
int inputValue1 = getValue(inputName1);
int inputValue2 = getValue(inputName2);
if (inputValue1 == -1 || inputValue2 == -1) { return false; }
int outputValue;
switch (operation[0]) {
case 'O':
outputValue = inputValue1 | inputValue2;
break;
case 'A':
outputValue = inputValue1 & inputValue2;
break;
case 'L':
outputValue = inputValue1 << inputValue2;
break;
case 'R':
outputValue = inputValue1 >> inputValue2;
break;
default:
return false;
}
resolvedValues[outputName] = outputValue;
return true;
}
bool calculateUnary(std::string instruction) {
std::istringstream iss(instruction);
std::string inputName, outputName;
iss >> inputName;
bool actionNot = false;
if (inputName == "NOT") {
iss >> inputName;
actionNot = true;
}
iss >> outputName >> outputName;
int inputValue = getValue(inputName);
if (inputValue == -1) { return false; }
if (actionNot) {
inputValue = ~inputValue;
}
resolvedValues[outputName] = inputValue;
return true;
}
int getValue(std::string nameOrValue) {
bool isNumber = true;
for (char character : nameOrValue) {
isNumber &= std::isdigit(character);
}
if (isNumber) {
return std::stoi(nameOrValue);
} else if (resolvedValues.find(nameOrValue) != resolvedValues.end()) {
return resolvedValues[nameOrValue];
} else {
return -1;
}
}
void printValues() const {
for (const auto &pair : resolvedValues) {
std::cout << "Wire: " << pair.first << ", Value: " << pair.second << std::endl;
}
}
std::map<std::string, unsigned short> resolvedValues;
};
int main() {
Circuit circuit;
std::vector<std::string> input = getInputLinesVector();
circuit.calculateWireStrengths(input);
int wireA = circuit.getValue("a");
std::cout << "The signal at wireA is " << wireA << std::endl;
circuit.resolvedValues.clear();
circuit.resolvedValues["b"] = wireA;
input.erase(std::remove_if(input.begin(), input.end(),
[](const std::string &line) {
return line.find("-> b") == line.size() - 4;
}), input.end());
circuit.calculateWireStrengths(input);
std::cout << "Set wireB to part ones wireA signal, new wireA signal " << circuit.getValue("a") << std::endl;
return 0;
}