aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-20 16:42:02 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-20 16:42:02 +0800
commitd4206dc7bb5d7fb28cf874822bc4bcc10eafd880 (patch)
tree3338b3845616af776ffa5b99baa106daf77ddc5a /src
parentef26852c7af30a86688081bbc9e4418fe6382d0d (diff)
downloadadvent-of-code-d4206dc7bb5d7fb28cf874822bc4bcc10eafd880.tar.gz
advent-of-code-d4206dc7bb5d7fb28cf874822bc4bcc10eafd880.zip
2016 day12 part1
Diffstat (limited to 'src')
-rw-r--r--src/2016/day12/README.md28
-rw-r--r--src/2016/day12/aoc.cpp102
-rw-r--r--src/2016/day12/input23
-rw-r--r--src/2016/day12/input06
4 files changed, 158 insertions, 1 deletions
diff --git a/src/2016/day12/README.md b/src/2016/day12/README.md
index e69de29..f661fc5 100644
--- a/src/2016/day12/README.md
+++ b/src/2016/day12/README.md
@@ -0,0 +1,28 @@
+--- Day 12: Leonardo's Monorail ---
+You finally reach the top floor of this building: a garden with a slanted glass ceiling. Looks like there are no more stars to be had.
+
+While sitting on a nearby bench amidst some tiger lilies, you manage to decrypt some of the files you extracted from the servers downstairs.
+
+According to these documents, Easter Bunny HQ isn't just this building - it's a collection of buildings in the nearby area. They're all connected by a local monorail, and there's another building not far from here! Unfortunately, being night, the monorail is currently not operating.
+
+You remotely connect to the monorail control systems and discover that the boot sequence expects a password. The password-checking logic (your puzzle input) is easy to extract, but the code it uses is strange: it's assembunny code designed for the new computer you just assembled. You'll have to execute the code and get the password.
+
+The assembunny code you've extracted operates on four registers (a, b, c, and d) that start at 0 and can hold any integer. However, it seems to make use of only a few instructions:
+
+cpy x y copies x (either an integer or the value of a register) into register y.
+inc x increases the value of register x by one.
+dec x decreases the value of register x by one.
+jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not zero.
+The jnz instruction moves relative to itself: an offset of -1 would continue at the previous instruction, while an offset of 2 would skip over the next instruction.
+
+For example:
+
+cpy 41 a
+inc a
+inc a
+dec a
+jnz a 2
+dec a
+The above code would set register a to 41, increase its value by 2, decrease its value by 1, and then skip the last dec a (because a is not zero, so the jnz a 2 skips it), leaving register a at 42. When you move past the last instruction, the program halts.
+
+After executing the assembunny code in your puzzle input, what value is left in register a?
diff --git a/src/2016/day12/aoc.cpp b/src/2016/day12/aoc.cpp
index c32c5b6..aac00ff 100644
--- a/src/2016/day12/aoc.cpp
+++ b/src/2016/day12/aoc.cpp
@@ -2,5 +2,105 @@
namespace aoc2016 {
-std::pair<int64_t, int64_t> day12(line_view) { return {0, 0}; }
+static int registers[4] = {0, 0, 0, 0};
+
+int& get(int i) { return registers[i]; }
+// c = a,b,c,d
+int& get(char c) { return get(c - 'a'); }
+
+struct instruction {
+ line_view todo;
+
+ instruction(line_view l) : todo(l) {}
+
+ int which() {
+ const char* p = todo.line;
+ switch (*p) {
+ case 'c':
+ return 0;
+ case 'i':
+ return 1;
+ case 'd':
+ return 2;
+ case 'j':
+ return 3;
+ default:
+ break;
+ }
+ return 4;
+ }
+};
+
+static void get_number(const char** pp, int* d) {
+ const char* p = *pp;
+ int sign = 1;
+ if (*p == '-') {
+ sign = -1;
+ p += 1;
+ }
+ while (*p >= '0' && *p <= '9') {
+ *d = *d * 10 + *p - '0';
+ p++;
+ }
+ *d *= sign;
+ *pp = p;
+}
+
+typedef void (*todo_f)(size_t*, const char*);
+void cpy(size_t* i, const char* p) {
+ int d{0};
+ if (*p >= '0' && *p <= '9') {
+ get_number(&p, &d);
+ p += 1;
+ } else {
+ d = get(*p);
+ p += 2;
+ }
+ get(*p) = d;
+ *i += 1;
+}
+
+void inc(size_t* i, const char* p) {
+ get(*p) += 1;
+ *i += 1;
+}
+
+void dec(size_t* i, const char* p) {
+ get(*p) -= 1;
+ *i += 1;
+}
+
+void jnz(size_t* i, const char* p) {
+ bool condition = *p >= '0' && *p <= '9' ? *p - '0' : get(*p) != 0;
+ if (condition) {
+ p += 2;
+ int offset{0};
+ get_number(&p, &offset);
+ *i += offset;
+ } else {
+ *i += 1;
+ }
+}
+
+void non(size_t* i, const char* p) { *i += 1; }
+
+void exec(size_t i, const std::vector<instruction>& todos) {
+ if (i < todos.size()) {
+ todo_f fs[5] = {cpy, inc, dec, jnz, non};
+ auto d = todos[i];
+ std::cout << d.todo << std::endl;
+ fs[d.which()](&i, d.todo.line + 4);
+ exec(i, todos);
+ }
+}
+
+std::pair<int64_t, int64_t> day12(line_view file) {
+ std::vector<instruction> todos;
+ per_line(file, [&todos](line_view lv) {
+ todos.emplace_back(line_view{lv.line, lv.length - 1});
+ return true;
+ });
+ // exec(0, todos);
+ return {0, 0};
+}
} // namespace aoc2016
diff --git a/src/2016/day12/input b/src/2016/day12/input
index e69de29..7b0f5e2 100644
--- a/src/2016/day12/input
+++ b/src/2016/day12/input
@@ -0,0 +1,23 @@
+cpy 1 a
+cpy 1 b
+cpy 26 d
+jnz c 2
+jnz 1 5
+cpy 7 c
+inc d
+dec c
+jnz c -2
+cpy a c
+inc a
+dec b
+jnz b -2
+cpy c b
+dec d
+jnz d -6
+cpy 19 c
+cpy 14 d
+inc a
+dec d
+jnz d -2
+dec c
+jnz c -5
diff --git a/src/2016/day12/input0 b/src/2016/day12/input0
index e69de29..01a004b 100644
--- a/src/2016/day12/input0
+++ b/src/2016/day12/input0
@@ -0,0 +1,6 @@
+cpy 41 a
+inc a
+inc a
+dec a
+jnz a 2
+dec a