From 54dc0316694cc4a6c8c4f1d03f890ca1af09c15b Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 2 Dec 2019 00:23:37 -0500 Subject: Solve day 2 part 2 --- 2019/day02.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to '2019') 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 #include -int main(void) { - int v[1024] = {0}; - for (int i = 0; EOF != scanf("%d,", &v[i]); ++i); - v[1] = 12; - v[2] = 2; +#include +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; + } + } } -- cgit 1.4.1