diff options
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; + } + } } |