-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path12.cpp
144 lines (124 loc) · 3.23 KB
/
12.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
#include <chrono>
#include <iostream>
#include <vector>
using std::string;
using size_type = std::string::size_type;
using std::vector;
enum OpCode {
INC,
DEC,
COPY,
JNZ,
};
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();
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;
}
}
ip += 1;
}
return registers[0];
}
vector<Instruction> parse_input() {
vector<Instruction> instructions;
string line;
while (std::getline(std::cin, line)) {
switch (line[0]) {
case 'c': {
size_type 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_type pos = line.find(' ') + 1;
instructions.push_back(Instruction{OpCode::INC, line[pos] - 'a', 0});
break;
}
case 'd': {
size_type pos = line.find(' ') + 1;
instructions.push_back(Instruction{OpCode::DEC, line[pos] - 'a', 0});
break;
}
case 'j':
size_type 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;
}
}
return instructions;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
vector<Instruction> instructions = parse_input();
int registers_pt1[4] = {0, 0, 0, 0};
pt1 = run_vm(registers_pt1, instructions);
int registers_pt2[4] = {0, 0, 1, 0};
pt2 = run_vm(registers_pt2, instructions);
std::cout << "--- Day 12: Leonardo's Monorail ---\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;
}