mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Solution to day 23, in C.
This commit is contained in:
@@ -8,7 +8,7 @@ The current plan, in no particular order:
|
|||||||
|
|
||||||
- [x] AWK - [Day 04](./day-04/solution.awk)
|
- [x] AWK - [Day 04](./day-04/solution.awk)
|
||||||
- [x] Bash/shell script - [Day 02](./day-02/solution.sh)
|
- [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 01](./day-01/solution.cpp)
|
||||||
- [x] C# - [Day 07](./day-07/solution.cs)
|
- [x] C# - [Day 07](./day-07/solution.cs)
|
||||||
- [x] Clojure - [Day 03](./day-03/solution.clj)
|
- [x] Clojure - [Day 03](./day-03/solution.clj)
|
||||||
|
|||||||
1
2017/day-23/.gitignore
vendored
Normal file
1
2017/day-23/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
solution
|
||||||
5
2017/day-23/Makefile
Normal file
5
2017/day-23/Makefile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
CFLAGS=-Wall -Wextra -pedantic -O2 -g
|
||||||
|
LD_LIBS=-lm
|
||||||
|
|
||||||
|
all: solution
|
||||||
|
./$<
|
||||||
32
2017/day-23/input.txt
Normal file
32
2017/day-23/input.txt
Normal file
@@ -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
|
||||||
98
2017/day-23/solution.c
Normal file
98
2017/day-23/solution.c
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user