From 234def612fc4514e0a5ef4b43f2c4c6095b76c24 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 9 Dec 2020 00:20:49 -0500 Subject: Solve day 9 part 1 --- 2020/day09.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2020/day09.c diff --git a/2020/day09.c b/2020/day09.c new file mode 100644 index 0000000..f67fb4e --- /dev/null +++ b/2020/day09.c @@ -0,0 +1,28 @@ +#include +#include +enum { Cap = 25 }; +static int ring[Cap]; +static size_t index; +static void push(int x) { + ring[index++ % Cap] = x; +} +static int valid(int x) { + for (int i = 0; i < Cap; ++i) { + for (int j = 0; j < Cap; ++j) { + if (ring[i] == ring[j]) continue; + if (ring[i] + ring[j] == x) return 1; + } + } + return 0; +} +int main(void) { + int x; + while (EOF != scanf("%d\n", &x)) { + if (index < Cap || valid(x)) { + push(x); + } else { + printf("%d\n", x); + break; + } + } +} -- cgit 1.4.1