From 2545a97506fe589917491ff9dfe3bddc0d2cde0f Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 6 Dec 2019 03:10:23 -0500 Subject: Solve day 5 part 1 --- 2019/day05.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2019/day05.c (limited to '2019') diff --git a/2019/day05.c b/2019/day05.c new file mode 100644 index 0000000..3b38124 --- /dev/null +++ b/2019/day05.c @@ -0,0 +1,38 @@ +#include +#include + +static int input(void) { + return 1; +} + +static void output(int x) { + printf("%d\n", x); +} + +static int ip; +static int mem[1024]; + +static int *p(int mode, int param) { + return (mode ? &mem[ip + param] : &mem[mem[ip + param]]); +} + +static void run(void) { + while (mem[ip] != 99) { + int op = mem[ip] % 100; + int m1 = mem[ip] / 100 % 10; + int m2 = mem[ip] / 1000 % 10; + int m3 = mem[ip] / 10000 % 10; + switch (op) { + break; case 1: *p(m3, 3) = *p(m1, 1) + *p(m2, 2); ip += 4; + break; case 2: *p(m3, 3) = *p(m1, 1) * *p(m2, 2); ip += 4; + break; case 3: *p(m1, 1) = input(); ip += 2; + break; case 4: output(*p(m1, 1)); ip += 2; + break; default: abort(); + } + } +} + +int main(void) { + for (int i = 0; EOF != scanf("%d,", &mem[i]); ++i); + run(); +} -- cgit 1.4.1