-
Notifications
You must be signed in to change notification settings - Fork 2
/
25.cpp
166 lines (147 loc) · 3.63 KB
/
25.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <chrono>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
enum OpCode {
INC,
DEC,
COPY,
JNZ,
OUT,
};
struct Instruction {
OpCode op;
int a;
int b;
};
int run_vm(int registers[4], const vector<Instruction>& instructions) {
auto ip = instructions.begin();
auto end = instructions.end();
bool out = true;
int nsignals = 0;
while (ip != end) {
const Instruction& ins = *ip;
switch (ins.op) {
case OpCode::COPY: {
int value;
if (ins.a & (1 << 31)) {
value = registers[ins.a & 0xFF];
} else {
value = ins.a;
}
registers[ins.b] = value;
break;
}
case OpCode::INC: {
registers[ins.a] += 1;
break;
}
case OpCode::DEC: {
registers[ins.a] -= 1;
break;
}
case OpCode::JNZ: {
int value;
if (ins.a & (1 << 31)) {
value = registers[ins.a & 0xFF];
} else {
value = ins.a;
}
if (value != 0) {
ip += ins.b;
continue;
}
break;
}
case OpCode::OUT: {
int value = registers[ins.a & 0xFF];
if (value != !out) {
return false;
}
if (++nsignals > 8) {
return true;
}
out = !out;
break;
}
}
ip += 1;
}
return false;
}
vector<Instruction> parse_input() {
vector<Instruction> instructions;
string line;
while (std::getline(std::cin, line)) {
switch (line[0]) {
case 'c': {
size_t pos = line.find(' ') + 1;
int value;
// if this refers to a register address
// set the first bit to 1
if (line[pos] >= 'a' && line[pos] <= 'd') {
value = (line[pos] - 'a') | (1 << 31);
} else {
value = std::stoi(&line[pos]);
}
pos = line.find(' ', pos) + 1;
instructions.push_back(
Instruction{OpCode::COPY, value, line[pos] - 'a'});
break;
}
case 'i': {
size_t pos = line.find(' ') + 1;
instructions.push_back(Instruction{OpCode::INC, line[pos] - 'a', 0});
break;
}
case 'd': {
size_t pos = line.find(' ') + 1;
instructions.push_back(Instruction{OpCode::DEC, line[pos] - 'a', 0});
break;
}
case 'j': {
size_t pos = line.find(' ') + 1;
int v1;
if (line[pos] >= 'a' && line[pos] <= 'd') {
v1 = (line[pos] - 'a') | (1 << 31);
} else {
v1 = std::stoi(&line[pos]);
}
pos = line.find(' ', pos) + 1;
int v2 = std::stoi(&line[pos]);
instructions.push_back(Instruction{OpCode::JNZ, v1, v2});
break;
}
case 'o': {
size_t pos = line.find(' ') + 1;
int v1 = (line[pos] - 'a') | (1 << 31);
instructions.push_back(Instruction{OpCode::OUT, v1, 0});
break;
}
}
}
return instructions;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
vector<Instruction> instructions = parse_input();
for (int i = 0; i < 1000; i++) {
int registers_pt1[4] = {i, 0, 0, 0};
if (run_vm(registers_pt1, instructions)) {
pt1 = i;
break;
}
}
std::cout << "--- Day 25: Clock Signal ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << duration.count() << " μs"
<< "\n";
return EXIT_SUCCESS;
}