aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day23/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2016/day23/aoc.cpp')
-rw-r--r--src/2016/day23/aoc.cpp65
1 files changed, 44 insertions, 21 deletions
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