Day20 Day21 C WIP

This commit is contained in:
dobiadi
2023-12-24 13:01:39 +01:00
parent 1c22f73a2b
commit 1f855400aa
9 changed files with 516 additions and 0 deletions

BIN
day20/c/day20 Executable file
View File

Binary file not shown.

204
day20/c/day20.c Normal file
View File

@@ -0,0 +1,204 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_MAX_LENGTH 256
#define MAX_CONNECTED 16
#define MAX_MODULES 128
#define MAX_QUEUE 1024
typedef enum module_type {
FLIPFLOP,
CONJUNCTION,
BROADCAST,
} module_type_t;
typedef struct module {
module_type_t type;
char name[16];
int name_length;
int state;
struct module *inputs[MAX_CONNECTED];
int input_memory[MAX_CONNECTED];
int input_num;
struct module *outputs[MAX_CONNECTED];
int output_num;
char output_names[MAX_CONNECTED][16];
int output_name_lengths[MAX_CONNECTED];
} module_t;
typedef struct signal {
int value;
module_t *module;
module_t *source;
} signal_t;
int main() {
char *p, *buf, c, *q;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
module_t *modules = (module_t*)malloc(MAX_MODULES * sizeof(module_t));
memset(modules, 0, MAX_MODULES * sizeof(module_t));
int modules_num = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
if (*p == '%' || *p == '&' || *p == '#') {
switch (*p) {
case '%':
modules[modules_num].type = FLIPFLOP;
break;
case '&':
modules[modules_num].type = CONJUNCTION;
break;
}
p++;
} else {
modules[modules_num].type = BROADCAST;
}
q = p;
while(*p != ' ') p++;
strncpy(modules[modules_num].name, q, p - q);
modules[modules_num].name_length = p - q;
p++;
while(*p != ' ') p++;
p++;
// Read outputs
while(*p != '\0') {
q = p;
while (*p != '\n' && *p != ',') p++;
strncpy(modules[modules_num].output_names[modules[modules_num].output_num], q, p - q);
modules[modules_num].output_name_lengths[modules[modules_num].output_num] = p - q;
modules[modules_num].output_num++;
p+=2;
}
modules_num++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
// Link up modules
for (int i = 0; i < modules_num; i++) {
// Link outputs
int found = 0;
for (int j = 0; j < modules[i].output_num; j++) {
for (int k = 0; k < modules_num; k++) {
if (!strcmp(modules[k].name, modules[i].output_names[j])) {
modules[i].outputs[j] = &modules[k];
found++;
break;
}
}
}
// Link inputs
for (int j = 0; j < modules_num; j++) {
for (int k = 0; k < modules[j].output_num; k++) {
if (!strcmp(modules[i].name, modules[j].output_names[k])) {
modules[i].inputs[modules[i].input_num] = &modules[j];
modules[i].input_num++;
break;
}
}
}
}
// Find start
module_t *broadcast;
for (int i = 0; i < modules_num; i++) {
if (modules[i].type == BROADCAST) {
broadcast = &modules[i];
break;
}
}
signal_t signal_queue[MAX_QUEUE];
unsigned long low_pulses = 0, high_pulses = 0;
int turned_on = 0;
#ifdef PART2
for(unsigned long l = 0; !turned_on; l++) {
#else
for (unsigned long l = 0; l < 1000; l++) {
#endif
memset(signal_queue, 0, MAX_QUEUE * sizeof(signal_t));
int signal_queue_length = 1;
signal_queue[0].value = 0;
signal_queue[0].module = broadcast;
signal_queue[0].source = NULL;
while (signal_queue_length > 0) {
// Pop first
signal_t signal = signal_queue[0];
for (int i = 0; i < signal_queue_length - 1; i++) {
memcpy(&signal_queue[i], &signal_queue[i+1], sizeof(signal_t));
}
signal_queue_length--;
int output;
if (signal.value) {
high_pulses++;
} else {
low_pulses++;
}
if (signal.module == NULL) {
if (signal.value == 0) {
printf("Machine turn on: %lu\n", l+1);
turned_on = 1;
break;
}
continue;
}
switch (signal.module->type) {
case FLIPFLOP:
if (signal.value == 0) {
signal.module->state = !signal.module->state;
output = signal.module->state;
} else {
output = -1;
}
break;
case BROADCAST:
output = signal.value;
break;
case CONJUNCTION:
// Update input memory
for (int i = 0; i < signal.module->input_num; i++) {
if (signal.module->inputs[i] == signal.source) {
signal.module->input_memory[i] = signal.value;
}
}
int all_high = 1;
for (int i = 0; i < signal.module->input_num; i++) {
if (signal.module->input_memory[i] == 0) {
all_high = 0;
break;
}
}
output = !all_high;
break;
}
if (output != -1) {
for (int i = 0; i < signal.module->output_num; i++) {
signal_queue[signal_queue_length].value = output;
signal_queue[signal_queue_length].module = signal.module->outputs[i];
signal_queue[signal_queue_length].source = signal.module;
signal_queue_length++;
}
}
}
}
printf("%lu\n", high_pulses * low_pulses);
free(buf);
free(modules);
}

58
day20/input.txt Normal file
View File

@@ -0,0 +1,58 @@
%db -> cq
%rj -> gp, nd
%ff -> bk
%rc -> gp
%bk -> tv
%xz -> tf, bn
%gs -> bn
%ps -> rs, gp
%jr -> gp, cg
&pm -> vf
%pn -> pp, rt
%nv -> jr
%rs -> nv
%kz -> mj
%nd -> rc, gp
%nm -> rt, db
%dg -> rt, xl
%vg -> gn
%hc -> vr
%ft -> lf, bn
%mj -> hc, cz
%vb -> ft
%qd -> cz, sz
%pp -> rt
%cq -> rt, vg
%sr -> vb
%lf -> vx, bn
%lh -> pn, rt
%ls -> sl, cz
%tv -> gp, rj
%tf -> sr, bn
&mk -> vf
%bs -> rt, lh
%vx -> bn, gs
&bn -> fs, bv, vb, mk, sr, bz, cf
%rr -> ls
%bv -> xz
%hp -> bs, rt
&pk -> vf
%cg -> rq
%gn -> rt, dg
&cz -> hc, kz, rr, hf, sh
%sl -> cz, kz
broadcaster -> sh, nm, ps, fs
%cf -> bv
&vf -> rx
&rt -> pk, xl, nm, vg, db
%xl -> hp
%sh -> rr, cz
%bz -> cf
%fz -> dn, cz
&gp -> rs, nv, pm, cg, ff, bk, ps
%fs -> bz, bn
&hf -> vf
%vr -> cz, qd
%rq -> gp, ff
%sz -> cz, fz
%dn -> cz

5
day20/sample.txt Normal file
View File

@@ -0,0 +1,5 @@
broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a

5
day20/sample2.txt Normal file
View File

@@ -0,0 +1,5 @@
broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output

BIN
day21/c/day21 Executable file
View File

Binary file not shown.

102
day21/c/day21.c Normal file
View File

@@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_MAX_LENGTH 256
typedef enum tile_type {
GARDEN_PLOT,
ROCK,
WALKED,
} tile_type_t;
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Please specify the step number\n");
return 1;
}
int step_number = 0;
sscanf(argv[1], "%i", &step_number);
char *p, *buf, c;
tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
int x = 0, y = 0;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
int start[2];
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
x = 0;
while (*p != '\n') {
switch (*p) {
case '.':
map[x][y] = GARDEN_PLOT;
break;
case '#':
map[x][y] = ROCK;
break;
case 'S':
map[x][y] = GARDEN_PLOT;
start[0] = x;
start[1] = y;
break;
}
x++;
p++;
}
y++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
tile_type_t walked_curr[LINE_MAX_LENGTH][LINE_MAX_LENGTH], walked_next[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
memcpy(walked_curr, map, sizeof(map));
walked_curr[start[0]][start[1]] = WALKED;
for (int i = 0; i < step_number; i++) {
memcpy(walked_next, map, sizeof(map));
for (int j = 0; j < y; j++) {
for (int k = 0; k < x; k++) {
if (walked_curr[k][j] == WALKED) {
// UP
if (j > 0 && walked_next[k][j-1] == GARDEN_PLOT) {
walked_next[k][j-1] = WALKED;
}
// LEFT
if (k > 0 && walked_next[k-1][j] == GARDEN_PLOT) {
walked_next[k-1][j] = WALKED;
}
// DOWN
if (j < y - 1 && walked_next[k][j+1] == GARDEN_PLOT) {
walked_next[k][j+1] = WALKED;
}
// RIGHT
if (k < x - 1 && walked_next[k+1][j] == GARDEN_PLOT) {
walked_next[k+1][j] = WALKED;
}
}
}
}
memcpy(walked_curr, walked_next, sizeof(map));
}
int part1 = 0;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (walked_curr[j][i] == WALKED) {
part1++;
}
}
}
printf("%i\n", part1);
free(buf);
}

