diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-27 23:31:34 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-27 23:31:34 +0800 |
commit | 12a9e8f7fe9c11efc5bd24e288af19d94e3466de (patch) | |
tree | b071bde44db6354338fa287065dede6173ca7435 | |
parent | 505fa24b22d0093516ed3d725bec68912e0f878a (diff) | |
download | advent-of-code-12a9e8f7fe9c11efc5bd24e288af19d94e3466de.tar.gz advent-of-code-12a9e8f7fe9c11efc5bd24e288af19d94e3466de.zip |
2016 day23 part1
-rw-r--r-- | src/2016/day12/aoc.cpp | 10 | ||||
-rw-r--r-- | src/2016/day23/README.md | 11 | ||||
-rw-r--r-- | src/2016/day23/aoc.cpp | 65 | ||||
-rw-r--r-- | src/2016/day23/input0 | 7 | ||||
-rw-r--r-- | test/test_2016.cpp | 2 |
5 files changed, 67 insertions, 28 deletions
diff --git a/src/2016/day12/aoc.cpp b/src/2016/day12/aoc.cpp index 2b853ec..38b4019 100644 --- a/src/2016/day12/aoc.cpp +++ b/src/2016/day12/aoc.cpp @@ -85,12 +85,10 @@ static void jnz(size_t* i, const char* p) { static void non(size_t* i, const char* p) { *i += 1; } static size_t 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); - } + 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); return i; } diff --git a/src/2016/day23/README.md b/src/2016/day23/README.md index 1423adb..ce382b2 100644 --- a/src/2016/day23/README.md +++ b/src/2016/day23/README.md @@ -41,3 +41,14 @@ The rest of the electronics seem to place the keypad entry (the number of eggs, What value should be sent to the safe? +--- Part Two --- + +The safe doesn't open, but it does make several angry noises to express its frustration. + +You're quite sure your logic is working correctly, so the only other thing is... you check the painting again. As it turns out, colored eggs are still eggs. Now you count 12. + +As you run the program with this new input, the prototype computer begins to overheat. You wonder what's taking so long, and whether the lack of any instruction more powerful than "add one" has anything to do with it. Don't bunnies usually multiply? + +Anyway, what value should actually be sent to the safe? + + diff --git a/src/2016/day23/aoc.cpp b/src/2016/day23/aoc.cpp index 9b6a6ba..2a73886 100644 --- a/src/2016/day23/aoc.cpp +++ b/src/2016/day23/aoc.cpp @@ -15,23 +15,24 @@ struct instruction23 { instruction23(line_view l) : todo(l) {} int which() { - std::cout << todo << std::endl; + // std::cout << todo << std::endl; const char* p = todo.line; + bool toggle_is_odd = toggles & 1; switch (*p) { case 'c': - return 0; + return toggle_is_odd ? 3 : 0; case 'i': - return 1; + return toggle_is_odd ? 2 : 1; case 'd': - return 2; + return toggle_is_odd ? 1 : 2; case 'j': - return 3; + return toggle_is_odd ? 0 : 3; case 't': - return 4; + return toggle_is_odd ? 1 : 4; default: break; } - return 4; + return 5; } }; @@ -53,14 +54,16 @@ static void get_number(const char** pp, int* d) { typedef void (*todo_f)(size_t*, const char*, const std::vector<instruction23>&); static void cpy(size_t* i, const char* p, const std::vector<instruction23>& todos) { int d{0}; - if (*p >= '0' && *p <= '9') { - get_number(&p, &d); - p += 1; - } else { + if (*p >= 'a' && *p <= 'd') { d = get(*p); p += 2; + } else { + get_number(&p, &d); + p += 1; + } + if (*p >= 'a' && *p <= 'd') { + get(*p) = d; } - get(*p) = d; *i += 1; } @@ -75,11 +78,25 @@ static void dec(size_t* i, const char* p, const std::vector<instruction23>& todo } static void jnz(size_t* i, const char* p, const std::vector<instruction23>& todos) { - bool condition = *p >= '0' && *p <= '9' ? *p - '0' : get(*p) != 0; + int d{0}; + bool condition = false; + bool is_number = false; + if (*p >= '0' && *p <= '9') { + is_number = true; + get_number(&p, &d); + condition = d != 0; + } else { + condition = get(*p) != 0; + } + if (condition) { - p += 2; + p += is_number ? 1 : 2; int offset{0}; - get_number(&p, &offset); + if (*p >= 'a' && *p <= 'd') { + offset = get(*p); + } else { + get_number(&p, &offset); + } *i += offset; } else { *i += 1; @@ -97,11 +114,17 @@ static void tgl(size_t* i, const char* p, const std::vector<instruction23>& todo static void non(size_t* i, const char* p, const std::vector<instruction23>&) { *i += 1; } static size_t exec(size_t i, const std::vector<instruction23>& todos) { - if (i < todos.size()) { - todo_f fs[6] = {cpy, inc, dec, jnz, tgl, non}; - auto d = todos[i]; - fs[d.which()](&i, d.todo.line + 4, todos); - } + todo_f fs[6] = {cpy, inc, dec, jnz, tgl, non}; + // const char *ds[6] = {"cpy", "inc", "dec", "jnz", "tgl", "non"}; + auto d = todos[i]; + auto x = d.which(); + + // char param[30] = {0}; + // memcpy(param, d.todo.line + 4, d.todo.length - 4); + // printf("%s %s\n", ds[x], param); + + fs[x](&i, d.todo.line + 4, todos); + // printf("%d %d %d %d\n", get('a'), get('b'), get('c'), get('d')); return i; } @@ -116,6 +139,6 @@ std::pair<int64_t, int64_t> day23(line_view file) { i = exec(i, todos); } - return {0, 0}; + return {get('a'), 0}; } } // namespace aoc2016 diff --git a/src/2016/day23/input0 b/src/2016/day23/input0 index e69de29..bcabc2c 100644 --- a/src/2016/day23/input0 +++ b/src/2016/day23/input0 @@ -0,0 +1,7 @@ +cpy 2 a +tgl a +tgl a +tgl a +cpy 1 a +dec a +dec a diff --git a/test/test_2016.cpp b/test/test_2016.cpp index 5fa17e7..76bf2e9 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -207,7 +207,7 @@ TEST_CASE("Grid Computing", "[2016]") { TEST_CASE("Safe Cracking", "[2016]") { line_view lv = load_file("../src/2016/day23/input"); auto p = aoc2016::day23(lv); - REQUIRE(0 == p.first); + REQUIRE(13140 == p.first); REQUIRE(0 == p.second); } |