about summary refs log tree commit diff
path: root/day23.cc
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-12-25 13:29:16 +0100
committerLeah Neukirchen <leah@vuxu.org>2017-12-25 13:29:16 +0100
commit8f688120afe73ec3b25d31294b5886f5651bde2e (patch)
tree5381345f0929f52077387ef192789a0b994eb44d /day23.cc
parent11269ed8c1be4af3b0bb8c355257481215b7dc9b (diff)
downloadadventofcode2017-8f688120afe73ec3b25d31294b5886f5651bde2e.tar.gz
adventofcode2017-8f688120afe73ec3b25d31294b5886f5651bde2e.tar.xz
adventofcode2017-8f688120afe73ec3b25d31294b5886f5651bde2e.zip
day23
Diffstat (limited to 'day23.cc')
-rw-r--r--day23.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/day23.cc b/day23.cc
new file mode 100644
index 0000000..ae9b3e6
--- /dev/null
+++ b/day23.cc
@@ -0,0 +1,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
+}