From 93734da352105b05055ed4a0114ab28f01d2108a Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 23 Dec 2017 11:40:44 +0100 Subject: [PATCH] Solution to day 23, in C. --- 2017/README.md | 2 +- 2017/day-23/.gitignore | 1 + 2017/day-23/Makefile | 5 +++ 2017/day-23/input.txt | 32 ++++++++++++++ 2017/day-23/solution.c | 98 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 2017/day-23/.gitignore create mode 100644 2017/day-23/Makefile create mode 100644 2017/day-23/input.txt create mode 100644 2017/day-23/solution.c diff --git a/2017/README.md b/2017/README.md index 1ac7519..b4a939a 100644 --- a/2017/README.md +++ b/2017/README.md @@ -8,7 +8,7 @@ The current plan, in no particular order: - [x] AWK - [Day 04](./day-04/solution.awk) - [x] Bash/shell script - [Day 02](./day-02/solution.sh) -- [ ] C +- [c] C - [Day 23](./day-23/solution.c) - [x] C++ - [Day 01](./day-01/solution.cpp) - [x] C# - [Day 07](./day-07/solution.cs) - [x] Clojure - [Day 03](./day-03/solution.clj) diff --git a/2017/day-23/.gitignore b/2017/day-23/.gitignore new file mode 100644 index 0000000..a4bd26d --- /dev/null +++ b/2017/day-23/.gitignore @@ -0,0 +1 @@ +solution diff --git a/2017/day-23/Makefile b/2017/day-23/Makefile new file mode 100644 index 0000000..15cf9cb --- /dev/null +++ b/2017/day-23/Makefile @@ -0,0 +1,5 @@ +CFLAGS=-Wall -Wextra -pedantic -O2 -g +LD_LIBS=-lm + +all: solution + ./$< diff --git a/2017/day-23/input.txt b/2017/day-23/input.txt new file mode 100644 index 0000000..274c84f --- /dev/null +++ b/2017/day-23/input.txt @@ -0,0 +1,32 @@ +set b 84 +set c b +jnz a 2 +jnz 1 5 +mul b 100 +sub b -100000 +set c b +sub c -17000 +set f 1 +set d 2 +set e 2 +set g d +mul g e +sub g b +jnz g 2 +set f 0 +sub e -1 +set g e +sub g b +jnz g -8 +sub d -1 +set g d +sub g b +jnz g -13 +jnz f 2 +sub h -1 +set g b +sub g c +jnz g 2 +jnz 1 3 +sub b -17 +jnz 1 -23 diff --git a/2017/day-23/solution.c b/2017/day-23/solution.c new file mode 100644 index 0000000..bade8a4 --- /dev/null +++ b/2017/day-23/solution.c @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +#define MAX_INSTR 1000 + +#define SETVAL + +typedef struct { + char op[4]; + char op_a[2]; + char op_b[10]; +} instr_t; + +int get_val(const char* identifier, const int* registers) +{ + if (isalpha(identifier[0])) { + return registers[identifier[0] - 'a']; + } else { + return atoi(identifier); + } +} + +int run_program(const instr_t program[], const int max_instr, int registers[]) +{ + int iptr = 0; + int muls = 0; + while (iptr < max_instr) { + instr_t const* cur = &program[iptr]; + int op_b = get_val(cur->op_b, registers); + + int* res = ®isters[cur->op_a[0] - 'a']; + + if (!strcmp(cur->op, "set")) { + *res = op_b; + } else if (!strcmp(cur->op, "sub")) { + *res -= op_b; + } else if (!strcmp(cur->op, "mul")) { + *res *= op_b; + ++muls; + } else if (!strcmp(cur->op, "jnz")) { + if (get_val(cur->op_a, registers) != 0) { + iptr += op_b; + continue; + } + } + iptr += 1; + } + + return muls; +} + +int is_prime(long long int num) { + for (long long int i = 2; i * i <= num; ++i) { + if (num % i == 0) { + return 0; + } + } + + return 1; +} + +int run_optimized() +{ + int b = 84 * 100 + 100000; + int c = b + 17000; + int h = 0; + + for (; b <= c; b += 17) { + // Test if B is prime, result = f == 1; + if (!is_prime(b)) { + h += 1; + } + } + + return h; +} + +int main() +{ + instr_t program[MAX_INSTR]; + int max_instr = 0; + + int debug_registers[8] = {0}; + + while (scanf("%s %s %s", program[max_instr].op, program[max_instr].op_a, program[max_instr].op_b) != EOF) { + ++max_instr; + } + + int muls = run_program(program, max_instr, debug_registers); + printf("Muls executed: %d\n", muls); + + printf("Final value of 'h': %d\n", run_optimized()); + + return 0; +}