diff options
author | June McEnroe <june@causal.agency> | 2019-12-02 00:23:37 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-12-02 00:25:31 -0500 |
commit | 54dc0316694cc4a6c8c4f1d03f890ca1af09c15b (patch) | |
tree | e71c994b1e467a4abca6ef83c8cd6e0fe2e29f02 /2019 | |
parent | Solve day 2 part 1 (diff) | |
download | aoc-54dc0316694cc4a6c8c4f1d03f890ca1af09c15b.tar.gz aoc-54dc0316694cc4a6c8c4f1d03f890ca1af09c15b.zip |
Solve day 2 part 2
Diffstat (limited to '2019')
-rw-r--r-- | 2019/day02.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/2019/day02.c b/2019/day02.c index 411762f..c574b57 100644 --- a/2019/day02.c +++ b/2019/day02.c @@ -1,10 +1,7 @@ #include <stdio.h> #include <stdlib.h> -int main(void) { - int v[1024] = {0}; - for (int i = 0; EOF != scanf("%d,", &v[i]); ++i); - v[1] = 12; - v[2] = 2; +#include <string.h> +static void run(int *v) { for (int i = 0; v[i] != 99; i += 4) { switch (v[i]) { break; case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]]; @@ -12,5 +9,25 @@ int main(void) { break; default: abort(); } } +} +int main(void) { + int a[1024] = {0}; + for (int i = 0; EOF != scanf("%d,", &a[i]); ++i); + int v[1024]; + memcpy(v, a, sizeof(v)); + v[1] = 12; + v[2] = 2; + run(v); printf("%d\n", v[0]); + for (int noun = 0; noun < 100; ++noun) { + for (int verb = 0; verb < 100; ++verb) { + memcpy(v, a, sizeof(v)); + v[1] = noun; + v[2] = verb; + run(v); + if (v[0] != 19690720) continue; + printf("%d\n", 100 * noun + verb); + return 0; + } + } } |