From 6ca56a0af155f867c00c7ded859bdc787bb4a3b2 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 15 Dec 2020 00:49:30 -0500 Subject: Solve day 15 part 2 --- 2020/day15.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/2020/day15.c b/2020/day15.c index f5ee806..1a6a867 100644 --- a/2020/day15.c +++ b/2020/day15.c @@ -1,21 +1,35 @@ #include #include int main(void) { - int nums[2048]; - int len = 0; - while (0 < scanf("%d,", &nums[len])) { - len++; + static int turns[30000000]; + int turn = 1; + int last, next; + scanf("%d,", &last); + while (0 < scanf("%d,", &next)) { + turns[last] = turn; + last = next; + turn++; } - while (len < 2020) { - int last = nums[len-1]; - int next = 0; - for (int i = len - 2; i >= 0; --i) { - if (nums[i] == last) { - next = len - 1 - i; - break; - } + while (turn != 2020) { + if (turns[last]) { + next = turn - turns[last]; + } else { + next = 0; } - nums[len++] = next; + turns[last] = turn; + last = next; + turn++; } - printf("%d\n", nums[len-1]); + printf("%d\n", last); + while (turn != 30000000) { + if (turns[last]) { + next = turn - turns[last]; + } else { + next = 0; + } + turns[last] = turn; + last = next; + turn++; + } + printf("%d\n", last); } -- cgit 1.4.1