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
|
#include <charconv>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
namespace {
template<typename T>
struct CPU {
[[nodiscard]]
T get(const string& ref)
{
T value{};
auto [ptr, ec] = from_chars(ref.data(), ref.data() + ref.size(), value);
if ( ec == errc() ) {
return value;
}
return memory[ref];
}
void set(const string& ref, T value)
{
memory[ref] = value;
}
void sub(const string& ref, T value)
{
memory[ref] -= value;
}
void mul(const string& ref, T value)
{
memory[ref] *= value;
}
T execute(const vector<vector<string>>& code)
{
int mul_executed = 0;
for ( size_t ip = 0; ip < code.size(); ++ip ) {
const auto& line = code.at(ip);
const auto& opcode = line.at(0);
if ( opcode == "set" ) {
set(line.at(1), get(line.at(2)));
}
else if ( opcode == "sub" ) {
sub(line.at(1), get(line.at(2)));
}
else if ( opcode == "mul" ) {
++mul_executed;
mul(line.at(1), get(line.at(2)));
}
else if ( opcode == "jnz" ) {
auto value = get(line.at(1));
if ( value != 0 ) {
auto offset = get(line.at(2));
ip += static_cast<size_t>(offset - 1);
}
}
else {
cerr << "unknown opcode: " << opcode << '\n';
return -1;
}
}
return mul_executed;
}
map<string, T> memory;
};
vector<string>
split(const string& line)
{
stringstream strm{ line };
vector<string> data;
for ( string line; strm >> line; ) {
data.emplace_back(line);
}
return data;
}
vector<vector<string>>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
vector<vector<string>> data;
for ( string line; getline(file, line); ) {
const auto parts = split(line);
data.emplace_back(parts);
}
return data;
}
void
part1(const vector<vector<string>>& code)
{
CPU<long> cpu{};
cout << "Part1: " << cpu.execute(code) << '\n';
}
void
part2(const vector<vector<string>>& code)
{
CPU<long> cpu{};
cpu.memory["a"] = 1;
// execute only initialization code
cpu.execute({ code.begin(), code.begin() + 8 });
auto b_start = cpu.memory["b"];
auto c_end = cpu.memory["c"];
auto is_prime = [](long n) {
if ( n < 2 ) {
return false;
}
for ( long i = 2; i * i <= n; ++i ) {
if ( n % i == 0 ) {
return false;
}
}
return true;
};
const long step = 17;
long memory_h = 0;
for ( long b = b_start; b <= c_end; b += step ) {
if ( !is_prime(b) ) {
++memory_h;
}
}
cout << "Part2: " << memory_h << '\n';
}
} // namespace
int
main()
{
auto instr = read_file("data/day23.txt");
part1(instr);
part2(instr);
}
|