131
day21/input.txt Normal file
View File

@@ -0,0 +1,131 @@
...................................................................................................................................
...##......#....#...#.....#.#....#...#...#...#......#........................##......#..#..#.#........#....................##......
...#.......#..........#..#....#................#...##.....##...................#..........###......#.....#.........#....#.#........
........#..##.............#..#...##...#..#...##...............................#.........................#..........................
..............#............#..#..........#.#.#....#...#......................#..#......#.##...#............................#.......
..##...............#...#...#......#.......................................#..............##...............#.....#...........#...#..
.##............#.................#...#..#......#...................................#.......#.#..#......##................##........
......###...#...#........#..............#....#.......##.......................##.......#..#.......#.........#...................#..
.................#...........................................#......................#.....#.....##...........#...............#.....
..#...#....#.......##...........#..#.............#............................##........#.......#...#...#...#....#....##...........
..............#..#.....#.....#.........#..#..#..#..#.......#.#..#....##........#.....#.........#..............#........#..#...#....
...##...............#...#.#....#............##..#.......................................#.......#...........#..#...........##....#.
.......................................#......#...........#..........#.............#..........##.............#....#..##....#.......
..................................#.............#........#...#.##.....................#....................#.##....#.............#.
.#......##...#......#..#...#...........#...#.............#.............#..#.........#..#.#........#.#...........#..................
............#............#..........#........#..........#.....#.#...#.......#........#.#...........#....#.............#.........#..
.....#.......##............#...#.#.#...#...#..............##..#....#......#......................#...#...#..........#......#.#.....
........#....#....#......#..........#........................#.....#....................................#.....#....#..##.##....#...
............#.................#.##.........#.........#.....#........#.........#........#....###...........#.............#..........
....##....#..#............#...#.#..#.........................#.#....##.....................#........#.........#....#....#........#.
...#...#...........#..#..................#.......#.#..#............................................##................#.............
...........#......#....#........#.#..........................#.#......#......##.............#..#...#...#.#..................#..#...
.............................#........#............#........#........#.#.......#...............##....#........#...#..#..#..........
..................................#...........................#.#..#.........................#...#.#......#..............##.....##.
........................#..........................#..................#...#............................#.......#...........#...##..
.......#.#.....#....#.....#................................##...#.........##..#.....................#..............................
......#........##..........#.................#..##......#..#.......#..........#....................................................
....#..........#......................................................##.#..#.#.##..................#......#............##...#.....
.............#..........#.#..............................................#.#........#....#...........#.......................#.....
..#.............#....#........#.#..............#.......#......#.#..#......#.#...........................................##.........
..#.....#.....#....#...#....#..........#......#..............................#...........#............#..#.....##..........#.......
......#.#.#...#........................#..........#.#......#............#......................................#...................
.#...........#...#....................#..#..#....#.#........................................................................#.#....
........#.#...........................#...#......#.#.#..#......##..#.#..#..#...#.#.#..................#.......##.#....#........#...
......#......#......#.....#..............#.#.........##.................#....................#............#....##........#.........
........#.............#..#............#......#........#....#............#.....#...........#..#...............###........#.....#....
.....#.........##....#..................#..#...........#.#....##...........................#.............#..##..#.........#........
............#....#..#.................#.............#..#.....##.....#.....#.........#........#............#........#.........##....
.#.......#.#..........#...............#..............#.....#........#...#......#......##...........................#..#.#......##..
.##.#.........#.............................#............................#.#..#...#.............#...#.........#...#......#.........
................................#........#..#..........##......#..................................#................#.#...#...#.....
.........#........#...........#........................#.#.##.........#.....#..#................................#.............##...
.........#.#....#..#.........#.#....#.......#.............#........#............#...#..........#...##..........#........#........#.
..............................###.....#......#......#...#....#...........#...#...............#.....##.#.....................#..#...
..........................#......#...#..............................##.#.......#........##..........#................#...##.....#..
...........#............................#..................#...#..#.....................#...#.................................#....
..#.#...............................................#..........#..........#..............#............##...........................
........#...#..................#.........#............#...............#.#..#.........#....#...............#.............##.........
............................##....................##......#..#....#.......#....#........................#...#.........#.....#....#.
.....#.#...............#....#..#....#...........#........#.#..................#.....##....#.......#......#......................#..
...........#.........#.#.......#......#.#.#.......#.#...#...................#...#......#..............#...#....#.................#.
...#..#...........#.............#..........##..............................###...........#.#.....##.......#.............#..........
...##.....................##...#..#.....#.........#.#....#.#.....................#..............#.#...#...#......#..............#..
......##................#.................#.#........#.#..#................#............#......#.#..............#...............#..
.......#...........#.................................##..................#...#........#.......#..##...##...........#.........#.....
..............#........#.............#..........#...............#..#.......#.........#..#...............#......#..#.........##.#...
.....#..........................................#...............#....#......#..#................................#...............#..
...............#.......#..#.........#......#..................#........#................#.#...........#....##......#............#..
...........................#.....##.........#...#...........#........#..#................#.#.#............#..#...#.....#...........
................#.....#..#.#......#.............#.....#...........##....#...#.#.....#.........#....................................
....................................#...........#..............#................#............#..#...............#.##..#.##.........
.........#.#..............#........#.........#..#...#........#............##......##.......#....##.............#...................
..........##.....................#................#...#....#........#.....#..........#........#................#..###..............
......................#............................#........................#.#..#..........#......................................
........#...........#.#....#.........#.....#..........#..#.#..............#.......................#..#................#.#..........
.................................................................S.................................................................
.........#.....#..#..........#.....#...........#...............##..#...............#.##....#...........#........#.#....#....#......
.........##...#......#..#.#................#...........#..#...........#...#....#..............#......#..#.................##.......
.....................##.....................##...#.......#......#.............................#..#.................................
........................#..............#....#.................#....#...........#.................#...#.........#........#..........
.........#..#.#...#.#..................#..............#.#...###....#......#......#.#....#..##..............#............#..........
..#.......#...#..............#...#..##............#.........#......#.#....#.#..........................#......#..#.....#...........
..#........#......##.............................#..............#..#....................##.......#....#..#..#..........#...........
.....................#.....#...........#.##................#.#....#.#.#............#...#..##.............#.....##..................
..##............#........#........#...#......#..........##.................#...#.#..........#...................................##.
...#.............#....#...#..#........#.........#......#..................#..#..#..........#.....#........#.#....##............#...
...#.............#...............................#.......#....#..........#..........................##......##...#.............#...
....#................#..#.#................#.....#.......#..................#.......................#.#............................
....#.#..............#..#.........#..#..#................#..#...#.#..............#......#...................#......................
.#......#....................#.................#........##......#......##..........#...............#..##.#.....#.............#.....
...##................#..........#......##.....#........#...#..................#........#......#....#..#.....#......................
......#..................#......#.......#.......#..............#......#.........#.......#..#.#.#...##.....#.............#..........
.#...................#...#....#......#.#.....#.....#..................#.#......#.......#.....#..#......................#.....#.....
...#......................#.......#..#.........#............#...#......##..........#...............................................
...#..#........#........#...........#.#...#...#.#..#.#...#..................#........#........#.#.##...#...........#...............
........#.......................#.............#....#...##.........#....................................#..............#...#........
...#...........##........#..#.......#...............................#..................#.....#.......#.#.........#.................
..........#...............#.###...................#..................................#.....#...#................##...............#.
..#......#..........................#...........................#..................#..#........#....##...................##......#.
....#...........#..#..........................#......###................#...........................#.........#.#........#.#.......
...#...#..#........#...............#...........#.......#........................##..#...............#............#...........#.....
.....#..........................#..#........#.......#......#......#..........#..#.....#.....#..#.................#........#........
....#...##.......................#...#.....#.....................................##.#..#....#....#................#................
..#.....##.........#..............#.#................#.........#...#.................#.........#..............................#....
............#...........#........#........................#.#.......#..#..#.........##...................#......##.#.......#.......
.........#......#......#............#.....#...##...#...............................#.....#..............##............#..#.....##..
...........................#.................#...........#.........#....#.#.#....#......#..#....................#......#...........
....#.#..............................#......#..........#...........#....#.............#.#..............................#.....#..#..
...........#...........#.............##....#......#.........#................#........#...............................#............
...#........#.#....#.....................#..........#.......#...#.........#..........................##........#.#.#...##..........
....#.#....#.........#.......#....................#...........#..........#....#......................#.............#.....#..#....#.
..........#................................#......#....#......#.................................................#..................
.............#................................#...#.##.#.....#.......#....#............###.......#........##.......................
..........#......##.............#.........#............................#.#..#.......................................#.....#......#.
...#.....#.......#............#.............#.#...#.....#.............#..#..........##.......................#.....................
..........................#......#..................#..#........#............#.......##......................###.......#.....#.....
............#.#....#.#................................##......#..............#..#.#........................#......#...#...#........
......##..........#......###...#......................#.........#.#............#..............#......#..............#.......#......
............#........#.....#........#..#.......#...#...................#.....#..................##.............#.................#.
........#.#........##..................................#......#......................................#.....................#.#...#.
.#.......##......#.....#..........#................#...................##..#...............#.......##....................#.........
...#..#..#.............................#...................#................#....................................#........#........
.....#....................................#.........##.............#...........................#...................................
............................#....#......#...................#..................................#...........................##......
...............#...#..#...#.###.#...........#..............#..........#...............##........#.##.#....#.#.............##.....#.
...........#.##......#...#.#.........#.#.#...#.........................................#..........#.............#.#.............#..
........#...............#........#........................#.#......#......#......................#...........#.........#.#...#.....
....#..#.............#......................#...................#.....##...............#...#........#.................#............
............#......#...#.....#..........#....#...............##.....##..#.........#....#..#.......#...#..#.........................
......#...........#.......#...#.#.....#...#......#...........#......#................#................#....#.#...#.................
.......#.#...#.......#......#.................................#..............................#...........#.............#.........#.
...................................#.....#.........#.............................#..#............#...................#...#.........
...............#......#...........................#................#...............##...........#......#........#....##......#.#...
...............#....................##....#...#.....#.................................#..#.............#.#..#.....#..#.#...........
.............................#......##..#........................................##....#.................#......#....#...#.........
......#.#.#...............#.......#............................................................#................#..........#..#....
...#.#...........#..#......#....###...............#..#..#......................#....#..............#......#................#.......
.......................#....#.....#..#.##.#..........#.....................#..................................#....####.##.........
......#..........##...#...........................#....#.................#........#.......................#.................#..#...
.................#..#...................#..#...#...##...#..................#..........#.#....#..#...........................#....#.
...................................................................................................................................

11
day21/sample.txt Normal file
View File

@@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........