about summary refs log tree commit diff
path: root/day23.cc
blob: ae9b3e6ff38f4d6964e727484d65790ceea93828 (plain) (blame)
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
#include <array>
#include <iostream>
#include <string>
#include <algorithm>
#include <variant>
#include <vector>
#include <deque>
#include <memory>

using namespace std;

int
main() {
        vector<string> code;
        for (string line; getline(cin, line); )
                code.push_back(line);

	array<long, 256> reg{ 0 };

        long p1 = 0, p2 = 0;

        for (auto ip = begin(code); ip != end(code); ip++) {
                auto is_op = [&](string p) {
                        return equal(begin(p), end(p), begin(*ip));
                };

                long tmp;
                auto r = [&](int i) -> long& {
                        if (islower((*ip)[i]))
                                return reg[uint8_t((*ip)[i])];
                        else {
                                tmp = stol(&(*ip)[i]);
                                return tmp;
                        }
                };

                if (is_op("set")) r(4) = r(6);
                if (is_op("sub")) r(4) -= r(6);
                if (is_op("mul")) r(4) *= r(6), p1++;
                if (is_op("jnz") && r(4)) advance(ip, r(6) - 1);
        }

	int c = 100'000 + 100 * stol(&code[0][6]);
	for (int x = c; x <= c + 17'000; x += 17)
		for (int i = 2; i < x; i++)
			if (x % i == 0) {
				p2++;
				break;
			}

        cout << p1 << endl << p2 << endl;	// 8281 911
}