Compare commits

...

7 Commits

Author SHA1 Message Date
dobiadi
1f855400aa Day20 Day21 C WIP 2023-12-24 13:01:39 +01:00
dobiadi
1c22f73a2b Day23 C 2023-12-24 01:51:44 +01:00
dobiadi
23d9bd4106 Day22 C 2023-12-23 21:12:34 +01:00
dobiadi
6646cf557f Day19 C 2023-12-23 00:18:03 +01:00
dobiadi
4427590639 Day18 C 2023-12-22 13:34:50 +01:00
dobiadi
e8e253128f Day17 C 2023-12-21 12:46:10 +01:00
dobiadi
d07adb1b15 Day16 C 2023-12-19 19:21:17 +01:00
33 changed files with 5284 additions and 0 deletions

BIN
day16/c/day16 Executable file
View File

Binary file not shown.

208
day16/c/day16.c Normal file
View File

@@ -0,0 +1,208 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_MAX_LENGTH 256
typedef enum tile_type {
EMPTY,
MIRROR_LEFT_TO_UP,
MIRROR_LEFT_TO_DOWN,
SPLITTER_VERTICAL,
SPLITTER_HORIZONTAL,
} tile_type_t;
typedef enum light_direction {
LEFT_TO_RIGHT,
RIGHT_TO_LEFT,
UP_TO_DOWN,
DOWN_TO_UP,
} light_direction_t;
typedef struct tile {
tile_type_t type;
light_direction_t lights[4];
int lights_num;
} tile_t;
void traverse_light(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, int entry_x, int entry_y, int dir[2]);
int has_light(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, light_direction_t direction, int max_x, int max_y);
light_direction_t dir_to_direction(int dir[2]);
int count_energized(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y);
int main() {
char *p, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
memset(map, 0, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
int x = 0, y = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
x = 0;
while (*p != '\n') {
switch (*p) {
case '.':
map[x][y].type = EMPTY;
break;
case '/':
map[x][y].type = MIRROR_LEFT_TO_UP;
break;
case '\\':
map[x][y].type = MIRROR_LEFT_TO_DOWN;
break;
case '|':
map[x][y].type = SPLITTER_VERTICAL;
break;
case '-':
map[x][y].type = SPLITTER_HORIZONTAL;
break;
}
x++;
p++;
}
y++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
tile_t map_cpy[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
memcpy(map_cpy, map, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
int dir[2] = {1, 0};
traverse_light(map_cpy, x, y, -1, 0, dir);
int part1 = count_energized(map_cpy, x, y);
int max_energized = 0, energized;
// LEFT COLUMN
for (int i = 0; i < y; i++) {
dir[0] = 1;
dir[1] = 0;
memcpy(map_cpy, map, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
traverse_light(map_cpy, x, y, -1, i, dir);
energized = count_energized(map_cpy, x, y);
if (energized > max_energized) {
max_energized = energized;
}
}
// RIGHT COLUMN
for (int i = 0; i < y; i++) {
dir[0] = -1;
dir[1] = 0;
memcpy(map_cpy, map, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
traverse_light(map_cpy, x, y, x, i, dir);
energized = count_energized(map_cpy, x, y);
if (energized > max_energized) {
max_energized = energized;
}
}
// TOP ROW
for (int i = 0; i < x; i++) {
dir[0] = 0;
dir[1] = 1;
memcpy(map_cpy, map, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
traverse_light(map_cpy, x, y, i, -1, dir);
energized = count_energized(map_cpy, x, y);
if (energized > max_energized) {
max_energized = energized;
}
}
// BOTTOM ROW
for (int i = 0; i < x; i++) {
dir[0] = 0;
dir[1] = -1;
memcpy(map_cpy, map, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(tile_t));
traverse_light(map_cpy, x, y, i, y, dir);
energized = count_energized(map_cpy, x, y);
if (energized > max_energized) {
max_energized = energized;
}
}
printf("%i\n", part1);
printf("%i\n", max_energized);
free(buf);
}
void traverse_light(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, int entry_x, int entry_y, int dir[2]) {
light_direction_t curr_direction = dir_to_direction(dir);
int curr_x = entry_x, curr_y = entry_y;
while (!has_light(map, curr_x + dir[0], curr_y + dir[1], curr_direction, x - 1, y - 1)) {
curr_x += dir[0];
curr_y += dir[1];
map[curr_x][curr_y].lights[map[curr_x][curr_y].lights_num] = curr_direction;
map[curr_x][curr_y].lights_num++;
if (map[curr_x][curr_y].type == MIRROR_LEFT_TO_UP) {
int tmp = dir[0];
dir[0] = -dir[1];
dir[1] = -tmp;
} else if (map[curr_x][curr_y].type == MIRROR_LEFT_TO_DOWN) {
int tmp = dir[0];
dir[0] = dir[1];
dir[1] = tmp;
} else if (map[curr_x][curr_y].type == SPLITTER_HORIZONTAL && dir[1] != 0) {
dir[0] = 1;
dir[1] = 0;
int recurse_dir[2] = {-1, 0};
traverse_light(map, x, y, curr_x, curr_y, recurse_dir);
} else if (map[curr_x][curr_y].type == SPLITTER_VERTICAL && dir[0] != 0) {
dir[0] = 0;
dir[1] = 1;
int recurse_dir[2] = {0, -1};
traverse_light(map, x, y, curr_x, curr_y, recurse_dir);
}
curr_direction = dir_to_direction(dir);
}
}
int has_light(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, light_direction_t direction, int max_x, int max_y) {
if (x < 0 || y < 0 || x > max_x || y > max_y) {
return 1;
}
for (int i = 0; i < map[x][y].lights_num; i++) {
if (map[x][y].lights[i] == direction) {
return 1;
}
}
return 0;
}
light_direction_t dir_to_direction(int dir[2]) {
if (dir[0] == 1) {
return LEFT_TO_RIGHT;
} else if (dir[0] == -1) {
return RIGHT_TO_LEFT;
} else if (dir[1] == 1) {
return UP_TO_DOWN;
}
return DOWN_TO_UP;
}
int count_energized(tile_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y) {
int sum = 0;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (map[j][i].lights_num > 0) {
sum++;
}
}
}
return sum;
}

110
day16/input.txt Normal file
View File

@@ -0,0 +1,110 @@
\.............\................\....\..../....\/...............................\...-..........-.\.-....../....
|\......../|....\..............-\............./...........................-..........-......\........\........
.......||......../....-./.../-...|.|..-......-.|....../................\............|...................-..|..
........./......-.............-\......................|.\./.......|........./......-......../..-.........|....
....../....................-............................................../...../...|.........................
....................\....|/............................./.......-....|......../..........................\....
.|....../|.........-......|.............\..|..................|......../.................|.|............-.|...
.....-............\\............\................/......-.........\................\..|../....................
............|..|.......|......../................\.......................|../....../......./.....|............
.\...........\..\.............../.........-/....|......................./.....-./-.........../-........-......
|................\.\-.............../..-............./........./.................\.............|...-.....-....
.........-....|..|......................................\.||...-../..|............../...../.../.........-.....
.....-....-\.................|...........|../.....\...............................|...../../|.................
.\......\......................\....../.../.....-.../.........................../..|...........|..\...........
-............|.........-.-........-.........................|...\...-...........\.|...-....../..........|.....
.........-.......|......\.....\..-...../..../.....\.../............/..........-......./....../...........\.../
..............\........................|.............................-../......-..-...../..................\..
.....\......-.......-.......-......../../../........\.......-..........|......./........................../...
.\...\...................|./\|.-..\...............................\..\.....-....|......................./.....
.................-...|....|.../...../.|.||.....-.|............................-..............\................
........................................../.......................|........\......../.../..............|.....\
.......\......./............................./........../../..........................-\..................|...
...........-...|..\..........-.........\.................././................|./.......|............../.......
......./..................................\...-.........\........\....../............................\......\-
-.|..-|...............|..........................\..........|.-.........\............-.|.............\.....|..
.............................../..............-.....-.......-........................|.......-................
.........\.........-.........|...../......../......./....-...\...................\........\...................
..........\.........\........../...|.................|./...\.......\..........-...............................
./...................|........../\......../....................|..-...........\...\...........|..............|
.....|...........\-.\.......-..........................-....-...................|....|.|...............-......
.|.././......................|.......|.......|..............\...../.................../.\...-.................
...........-........./....|.................|..-.....-............./...................................-..../.
...................\/..|.....-............../-......-..........|........................./...\....-|....|-....
........................./..-..|.\-...............-......-/...../.......\..|................-...\.|.....\.....
\...../.........................../....................-..........|........../..-..-.......\........../.././|.
........................../...\|..................................................../...|.-........-.....|..\.
....\.............................|./.....|...../....\.............\...../.......-...............\..-..../....
.........-.........................|................|........\/.\-...................\..-......./../..........
...-.../............/.................-..........\....../............../...../..........|.....................
..........\...\..................|...-......\\......./...-.............-\.................|...../......\......
/..\/.............\....\.............\.......................................|.....\....-............\........
....../..\.|................................\.........../.......-.......-/.....|...................-.....-....
........-....|.............................|....../...............\.......................--..................
................/......|.........-....................-..............|.......................................|
.........\........|...........................\.............|.............................../.................
...|..\....-......../.\...-............................................................/....-...../...........
.............\\\.../...../.../..........\..............\...........\........................|....../.........\
......-..............|..-..|....-\..\..-...-............\.................\...-.\/.....\......-.\....--.../...
........./...................../...../..|...\.\/.........\.../........................................\.../-..
........\.......|.......|\..|........../..../\........../........\..../..............././..../.............-.\
........................|-............|............................./.-.|...|............../....-\............
../........-.../....................|....................-/.............../.....-...........-....\......||....
.....\..|.....-|../....|./....-|.......-...../..-..\..............\...................../.................|...
...-......../.\...........\.........\../.....|............\..................../.........\................/.\/
...../...|............-..\.../............/............-..............--.-..........././............-........|
.....|...........\.....-...\..../.....-........-.....\.|..............\....-............./...../..............
....\.....-.|....\..../..............\|../.........../......|....\/.......................|............|....-.
........|..--..............|..............-........|.......|.-.........\.............-...........\............
./.||....\......./....\./........../...........././....\.................../..................\...../.........
.......|..\...-.\..\..|.........../..................................-....|........\......|....|..............
...|..........|.........-...............|...............................-.......|...........-..\.......\......
.\.........../../............................-..|...\.....-.........../......-|.../.....|.................-...
........|....../..\..\|..|......|.........|...............................\../.....\....-/........-...........
.................................................\.......|........|.\.-....../.......|.......|............\...
.......|.......\.......|.........|......-......|...|...\.............\.............|/.........................
..........|....................|..........\..........-......./................................................
...../.......|....................|......................././.................................................
........\.............|........--..........\.............../......|...................../............|..\.....
....-............|.........|....|.-.\../-...\....-...-................\|..\-.......|........-............../..
................/........\.....\.....././....|....................-...............\...........-......\........
.\.....|......|...|\......\.\/....\.............-............\.....\.......-...-........-....//..../.....|..-.
..................|..|.-.................../\...........|..../..........\|.....-..|.../.......................
-......-......./........./../.............../..................-..\.......\............|...../../....../......
....../............/...........\.............\............./.........../.......\......|....|.........../......
...\........|..................|............-............-.....-..\..-................/........../........||..
......../..|..-../..../......................../.........\./.-................../...................-.........
.../....|....\.......-..\..........-....\.............|\........../...............-........|..................
..............\.............\|.........-......................-........\|......./.......-.................|...
/..../...-||\-...|\...............................\.......................\.............-................-....
.......-..-....|..................|..............................................................///...-.../.|
......|.......-/........-.-......|....\..-...|...........\...........\.....................-..../.........../.
......-...........|-..\.......|...|.\|.................-....\...................\.\........\.\./.........\....
....../............../.........|...|.......|..../.............................................................
.........../...................-./...\..........\-............./...........\.../.....|......................-.
/..../..../........................................|.\......-/......./....\.........................-......|..
.\./.|.-..........................|...|...........|....../....-....../|...............\..............\...-....
....|...\..........|......-..................|........../..|.-...\........./........|................|........
...\....\........../........../.........\..............-...........................-.|...../..........\.......
...|...........-...........................\........-.........................\...-..-.....\.......|........-.
......\|...........................\.....................................-............/....\...........|......
................../............-........................\.................................................-...
.../....|...............|....................../\.........|....................\....|............\.........|-.
.......................................|....-.........../....\-........|......./............-..|.............\
/.........\..\.........|...............\.......\........./................\....................|.....|........
..................................|.....|.-..|.../......................../-\......|..........................
.................|.....................................................-............|.....|..../.\/..........|
.......\....\....-........|.|..../......-......\...../....................-...........\............-..........
....-.................................-..........-......-................................../.........-\...|...
.....................|......................|.........-............../.../.....-........\.....................
.\./..........-.\...\||................./../........\.........|...\........\....\.\...............-|......|...
..\......//.........../.\........\......|......|........\......\.....-..........|.-..|...../|.................
/.....................\.................-........|...................\.....-..............-/.....\..|../../...
...|..-.../............./.|.................../............/..\...........-....|-............\.......--.......
..............\..\/.....\.........|.\.........................|............|........./................./-..|-.
...........................-.................../...................|\..........\/..|.-........................
.....-.............................../....................\.........|......................|..................
..................\.........../...........\..\/.........|...........|..-........|....\..........-...../.|.....
..........|.......\..\...........\.../......................-...........-.....-........../\|..................
....../.......-........................\|...//...\..-......\.......|..\....................-..........\..-....
|./....|........-./........../.\...\.....|../..../.......-........../.\.....-.................................

10
day16/sample.txt Normal file
View File

@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

BIN
day17/c/day17 Executable file
View File

Binary file not shown.

185
day17/c/day17.c Normal file
View File

@@ -0,0 +1,185 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define LINE_MAX_LENGTH 256
#define NUM_0_CHARCODE 48
#define equal(a,b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
#define MAX_NODES 128*1024
#define MAX_PATH 1024
#define BIT_0 0x0000001
#define BIT_1 0x0000002
#define BIT_2 0x0000004
#define BIT_3 0x0000008
typedef struct node {
int position[2];
int weight;
int dir[2];
int forward_count;
} node_t;
void insert_to_queue(node_t *nodes, int *nodes_num, node_t node);
long dir_to_bit(int dir[2]);
void set_tmp(node_t *tmp, int next_x, int next_y, uint8_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], node_t curr, int next_dir[2], int fwd);
void find_path(node_t *curr, int start[2], int finish[2], uint8_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, int min_fwd, int max_fwd);
int main() {
char *p, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
uint8_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
memset(map, 0, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(uint8_t));
int x = 0, y = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
x = 0;
while (*p != '\n') {
map[x][y] = *p - NUM_0_CHARCODE;
p++;
x++;
}
y++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
int start[2] = {0, 0};
int finish[2] = {x - 1, y - 1};
node_t curr;
find_path(&curr, start, finish, map, x, y, 0, 3);
printf("%i\n", curr.weight);
find_path(&curr, start, finish, map, x, y, 4, 10);
printf("%i\n", curr.weight);
free(buf);
}
void insert_to_queue(node_t *nodes, int *nodes_num, node_t node) {
int inserted = 0;
for (int i = 0; i < *nodes_num; i++) {
if (node.weight < nodes[i].weight) {
// Shift to the right
for (int j = *nodes_num; j > i; j--) {
memcpy(&nodes[j], &nodes[j-1], sizeof(node_t));
}
memcpy(&nodes[i], &node, sizeof(node_t));
inserted = 1;
break;
}
}
if (!inserted) {
memcpy(&nodes[*nodes_num], &node, sizeof(node_t));
}
(*nodes_num)++;
}
long dir_to_bit(int dir[2]) {
if (dir[0] == 1) {
return BIT_0;
}
if (dir[0] == -1) {
return BIT_1;
}
if (dir[1] == 1) {
return BIT_2;
}
return BIT_3;
}
void set_tmp(node_t *tmp, int next_x, int next_y, uint8_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], node_t curr, int next_dir[2], int fwd) {
tmp->position[0] = next_x;
tmp->position[1] = next_y;
tmp->weight = curr.weight + map[next_x][next_y];
tmp->dir[0] = next_dir[0];
tmp->dir[1] = next_dir[1];
tmp->forward_count = fwd ? curr.forward_count + 1 : 1;
}
void find_path(node_t *curr, int start[2], int finish[2], uint8_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, int min_fwd, int max_fwd) {
node_t tmp;
tmp.position[0] = start[0];
tmp.position[1] = start[1];
tmp.weight = 0;
tmp.dir[0] = 1;
tmp.dir[1] = 0;
tmp.forward_count = 1;
long visited[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
memset(visited, 0, LINE_MAX_LENGTH * LINE_MAX_LENGTH * sizeof(long));
node_t *nodes = (node_t*)malloc(MAX_NODES * sizeof(node_t));
memset(nodes, 0, MAX_NODES * sizeof(node_t));
int nodes_num = 1;
memcpy(&nodes[0], &tmp, sizeof(node_t));
int next_x, next_y, next_dir[2];
for (;;) {
// Pop first node
memcpy(curr, &nodes[0], sizeof(node_t));
if (equal(curr->position, finish) && curr->forward_count >= min_fwd) {
break;
}
// Shift entries
for (int i = 0; i < nodes_num - 1; i++) {
memcpy(&nodes[i], &nodes[i+1], sizeof(node_t));
}
nodes_num--;
// Check forward direction
if (curr->forward_count != max_fwd) {
next_dir[0] = curr->dir[0];
next_dir[1] = curr->dir[1];
next_x = curr->position[0] + next_dir[0];
next_y = curr->position[1] + next_dir[1];
if (next_x >= 0 && next_x < x && next_y >= 0 && next_y < y && !(visited[next_x][next_y] & (dir_to_bit(next_dir) << (curr->forward_count * 4)))) {
visited[next_x][next_y] |= (dir_to_bit(next_dir) << (curr->forward_count * 4));
set_tmp(&tmp, next_x, next_y, map, *curr, next_dir, 1);
insert_to_queue(nodes, &nodes_num, tmp);
}
}
next_dir[0] = curr->dir[0] == 0 ? 1 : 0;
next_dir[1] = curr->dir[1] == 0 ? 1 : 0;
next_x = curr->position[0] + next_dir[0];
next_y = curr->position[1] + next_dir[1];
if (curr->forward_count >= min_fwd && next_x >= 0 && next_x < x && next_y >= 0 && next_y < y && !(visited[next_x][next_y] & (dir_to_bit(next_dir) << (max_fwd * 4)))) {
visited[next_x][next_y] |= (dir_to_bit(next_dir) << (max_fwd * 4));
set_tmp(&tmp, next_x, next_y, map, *curr, next_dir, 0);
insert_to_queue(nodes, &nodes_num, tmp);
}
next_dir[0] = curr->dir[0] == 0 ? -1 : 0;
next_dir[1] = curr->dir[1] == 0 ? -1 : 0;
next_x = curr->position[0] + next_dir[0];
next_y = curr->position[1] + next_dir[1];
if (curr->forward_count >= min_fwd && next_x >= 0 && next_x < x && next_y >= 0 && next_y < y && !(visited[next_x][next_y] & (dir_to_bit(next_dir) << (max_fwd * 4)))) {
visited[next_x][next_y] |= (dir_to_bit(next_dir) << (max_fwd * 4));
set_tmp(&tmp, next_x, next_y, map, *curr, next_dir, 0);
insert_to_queue(nodes, &nodes_num, tmp);
}
if (nodes_num == 0) {
printf("No path to end\n");
break;
}
}
free(nodes);
}

141
day17/input.txt Normal file
View File

@@ -0,0 +1,141 @@
322222331232132221112333141131141421432535111315454231154425333445331325214334352425212345343422113421533234333342231121312224211232132331311
112222221132312141233334323313423341313142332514144441221332524235451353431144233453551355535521415123254413343222433142441412131232231122221
233311211132123132221413413342313144254415324345245135411343525533355513313524342444244552154352343444341241224233132212231434412321112213132
122121222123133442422242231341123325225213254443245241522123133531323555532233151534115233215142445532322342151213321214342313432231323113123
333221122312222411121442113241232534233432521555121441223215535431542235361313542254543322532531154144522513554533443441341444111111321121333
211331211214432233123223424223124532151344214214542223331353264642443432464436224423224313223252125355413525423152224342423114131411112111123
321223112344113433231424123325532125514251543332123251155363265243452334535223565553644354421552113311325513113444511443343413432243231313333
331112133142443233432421114422112423521245553211111523242563534534562453423253433464252355615233351324133352335421242442142134213132421212232
123123132324211421233414412211533554345522213452545454536356224246525264445422262363524343225625252212421135334423251133211443213214112122122
121221241431433223122131445353552233142144512443252243362553462252633665622344555425653436444465214112414411121121114514124141222324134111222
133112144124112314211245532541231542542135512356236336563252252643564354434566234566342436235632443531431233413141321355444213214223322423332
322132441311211324331354355511325535454343332336355243323534546646445434666434356245233443234355464421551444314452551232413312111222342422132
332112144311441331145235233355512421155156656566655324663456462643234333652442646456526324524435666264354525124554151514344111232333123121232
112322143122443321255444531524442254522435344466544446225563232454254336646644453432456442265426224633364413545552141231232342443134312424322
211112433314214414121554231413313431264645245424625662356556356363526434463335423263455622432234243652233651124423324145351422433444413233242
123412313414223441541432354411133533264565324623356522334552452232222235346254322544256225266444465563624663145242355442225325131344441434431
122232241113233344414343445245334526626224652262554436326624425654546753366453737333434566325324232256666566465541522125315544113322131424313
441131142343322224354232433122154334244453262644636226432735355445376336457435675653434424226226554263663436634131141121112212521241142321232
444124213331242524531555541123263435226643536536425642566335447454777555373634744664333634623364456664254352535441433522414143232432133312141
142323312314135231332412115353444646325624242262433637566663535477543343575346673575566463756653266553236665325236325123231544443414221443432
133332214211214512511334522363626634345432634234666347475673545334567454333665333576563637635363565362524656455244455214423524135332241112343
114332313215243122212252352362462564564232565327436544463373743636663353454736374665776566476746432522662336422255461425241315315553244323324
244113213443433453312254442326242365253345446477645575746733344546446356353436466556776654557445633664365364254554262454122333435243234441244
123133241351413144322431436253253364555363566547664357364763645337356755445663333755367347437736645644534466442424465622335515552345223114132
141313223214133413535525225635234556344253774463564757456364334433557373347466336733657334573673434763453634355653256221125514413333512421413
211121435455322131411522253334522524622544466575377334767736555357537733536675363657467556556673734365355346223246664252445312521512143341213
332112111553531125445446523366463445537367445346365446335537556536434636555356675375456337733736757365346555654534526656553112414115442343431
334323341513433241114523442435633456453436453567566443375366734684585655664865675435643534674434744767767333656223563665331323153212455312432
214314422254423141124253362526654246747334664673454533366676454748888877776756674465474764467464765335636744663634563446233514225243123132134
221422142552342124425653343364465434643664433546553363355685647884564674785885575486566465774563367363755763646622533526434545511232231432332
131433153555224431352333622333525534653646537757446733746685664657876486887856687457474885575443346475434476536426665533526555423525114543143
414241342153331114533342663553345453557573336633565378468874774454686655858846664676674578677565646434577735336422624662264235213343545134142
422244331152252533356444644642455653634367437537666758685886576658586455555774455576784768545547457776764576377324655644335242441215542311123
224514311332324242565636562655637475653434634444657476468485645887444747545566846648888555466687367765343444634536335554435446552553424521452
324235324155152356524453432522667443344757556358656654687574754764658686465764884887485465874455475445346554773356334265444255333253343144414
422155422455233453265254553566435573557344535678585454448445848876874775857565648577744784557686584733547637647666563335352253255433145143325
433522423543246253243562266265653675734363374788654878446558787775868774745648585768584467654645865464634776476743662656334634244532254554223
112345432553324646256344452563676533734565448486848444474866678688445746558464865554564485485545584764747547774754536532455325263313454154144
421331512554232363542455634433545333654378757764885664588484868588998998685995846766466678454557847578576466763755573652655565366235332454123
134154422313262464256254654773353533337648867775767446685857787668566778565568657556848888464545675546643744436744557236545455522654511214542
112411314514523646326445544635655366776764888547846646744467988655959979885758577589657656577576456686655353733563633344446625362364333345133
211512544551565256423432474775576664545654687446758847488965895996789986579865686776897658554775844545786545467636655664426546522431523233541
254121351444222654563456537446766653446447844486786747879967767776888966755788565956589746644586858548478855347636455665553425352625443222352
451334343256326255354425675633565746775544566747785759695789698868776866668598597658557566666774884648467566677366444334355264652355223454352
155151522152425333432626354533656634555844757447476698598879888959659585587795857588675958656788578667867657566633733344526545352652553311352
323311225332555322334644436665373337767457786854668867567985596968898656885687676977785665879777747478584778573345654757554562666335411321112
142444221335424244536656477453464455744887865446887787855585777685796976868758586897765699887954868887755464763476753644534362545225642333444
311125151633663542456465333376767765786657565778767955557876559955689578586576585797986779967698764667848668665747635664435235222536423442522
144554313565643664236735676567364486685774468847969997585567999667988979868896977566559989556677745448577455763634475546563246245333642251442
124214126256353433325473676455454666788856858698575977697766599669866999879567996989966899797576864675847466646747644765443545446645464154342
234124226225525534535674553355765857686864865767897575989977768778898969976676695899888665967559967644586575564767537765734326524333465354225
111551143364233333665643467766438664557567678787898866578797588868798779999699767655579989956698785867665846684775346733737464263262223423535
421142363333253664544635635565747785478446775558697785969987878967866898678977898767665798797676555777844866875437565537556343535323245315143
345451135266466434675467566347387784585488857975558655988686966879969689867987676689658669555877886654554844677874374375647452532563465323312
242344235526446225574577365666857586685844858595768997599866987776696969789998767977998688696585596748446874845767546643477356665366322243513
532115353236434346534634733555468887444458798785586687666896797669699668879888678989789975575797967797446777787773765636465353253444235324124
235252242332246463467535455567845748488877859896995668669999977886978787798967787789697986965568759965575866767684446575767766556534632223343
143343325563554625676677466736585746688768796796597668897988677999977687767877778687688699596756555977655786864458675757535765244622653564445
545315646456655266556653677767587445855588796556985766998667888899887979788686979978889977557598975555844475855845377463446464555262342631255
225323565454336424365556773756857467464878759576599956696878999969999898888968786667778988659878759657948786484475467365633534655233653353235
224456254554422567564536775664546477547496965696857878869687787688788689898868798866776677786775879975586787856564673365735573535355336643124
543456555444426224545554573586765657466559558587778966798787896889889977799898886898997977867687585566966546665755565446657556435234526266454
315536325363343234433576335774558558648696679779999987876899698688897777878779896786769797695668779777566586765854453534633757444253423625124
151216343662665453744746445345584547677965779558577699779886978978777897877977999699897967999896857857567477554548633463477463436533242564425
543355354446632363335673367868545465458855986855886769967868668887898997997779797889797789986979895575796847754484547453753543325425346663312
535322533226325265767647536855854855778979965879769897779967969888779887797988788766667777977689986668684474868785466476357456665654234465422
231556524524226674547774473577586775489576795799987997979988777988878798898979988779998877686665678579598888858487544564677765355264626364355
354215356635232277535766636447878578669956779799558778996768979798988889979889779967876877966557957959587447848557557543533356453246253366234
544153364554564456764536556464688677765858958656696869678987689977798987889888897977876867798796895759888764846576743454376377365453456435242
525152435566645637336466733788745548648698786968877688677769779987978987789788999968789887968656659668895788467868645643335453444225226564223
554456254232442545777435733656455785856855968689797989978989998979989887797777797976786767789995886788998575456486644647567746642446655425652
552564443563356465645755534444484775746676866969686899686988899789877798797987988877867977966776888778685645444876565554734346554535452535642
532426532465544566736373337854774776469589868759887699866868799987778898988888988986999888977689658558785555676674563743364767746445624552333
224355342623332446467776757776465567785866775787976966967786797879987977979887897769767669698676797997899887656865744456675564542545425242511
423424563462454366676567475856764458447599968889989679788788997777777789778778978869897867878976867889765758656648566746737475334465564536233
454354245243524276633735643586686864846687557668997987876996879788778787797997888887866789999996989598656747776576584434473565343422544655523
134213565432233465576366665487445544577887868868667869978999789878777988888787798679687997698658996767868685676666886753643377763624563546545
532526426424453576553556543646845455884766968656687677789798797787887887799978988987868899889988659759595786484466747636434764443634222623112
542124434443543643563654343446677677865867697586879688897676867878998898978989799997897676878989798989894674848855564375747446466225322665122
255252546255422477374435674555867867846875859668568976669679866687988897888887767987676867685686896869984666465866436477476453332334245662542
521356225624562566737635377668646645674597898979975767987777986999779797979787989767687876969858658797875578475575844544575376744663524426112
445433354652342445476743633654745688748585786556759599669677668868787887798779686867799996687885695767966766658677433575764544533355666466235
233545323366363443337334575665656688465679988569788899696799768867686879798678896669999999678786979799758457575467834667547446422625542453532
324551224454322244447567665378587865658577789765599566876768689788999686766989687679969779997585677767748848548857743346556535524656256542114
351542435666443534653733675577668888446688685688566896889967698777767679879699797968969696779957888876558466675655337434566444452232256353141
311234446553626523657447336368764547755445787587888656797968979698698679799878676799677787958798569698645864667884564337475477634264343454123
115512526244454342364454343545675548487884588995597578869968989986689887798879779679776976775797956658748777646557343436344353622662465244111
122211552645355262744655766376755587575864856558998666687987677799978987698878777798777775786979767587568477585747657336557735534625335232455
425345443424245433375455547633778546574776765755765855888797968989997799876668999797669979886787688876547677654773776745434562333653263224523
555251326362654363237546435473346468884476467796588875986769677679987979967879988869668699867555669955445674845675654745564655365665446543555
251445423464566444547337744665776567644578465769676786559588667977677786989966798688666778655767877684666874657464563343775666634426234234531
341355143244643626455357536755754585648456787595697879688996577986866979878868977979659969966867565678667484454756454375754462663543452534243
411222154254544422667433433675666457848545545989798668975957675887979988699687798967697968568959784658856455566644673776554336545643436145141
111122522642643455345565565336453847775464574889689865766896866699599898777567755897685679777856958657445858653767456364347624464332533335154
524241542223363646625344447455676474586754455669665886589999589768559775556599589956665988786989478684546555454663534636576332446266461343514
113533134433523524446375667645566447688876847855859957979767989878967979789999855959685696789985867764675664874634464336565224222646642321321
245124345333555642252444454546376467867578884786479978989977659769956667868756689966765555985878864575655545774476473456565366462243363441453
325354232436363363425333443734637667468756576648479988858967756758868779757888957989878668976654457564455648465766544656664643642266233352414
451353323252436433336444757666564636547545444868484855686795879665895669878698685666795967568575477757788876676753437756326524635444422233212
314251335526245463465344464664557574685576548854568759856996687769699776867988576768857557867678466888568855544765437763432222442344311351454
221343454312356446253652533564454347687864847866685854878977995585678858876868579559676767585687778868678774743747547636565333442562245414433
153334455244353565443262456744343467578487478747457785674998657698766957675569587698964765845684864574444667455745635532662234653645215513531
323253541413342552245663443343675376543647876878458575556559588599988855669797967599547868547544658877584635537465644556623626662242233113253
534434414252452225355655323757543566777567687485754846887585887865899558767759999578775754644485876644443336476447545444244442464511524114225
142314213444164544544553446364555576745367646487857876847657447545869776658985875747755667746444686784736465576454773642446666332553312311413
214112112255155462442242365344557773467374544846466557747448576848476667446464875867465456748844657557337475753345766254556234333342332325121
244153353221226223323534344244636436774454367767748485587468775458546674855876677774876558575784878757743364775773736226355466624115153114123
441232413554323455446346354233666374764645356675485547668775864485587868865755854785774464468786577433435436446443526356433456631223524422345
143221413241342433544645523533734733466374434458654454486545647675776478466854875865487576784867635667346353555374563663532446535324324323523
323131242415143225354352262324364766355466653533775477687544766487866677554777558458758787667587564446675477346743353245263356544252335112343
421335544413315446545626342233335667476654573777557766545848667557886768867758486784545665457636443337735636345564533262244236355135432322131
312244554415442331522246453334533637436756764474737765568656744654876674686547644877844868736646336544336346545536354445556333222132135454443
321333454221512143442325262626664335573736534565367744484775854448556587586646887786475476467666656577334434366334646444445335553141543541233
341133211142213555542466322563256567337574674733364756537584587757866448657755774474664364453535637443745353553456662464533532454235524451123
421223541232113225443246365633626264757357575466645334557538454665677655677477786664433654335355636634647763565546635663546221155245241142144
323422431143311254424526246335546255453335743347554637636473547445746785666646745773655646565733765464747364365366655543643333425255152231113
433444113153233523221332663653526543537447366535353747643746373675543536536676575675536557447354456764537266436522466222351315532533422224334
124222124142422434345123524266324224352356353365644745467673444747464463633433737347353333763357453336762446354252356335425421133544211144442
412231321555333442353123332362425543424533553573666735637763454533745366765376746433756447475333356464464222432332345352341525223324314223324
331312433213455231253313366665632226633556433475477753337363656373433773757536533656447774556433367326623645656523564215343224513551431132223
224112234222241332225213354662554264323422253344474667333733456554564565467557537447534475756667355536646365532333536213215244225153322244412
244113243235224213541532412425436353634323524524735645546553763765557473655733554645336476657756623642432424252542344521515245415325122342431
333421331131543452331544433322665453665253334334233336445435345646447373734355537674545543737663646626335655635662215142322433242354342243112
133313231314112552244245534355643622352352432342233334476466373555376336634767555645553454753662546264266543564554134444222253115423131221131
131231412441413152554252155332345653224645365352224252676774544675574675674375465365754635336426223222236455655231354243412255433333333111224
332324343332244323555523343522424636242334346443522643425664563557445657753663433433742554644552342556462255264545525242315522431444111241243
242131114222434333535315551155432535643423333423326353532254255655746367555476375465645466663246524345534363325342524554524235232124123342242
213443214424214122543235113333321125533542665643265452423232525225353624456522652323524352424663342645352244451451351414355352332211232222141
331324322142214311244251224225412153666422233553532445352342364235225362266256253442662643454463636563552424333135251344313153142323433132323
133324412321343111133453143214253353444663336233236323356323263332552632535244356555555246466334633636434214335153524233455513241242143233331
112312232341121314233341331125215255233566334652552425242266535646254524434335664425346234266623244222224122425211534551415411323432223422431
111221111132441212123422453234455112532453426625322352463453654552623534226653432234662654524636336532544221415524444135141242433424422341213
132112142443434332213144334542214243531213325566463422625562222442645236252323332463366466355454556441122343223223544533444444111223232343312
123132114141224134221142521153123435354553455566553265365362456544335254345555663536355522235262422254555111123424125513443122342334241423213
222111222213331423242143325343351111342555413323153635663634225423236363264256253225335432323241313143325535154411221431341241322112213222333
233133121221424112412441244521151213235233325252323226243435524643246242326265222442643346314111554114124523453333352131423344424422332112111
313223121314322131323232111313433421514531352544111512123656226536435625655252522552646223335233154132134344324541142331432322433213231333223
122323311123331314242221442331134445133211444341324335245235554244244535654565323631324113113435231541311524113422324131342433243442233332323
131232121222413342213424423134225553425223335324412235455552131523111656231531242452533234455554145512342144111144431122223424423333132222113
111322123121134213313111224233414112541332235125122353234451522411134512122545415145522212213525144152312255153143114113221323132422321321122
212133123312121131311312443214311325443433324425455124152235512334253433334522144532143435341332543332153335423423341414241221311221213123233

13
day17/sample.txt Normal file
View File

@@ -0,0 +1,13 @@
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533

BIN
day18/c/day18 Executable file
View File

Binary file not shown.

369
day18/c/day18.c Normal file
View File

@@ -0,0 +1,369 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <limits.h>
#define LINE_MAX_LENGTH 256
#define MAX_ENTRIES 2048
#define MAX_QUEUE_LEN 256
#define MAX_EDGES 1024
#define MAX_BREAKPOINTS 512
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
typedef struct entry {
char direction;
long long count;
char rgb_string[10];
} entry_t;
typedef struct zone {
long long upper_left_corner[2];
long long lower_right_corner[2];
uint8_t outside;
} zone_t;
int cmp(const void* a, const void* b);
int on_edge(long long edges[MAX_EDGES][2][2], int edges_num, long long side[2][2]);
unsigned long long lake_volume(entry_t entries[], int entries_num);
void decode_rgbs(entry_t entries[], int entries_num);
int main() {
char *p, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
entry_t entries[MAX_ENTRIES];
memset(entries, 0, MAX_ENTRIES * sizeof(entry_t));
int entries_num = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
sscanf(buf, "%c %lli %s", &entries[entries_num].direction, &entries[entries_num].count, entries[entries_num].rgb_string);
entries_num++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
unsigned long long part1 = lake_volume(entries, entries_num);
decode_rgbs(entries, entries_num);
unsigned long long part2 = lake_volume(entries, entries_num);
printf("%llu\n", part1);
printf("%llu\n", part2);
free(buf);
}
int cmp(const void* a, const void* b) {
return *(long long*)a - *(long long*)b;
}
int on_edge(long long edges[MAX_EDGES][2][2], int edges_num, long long side[2][2]) {
for (int i = 0; i < edges_num; i++) {
if (edges[i][0][0] == side[0][0] && edges[i][0][0] == edges[i][1][0] && side[0][0] == side[1][0] && edges[i][0][1] <= side[0][1] && edges[i][1][1] >= side[1][1]) {
return 1;
}
if (edges[i][0][1] == side[0][1] && edges[i][0][1] == edges[i][1][1] && side[0][1] == side[1][1] && edges[i][0][0] <= side[0][0] && edges[i][1][0] >= side[1][0]) {
return 1;
}
}
return 0;
}
unsigned long long lake_volume(entry_t entries[], int entries_num) {
long long x = 0, y = 0, dir[2];
long long edges[MAX_EDGES][2][2];
memset(edges, 0, MAX_EDGES * 2 * 2 * sizeof(long long));
int edges_num = 0;
for (int i = 0; i < entries_num; i++) {
switch (entries[i].direction) {
case 'R':
dir[0] = 1;
dir[1] = 0;
edges[edges_num][0][0] = x;
edges[edges_num][0][1] = y;
edges[edges_num][1][0] = x + entries[i].count * dir[0];
edges[edges_num][1][1] = y + entries[i].count * dir[1];
edges_num++;
break;
case 'L':
dir[0] = -1;
dir[1] = 0;
edges[edges_num][1][0] = x;
edges[edges_num][1][1] = y;
edges[edges_num][0][0] = x + entries[i].count * dir[0];
edges[edges_num][0][1] = y + entries[i].count * dir[1];
edges_num++;
break;
case 'U':
dir[0] = 0;
dir[1] = -1;
edges[edges_num][1][0] = x;
edges[edges_num][1][1] = y;
edges[edges_num][0][0] = x + entries[i].count * dir[0];
edges[edges_num][0][1] = y + entries[i].count * dir[1];
edges_num++;
break;
case 'D':
dir[0] = 0;
dir[1] = 1;
edges[edges_num][0][0] = x;
edges[edges_num][0][1] = y;
edges[edges_num][1][0] = x + entries[i].count * dir[0];
edges[edges_num][1][1] = y + entries[i].count * dir[1];
edges_num++;
break;
}
x += entries[i].count * dir[0];
y += entries[i].count * dir[1];
}
// Find smallest and largest x and y
long long min_x = LLONG_MAX, max_x = LLONG_MIN, min_y = LLONG_MAX, max_y = LLONG_MIN;
for (int i = 0; i < edges_num; i++) {
if (edges[i][0][0] < min_x) {
min_x = edges[i][0][0];
}
if (edges[i][1][0] > max_x) {
max_x = edges[i][1][0];
}
if (edges[i][0][1] < min_y) {
min_y = edges[i][0][1];
}
if (edges[i][1][1] > max_y) {
max_y = edges[i][1][1];
}
}
// X breakpoints
long long x_breakpoints[MAX_BREAKPOINTS], x_breakpoints_num = 0;
for (int i = 0; i < edges_num; i++) {
int found = 0;
for (int j = 0; j < x_breakpoints_num; j++) {
if (edges[i][0][0] == x_breakpoints[j]) {
found = 1;
break;
}
}
if (!found) {
x_breakpoints[x_breakpoints_num] = edges[i][0][0];
x_breakpoints_num++;
}
found = 0;
for (int j = 0; j < x_breakpoints_num; j++) {
if (edges[i][1][0] == x_breakpoints[j]) {
found = 1;
break;
}
}
if (!found) {
x_breakpoints[x_breakpoints_num] = edges[i][1][0];
x_breakpoints_num++;
}
}
qsort(x_breakpoints, x_breakpoints_num, sizeof(x_breakpoints[0]), &cmp);
// Y breakpoints
long long y_breakpoints[MAX_BREAKPOINTS], y_breakpoints_num = 0;
for (int i = 0; i < edges_num; i++) {
int found = 0;
for (int j = 0; j < y_breakpoints_num; j++) {
if (edges[i][0][1] == y_breakpoints[j]) {
found = 1;
break;
}
}
if (!found) {
y_breakpoints[y_breakpoints_num] = edges[i][0][1];
y_breakpoints_num++;
}
found = 0;
for (int j = 0; j < y_breakpoints_num; j++) {
if (edges[i][1][1] == y_breakpoints[j]) {
found = 1;
break;
}
}
if (!found) {
y_breakpoints[y_breakpoints_num] = edges[i][1][1];
y_breakpoints_num++;
}
}
qsort(y_breakpoints, y_breakpoints_num, sizeof(y_breakpoints[0]), &cmp);
zone_t **zones = (zone_t**)malloc((x_breakpoints_num - 1) * sizeof(zone_t*));
for (int i = 0; i < x_breakpoints_num - 1; i++) {
zones[i] = (zone_t*)malloc((y_breakpoints_num - 1) * sizeof(zone_t));
memset(zones[i], 0, (y_breakpoints_num - 1) * sizeof(zone_t));
}
zone_t *curr;
for (int i = 0; i < y_breakpoints_num - 1; i++) {
for (int j = 0; j < x_breakpoints_num - 1; j++) {
curr = &zones[j][i];
curr->upper_left_corner[0] = x_breakpoints[j];
curr->upper_left_corner[1] = y_breakpoints[i];
curr->lower_right_corner[0] = x_breakpoints[j + 1];
curr->lower_right_corner[1] = y_breakpoints[i + 1];
}
}
// Mark zones as outside
int found = 1;
while (found) {
found = 0;
for (int i = 0; i < y_breakpoints_num - 1; i++) {
for (int j = 0; j < x_breakpoints_num - 1; j++) {
if (zones[j][i].outside) {
continue;
}
// LEFT
long long side[2][2];
side[0][0] = zones[j][i].upper_left_corner[0];
side[0][1] = zones[j][i].upper_left_corner[1];
side[1][0] = zones[j][i].upper_left_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (j == 0 && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
if (j > 0 && zones[j-1][i].outside && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
// RIGHT
side[0][0] = zones[j][i].lower_right_corner[0];
side[0][1] = zones[j][i].upper_left_corner[1];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (j == x_breakpoints_num - 2 && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
if (j < x_breakpoints_num - 2 && zones[j+1][i].outside && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
// UP
side[0][0] = zones[j][i].upper_left_corner[0];
side[0][1] = zones[j][i].upper_left_corner[1];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].upper_left_corner[1];
if (i == 0 && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
if (i > 0 && zones[j][i-1].outside && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
// DOWN
side[0][0] = zones[j][i].upper_left_corner[0];
side[0][1] = zones[j][i].lower_right_corner[1];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (i == y_breakpoints_num - 2 && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
if (i < y_breakpoints_num - 2 && zones[j][i+1].outside && !on_edge(edges, edges_num, side)) {
zones[j][i].outside = 1;
found = 1;
}
}
}
}
unsigned long long sum = 0;
unsigned long long out = 0;
for (int i = 0; i < y_breakpoints_num - 1; i++) {
for (int j = 0; j < x_breakpoints_num - 1; j++) {
if (zones[j][i].outside) {
out++;
}
}
}
for (int i = 0; i < y_breakpoints_num - 1; i++) {
for (int j = 0; j < x_breakpoints_num - 1; j++) {
if (zones[j][i].outside) {
continue;
}
sum += (zones[j][i].lower_right_corner[0] - zones[j][i].upper_left_corner[0] - 1) * (zones[j][i].lower_right_corner[1] - zones[j][i].upper_left_corner[1] - 1);
// RIGHT
long long side[2][2];
side[0][0] = zones[j][i].lower_right_corner[0];
side[0][1] = zones[j][i].upper_left_corner[1];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (!on_edge(edges, edges_num, side)) {
sum += side[1][1] - side[0][1] - 1;
}
// DOWN
side[0][1] = zones[j][i].lower_right_corner[1];
side[0][0] = zones[j][i].upper_left_corner[0];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (!on_edge(edges, edges_num, side)) {
sum += side[1][0] - side[0][0] - 1;
}
// CORNER
side[0][1] = zones[j][i].lower_right_corner[1];
side[0][0] = zones[j][i].lower_right_corner[0];
side[1][0] = zones[j][i].lower_right_corner[0];
side[1][1] = zones[j][i].lower_right_corner[1];
if (!on_edge(edges, edges_num, side)) {
sum++;
}
}
}
for (int i = 0; i < edges_num; i++) {
sum += (edges[i][1][0] - edges[i][0][0]) + (edges[i][1][1] - edges[i][0][1]);
}
for (int i = 0; i < x_breakpoints_num - 1; i++) {
free(zones[i]);
}
free(zones);
return sum;
}
void decode_rgbs(entry_t entries[], int entries_num) {
for (int i = 0; i < entries_num; i++) {
char value[6], *p;
memset(value, 0, 6);
p = entries[i].rgb_string;
while(*p != '#') p++;
p++;
strncpy(value, p, 5);
sscanf(value, "%llx", &entries[i].count);
switch(entries[i].rgb_string[7]) {
case '0':
entries[i].direction = 'R';
break;
case '1':
entries[i].direction = 'D';
break;
case '2':
entries[i].direction = 'L';
break;
case '3':
entries[i].direction = 'U';
break;
}
}
}

660
day18/input.txt Normal file
View File

@@ -0,0 +1,660 @@
L 3 (#07b412)
D 5 (#713ad1)
L 2 (#11e6d2)
D 4 (#6ba7d3)
L 3 (#7cfca2)
U 5 (#0a85c3)
L 2 (#3a5282)
U 4 (#6f91a3)
L 3 (#32c2f0)
U 6 (#2dd7b3)
L 6 (#848c30)
U 7 (#077a43)
R 5 (#ba4692)
U 6 (#49b551)
R 3 (#14d162)
U 8 (#397871)
R 5 (#1c73f2)
D 8 (#7130e1)
R 4 (#c84852)
U 2 (#203b91)
R 3 (#288e30)
U 5 (#91ba91)
R 4 (#5dda70)
U 3 (#91ba93)
R 4 (#732500)
U 4 (#0676f1)
L 9 (#6701a2)
U 4 (#5c38c1)
L 7 (#0f7480)
U 4 (#921b21)
L 6 (#83d9e0)
U 2 (#28c691)
L 7 (#1b6de0)
U 2 (#02cee1)
L 4 (#5b66b0)
U 4 (#bdb093)
L 6 (#75eb20)
U 4 (#928251)
L 4 (#6bb430)
D 7 (#44ad91)
L 2 (#806b50)
U 7 (#7d1391)
L 4 (#7a3652)
U 5 (#9cbf41)
L 5 (#905712)
U 2 (#70beb3)
L 2 (#bede02)
U 7 (#70beb1)
R 3 (#a2c232)
U 5 (#7b0ad1)
R 5 (#01d012)
U 5 (#2b1901)
R 7 (#698c32)
U 4 (#a19d91)
R 4 (#7dbb92)
U 3 (#8feb41)
R 5 (#021ab2)
U 3 (#42a773)
R 6 (#99c6c2)
D 7 (#8f7b33)
R 5 (#01fd82)
D 5 (#8e7263)
R 2 (#37e492)
U 5 (#10bff3)
R 5 (#a09242)
U 7 (#126723)
R 3 (#864230)
D 8 (#70bc43)
R 5 (#7cf4a0)
D 2 (#8135d3)
R 6 (#144b70)
U 5 (#0c51e3)
L 6 (#00e590)
U 4 (#44ba73)
L 2 (#4177e2)
U 4 (#a4c5b1)
L 6 (#6f2f12)
U 3 (#9a4ce1)
L 4 (#979402)
D 7 (#2a0ce1)
L 3 (#0c1b70)
U 7 (#58ca71)
L 3 (#95d640)
U 4 (#549241)
R 10 (#a1f1b2)
U 3 (#1753b1)
L 5 (#1d32d2)
U 5 (#9a58d3)
L 5 (#4dc1b2)
U 6 (#1ab693)
L 4 (#5e8df2)
U 3 (#4dabf3)
R 6 (#972492)
U 5 (#3198c3)
R 4 (#972490)
U 3 (#54b613)
R 6 (#4a8bb2)
U 2 (#a4c5b3)
R 3 (#126042)
U 9 (#54acc3)
L 5 (#2b4682)
U 4 (#88f453)
L 4 (#2b4680)
U 8 (#4a4283)
L 2 (#8bfdf0)
U 2 (#a76851)
L 4 (#0a9b30)
U 6 (#43a141)
L 3 (#14fd80)
U 3 (#7e5391)
L 6 (#43ba40)
D 2 (#146e93)
L 4 (#9c71b0)
D 7 (#5ae483)
L 5 (#0bcd70)
U 6 (#52a1c3)
L 4 (#3307f0)
U 4 (#1da3f3)
L 3 (#7a8c80)
D 5 (#89c463)
L 2 (#2984e0)
D 2 (#828673)
L 6 (#2cf252)
U 7 (#500061)
L 4 (#a07ff2)
U 3 (#565f21)
R 3 (#09ef22)
U 3 (#a65f83)
R 5 (#410672)
U 4 (#24fe73)
L 5 (#b6ed72)
U 8 (#4574d3)
L 6 (#2e3142)
D 8 (#7ba311)
L 3 (#1bc442)
U 6 (#38b161)
L 4 (#8ada92)
U 2 (#24cdf1)
L 3 (#37b3e2)
U 3 (#3a3ec1)
R 5 (#242ea2)
U 2 (#1fe073)
R 5 (#9ad182)
U 6 (#4a9193)
R 6 (#34c480)
U 2 (#9ff123)
R 5 (#34c482)
U 4 (#08fe03)
R 3 (#46d292)
D 10 (#19fca3)
R 3 (#1ea7d2)
D 6 (#c2d531)
R 8 (#614f82)
D 6 (#c2d533)
R 6 (#777202)
D 2 (#38f573)
R 3 (#84e142)
D 3 (#036d11)
R 5 (#113352)
D 5 (#036d13)
R 3 (#7ad752)
U 4 (#969de3)
R 7 (#7887d0)
U 3 (#31aa61)
R 4 (#affca0)
U 6 (#31aa63)
R 2 (#374010)
U 6 (#574d53)
L 4 (#4edbd2)
U 2 (#2271c1)
L 7 (#595d32)
U 3 (#2271c3)
R 3 (#2c6532)
U 5 (#2754d3)
R 8 (#8b2652)
U 4 (#b27ab3)
R 6 (#824b80)
D 4 (#587173)
R 4 (#3715b0)
D 5 (#8f0b61)
R 3 (#6a2ac0)
D 3 (#1e0b21)
R 5 (#29a290)
D 3 (#8850b3)
R 5 (#9013e0)
D 3 (#24c5d3)
R 5 (#2b12d0)
U 6 (#4090a3)
R 3 (#9cddd0)
D 6 (#a93843)
R 4 (#6fff30)
D 4 (#3f1121)
L 7 (#76f2c0)
D 4 (#4f9e11)
L 3 (#76f2c2)
D 8 (#480d31)
L 6 (#694740)
D 5 (#177e81)
L 9 (#692aa2)
D 3 (#10b971)
R 9 (#42f012)
D 3 (#10b973)
R 3 (#2c6202)
D 8 (#851881)
R 3 (#04ae62)
D 2 (#470d81)
R 4 (#155e10)
U 3 (#01b053)
R 2 (#c55dd0)
U 5 (#01b051)
R 9 (#026f30)
D 2 (#113081)
R 2 (#694742)
D 4 (#22d821)
L 5 (#03eb80)
D 5 (#3bc5d1)
L 4 (#69bf90)
D 3 (#3fdce3)
L 4 (#2d1350)
D 2 (#3fdce1)
L 3 (#65f500)
U 4 (#bd7f81)
L 8 (#8382e0)
D 4 (#160053)
L 7 (#5ff9f0)
D 4 (#ab40d3)
R 6 (#5ff9f2)
D 2 (#58d1b3)
R 2 (#6bcc80)
D 6 (#b187c3)
R 6 (#73b290)
D 3 (#9fbf43)
R 4 (#630f00)
D 2 (#4f1b13)
R 7 (#543b30)
D 7 (#114c43)
R 6 (#629ac0)
D 6 (#1bedb3)
R 4 (#016f50)
D 2 (#285e31)
R 3 (#611460)
D 7 (#0dfd61)
R 2 (#75f3e0)
D 2 (#1e79b1)
R 4 (#144e90)
U 5 (#d16ff1)
R 4 (#144e92)
U 6 (#113511)
R 5 (#610450)
U 3 (#1291c1)
R 2 (#328eb0)
U 5 (#2f3f73)
R 9 (#126af2)
U 4 (#946843)
L 6 (#126af0)
U 5 (#866453)
L 7 (#352e70)
U 4 (#5f8413)
L 7 (#c618e2)
U 3 (#6c2813)
L 2 (#c618e0)
U 3 (#1d3db3)
R 8 (#37e470)
U 2 (#1705b3)
R 6 (#6ab412)
U 6 (#961873)
R 3 (#6ab410)
U 8 (#30e6f3)
R 5 (#3308e0)
U 3 (#1fc8c1)
R 3 (#3450a0)
D 8 (#9d4391)
R 3 (#6a9c70)
D 2 (#25d071)
R 4 (#3e5330)
D 6 (#401381)
R 5 (#399052)
D 3 (#ab9e81)
R 4 (#2622a2)
D 4 (#68c4c3)
R 4 (#2c5f92)
D 3 (#8021d3)
R 5 (#75bd02)
D 3 (#1885a1)
R 3 (#67b420)
D 3 (#7df001)
R 6 (#67b422)
D 2 (#5270f1)
R 3 (#2bbed2)
D 9 (#863371)
L 3 (#45ba70)
D 6 (#49f553)
L 2 (#693800)
D 6 (#49f551)
L 7 (#450b90)
D 7 (#3b7aa1)
R 5 (#399050)
D 2 (#0b37f1)
R 4 (#5cbff0)
U 4 (#573103)
R 3 (#3668c0)
U 9 (#573101)
R 4 (#7d0a50)
U 7 (#316341)
R 3 (#4cbab0)
U 8 (#5c0f21)
R 3 (#22b3a2)
U 3 (#804941)
R 4 (#22b3a0)
U 7 (#173641)
R 5 (#1b7230)
U 3 (#8a0511)
R 4 (#1a5f82)
D 5 (#52eda1)
R 4 (#1a5f80)
U 5 (#38db21)
R 5 (#2f4720)
U 2 (#2a75c1)
R 9 (#aa4d40)
U 3 (#538141)
L 3 (#7ad770)
U 5 (#66ec01)
L 5 (#53e812)
U 3 (#539a81)
L 4 (#53e810)
U 5 (#413821)
L 5 (#618330)
D 5 (#a4bf71)
L 6 (#56e4b2)
U 3 (#acf591)
L 2 (#890732)
U 8 (#2a62d1)
R 4 (#458e22)
U 5 (#2ef8e3)
R 4 (#273c60)
U 4 (#439bc3)
R 2 (#273c62)
D 4 (#64c3c3)
R 5 (#0e0c12)
D 5 (#9481d1)
R 3 (#5321d2)
U 4 (#5d6361)
R 7 (#531760)
U 7 (#068711)
R 7 (#216570)
U 7 (#a20e51)
L 3 (#216572)
D 3 (#46afb1)
L 8 (#4e8f70)
D 6 (#1261b1)
L 5 (#3840e0)
U 6 (#7b0901)
L 3 (#75cf00)
U 3 (#b56e51)
L 7 (#43b850)
U 3 (#01dfc1)
L 6 (#067e40)
U 7 (#613913)
R 5 (#30ff10)
U 7 (#285d33)
R 5 (#72e5c0)
D 7 (#285d31)
R 3 (#3fdcf0)
U 6 (#601d03)
R 4 (#312812)
U 4 (#4ea643)
R 6 (#8253e2)
D 7 (#4ea641)
R 2 (#3045d2)
U 7 (#8362b3)
R 7 (#631a70)
U 4 (#ba56c3)
R 6 (#28e520)
U 2 (#568a53)
R 3 (#c1d452)
U 2 (#3e2a33)
R 5 (#c1d450)
U 5 (#0f6e43)
R 4 (#c65ae0)
U 3 (#790e83)
R 2 (#012b40)
U 7 (#2ea6b3)
R 4 (#358722)
D 2 (#7b3bf3)
R 6 (#adbd32)
D 4 (#4f7b63)
R 5 (#0d26f2)
U 5 (#47f363)
R 2 (#4c6160)
D 5 (#70ff63)
R 5 (#284290)
D 3 (#231103)
L 7 (#540e80)
D 2 (#8b51b3)
L 5 (#662c40)
D 4 (#22e7e3)
R 7 (#1d46a0)
D 3 (#343a43)
L 6 (#4d7d90)
D 4 (#31a643)
L 10 (#4e6b30)
U 4 (#31a641)
L 4 (#5abc90)
D 3 (#799953)
L 4 (#9e4ed0)
D 5 (#3b73e3)
R 7 (#390760)
D 3 (#bd3533)
L 7 (#390762)
D 5 (#075bd3)
L 3 (#1b7350)
D 3 (#a01d23)
R 4 (#a56752)
D 2 (#6a4313)
R 5 (#529462)
D 4 (#843c43)
R 4 (#829482)
U 4 (#7914e3)
R 3 (#2f0b90)
U 7 (#5f3d53)
R 6 (#114980)
D 7 (#60b7e3)
R 4 (#b9b2f0)
D 4 (#60b7e1)
R 5 (#2143f0)
D 3 (#2f86d3)
L 7 (#5f4440)
D 4 (#366293)
L 5 (#5baae0)
U 8 (#80a9a3)
L 3 (#61f680)
D 8 (#80a9a1)
L 5 (#8bdce0)
U 4 (#46e4d1)
L 5 (#120950)
D 8 (#7c15b1)
L 4 (#641450)
D 2 (#047bf1)
L 2 (#3b00c0)
D 7 (#a18c33)
L 3 (#543b70)
D 2 (#a18c31)
L 7 (#466150)
D 4 (#5c3ad1)
R 4 (#5caa80)
D 2 (#230201)
R 6 (#955102)
D 6 (#b200c1)
R 7 (#955100)
D 3 (#1d8ff1)
R 3 (#26f9d0)
D 8 (#160d71)
R 2 (#483920)
D 4 (#4577c3)
R 8 (#92ef90)
D 5 (#a57e83)
R 7 (#0715a0)
D 3 (#1da9e3)
L 2 (#627970)
D 6 (#573ee1)
L 8 (#006f30)
D 4 (#735da1)
L 4 (#006f32)
D 4 (#266951)
R 3 (#9745a0)
D 6 (#412bb1)
R 7 (#83f8f2)
D 2 (#6c4d31)
R 4 (#6a7e72)
D 3 (#578921)
L 7 (#6c5d92)
D 2 (#5485c1)
L 5 (#300852)
D 4 (#cdf091)
L 2 (#544dd2)
D 3 (#1259f1)
L 7 (#882292)
D 6 (#03e4b1)
R 7 (#cdf192)
D 3 (#5f3c41)
R 4 (#c836b0)
D 5 (#4cfdc1)
L 2 (#36a590)
D 4 (#244753)
L 2 (#73bd60)
D 3 (#244751)
L 7 (#72cc90)
U 4 (#0e0d21)
L 5 (#21ce60)
D 4 (#523dd1)
L 9 (#041252)
D 3 (#8cfc81)
R 5 (#041250)
D 5 (#365031)
R 6 (#21ce62)
U 5 (#17b3e1)
R 5 (#75a900)
D 3 (#8d4fe1)
R 4 (#75a902)
D 3 (#342091)
R 5 (#8d5570)
D 5 (#a6f9a3)
R 5 (#59f560)
D 4 (#49ae63)
R 5 (#59f562)
D 5 (#2dc6c3)
R 4 (#8fb550)
U 6 (#884bf3)
R 3 (#17ea50)
U 4 (#560143)
R 5 (#623ad0)
D 8 (#5dfc41)
R 5 (#ca0040)
D 2 (#5bc201)
R 4 (#ca0042)
D 3 (#7008b1)
R 7 (#b87530)
U 4 (#5e8821)
R 4 (#817080)
U 7 (#69e951)
R 2 (#518e30)
U 4 (#0e9831)
R 5 (#29ea82)
D 4 (#26f041)
R 9 (#6a1862)
D 4 (#8d3971)
L 4 (#6a1860)
D 4 (#215e71)
L 5 (#29ea80)
D 3 (#282e61)
R 3 (#bc6172)
D 8 (#17c4f1)
L 6 (#1abf10)
U 5 (#1a15e1)
L 3 (#2ed960)
D 5 (#24f4f3)
L 4 (#9d6b00)
D 6 (#24f4f1)
L 2 (#704c40)
D 4 (#45b0c1)
L 5 (#93a182)
D 6 (#53b3b1)
L 5 (#5f72d2)
U 6 (#76a9f1)
L 4 (#643b62)
D 3 (#3f41f1)
L 6 (#691122)
D 2 (#606491)
L 5 (#4c5b02)
D 5 (#6d1fa1)
L 2 (#bf6472)
D 6 (#6d1fa3)
L 6 (#989bb2)
U 4 (#606493)
L 3 (#348412)
U 6 (#9e4561)
L 4 (#9b1742)
U 8 (#42a901)
L 5 (#09f8f2)
U 3 (#63a8c3)
R 8 (#591640)
U 4 (#2dcda3)
L 8 (#591642)
U 4 (#4f7803)
L 2 (#3e5fe2)
U 5 (#0b9e93)
L 3 (#cae6b2)
U 4 (#7399f3)
L 7 (#28c482)
U 6 (#d2ee83)
L 3 (#28c480)
U 6 (#2f0423)
L 3 (#320f52)
U 2 (#5f4ab1)
L 4 (#8d6672)
U 6 (#b09261)
L 6 (#718952)
D 8 (#750591)
L 4 (#33df82)
D 5 (#1052a1)
L 4 (#9a90e2)
U 6 (#8eb861)
L 2 (#0c1e92)
U 4 (#53b871)
L 4 (#0739f2)
U 4 (#76cfb1)
L 6 (#9b6922)
U 7 (#76cfb3)
L 5 (#846eb2)
D 4 (#53b873)
L 3 (#6b7ae2)
D 4 (#59dd21)
L 2 (#60b0b2)
D 6 (#a53b01)
R 5 (#767632)
D 4 (#6c6bb1)
L 4 (#b3a932)
D 3 (#0660a1)
L 5 (#12b390)
U 4 (#9c9d01)
L 4 (#12b392)
U 3 (#910691)
L 4 (#8dec12)
U 6 (#65ba53)
L 3 (#1e02b2)
U 2 (#b05803)
L 2 (#1e02b0)
U 8 (#8a5d93)
L 2 (#3bea42)
U 5 (#032da3)
R 8 (#0aabc2)
U 3 (#4a0a13)
L 8 (#9f1c02)
U 4 (#58a0f3)
L 3 (#9f1c00)
D 3 (#3a2cf3)
L 7 (#169b22)
D 2 (#004253)
L 2 (#841fd2)
D 9 (#0b8473)
L 4 (#3f85a2)
D 6 (#134bd3)
L 2 (#17f832)
D 5 (#8870f3)
L 2 (#87cf62)
D 3 (#8b9fa3)
L 2 (#6ca082)
D 3 (#9a6ea3)
L 7 (#60cbc2)
D 4 (#9a6ea1)
L 2 (#296752)
D 3 (#b0d5c3)
L 8 (#0e0100)
D 5 (#218503)
L 6 (#36a620)
D 7 (#071463)
L 3 (#669dd0)
D 3 (#3099a1)
L 7 (#385d40)
D 5 (#3099a3)
L 7 (#3c66b0)
D 3 (#8fec33)
L 2 (#4ce142)
D 4 (#4b7051)
L 8 (#55b322)
D 3 (#4b7053)
L 7 (#38cd62)
D 3 (#668b13)
L 4 (#36a622)
D 2 (#581f83)
L 4 (#0e0102)
D 7 (#356bd3)
L 5 (#3eb472)
D 3 (#4ca003)
L 3 (#6afe62)
D 5 (#4ca001)
L 5 (#536dd2)
U 8 (#978bb3)
L 5 (#6118d2)
U 7 (#aec403)

14
day18/sample.txt Normal file
View File

@@ -0,0 +1,14 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

BIN
day19/c/day19 Executable file
View File

Binary file not shown.

303
day19/c/day19.c Normal file
View File

@@ -0,0 +1,303 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_MAX_LENGTH 256
#define MAX_INSTRUCTIONS 16
#define MAX_WORKFLOWS 1024
#define MAX_PARTS 2048
#define NUM_0_CHARCODE 48
#define NUM_9_CHARCODE NUM_0_CHARCODE + 9
#define MAX_BREAKPOINTS 1024
typedef struct instruction {
char category;
char operator;
int value;
char destination[16];
int destination_length;
} instruction_t;
typedef struct workflow {
char name[16];
int name_length;
instruction_t instructions[MAX_INSTRUCTIONS];
int instructions_num;
char default_action[16];
int default_action_length;
} workflow_t;
typedef struct part {
int x;
int m;
int a;
int s;
} part_t;
typedef struct breakpoints {
int x[MAX_BREAKPOINTS];
int x_num;
int m[MAX_BREAKPOINTS];
int m_num;
int a[MAX_BREAKPOINTS];
int a_num;
int s[MAX_BREAKPOINTS];
int s_num;
} breakpoints_t;
int cmp(const void *a, const void *b);
char get_workflow_result(workflow_t workflows[], int workflows_num, part_t part, workflow_t in);
int main() {
char *p, *q, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
int first = 1;
workflow_t *workflows = (workflow_t*)malloc(MAX_WORKFLOWS * sizeof(workflow_t));
memset(workflows, 0, MAX_WORKFLOWS * sizeof(workflow_t));
int workflows_num = 0;
part_t *parts = (part_t*)malloc(MAX_PARTS * sizeof(part_t));
memset(parts, 0, MAX_PARTS * sizeof(part_t));
int parts_num = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
if (buf[0] == '\n') {
first = 0;
} else {
p = buf;
if (first) {
while (*p != '{') p++;
strncpy(workflows[workflows_num].name, buf, p - buf);
workflows[workflows_num].name_length = p - buf;
p++;
while(*p != '\n') {
q = p;
while(*p != '<' && *p != '>' && *p != '}') p++;
// Default action
if (*p == '}') {
strncpy(workflows[workflows_num].default_action, q, p - q);
workflows[workflows_num].default_action_length = p - q;
} else {
workflows[workflows_num].instructions[workflows[workflows_num].instructions_num].category = *q;
workflows[workflows_num].instructions[workflows[workflows_num].instructions_num].operator = *p;
p++;
sscanf(p, "%i", &workflows[workflows_num].instructions[workflows[workflows_num].instructions_num].value);
while (*p != ':') p++;
p++;
q = p;
while (*p != ',') p++;
strncpy(workflows[workflows_num].instructions[workflows[workflows_num].instructions_num].destination, q, p - q);
workflows[workflows_num].instructions[workflows[workflows_num].instructions_num].destination_length = p - q;
workflows[workflows_num].instructions_num++;
}
p++;
}
workflows_num++;
} else {
while (*p < NUM_0_CHARCODE || *p > NUM_9_CHARCODE) p++;
sscanf(p, "%i", &parts[parts_num].x);
while (*p != ',') p++;
while (*p < NUM_0_CHARCODE || *p > NUM_9_CHARCODE) p++;
sscanf(p, "%i", &parts[parts_num].m);
while (*p != ',') p++;
while (*p < NUM_0_CHARCODE || *p > NUM_9_CHARCODE) p++;
sscanf(p, "%i", &parts[parts_num].a);
while (*p != ',') p++;
while (*p < NUM_0_CHARCODE || *p > NUM_9_CHARCODE) p++;
sscanf(p, "%i", &parts[parts_num].s);
parts_num++;
}
}
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
// Find start
workflow_t *start;
for (int i = 0; i < workflows_num; i++) {
if (!strcmp(workflows[i].name, "in")) {
start = &workflows[i];
break;
}
}
int part1 = 0;
for (int i = 0; i < parts_num; i++) {
char result = get_workflow_result(workflows, workflows_num, parts[i], *start);
if (result == 'A') {
part1 += parts[i].x + parts[i].m + parts[i].a + parts[i].s;
}
}
printf("%i\n", part1);
breakpoints_t breakpoints;
memset(&breakpoints, 0, sizeof(breakpoints_t));
for (int i = 0; i < workflows_num; i++) {
for (int j = 0; j < workflows[i].instructions_num; j++) {
if (workflows[i].instructions[j].category == 'x') {
int value = workflows[i].instructions[j].operator == '<' ? workflows[i].instructions[j].value : workflows[i].instructions[j].value + 1;
int found = 0;
for (int k = 0; k < breakpoints.x_num; k++) {
if (breakpoints.x[k] == value) {
found = 1;
break;
}
}
if (!found) {
breakpoints.x[breakpoints.x_num] = value;
breakpoints.x_num++;
}
}
if (workflows[i].instructions[j].category == 'm') {
int value = workflows[i].instructions[j].operator == '<' ? workflows[i].instructions[j].value : workflows[i].instructions[j].value + 1;
int found = 0;
for (int k = 0; k < breakpoints.m_num; k++) {
if (breakpoints.m[k] == value) {
found = 1;
break;
}
}
if (!found) {
breakpoints.m[breakpoints.m_num] = value;
breakpoints.m_num++;
}
}
if (workflows[i].instructions[j].category == 'a') {
int value = workflows[i].instructions[j].operator == '<' ? workflows[i].instructions[j].value : workflows[i].instructions[j].value + 1;
int found = 0;
for (int k = 0; k < breakpoints.a_num; k++) {
if (breakpoints.a[k] == value) {
found = 1;
break;
}
}
if (!found) {
breakpoints.a[breakpoints.a_num] = value;
breakpoints.a_num++;
}
}
if (workflows[i].instructions[j].category == 's') {
int value = workflows[i].instructions[j].operator == '<' ? workflows[i].instructions[j].value : workflows[i].instructions[j].value + 1;
int found = 0;
for (int k = 0; k < breakpoints.s_num; k++) {
if (breakpoints.s[k] == value) {
found = 1;
break;
}
}
if (!found) {
breakpoints.s[breakpoints.s_num] = value;
breakpoints.s_num++;
}
}
}
}
qsort(breakpoints.x, breakpoints.x_num, sizeof(breakpoints.x[0]), &cmp);
qsort(breakpoints.m, breakpoints.m_num, sizeof(breakpoints.m[0]), &cmp);
qsort(breakpoints.a, breakpoints.a_num, sizeof(breakpoints.a[0]), &cmp);
qsort(breakpoints.s, breakpoints.s_num, sizeof(breakpoints.s[0]), &cmp);
double part2 = 0;
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < breakpoints.x_num + 1; i++) {
for (int j = 0; j < breakpoints.m_num + 1; j++) {
for (int k = 0; k < breakpoints.a_num + 1; k++) {
for (int l = 0; l < breakpoints.s_num + 1; l++) {
unsigned long x0 = i == 0 ? 1 : breakpoints.x[i - 1];
unsigned long x1 = i == breakpoints.x_num ? 4001 : breakpoints.x[i];
unsigned long m0 = j == 0 ? 1 : breakpoints.m[j - 1];
unsigned long m1 = j == breakpoints.m_num ? 4001 : breakpoints.m[j];
unsigned long a0 = k == 0 ? 1 : breakpoints.a[k - 1];
unsigned long a1 = k == breakpoints.a_num ? 4001 : breakpoints.a[k];
unsigned long s0 = l == 0 ? 1 : breakpoints.s[l - 1];
unsigned long s1 = l == breakpoints.s_num ? 4001 : breakpoints.s[l];
part_t metapart = {
.x = x0,
.m = m0,
.a = a0,
.s = s0,
};
char result = get_workflow_result(workflows, workflows_num, metapart, *start);
#pragma omp critical
if (result == 'A') {
part2 += (double)(x1 - x0) * (double)(m1 - m0) * (double)(a1 - a0) * (double)(s1 - s0);
}
}
}
}
}
printf("%f\n", part2);
free(buf);
free(workflows);
}
int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
char get_workflow_result(workflow_t workflows[], int workflows_num, part_t part, workflow_t in) {
for (int i = 0; i < in.instructions_num; i++) {
int value;
switch (in.instructions[i].category) {
case 'x':
value = part.x;
break;
case 'm':
value = part.m;
break;
case 'a':
value = part.a;
break;
case 's':
value = part.s;
break;
}
int matches;
switch (in.instructions[i].operator) {
case '<':
matches = value < in.instructions[i].value;
break;
case '>':
matches = value > in.instructions[i].value;
break;
}
if (matches) {
if (!strcmp(in.instructions[i].destination, "A")) {
return 'A';
}
if (!strcmp(in.instructions[i].destination, "R")) {
return 'R';
}
for (int j = 0; j < workflows_num; j++) {
if (!strcmp(workflows[j].name, in.instructions[i].destination)) {
return get_workflow_result(workflows, workflows_num, part, workflows[j]);
}
}
}
}
if (!strcmp(in.default_action, "A")) {
return 'A';
}
if (!strcmp(in.default_action, "R")) {
return 'R';
}
for (int i = 0; i < workflows_num; i++) {
if (!strcmp(workflows[i].name, in.default_action)) {
return get_workflow_result(workflows, workflows_num, part, workflows[i]);
}
}
return 'F';
}

769
day19/input.txt Normal file
View File

@@ -0,0 +1,769 @@
mxf{m<2310:phv,zs}
zjr{s>1167:zqt,skq}
hm{x>2975:rkl,x>2818:vhs,a>1062:bkc,kjg}
dkl{m>2321:ctj,x>3530:A,gj}
zk{s<1218:R,A}
zkd{m>864:A,a>488:R,jcp}
zdl{m>3429:A,m>2961:R,R}
xmd{a<576:A,a<1045:R,a<1386:R,A}
kzz{m>1106:R,a<508:R,x>2970:A,R}
gdr{x<139:R,x<183:A,s<222:A,R}
rb{m<3239:R,a<3546:A,A}
xns{x<621:vrz,gm}
nd{s>2927:xnx,x<3488:A,a<434:pg,A}
fl{s<3624:qg,m>952:qf,kjt}
vhm{x<3815:R,s>1774:A,m>643:R,A}
krp{a<2439:R,m>298:R,x<1440:R,R}
njj{x<2856:cjc,x<2936:R,a>598:R,kzz}
jp{m>1745:R,x<1239:A,a<625:lt,qbq}
rhd{a<3515:R,a<3543:A,s<3959:R,R}
htm{a>284:R,m<1961:R,s<2226:R,A}
kjj{a>704:fcn,s<439:lq,R}
jkk{s>1376:R,a<341:A,A}
jt{s<3384:A,x<3143:R,xgd}
bcm{a<1408:R,m<425:R,A}
hc{s>1126:A,m>1159:A,R}
jff{a>3411:A,s<804:jn,A}
knh{s>2643:szq,A}
klj{x>1392:A,a<2628:R,R}
kfx{a>1268:R,A}
cxd{a>1275:A,s>1768:R,R}
clq{a>1293:R,x>3243:R,R}
mpc{a<2362:R,R}
ng{a>578:A,x>1430:A,R}
hh{a>2697:A,s<1336:R,a<2167:A,A}
cjc{a<661:R,s>1113:A,x<2787:A,A}
fgv{m>3553:R,m<3400:A,m>3492:A,A}
pqk{m<110:R,A}
zqt{m<1713:mg,s>1555:jfx,s<1309:zm,A}
jrz{a<665:nh,tgp}
vsf{s<2496:R,R}
xdc{a>2214:R,A}
kkm{s>3217:A,x<3895:R,a<857:A,R}
sbh{s>1082:ct,sgx}
kcn{x<3313:A,x<3389:mmt,phd}
sgg{a<817:rf,s>2209:nhz,R}
jd{s>2746:R,a>2645:R,R}
pb{x>2553:A,qb}
hq{a<1200:A,a<1322:A,m<968:R,R}
kfz{x<3631:A,R}
fvf{a>917:drd,s>3325:qt,kcn}
ctj{m>2668:R,A}
tbl{x>2400:ql,m<2122:cs,m>2504:rq,tpp}
kn{m<1806:R,a<588:R,x>3133:R,A}
sv{a<1325:R,a>1460:R,m>1718:R,A}
rkl{x>3112:A,s>3342:R,R}
mg{m>1291:R,A}
tm{x>1917:bs,A}
rkq{a<2465:bxc,x>1862:nxc,sp}
tk{a<1228:A,a<1455:R,A}
vts{s>3466:jq,xns}
nh{x<2134:rr,x<2484:pbv,m<976:lvp,mv}
sz{x<3075:R,m>359:R,R}
btr{x>3197:qkh,ph}
ff{m>1946:A,s<2407:zc,A}
cmk{s>2606:R,A}
jr{a>804:A,a<475:R,R}
rf{x<415:A,s>2228:R,a<735:R,R}
drd{s>3365:tk,s<3276:bcm,A}
hvq{s<2401:hsn,s>3374:fl,xdl}
vpn{s<1325:fvm,m<689:R,R}
vj{s<1347:A,x>673:R,R}
tnz{s>999:A,x>2657:gk,A}
rft{a<401:pqp,qdm}
gkm{m>430:A,m>196:A,s>3374:R,A}
vhs{m>623:R,R}
zzt{x>3410:A,m>2991:R,m<2312:A,A}
pth{x<3816:A,s>3893:A,R}
vmd{s>3539:zr,gr}
dlh{x<3015:R,A}
lv{a<2026:A,m<292:R,x>588:R,A}
pr{x>3172:rjk,a<1192:R,m<595:A,A}
dzq{a<812:bgx,s>1212:nhn,nnd}
kxf{x>3058:R,A}
tgp{a>1311:kkg,a>960:jx,hmf}
npv{s<668:A,x>1167:A,R}
pd{a<1327:A,a<1446:rp,R}
krf{m>228:A,A}
cg{m<3308:nr,a>1054:fxs,cgf}
qd{a>2742:qmb,x>1719:mt,s>818:nrs,mpc}
bsc{x>1586:R,a<1207:R,R}
kcb{a<1236:A,s<1368:A,A}
mf{a<2852:zv,m<2664:prx,rb}
pnj{a<754:R,rcb}
zr{x>1721:A,x>683:R,x>265:R,A}
bn{s>1397:A,a<219:R,R}
hx{a>953:A,a>428:A,x>539:R,R}
jv{s<837:pr,fsl}
qmb{s<1185:R,A}
zcf{x>784:A,R}
jpf{s<2409:A,s<2671:A,x<1126:A,R}
kj{a>836:jv,x>3467:tjj,rl}
cnc{s<3455:A,s<3662:R,a>1191:A,R}
vbg{s>2501:R,x<3479:xhj,x>3748:R,R}
in{a>1666:mb,x<1861:dql,sl}
xg{x<749:dgs,A}
pj{m<830:R,R}
jb{m>3287:A,m>3057:gfh,A}
zdp{a>3403:R,a>3071:R,m>808:R,A}
ggc{x>1433:A,s>2595:R,R}
pxr{s<3463:R,m<792:R,A}
lk{x<724:R,s<3706:A,trm}
mjv{s<3762:A,bxn}
hl{a>556:ks,s<2836:A,m<1771:kc,kjd}
gf{x>1994:R,m<3457:xkg,s>3446:R,A}
bxz{m<718:R,A}
bv{s<2868:ggc,R}
pg{a<269:A,x>3764:R,m>3066:R,R}
vxn{s>1686:R,a>1312:R,m<2228:A,A}
jj{a>673:A,R}
zsx{x>3204:R,A}
cs{s<1229:A,R}
qdm{a<550:A,m>833:R,A}
vkl{s>365:A,x<236:gdr,s<161:zt,vxr}
cb{x<1464:A,x<1513:R,m<1814:R,R}
fn{m>2717:A,s>3698:A,R}
jzq{x>330:A,m>2865:R,A}
gn{a<1001:A,R}
cvv{m>817:fr,x<1974:mrv,m<316:vd,kh}
spl{a<1511:R,A}
qg{a<3083:csf,a>3444:mz,vmd}
tf{x>3535:R,a<669:gxm,s>2146:clq,A}
sg{s<463:lx,s<936:tq,R}
pnb{s<1080:R,m<3125:A,m<3446:A,R}
phd{x<3427:A,R}
hmf{a>804:gsn,s<1343:btv,vsf}
cdj{a>358:mdl,a<238:A,nk}
xs{x>2559:A,m>1333:A,A}
phv{m<2219:R,m<2257:R,x<565:A,A}
rx{s<2069:nvr,m>2736:qr,R}
vld{x>969:A,s>1063:A,m<689:R,A}
drn{s>1516:kx,m<611:kbn,jff}
jlg{s<2849:tgr,qlb}
nrn{a<1365:xmh,a>1565:A,x<1595:R,fgv}
fx{x>3184:R,a<1161:R,x<2964:A,R}
nfm{m<3293:R,m<3660:A,A}
cft{a<735:A,a>1165:jdq,m<1013:ctl,xdg}
grs{x<630:chb,bp}
tsq{s<3424:klj,s>3632:rkq,tj}
ll{a<566:R,R}
zc{m<1722:A,x>1506:A,x>1464:R,A}
tmx{s>3389:A,x>2770:A,R}
jdq{s>1592:A,R}
hhb{m<728:R,s<3217:bf,kfz}
chb{s>1613:A,A}
zcv{m<1690:A,a>1254:cb,R}
gdt{s<2338:R,a>529:R,A}
lrd{s>1816:A,s<1737:R,x>3648:vhm,mxs}
dl{s>1645:A,A}
rdr{m<3263:lmt,nrn}
dbp{x>1326:R,A}
vr{m<2215:R,A}
dqb{m>1367:A,s>3854:A,A}
mj{x<3291:xmd,m<1474:A,x>3594:fp,mk}
cvq{s<3167:vz,x>3484:dzt,m>1895:qsk,dj}
hrq{m>912:zn,s<2570:pk,x<3198:A,A}
tq{m<525:A,A}
cx{s>971:xbb,a>2286:R,s>558:lv,R}
vlz{s<432:bdx,m>419:R,m<201:fh,cm}
hsm{x>3894:psb,s>3500:A,zh}
pmv{m>1260:xn,x<1634:tkk,A}
mt{m<3482:lps,R}
qs{x>214:R,m<2255:A,A}
pf{m>399:R,x>3064:R,R}
fd{s>3477:A,R}
cmp{a>553:A,R}
gbx{s>657:R,m>939:A,m<824:A,A}
hsn{a>2953:drn,m>656:kzh,x<1575:xm,vpx}
vmr{a<696:A,m>594:R,s<2925:A,R}
hv{m>2584:R,m>2556:A,R}
gq{m<3154:A,x>3367:hmd,rsn}
tpc{x>3467:A,m>1865:A,m<1087:R,R}
vf{x>567:A,R}
xq{s>2490:ll,jj}
dx{m<213:mx,x<981:qtt,a<1191:A,ktx}
sq{s<518:jtt,a<1365:fbb,spl}
ds{m<1024:R,x<1332:A,A}
nn{m<337:srx,tx}
xf{a>645:cnc,R}
ltv{m<3188:cp,pt}
gjp{a<3444:R,A}
tbx{a>2651:bv,zx}
qf{a<3156:cmf,s>3778:gkk,x<1377:lk,jqq}
qdf{m>3031:A,s>3921:cqp,fg}
zx{s>2703:R,s<2561:gts,a>2305:A,snk}
xlj{a<240:R,R}
rl{x>3006:cdj,a>458:njj,vpn}
psq{a>939:tnp,m<1665:mnk,rpj}
bc{a<486:knh,m<3114:gth,lcr}
zq{x<1396:R,x<1668:A,R}
xm{x<973:cx,llb}
fzx{x>1946:A,x>1916:A,s>2523:dgq,bz}
gr{s<3443:A,R}
rsh{m<2322:A,R}
lzn{m<3606:A,m<3836:vql,a<423:R,R}
rsn{s<2344:A,a>1284:A,m<3610:A,A}
rg{m<752:A,s<3018:A,s<3072:R,R}
drs{a>2000:A,A}
ctl{s<1467:R,x>675:R,a<885:R,A}
fvm{a>168:A,s>868:R,R}
mtn{a>310:R,R}
rbt{x>3216:R,A}
dzt{m<1618:prn,zxd}
cp{x>616:A,m<2469:qs,R}
zkk{a>806:R,s<1275:A,a>636:A,R}
jrp{a>1285:A,A}
crv{m>183:R,x>1678:mbr,A}
mk{m>2781:R,x<3408:R,x<3508:A,A}
qvp{x<2839:R,s>2645:A,R}
tj{m>3041:jxs,m<2346:R,A}
gj{s<1310:R,R}
dd{a>700:dx,s<1439:xg,rfd}
ptj{m>919:cft,a<819:bq,grs}
qbq{a>1005:A,x>1477:A,A}
nmh{a>884:R,a>308:R,s<2046:R,hnp}
lx{a<901:A,m>593:R,R}
pqd{x>1493:A,x<1235:A,A}
bnh{m<1280:R,s<3302:R,R}
tv{a<332:zg,gdt}
rnd{a<1518:R,a>1596:A,A}
jlb{a>2132:R,s>736:R,x<1111:A,R}
mmz{x>992:R,a<1403:R,x<929:A,R}
qfp{m>1069:R,A}
pt{s>1650:vf,s>1452:R,x<446:A,vj}
tkk{s<1160:A,m>1174:R,a<673:R,A}
kx{x>2642:R,s<1888:A,R}
dq{x<1992:sc,x>2131:mjs,cmp}
xbb{s>1608:A,a<2166:A,s<1285:R,A}
btv{s>646:R,A}
br{a<1023:kxf,sds}
nr{m>2923:R,s>3647:R,m>2756:R,R}
nhn{x<1067:ltv,rdr}
mbr{x>3214:A,A}
mrv{m>400:R,m<212:A,A}
vpx{a>2478:tnz,s>821:nn,a<2119:vlz,vkv}
hn{x>2956:A,A}
jz{a<775:R,A}
pqp{m>731:A,A}
gbf{a>1519:pvj,m>715:gbx,rm}
mz{m>549:R,x<2243:krf,a>3781:R,jm}
gth{m>2216:brs,m>1852:xxk,a>662:R,A}
bmn{s<892:A,R}
pgh{s<2509:A,m<3134:R,R}
kqd{s<2377:A,A}
nhq{x>538:A,s<315:R,m<296:R,R}
zhr{m>2625:kqd,tp}
nrt{s<1274:A,x<1313:A,A}
mp{a>2286:jd,psr}
zn{m>1099:A,R}
vkv{m>221:pf,s<542:qm,m<110:A,A}
xdl{a<2851:nqc,s<2936:cvv,x>1808:kp,km}
kfs{a>1392:bcv,s>2659:A,R}
rcl{s>3400:R,m<528:A,R}
fsl{s>1571:cxd,m>988:jrp,A}
bd{m<486:R,R}
sr{x<445:A,zdl}
qt{s<3423:gkm,x<3311:A,m>745:R,bqz}
zfp{x>1123:nqq,A}
gbd{a>1434:A,x<2274:A,m>947:R,R}
kbn{x<1675:R,sz}
mb{m<1446:hvq,ch}
bcg{a>2036:A,a<1912:A,R}
mv{s<2113:jkk,s>2853:fd,xs}
dgq{a<1317:R,m>2464:R,m<2017:A,A}
dt{s<2641:R,m<702:A,R}
stf{m<199:A,R}
sh{m>261:R,a<915:A,R}
dv{a>2613:A,R}
zd{a<554:cdx,a<841:kjj,x<798:mlm,sg}
jn{a<3252:A,x<2123:R,A}
lvp{a>277:A,nf}
fh{m>123:R,A}
lt{x>1632:R,s<654:R,a<325:R,R}
lcr{a<720:A,m>3584:A,R}
jfx{s>1699:R,x>616:A,A}
hg{s<2617:xmj,a<850:R,s>2800:A,A}
vpd{m<2602:R,a<1388:R,x<2900:rnd,A}
mc{x<1595:A,x<1769:R,x>1809:A,A}
hnq{x>2458:A,s<1704:R,A}
bp{x<1089:A,x<1479:A,R}
lps{x>2602:R,A}
gmh{m<2825:R,A}
dmp{a<1343:dl,x<2089:A,x>2165:A,R}
bbg{x<415:A,m<580:A,a>1069:mdk,nj}
qlb{m<838:nl,m<1169:pq,x<799:pl,dxj}
ms{s<3815:hn,a<1068:lqt,qdf}
lh{s<885:vkl,m>1432:pn,a>1109:pd,vb}
fg{s<3875:R,m<2292:A,x>2987:A,R}
sgx{x>2162:R,x>2109:A,R}
fb{x>2219:bc,sx}
gh{a<2425:R,R}
zj{s<1096:xlj,R}
rc{a>1099:R,m>1634:jpf,x<1066:A,R}
lxp{a>1410:tpc,s>2961:qgx,R}
rsc{m<134:A,m>220:A,R}
xr{s<789:pj,m>812:hq,s<967:xvt,vld}
jq{a<892:zcf,m<3084:fn,R}
jx{m<668:bzx,a>1083:R,A}
ft{s>2537:R,x>3546:R,x>3014:A,pgh}
db{a<998:R,s<1612:R,x>2784:A,R}
vd{s>2660:pqk,a<3336:rsc,a>3760:hvb,A}
cn{s>2852:R,m>2099:rsh,dxk}
tjz{x<3743:R,s>3348:ssv,s>3252:R,kkm}
nlt{a<205:R,x>1400:R,a<276:A,A}
xvt{m>687:R,x<799:A,A}
hp{m>1850:hg,m<1700:rc,s>2663:hl,jr}
kch{m>1902:A,R}
llb{s<1505:R,x<1208:R,x>1337:krp,R}
tmk{x<647:tcj,x<1410:hp,x>1663:xq,cgj}
psj{x<2003:hnh,R}
szq{x<2455:A,x>2571:R,R}
nxc{s<3784:A,s<3926:A,x>3006:A,A}
ph{x>2866:ts,ngq}
zh{a>920:A,R}
md{x>809:hf,x<467:lh,zjr}
kd{a<1002:jt,x<3170:vpd,pfg}
sc{x<1928:A,a<406:R,m>2129:R,A}
vg{a<2146:R,m<798:R,s>1349:R,A}
clg{s<1574:A,A}
gd{a<1867:R,s>1838:R,m>3359:R,A}
dxk{a<463:A,s<2763:R,s<2797:R,R}
prx{a>3468:vk,m<2059:rbt,x>3171:hk,qvp}
fcn{x<1040:R,s>626:R,x<1572:A,A}
srx{m>147:R,m<80:A,R}
dzv{x>2236:zhr,a<1268:zll,tt}
bx{s>2165:A,x<3424:A,s<2155:A,R}
sx{m<2846:dq,s<2128:mq,s>3114:gf,lzn}
mmt{m>667:R,R}
zll{s>2412:sfz,x>2017:sbh,tm}
hnp{s>2072:R,R}
pl{x>413:R,m<1336:R,x<211:R,jz}
dr{a>1530:R,A}
qzf{s>2145:bx,a<1033:R,kfx}
rj{a<3192:A,s>3193:R,m>697:A,A}
fnd{x>449:A,s>115:R,R}
vk{m<2203:A,s>2735:R,x>3249:R,A}
vxh{m>559:bxz,x<777:R,R}
tpp{x<1376:hh,bmn}
nk{s<810:R,a>313:A,a>264:R,A}
nvr{s>792:A,x>1945:R,R}
zxd{x<3810:xf,m<2542:hsm,cg}
kz{s<606:R,m>1077:A,A}
bkc{x>2757:A,R}
xnx{m>3422:A,a>711:R,s<3019:R,A}
tjf{a<744:R,a>1261:A,a>1039:fx,zsx}
kc{m>1735:R,A}
tgr{x>786:xjb,bbg}
td{a>3522:xqd,x>2536:R,x<2142:dlg,rj}
zrh{m>1071:md,s<1184:kg,jrv}
gsn{a<885:A,a>912:R,R}
ldb{a>254:R,R}
qx{a>982:A,m<2418:R,R}
kp{s<3127:rg,s>3291:pb,x<3205:td,hhb}
rr{s<2162:R,R}
sj{m<572:R,A}
st{x<2946:A,x>3298:A,a<700:R,R}
sp{m<2339:R,s>3855:A,R}
zzx{m<3186:dm,xl}
bcv{s<2617:A,a>1559:R,x>1488:A,A}
xhj{s>2477:A,R}
fp{x>3859:R,R}
nx{m<1903:A,bsc}
gpf{m<821:A,m>1201:R,s<3661:R,R}
bxc{x>1591:R,A}
mx{m>85:R,R}
dql{s>1910:lpf,m>2088:dzq,zrh}
fvx{a<1533:A,A}
snb{m>1043:A,s<1569:vg,m<829:A,mh}
rp{x<289:A,A}
xxk{m<1974:A,s<1985:A,R}
qr{s<3186:A,a>1382:R,s>3578:A,A}
jrv{m<380:dd,m<830:hhr,ptj}
skq{x>635:A,hx}
rpj{s>3338:kn,a>391:A,R}
zs{m>2355:R,a>863:A,s<3650:A,A}
rvd{s>1220:R,s<494:jzq,s<762:nlj,A}
qm{x<2515:R,R}
sqt{a<1023:A,R}
lcl{m<2710:A,m<3464:R,m<3759:R,R}
gkk{s<3899:R,a>3581:A,a<3426:R,rhd}
fr{s<2683:njl,R}
nhz{a<947:A,m>1908:R,m>1673:A,A}
rlp{s<774:A,R}
bf{s<3175:A,R}
psr{x>1061:R,a<1875:A,x>357:R,A}
nqs{x<319:A,s<3699:A,x>463:kch,R}
fxs{s<3617:R,a<1444:A,A}
dxj{m>1297:gn,flt}
mhl{m>1106:R,s>1412:A,s<1288:R,R}
cgf{x<3874:R,m>3565:A,A}
qtt{x<603:A,m<300:R,x>743:R,R}
sd{x<3134:A,x>3645:qfp,x<3407:hc,R}
lpf{m>2444:cz,m<1547:jlg,s<3133:tmk,bt}
bbp{a>1097:lxp,m<2430:mvh,nd}
kkg{s>1504:fvx,s>961:gbd,x<2356:dr,A}
vxr{m<1531:A,m<1719:R,x<357:R,R}
mvh{m<1146:vmr,a<380:R,R}
csf{x>2650:pxr,a<2576:R,vv}
qzs{m<868:R,x<471:A,R}
sl{x<2677:hrh,s>1997:cvq,xpq}
hnh{s<2435:A,a<1516:A,a>1606:R,R}
bq{x<943:qzs,m>870:cr,mvd}
rq{a>2886:nrt,m>2662:gh,hv}
dm{a<495:mtn,a>678:bpt,ng}
mxs{a>470:R,x>3575:A,A}
gxm{m>1455:A,a>431:R,R}
bzx{a>1117:R,s<2331:A,A}
tcj{a>1108:jkx,s>2661:cn,a>647:sgg,htm}
zl{x<1326:R,a<186:R,A}
bqz{a>544:A,a<307:R,A}
xkg{x<1915:A,A}
cdx{a>339:rlp,x<772:A,a>152:npv,jk}
bz{a>1306:R,x<1884:R,x<1901:R,A}
hd{x>441:A,m>802:A,A}
kh{x>3037:R,cmk}
mn{a<482:A,A}
pk{s<2557:A,R}
nqq{a>559:A,s<3600:A,x>1458:A,R}
nrs{a>2275:R,a>2016:R,gd}
rpk{s<2091:nmh,s>2183:mj,m<2279:tf,qzf}
mlm{x<388:R,m>571:A,s<752:nhq,sh}
jxs{a>2736:A,R}
ks{x<974:A,a>1269:A,x>1159:R,R}
xmh{m<3623:A,m<3765:A,R}
hrh{m<1654:jrz,a<980:fb,dzv}
tp{m<2079:hnq,kcb}
fv{m>1406:ft,s<2551:vbg,hrq}
bpt{a<758:A,m<2540:A,R}
vql{x<1984:A,a>515:A,a<192:A,A}
bs{m>2823:A,A}
nf{m>644:R,R}
ssv{s<3440:A,A}
rfd{s<1718:clg,s>1797:stf,m<251:R,A}
lq{a>646:R,x>1191:R,R}
rv{a<531:zj,rvd}
tz{s>403:A,s>222:R,A}
gk{a<2728:R,x<3291:A,a>2857:R,R}
vjj{x>745:zfp,nqs}
cqp{x>2993:A,m<2484:R,a<1361:A,A}
xqd{s>3201:A,R}
cr{s>1438:A,a<283:A,x<1533:A,A}
xdg{s>1494:A,R}
xjb{s<2508:pqd,s>2732:xcm,s>2589:dt,ds}
nnd{a>1189:sq,dck}
brs{s<1860:A,x>2522:R,A}
jm{s<3472:A,m>227:A,m<99:A,A}
mh{m<948:A,m>1009:R,R}
lm{s<2631:A,A}
qgx{m>1733:A,a<1279:A,s>3061:R,A}
kjg{m>757:tmx,A}
njl{a<3412:R,s>2568:R,s>2458:R,A}
mnk{m>1403:phx,m>1317:R,a>417:st,bnh}
vb{a>478:zkk,bn}
tnp{x<3103:A,x>3353:dkv,m<1534:A,sv}
plz{a<2245:A,A}
hhr{s>1496:vxh,sj}
nv{m>3730:R,R}
km{x<978:hd,rqf}
dgs{m<245:A,s<1274:A,R}
tnb{s>3817:pth,x>3774:A,gpf}
cq{a<1034:mn,mmz}
dck{s>410:cxs,m<2779:qx,jb}
cm{a>1896:R,A}
kms{a>2292:R,x<1613:drs,m>1261:bcg,A}
bdx{m>310:R,s<240:A,s>356:R,A}
rqf{s>3178:A,zdp}
xmj{m>2206:A,R}
hf{m>1482:jp,x<1267:cq,pmv}
vv{s<3498:A,x>954:A,x>375:A,R}
bxn{m>622:A,x<3163:A,A}
lqt{m<3266:A,a<374:A,a<656:R,A}
kjd{a>215:R,A}
lvs{s<2888:lm,A}
jcz{x>988:xd,mxf}
bt{m<2092:vjj,jcz}
gm{a>795:A,m>3022:R,R}
prn{s>3549:tnb,tjz}
tjj{s<1166:zkd,s>1652:lrd,x<3663:sxt,rft}
tt{a>1417:psj,x>2002:dmp,a>1352:rx,fzx}
nj{m<1121:R,R}
qkh{m>3145:pnj,dkl}
ch{s>2396:dg,cv}
zm{x>625:A,x<542:A,m>1927:A,R}
lmt{m>2506:R,s<1559:R,m>2329:mc,vxn}
fbb{a<1249:R,s<765:A,R}
nlj{s<671:R,x<425:R,R}
frt{m<454:srz,a>1436:gbf,xr}
cv{m>2927:qd,tbl}
vz{s>2596:bbp,s<2247:rpk,s>2462:fv,ssd}
pbv{s>1407:ldb,s>929:A,kz}
bgx{x<941:rv,zzx}
pfg{x>3349:zzt,nfm}
ssd{m<2231:tjf,a>683:gq,tv}
cmf{s<3771:xj,m<1213:plz,dqb}
qsk{s<3510:kd,ms}
mjs{s<1429:A,A}
dj{s>3501:br,m>1254:psq,x<3181:hm,fvf}
hmd{m<3500:R,m>3704:R,A}
tx{x>2397:R,x<1870:A,s<1560:A,R}
ts{a<1021:A,x>2977:R,lcl}
sfz{m>2706:A,s>3253:A,A}
vrz{m>3406:R,m<2945:A,A}
psb{x>3943:R,R}
hvb{m>189:A,m<82:R,A}
xk{x<1541:dbp,m<3333:xpg,s<2457:R,nv}
jkx{s<2566:R,A}
trm{a>3458:A,A}
xl{a>319:A,m>3484:zl,nlt}
mvd{a>391:A,m>852:R,A}
mdk{m<1132:R,m>1384:A,R}
ql{a>2685:dlh,xdc}
tg{x>1137:xk,sr}
xcm{s>2808:A,R}
dkv{m>1597:A,a<1295:R,m>1451:A,A}
pq{x>685:zq,A}
hcq{m<1264:A,a>3456:R,x<3097:R,R}
gfh{a<942:R,s>187:A,m>3151:A,R}
nqc{m>952:kms,m<498:crv,x<1886:mp,lvs}
zv{a<2363:A,s>2848:A,dv}
kg{a<990:zd,frt}
sxt{x>3571:mhl,s>1399:R,R}
rcb{s>831:R,R}
ct{m<2782:R,x<2144:R,R}
jk{x>1280:R,a<62:R,a<97:A,A}
xpg{s<2389:R,x>1653:R,x>1605:A,A}
rm{s>756:R,s<272:A,A}
zt{m<1571:R,x>357:R,s<90:R,R}
kzh{x>2664:sd,s<1123:jlb,snb}
hk{a>3113:A,m<2299:R,R}
xgd{s>3436:A,s<3413:A,m<2980:A,R}
xn{a<733:A,x>1650:R,a<1166:A,R}
zg{a>211:R,R}
phx{m>1520:R,s<3316:R,A}
nl{x>1066:rcl,R}
gts{a<2156:R,A}
cz{s<3196:tg,vts}
flt{x<1306:R,m>1253:A,s<3466:R,R}
mq{m<3476:R,a>643:R,m>3779:A,zk}
xpq{m>1741:btr,kj}
dlg{s<3232:R,s>3262:R,R}
jcp{m<292:A,x<3654:A,a<245:A,A}
sds{a>1440:A,x<3037:R,A}
kjt{x<2493:bd,mjv}
xd{x>1460:R,vr}
dg{s>3110:tsq,x>2350:mf,tbx}
snk{a>2064:A,m>2299:R,A}
xj{s<3720:R,a>2638:A,A}
cgj{a<890:ff,x>1546:nx,m<1898:zcv,kfs}
srz{a>1345:A,A}
pn{s<1253:A,R}
ktx{a<1448:A,a<1571:A,A}
mdl{s<786:R,m<811:A,A}
jqq{s>3703:gjp,hcq}
rjk{s>459:A,x>3536:A,A}
cxs{s>846:pnb,s>638:R,A}
jtt{s>281:gmh,x<928:fnd,s<175:A,A}
ngq{s>979:db,x<2798:tz,m>2572:R,sqt}
qb{m>669:R,A}
pvj{m>712:R,x>690:R,a>1577:R,A}
{x=396,m=612,a=2012,s=177}
{x=39,m=25,a=812,s=272}
{x=1930,m=15,a=99,s=905}
{x=373,m=1162,a=3153,s=1794}
{x=1045,m=108,a=1143,s=639}
{x=2178,m=621,a=580,s=172}
{x=1292,m=1910,a=122,s=2528}
{x=275,m=192,a=83,s=2992}
{x=2218,m=157,a=99,s=545}
{x=822,m=2795,a=82,s=150}
{x=2178,m=12,a=1672,s=1504}
{x=2463,m=620,a=882,s=1746}
{x=1025,m=1370,a=21,s=45}
{x=3161,m=944,a=884,s=1590}
{x=285,m=849,a=595,s=2630}
{x=423,m=480,a=177,s=2385}
{x=2022,m=777,a=1166,s=371}
{x=195,m=1446,a=1622,s=727}
{x=1831,m=1163,a=710,s=98}
{x=2054,m=767,a=416,s=462}
{x=814,m=1038,a=1871,s=118}
{x=2109,m=136,a=69,s=265}
{x=30,m=745,a=2265,s=2135}
{x=92,m=1923,a=1269,s=62}
{x=2952,m=875,a=82,s=199}
{x=1527,m=550,a=926,s=34}
{x=84,m=1894,a=1418,s=3020}
{x=1419,m=2050,a=2634,s=992}
{x=19,m=1722,a=207,s=2292}
{x=122,m=3195,a=179,s=979}
{x=970,m=3603,a=193,s=1431}
{x=515,m=681,a=501,s=459}
{x=682,m=2621,a=1754,s=649}
{x=1972,m=2722,a=215,s=2720}
{x=358,m=2273,a=248,s=1091}
{x=103,m=468,a=48,s=467}
{x=111,m=251,a=199,s=55}
{x=1536,m=541,a=1850,s=1349}
{x=318,m=1491,a=1124,s=1821}
{x=2441,m=1571,a=981,s=1207}
{x=639,m=2165,a=588,s=658}
{x=704,m=1423,a=2397,s=1207}
{x=51,m=161,a=833,s=1036}
{x=375,m=188,a=1668,s=681}
{x=141,m=744,a=732,s=77}
{x=341,m=1182,a=1040,s=249}
{x=265,m=873,a=281,s=1691}
{x=1022,m=37,a=1865,s=338}
{x=1580,m=137,a=283,s=1031}
{x=1361,m=1432,a=1179,s=2447}
{x=617,m=1360,a=305,s=54}
{x=235,m=733,a=60,s=2192}
{x=362,m=181,a=1297,s=835}
{x=133,m=132,a=1327,s=64}
{x=573,m=263,a=741,s=98}
{x=799,m=898,a=2180,s=732}
{x=186,m=517,a=681,s=1168}
{x=47,m=2806,a=3577,s=578}
{x=3218,m=1130,a=1204,s=414}
{x=2468,m=2891,a=900,s=2094}
{x=215,m=1069,a=1689,s=1228}
{x=372,m=937,a=725,s=412}
{x=1434,m=1549,a=1539,s=1007}
{x=3222,m=841,a=1854,s=1272}
{x=2326,m=1637,a=1918,s=30}
{x=1248,m=266,a=750,s=2521}
{x=110,m=1288,a=364,s=749}
{x=752,m=803,a=257,s=316}
{x=1777,m=1615,a=942,s=110}
{x=227,m=1524,a=1800,s=309}
{x=105,m=246,a=1723,s=3455}
{x=713,m=1172,a=833,s=804}
{x=3026,m=39,a=376,s=71}
{x=946,m=3437,a=1461,s=149}
{x=1878,m=303,a=207,s=1081}
{x=2982,m=2510,a=14,s=3078}
{x=818,m=1042,a=943,s=958}
{x=2349,m=7,a=802,s=822}
{x=140,m=473,a=2454,s=3331}
{x=1014,m=796,a=1453,s=348}
{x=1578,m=2615,a=539,s=294}
{x=993,m=276,a=564,s=2618}
{x=2908,m=327,a=705,s=1430}
{x=46,m=361,a=291,s=1076}
{x=2267,m=212,a=952,s=365}
{x=677,m=2277,a=550,s=1061}
{x=226,m=1391,a=653,s=2321}
{x=1654,m=15,a=27,s=421}
{x=974,m=241,a=255,s=283}
{x=202,m=84,a=1805,s=81}
{x=337,m=529,a=254,s=42}
{x=765,m=2055,a=473,s=413}
{x=1731,m=81,a=370,s=133}
{x=2839,m=739,a=513,s=990}
{x=376,m=660,a=924,s=65}
{x=627,m=1486,a=1422,s=2326}
{x=2688,m=2,a=659,s=347}
{x=1388,m=1650,a=1875,s=363}
{x=305,m=1406,a=1285,s=50}
{x=1095,m=382,a=2098,s=1120}
{x=1172,m=402,a=1935,s=1646}
{x=27,m=357,a=714,s=355}
{x=42,m=45,a=217,s=1134}
{x=22,m=415,a=624,s=358}
{x=324,m=3057,a=221,s=3288}
{x=540,m=179,a=1033,s=568}
{x=7,m=999,a=453,s=316}
{x=231,m=428,a=1092,s=8}
{x=2221,m=3036,a=22,s=735}
{x=247,m=516,a=339,s=1756}
{x=3140,m=2229,a=1643,s=2012}
{x=1248,m=500,a=624,s=779}
{x=1245,m=1806,a=579,s=2269}
{x=989,m=442,a=671,s=419}
{x=266,m=1547,a=992,s=664}
{x=1078,m=24,a=1069,s=1848}
{x=908,m=1979,a=1027,s=956}
{x=78,m=719,a=1328,s=105}
{x=901,m=601,a=3503,s=1239}
{x=949,m=851,a=356,s=1086}
{x=315,m=565,a=1868,s=857}
{x=1426,m=1398,a=918,s=1629}
{x=1286,m=2229,a=500,s=16}
{x=360,m=481,a=2006,s=991}
{x=1514,m=2442,a=1921,s=183}
{x=2155,m=1924,a=616,s=986}
{x=2121,m=1323,a=352,s=958}
{x=756,m=2750,a=543,s=2608}
{x=833,m=168,a=2410,s=232}
{x=3,m=810,a=451,s=40}
{x=1151,m=452,a=1642,s=965}
{x=560,m=2062,a=2265,s=846}
{x=1,m=246,a=905,s=323}
{x=1642,m=193,a=448,s=25}
{x=86,m=486,a=588,s=75}
{x=2360,m=5,a=417,s=2689}
{x=676,m=947,a=402,s=355}
{x=1869,m=44,a=944,s=1139}
{x=36,m=1033,a=588,s=391}
{x=63,m=31,a=608,s=817}
{x=1115,m=2098,a=617,s=702}
{x=2117,m=848,a=2371,s=3170}
{x=270,m=2617,a=393,s=330}
{x=325,m=3377,a=870,s=1077}
{x=275,m=139,a=876,s=648}
{x=657,m=231,a=2197,s=915}
{x=1934,m=1410,a=1965,s=1574}
{x=251,m=841,a=480,s=24}
{x=771,m=479,a=107,s=2259}
{x=1364,m=166,a=27,s=664}
{x=774,m=303,a=909,s=93}
{x=264,m=164,a=3471,s=176}
{x=681,m=2247,a=1660,s=837}
{x=1348,m=204,a=990,s=219}
{x=650,m=1469,a=1269,s=1160}
{x=1121,m=534,a=2270,s=927}
{x=322,m=1871,a=693,s=170}
{x=540,m=462,a=929,s=99}
{x=3015,m=915,a=808,s=3661}
{x=248,m=383,a=471,s=106}
{x=1536,m=1350,a=2067,s=804}
{x=1656,m=56,a=1629,s=995}
{x=17,m=236,a=288,s=218}
{x=663,m=1611,a=317,s=1272}
{x=630,m=2397,a=1538,s=805}
{x=211,m=2,a=137,s=514}
{x=2340,m=191,a=173,s=618}
{x=2154,m=6,a=42,s=884}
{x=526,m=1292,a=694,s=1722}
{x=606,m=355,a=1532,s=1351}
{x=1405,m=543,a=126,s=3}
{x=548,m=429,a=1382,s=572}
{x=95,m=1008,a=1790,s=42}
{x=1796,m=1272,a=188,s=201}
{x=1437,m=3161,a=754,s=4}
{x=891,m=447,a=1389,s=145}
{x=183,m=894,a=821,s=1455}
{x=457,m=79,a=1586,s=1179}
{x=1631,m=828,a=2697,s=73}
{x=56,m=506,a=1988,s=448}
{x=3408,m=971,a=2130,s=1899}
{x=347,m=1915,a=193,s=1460}
{x=308,m=3,a=1189,s=191}
{x=2,m=354,a=2761,s=287}
{x=634,m=864,a=101,s=233}
{x=632,m=91,a=1935,s=202}
{x=27,m=1364,a=2765,s=2}
{x=367,m=510,a=473,s=132}
{x=987,m=2516,a=1834,s=1121}
{x=626,m=845,a=176,s=265}
{x=577,m=823,a=2129,s=23}
{x=802,m=295,a=389,s=226}
{x=844,m=301,a=549,s=126}
{x=1139,m=235,a=9,s=1183}
{x=663,m=2444,a=554,s=1516}
{x=1244,m=355,a=252,s=1023}
{x=151,m=2238,a=392,s=93}
{x=99,m=1911,a=362,s=1272}
{x=71,m=146,a=46,s=1359}
{x=1140,m=650,a=75,s=1458}

17
day19/sample.txt Normal file
View File

@@ -0,0 +1,17 @@
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}
{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}

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####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

BIN
day22/c/day22 Executable file
View File

Binary file not shown.

140
day22/c/day22.c Normal file
View File

@@ -0,0 +1,140 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define LINE_MAX_LENGTH 256
#define MAX_BRICKS 2048
#define NUM_0_CHARCODE 48
#define NUM_9_CHARCODE NUM_0_CHARCODE + 9
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define in_range(c,a,b) ((c) <= max(a,b) && (c) >= min(a,b))
typedef struct corner {
int x;
int y;
int z;
} corner_t;
typedef struct brick {
corner_t corners[2];
int fallen;
} brick_t;
int fall(brick_t bricks[], int bricks_num);
int intersect(brick_t brick_a, brick_t brick_b);
int main() {
char *p, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
brick_t bricks[MAX_BRICKS];
int bricks_num = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
sscanf(p, "%i", &bricks[bricks_num].corners[0].x);
while (*p != ',') p++;
p++;
sscanf(p, "%i", &bricks[bricks_num].corners[0].y);
while (*p != ',') p++;
p++;
sscanf(p, "%i", &bricks[bricks_num].corners[0].z);
while (*p != '~') p++;
p++;
sscanf(p, "%i", &bricks[bricks_num].corners[1].x);
while (*p != ',') p++;
p++;
sscanf(p, "%i", &bricks[bricks_num].corners[1].y);
while (*p != ',') p++;
p++;
sscanf(p, "%i", &bricks[bricks_num].corners[1].z);
bricks_num++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
while (fall(bricks, bricks_num));
for (int i = 0; i < bricks_num; i++) {
bricks[i].fallen = 0;
}
int part1 = 0;
int part2 = 0;
#pragma omp parallel for schedule(dynamic)
for (int i = 0; i < bricks_num; i++) {
brick_t bricks_copy[MAX_BRICKS];
memcpy(bricks_copy, bricks, sizeof(bricks));
bricks_copy[i].corners[0].z = 0;
bricks_copy[i].corners[1].z = 0;
int fallen = 0;
while (fall(bricks_copy, bricks_num));
for (int j = 0; j < bricks_num; j++) {
fallen += bricks_copy[j].fallen;
}
#pragma omp critical
{
if (fallen == 0) {
part1++;
}
part2 += fallen;
}
}
printf("%i\n", part1);
printf("%i\n", part2);
free(buf);
}
int fall(brick_t bricks[], int bricks_num) {
int result = 0;
for (int i = 0; i < bricks_num; i++) {
int fallen = 0;
for (;;) {
bricks[i].corners[0].z--;
bricks[i].corners[1].z--;
if (bricks[i].corners[0].z <= 0 || bricks[i].corners[1].z <= 0) {
break;
}
int collision = 0;
for (int j = 0; j < bricks_num; j++) {
if (i == j) continue;
if (intersect(bricks[i], bricks[j])) {
collision = 1;
break;
}
}
if (collision) {
break;
}
bricks[i].fallen = 1;
fallen = 1;
}
bricks[i].corners[0].z++;
bricks[i].corners[1].z++;
result += fallen;
}
return result;
}
int intersect(brick_t brick_a, brick_t brick_b) {
if (
(in_range(brick_b.corners[0].x, brick_a.corners[0].x, brick_a.corners[1].x) || in_range(brick_b.corners[1].x, brick_a.corners[0].x, brick_a.corners[1].x) || in_range(brick_a.corners[1].x, brick_b.corners[0].x, brick_b.corners[1].x) || in_range(brick_a.corners[0].x, brick_b.corners[0].x, brick_b.corners[1].x)) &&
(in_range(brick_b.corners[0].y, brick_a.corners[0].y, brick_a.corners[1].y) || in_range(brick_b.corners[1].y, brick_a.corners[0].y, brick_a.corners[1].y) || in_range(brick_a.corners[1].y, brick_b.corners[0].y, brick_b.corners[1].y) || in_range(brick_a.corners[0].y, brick_b.corners[0].y, brick_b.corners[1].y)) &&
(in_range(brick_b.corners[0].z, brick_a.corners[0].z, brick_a.corners[1].z) || in_range(brick_b.corners[1].z, brick_a.corners[0].z, brick_a.corners[1].z) || in_range(brick_a.corners[1].z, brick_b.corners[0].z, brick_b.corners[1].z) || in_range(brick_a.corners[0].z, brick_b.corners[0].z, brick_b.corners[1].z))
) {
return 1;
}
return 0;
}

1439
day22/input.txt Normal file
View File

File diff suppressed because it is too large Load Diff

7
day22/sample.txt Normal file
View File

@@ -0,0 +1,7 @@
1,0,1~1,2,1
0,0,2~2,0,2
0,2,3~2,2,3
0,0,4~0,2,4
2,0,5~2,2,5
0,1,6~2,1,6
1,1,8~1,1,9

BIN
day23/c/day23 Executable file
View File

Binary file not shown.

219
day23/c/day23.c Normal file
View File

@@ -0,0 +1,219 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define LINE_MAX_LENGTH 256
#define MAX_QUEUE 256*256
#define cmp(a,b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
#define abs(a) ((a) < 0 ? -(a) : (a))
typedef enum tile_type {
PATH,
FOREST,
SLOPE_UP,
SLOPE_DOWN,
SLOPE_LEFT,
SLOPE_RIGHT,
} tile_type_t;
int check_direction(int curr[2], int dir[2], uint8_t **visited, tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH]);
int highest_cost(int curr[2], tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, uint8_t **visited, int cost, int finish[2], int ignore_slopes, int walkable);
int curr_max = 0;
int directions[4][2] = {
{0, -1},
{0, 1},
{-1, 0},
{1, 0}
};
int main() {
char *p, *buf, c;
buf = (char *)malloc(LINE_MAX_LENGTH);
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH];
int x = 0, y = 0;
int walkable = 0;
while ((c = getchar()) != EOF) {
*p++ = c;
if (c == '\n') {
p = buf;
x = 0;
while (*p != '\n') {
switch (*p) {
case '.':
map[x][y] = PATH;
walkable++;
break;
case '#':
map[x][y] = FOREST;
break;
case '^':
map[x][y] = SLOPE_UP;
walkable++;
break;
case 'v':
map[x][y] = SLOPE_DOWN;
walkable++;
break;
case '<':
map[x][y] = SLOPE_LEFT;
walkable++;
break;
case '>':
map[x][y] = SLOPE_RIGHT;
walkable++;
break;
}
x++;
p++;
}
y++;
memset(buf, 0, LINE_MAX_LENGTH);
p = buf;
}
}
int start[2], finish[2];
start[1] = 0;
finish[1] = y - 1;
// Find start and finish
for (int i = 0; i < x; i++) {
if (map[i][0] == PATH) {
start[0] = i;
}
if (map[i][y - 1] == PATH) {
finish[0] = i;
}
}
uint8_t **visited = (uint8_t**)malloc(x * sizeof(uint8_t*));
for (uint8_t i = 0; i < x; i++) {
visited[i] = (uint8_t*)malloc(y * sizeof(uint8_t));
memset(visited[i], 0, y * sizeof(uint8_t));
}
printf("Part 1: %i\n", highest_cost(start, map, x, y, visited, 0, finish, 0, walkable));
for (uint8_t i = 0; i < x; i++) {
memset(visited[i], 0, y * sizeof(uint8_t));
}
printf("Part 2: %i\n", highest_cost(start, map, x, y, visited, 0, finish, 1, walkable));
free(buf);
for (int i = 0; i < y; i++) {
free(visited[i]);
}
free(visited);
}
int highest_cost(int curr[2], tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH], int x, int y, uint8_t **visited, int cost, int finish[2], int ignore_slopes, int walkable) {
if (cmp(curr, finish)) {
return cost;
}
// We will never reach the end
if ((walkable - cost) < (abs(finish[0] - curr[0]) + abs(finish[1] - curr[1])) || visited[finish[0]][finish[1]-1]) {
return -1;
}
visited[curr[0]][curr[1]] = 1;
int *options[4], options_num = 0, *dir, max = 0;
if (map[curr[0]][curr[1]] == SLOPE_UP && !ignore_slopes) {
dir = directions[0];
if (check_direction(curr, dir, visited, map)) {
options[0] = dir;
options_num = 1;
}
} else if (map[curr[0]][curr[1]] == SLOPE_DOWN && !ignore_slopes) {
dir = directions[1];
if (check_direction(curr, dir, visited, map)) {
options[0] = dir;
options_num = 1;
}
} else if (map[curr[0]][curr[1]] == SLOPE_LEFT && !ignore_slopes) {
dir = directions[2];
if (check_direction(curr, dir, visited, map)) {
options[0] = dir;
options_num = 1;
}
} else if (map[curr[0]][curr[1]] == SLOPE_RIGHT && !ignore_slopes) {
dir = directions[3];
if (check_direction(curr, dir, visited, map)) {
options[0] = dir;
options_num = 1;
}
} else {
for (int i = 0; i < 4; i++) {
int *dir = directions[i];
if (check_direction(curr, dir, visited, map)) {
options[options_num] = dir;
options_num++;
}
}
}
if (options_num == 0) {
return -1;
}
uint8_t ***visited_copies = (uint8_t***)malloc(options_num * sizeof(uint8_t**));
for (uint8_t i = 0; i < options_num - 1; i++) {
visited_copies[i] = (uint8_t**)malloc(x * sizeof(uint8_t*));
for (uint8_t j = 0; j < y; j++) {
visited_copies[i][j] = (uint8_t*)malloc(y * sizeof(uint8_t));
memcpy(visited_copies[i][j], visited[j], y * sizeof(uint8_t));
}
}
for (int i = 0; i < options_num; i++) {
int next[2];
next[0] = curr[0] + options[i][0];
next[1] = curr[1] + options[i][1];
int c;
if (i != options_num - 1) {
c = highest_cost(next, map, x, y, visited_copies[i], cost + 1, finish, ignore_slopes, walkable);
} else {
c = highest_cost(next, map, x, y, visited, cost + 1, finish, ignore_slopes, walkable);
}
if (c > max) {
max = c;
}
}
for (int i = 0; i < options_num - 1; i++) {
for (int j = 0; j < y; j++) {
free(visited_copies[i][j]);
}
free(visited_copies[i]);
}
free(visited_copies);
if (max > curr_max) {
curr_max = max;
printf("Current best: %i\n", curr_max);
}
return max;
}
int check_direction(int curr[2], int dir[2], uint8_t **visited, tile_type_t map[LINE_MAX_LENGTH][LINE_MAX_LENGTH]) {
int next_x = curr[0] + dir[0];
int next_y = curr[1] + dir[1];
if (next_y > 0 && !visited[next_x][next_y] && map[next_x][next_y] != FOREST) {
return 1;
}
return 0;
}

141
day23/input.txt Normal file
View File

@@ -0,0 +1,141 @@
#.###########################################################################################################################################
#.#...#...#.....###...#.......###...#####...#...#...#...#...#...#.......#...#.....#.....#.......#...#...#...###...#.....#.....#.............#
#.#.#.#.#.#.###.###.#.#.#####.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#####.#.#.#.###.#.###.#.#####.#.#.#.#.#.#.###.#.#.###.#.###.#.###########.#
#.#.#.#.#.#.#...#...#.#...#...#...#...#...#...#.#.#.#.#.#.#...#...#.....#.#.#...#.#.#...#.....#.#.#...#.#.#...#.#.#.#...#...#.#.....#.....#.#
#.#.#.#.#.#.#.###.###.###.#.###.#####.#.#######.#.#.#.#.#.#########.#####.#.###.#.#.#.#######.#.#.#####.#.###.#.#.#.#.#####.#.#####.#.###.#.#
#...#.#.#.#.#...#.#...###.#...#.#...#.#.....#...#.#...#.#.#.........#.....#...#.#.#.#...#...#.#.#.#.....#.#...#.#.#.#...#...#.#.....#...#...#
#####.#.#.#.###.#.#.#####.###.#.#.#.#.#####.#.###.#####.#.#.#########.#######.#.#.#.###.#.#.#.#.#.#.#####.#.###.#.#.###.#.###.#.#######.#####
#.....#.#.#.###...#.#...#.#...#.#.#...#.....#.#...#...#.#.#.###.....#.....#...#.#.#.#...#.#.#.#.#.#...###.#...#.#.#...#...#...#.....#...#...#
#.#####.#.#.#######.#.#.#.#.###.#.#####.#####.#.###.#.#.#.#.###.###.#####.#.###.#.#.#.###.#.#.#.#.###.###.###.#.#.###.#####.#######.#.###.#.#
#.#...#.#...#.....#.#.#.#.#...#.#.>.>.#.....#.#.#...#...#.#...#.#...###...#.#...#...#.#...#...#.#.#...#...#...#.#.###.#.....#...#...#...#.#.#
#.#.#.#.#####.###.#v#.#.#.###.#.###v#.#####.#.#.#.#######.###.#.#.#####.###.#.#######.#.#######.#.#.###.###.###.#.###.#.#####.#.#.#####.#.#.#
#...#...#...#.#...#.>.#.#.###.#.#...#.......#.#.#.#.>.>.#.#...#.#...#...#...#.#.....#.#.....#...#.#.###...#.#...#...#.#.#...#.#.#...#...#.#.#
#########.#.#.#.###v###.#.###.#.#.###########.#.#.#.#v#.#.#.###.###.#.###.###.#.###.#.#####.#.###.#.#####.#.#.#####.#.#.#.#.#.#.###.#.###.#.#
###.....#.#...#.#...###.#.#...#.#...###...###...#...#.#.#.#...#...#.#.#...###...#...#.#...#.#...#.#.>.>...#.#.#.....#.#.#.#.#.#.#...#.....#.#
###.###.#.#####.#.#####.#.#.###.###.###.#.###########.#.#.###.###.#.#.#.#########.###.#.#.#.###.#.###v#####.#.#.#####.#.#.#.#.#.#.#########.#
#...#...#.....#.#.....#.#.#.....###...#.#.....#.......#...###...#.#.#.#.#.>.>.#...#...#.#.#.#...#.###.#.....#.#.#...#.#.#.#.#.#.#.#.........#
#.###.#######.#.#####.#.#.###########.#.#####.#.###############.#.#.#.#.#.#v#.#.###.###.#.#.#.###.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#########
#...#.........#.....#.#.#.#...........#.###...#.......###.......#.#.#.#...#.#.#...#.#...#...#...#.#...#.#...#.#.#.#.#.#.#.#.#.#.#.#.#...#...#
###.###############.#.#.#.#.###########.###.#########.###.#######.#.#.#####.#.###.#.#.#########.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#
###...............#.#.#.#.#...#.......#...#.#...#...#...#...#...#.#...###...#.....#...#...#.....#.#...#.#.#.#.#.#.#.#.#.#.#.#.#.#.#...#.#.#.#
#################.#.#.#.#.###.#.#####.###.#.#.#.#.#.###.###.#.#.#.#######.#############.#.#.#####.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#####.#.#.#
#.................#...#...###.#.#.....#...#...#.#.#.#...###...#.#.#.......#...#...#.....#.#.#.....#...#...#...#.#.#...#.#.#...#...#...#...#.#
#.###########################.#.#.#####.#######.#.#.#.#########.#.#.#######.#.#.#.#.#####.#.#.#####.###########.#.#####.#.#########.#.#####.#
#.................###...#####...#.....#.#.......#.#...#...#.....#.#...#...#.#...#.#.#.....#...#.....#...###...#.#.....#...###...#...#.#...#.#
#################.###.#.#############.#.#.#######.#####.#.#.#####.###.#.#.#.#####.#.#.#########.#####.#.###.#.#.#####.#######.#.#.###.#.#.#.#
#.......#...#...#.#...#...#...#.....#...#.....###.....#.#.#.....#...#...#.#.#.....#.#.#.....#...#...#.#...#.#.#.......#...#...#.#.###...#...#
#.#####.#.#.#.#.#.#.#####.#.#.#.###.#########.#######.#.#.#####.###.#####.#.#.#####.#.#.###.#.###.#.#.###.#.#.#########.#.#.###.#.###########
#.#...#...#...#...#.#.....#.#.#...#.###.......#.......#.#...###.....###...#.#.#.....#.#.#...#.....#...###...#...###...#.#.#...#.#...........#
#.#.#.#############.#.#####.#.###.#.###.#######.#######.###.###########.###.#.#.#####.#.#.#####################.###.#.#.#.###.#.###########.#
#...#.#...#...#...#.#...#...#...#.#.....#.....#...#...#.#...#.........#.....#.#.#.....#.#.#...#.......#.........#...#.#.#.#...#...#.......#.#
#####.#.#.#.#.#.#.#.###.#.#####.#.#######.###.###.#.#.#.#.###.#######.#######.#.#.#####.#.#.#.#.#####.#.#########.###.#.#.#.#####.#.#####.#.#
#.....#.#.#.#.#.#.#...#.#.#.....#...#...#.#...###...#...#.#...#.......###...#...#...#...#.#.#.#...#...#.#.......#...#...#...#####...#...#.#.#
#.#####.#.#.#v#.#.###.#.#.#.#######.#.#.#.#.#############.#.###.#########.#.#######.#.###.#.#.###.#.###.#.#####.###.#################.#.#.#.#
#.......#...#.>.#...#.#.#.#...#...#...#...#.###...###.....#...#.#...#.....#.#.......#...#.#.#.....#...#...#...#...#.#...............#.#.#...#
#############v#####.#.#.#.###.#.#.#########.###.#.###v#######.#.#.#.#.#####.#.#########.#.#.#########.#####.#.###.#.#.#############.#.#.#####
#.........###.....#...#...#...#.#.......#...#...#.#.>.>.#...#.#.#.#.#.....#.#.#...#...#.#.#.......#...#...#.#...#.#.#.#.............#.#.....#
#.#######.#######.#########.###.#######.#.###.###.#.#v#.#.#.#.#.#.#.#####.#.#.#.#.#.#.#.#.#######.#.###.#.#v###.#.#.#.#.#############.#####.#
#.......#.#.....#.....#.....###.......#.#...#...#...#.#.#.#.#.#.#.#...###.#.#...#.#.#.#.#...#...#.#...#.#.>.>.#...#...#...............#...#.#
#######.#.#.###.#####.#.#############.#.###v###.#####.#.#.#.#.#.#.###.###.#.#####v#.#.#.###.#.#.#.###.#.###v#.#########################.#.#.#
#...###.#.#...#.#.....#.#.....#...#...#.#.>.>...#.....#...#.#.#...###...#.#.#...>.>.#...###.#.#.#.#...#.#...#.#...#...#...#.........#...#.#.#
#.#.###.#.###.#.#.#####.#.###.#.#.#.###.#.#v#####.#########.#.#########.#.#.#.###v#########.#.#.#.#.###.#.###.#.#.#.#.#.#.#v#######.#.###.#.#
#.#...#.#.###.#.#.....#.#.###.#.#...###...#.....#...#...###...#.......#.#.#.#...#.#...#...#.#.#.#.#.#...#.###.#.#.#.#.#.#.>.###...#...###...#
#.###.#.#.###.#.#####.#.#.###.#.###############.###.#.#.#######.#####.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.###.###.#.#.#.#.#.###v###.#.###########
#...#.#.#...#.#.......#.#.#...#.###...###...#...###...#.........#.....#...#.....#...#...#.#.#.#.#.#.#.###...#...#.#.#.#.###.....#.#...#...###
###.#.#.###.#.#########.#.#.###.###.#.###.#.#.###################.#######################.#.#.#.#.#.#.#####.#####.#.#.#.#########.#.#.#.#.###
#...#.#...#.#.#.......#.#.#...#.#...#.#...#.#.#.................#.....#.................#.#...#...#...#.....#...#...#...#.........#.#...#...#
#.###.###.#.#.#.#####.#.#.###.#.#.###.#.###.#.#.###############.#####.#.###############.#.#############.#####.#.#########.#########.#######.#
#...#.....#...#.#.....#...#...#.#...#...###...#...#...#.......#.#.....#...........#...#...#...###.....#.....#.#.###...###.........#.#.......#
###.###########.#.#########.###.###.#############.#.#.#.#####.#.#.###############.#.#.#####.#.###.###.#####.#.#.###.#.###########.#.#.#######
###.............#.....#...#.....###...#...###...#...#...#.....#...#...#...#...###...#...#...#...#...#.#.....#.#.#...#...#.........#.#.#...###
#####################.#.#.###########.#.#.###.#.#########.#########.#.#.#.#.#.#########.#.#####.###.#.#.#####.#.#.#####.#.#########.#.#.#.###
#.....................#.#.#.....#...#...#.....#.#...#.....###...###.#.#.#.#.#.#...#.....#.....#.###.#.#...###.#.#...#...#.........#.#...#...#
#.#####################.#.#.###.#.#.###########.#.#.#.#######.#.###.#.#.#.#.#.#.#.#.#########.#.###.#.###v###.#.###.#.###########v#.#######.#
#.#.....#.............#.#.#...#.#.#.............#.#.#.......#.#.###.#.#.#.#.#.#.#.#...#####...#.#...#...>.>.#.#.#...#...#.....#.>.#.#.......#
#.#.###.#.###########.#.#.###.#.#v###############.#.#######.#.#.###.#.#.#.#.#.#.#.###v#####.###.#.#######v#.#.#.#.#####.#.###.#.#v#.#.#######
#...#...#.#.....#...#.#.#.#...#.>.>...#...###.....#...#.....#.#.###.#.#.#.#.#.#.#...>.>.#...###.#.#.......#...#...#.....#...#...#.#.#.#...###
#####.###.#.###.#.#.#.#.#.#.#####v###.#.#.###.#######.#.#####.#.###.#.#.#.#.#.#.#####v#.#.#####.#.#.###############.#######.#####.#.#.#.#.###
#...#.....#...#...#...#.#...###...###...#...#.#.......#.....#.#.#...#.#.#.#.#.#...#...#...#...#...#.....#...###.....#...#...#.....#.#...#...#
#.#.#########.#########.#######.###########.#.#.###########.#.#.#.###.#.#.#.#.###.#.#######.#.#########.#.#.###.#####.#.#.###.#####.#######.#
#.#.#...#...#...#.....#.......#.......#...#...#...###...#...#.#.#...#.#.#.#.#.#...#.........#.#...#####...#...#...#...#...###.......#...#...#
#.#.#.#.#.#.###.#.###.#######.#######.#.#.#######.###.#.#v###.#.###.#.#.#.#.#.#.#############.#.#.###########.###.#.#################.#.#.###
#.#...#...#.....#.#...#...#...#.......#.#.......#...#.#.>.>...#.#...#.#.#.#.#.#...###.......#.#.#.............#...#.#.................#...###
#.###############.#.###.#.#.###.#######.#######.###.#.###v#####.#.###.#.#.#.#.###.###.#####.#.#.###############.###.#.#######################
#...#.....#...###.#.....#...###...#...#...#.....#...#...#.#...#...###...#...#.#...#...#...#...#.............###.#...#.......................#
###.#.###.#.#v###.###############.#.#.###.#.#####.#####.#.#.#.###############.#.###.###.#.#################.###.#.#########################.#
###.#.###.#.#.>...#...........#...#.#.....#.#...#...#...#.#.#.........#.......#.###.....#.#.......#.......#...#.#.#...###...#...............#
###.#.###.#.#v#####.#########.#.###.#######.#.#.###.#.###.#.#########.#.#######.#########.#.#####.#.#####.###.#.#.#.#.###.#.#.###############
#...#...#...#...#...#.........#.....#.......#.#...#.#.###.#.#.....#...#.......#...#.......#.#.....#.....#.....#...#.#.....#...#...#...###...#
#.#####.#######.#.###.###############.#######.###.#.#.###.#.#.###.#.#########.###.#.#######.#.#########.###########.###########.#.#.#.###.#.#
#.#.....#.....#...#...#...#...#...###.........#...#.#.###...#...#...#####...#.....#.........#...###...#.........###.....#.....#.#...#.#...#.#
#.#.#####.###.#####.###.#.#.#.#.#.#############.###.#.#########.#########.#.###################.###.#.#########.#######.#.###.#.#####.#.###.#
#...#...#...#.......###.#.#.#.#.#...#...........###...#...#...#...#...###.#.###.......#.........#...#.#.........#...###...###...#.....#.#...#
#####.#.###.###########.#.#.#.#.###.#.#################.#.#.#.###.#.#.###.#.###.#####.#.#########.###.#.#########.#.#############.#####.#.###
#.....#.....#.........#.#...#.#...#.#...#...#...###...#.#.#.#.###...#.....#...#.....#.#.........#...#.#.....#.....#.#...###...#...#.....#.###
#.###########.#######.#.#####.###.#.###.#.#.#.#.###.#.#.#.#.#.###############.#####.#.#########.###.#.#####.#.#####.#.#.###.#.#.###.#####.###
#.......#...#.#.......#.....#...#.#.###...#...#...#.#.#.#.#.#...#.....#.....#.#.....#...........###.#.......#.#.....#.#.#...#.#.....#...#...#
#######.#.#.#.#.###########.###.#.#.#############.#.#.#.#.#.###.#.###.#.###.#.#.###################.#########.#.#####.#.#.###.#######.#.###.#
#.......#.#...#.........#...#...#.#.#...###.......#.#.#.#.#.#...#.###...###...#.......#...###.....#.....#...#.#.#...#.#.#.#...#...###.#.#...#
#.#######.#############.#.###.###.#.#.#.###v#######.#.#.#.#.#.###v###################.#.#.###.###.#####v#.#.#.#.#.#.#.#.#.#.###.#.###.#.#v###
#.......#.#.............#...#...#.#.#.#...>.>...#...#.#.#...#...>.>.#...#...###.......#.#...#...#.#...>.>.#.#.#.#.#.#.#.#.#...#.#.....#.>.###
#######.#.#.###############.###.#.#.#.#####v###.#.###.#.#########v#.#.#.#.#.###.#######.###.###.#.#.###v###.#.#.#.#.#.#.#.###.#.#########v###
###...#...#.........###...#...#...#...###...#...#.#...#...#...#...#...#...#...#.........#...###.#.#...#...#.#.#.#.#.#.#.#.#...#.#...###...###
###.#.#############.###.#.###.###########.###.###.#.#####.#.#.#.#############.###########.#####.#.###.###.#.#.#.#.#.#.#.#.#.###.#.#.###.#####
#...#...............#...#.#...#...........###...#.#.#.....#.#.#...#...........#...#.......###...#.#...#...#.#.#.#.#.#.#.#.#.....#.#...#.#...#
#.###################.###.#.###.###############.#.#.#.#####.#.###.#.###########.#.#.#########.###.#.###.###.#.#.#.#.#.#.#.#######.###.#.#.#.#
#.#...........#...#...#...#...#.............#...#.#...###...#.#...#...###...#...#.#.........#...#.#.###...#...#...#...#...#.....#.#...#...#.#
#.#.#########.#.#v#.###.#####.#############.#.###.#######.###.#.#####.###.#.#.###.#########.###.#.#.#####.#################.###.#.#.#######.#
#.#.###...###...#.>.###.......#...###.......#.....###...#...#.#.....#...#.#.#.#...#...###...#...#.#...#...#...#...#...#...#...#...#...#...#.#
#.#.###.#.#######v#############.#.###.###############.#.###.#.#####.###.#.#.#.#.###.#.###v###.###.###.#.###.#.#.#.#.#.#.#.###.#######.#.#.#.#
#...#...#.......#.........#.....#.....#...###...###...#.....#.#.....#...#.#.#.#...#.#...>.>.#...#.....#.....#.#.#...#...#.###.......#.#.#.#.#
#####.#########.#########.#.###########.#.###.#.###.#########.#.#####.###.#.#.###.#.#####v#.###.#############.#.#########.#########.#.#.#.#.#
#.....#.......#.#...#...#.#.............#.....#.#...#...#...#.#...#...#...#...#...#...#...#.#...###...........#.........#.#...#...#.#.#.#...#
#.#####.#####.#.#.#.#.#.#.#####################.#.###.#.#.#.#.###.#.###.#######.#####.#.###.#.#####.###################.#.#.#.#.#.#.#.#.#####
#.#.....#.....#...#...#...#.......#.............#.....#...#.#.....#...#.......#.....#.#...#.#.....#.............#.......#...#...#.#.#.#.....#
#.#.#####.#################.#####.#.#######################.#########.#######.#####.#.###.#.#####.#############.#.###############.#.#.#####.#
#...#...#.#...#.......#...#...#...#...........#.......#...#.#...#...#.#.......#.....#.#...#.......#.......#.....#...#...#...#...#...#.......#
#####.#.#.#.#.#.#####.#.#.###.#.#############.#.#####.#.#.#.#.#.#.#.#.#.#######.#####.#.###########.#####.#.#######.#.#.#.#.#.#.#############
#...#.#.#...#...#.....#.#.#...#...###...#.....#.....#...#...#.#.#.#.#...#...###.......#.#...#.....#.#...#...###...#.#.#.#.#...#.###.........#
#.#.#.#.#########.#####.#.#.#####.###.#.#.#########.#########.#.#.#.#####.#.###########.#.#.#.###.#.#.#.#######.#.#.#.#.#.#####.###.#######.#
#.#...#.#...#...#.....#.#.#...#...#...#...###.....#...#...###.#.#.#.#...#.#.###...#...#.#.#.#.#...#.#.#.......#.#.#...#...#...#.....#...#...#
#.#####.#.#.#.#.#####.#.#.###.#.###.#########.###.###.#.#.###.#.#.#.#.#.#.#.###.#.#.#.#v#.#.#.#.###.#.#######.#.#.#########.#.#######.#.#.###
#.....#.#.#...#.#.....#.#...#.#.###...###...#...#.###.#.#.#...#.#.#.#.#.#.#.#...#.#.#.>.>.#...#.###...#.......#.#.#.........#.........#...###
#####.#.#.#####.#.#####.###.#.#.#####v###.#.###.#.###v#.#.#.###.#.#.#.#.#.#.#.###.#.###v#######.#######.#######.#.#.#########################
#.....#...#...#...#...#...#.#.#.#...>.>...#.#...#...>.>.#.#...#.#.#.#.#.#.#.#...#.#.#...#.....#...#.....#.....#.#.#.........#.........#.....#
#.#########.#.#####.#.###.#.#.#.#.###v#####.#.#######v###.###.#.#.#.#.#.#.#.###.#.#.#.###.###.###.#.#####.###.#.#.#########.#.#######.#.###.#
#.....#.....#.....#.#...#.#.#.#.#...#...###...###...#...#...#.#.#.#.#.#.#.#.#...#...#...#...#.....#.......#...#.#...#...###.#.#.......#...#.#
#####.#.#########.#.###.#.#.#.#.###.###.#########.#.###.###.#.#.#.#.#.#.#.#.#.#########.###.###############.###.###.#.#.###.#.#.#########.#.#
#.....#.#.........#...#.#.#.#.#.#...###.......#...#.#...###.#.#.#.#.#.#.#.#.#.#...#.....###.........#.......#...#...#.#...#...#...#.......#.#
#.#####.#.###########.#.#.#.#.#.#.###########.#.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.###############.#.#######.###.###.###.#######.#.#######.#
#...#...#.........#...#.#.#.#.#...#...........#...#...#...#.#.#.#.#.#.#...#.#.#.#.#.....#####.......#...#...#...#.###.#...#.....#...#...#...#
###.#.###########.#.###.#.#.#.#####.#############.#####.#.#.#.#.#.#.#.#####.#.#.#.#####.#####.#########v#.#.###.#.###.#.###v###.#####.#.#.###
###...#...###...#.#...#...#...###...#...#.......#.......#.#...#.#.#.#...#...#.#.#.......#.....#.......>.>.#.#...#...#.#...>.###.......#...###
#######.#.###.#.#v###.###########.###.#.#.#####.#########.#####.#.#.###.#.###.#.#########.#####.#######v###.#.#####.#.#####v#################
#.......#.....#.#.>.#.......#...#.....#...#.....#.........#...#...#.#...#.#...#...#...###.#...#.....###...#.#.....#.#...#...###...#...#.....#
#.#############.#v#.#######.#.#.###########.#####.#########.#.#####.#.###.#.#####.#.#.###.#.#.#####.#####.#.#####.#.###.#.#####.#.#.#.#.###.#
#.............#...#.......#.#.#...........#...###.......#...#...###...###.#...###...#...#...#.#.....#...#.#.#...#.#.#...#.#...#.#...#.#.#...#
#############.###########.#.#.###########.###.#########.#.#####.#########.###.#########.#####.#.#####.#.#.#.#.#.#.#.#.###.#.#.#.#####.#.#.###
#...#...#...#...........#.#.#...#.......#.....#...###...#.#.....#...#...#.#...#.........#...#.#.#.....#...#.#.#...#.#...#.#.#.#.#.....#.#...#
#.#.#.#.#.#.###########.#.#.###.#.#####.#######.#.###.###.#.#####.#.#.#.#.#.###.#########.#.#.#.#.#########.#.#####.###.#.#.#.#.#.#####.###.#
#.#...#...#.......#.....#...###...#...#...#.....#.#...###.#.#...#.#...#.#...###...........#.#.#.#.....#...#.#...#...#...#...#.#.#.#...#.#...#
#.###############.#.###############.#.###.#.#####.#.#####.#.#.#.#.#####.###################.#.#.#####.#.#.#.###.#.###.#######.#.#.#.#.#.#.###
#...............#.#.......#...#.....#.....#.#.....#.......#.#.#.#...#...###.................#...#...#...#.#.#...#...#.....#...#.#.#.#...#...#
###############.#.#######.#.#.#.###########.#.#############.#.#.###.#.#####.#####################.#.#####.#.#.#####.#####.#.###.#.#.#######.#
#...............#.#.....#...#.#.............#.....#.......#.#.#.###.#.#...#.......#...........#...#.....#.#...#.....#.....#.....#...###.....#
#.###############.#.###.#####.###################.#.#####.#.#.#.###.#.#.#.#######.#.#########.#.#######.#.#####.#####.#################.#####
#.......#...#...#...###.......###...#.............#.....#...#.#...#.#...#.###...#...#.........#.......#...#...#.......###...#...#.......#...#
#######.#.#.#.#.#################.#.#.#################.#####.###.#.#####.###.#.#####.###############.#####.#.###########.#.#.#.#.#######.#.#
#...###...#...#.....#...#...#.....#.#.........#...#.....#...#.#...#.#.....#...#.......###...###...###...#...#...#...#.....#.#.#.#.....#...#.#
#.#.###############.#.#.#.#.#.#####.#########.#.#.#.#####.#.#.#.###.#.#####.#############.#.###.#.#####.#.#####.#.#.#.#####.#.#.#####.#.###.#
#.#.................#.#.#.#.#.#.....#.........#.#.#...###.#.#.#.#...#.#...#.......#.....#.#.#...#.......#.#.....#.#.#.#.....#.#...###...#...#
#.###################.#.#.#.#.#.#####.#########.#.###.###.#.#.#.#.###.#.#.#######.#.###.#.#.#.###########.#.#####.#.#.#.#####.###.#######.###
#.#...#...#.....#...#.#.#.#.#.#.#...#.......#...#...#...#.#.#.#.#...#.#.#.###...#...#...#.#.#.....#.......#.....#.#.#.#.....#...#.#.....#...#
#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#######.#.#####.###.#.#.#.#.###.#.#.#.###.#.#####v###.#.#####.#.###########.#.#.#.#####.###.#.#.###.###.#
#...#...#.#.#...#.#.#.#.#.#.#.#.#.#.###.....#...#...#...#.#.#.#...#.#.#.#...#.#.#...>.>.#.#.#...#...#...........#.#.#.#.....###.#.#.#...#...#
#########.#.#.###.#.#.#.#.#.#.#.#.#.###v#######.#.###v###.#.#.###.#.#.#.###.#.#.#.#####.#.#.#.#.#####v###########.#.#.#.#######.#.#.#.###v###
#.........#.#...#.#...#.#.#.#.#.#.#...>.>...#...#.#.>.>.#.#.#...#.#.#.#...#...#.#...###.#.#...#.#...>.>.#...#...#.#.#.#.#...#...#...#.#.>.###
#.#########.###.#.#####.#.#.#.#.#.#########.#.###.#.###.#.#.###.#.#.#.###.#####.###.###.#.#####.#.#####.#.#.#.#.#.#.#.#.#.#.#.#######.#.#v###
#.....#...#.#...#.#.....#.#.#.#.#.#.........#...#.#...#.#.#.#...#.#.#.#...#.....#...#...#.....#.#...#...#.#.#.#.#.#.#.#.#.#.#.#.......#.#.###
#####.#.#.#.#.###.#.#####.#.#.#.#.#.###########.#.###.#.#.#.#.###.#.#.#.###.#####.###.#######.#.###.#.###.#.#.#.#.#.#.#.#.#.#.#.#######.#.###
#####...#...#.....#.......#...#...#.............#.....#...#...###...#...###.......###.........#.....#.....#...#...#...#...#...#.........#...#
###########################################################################################################################################.#

23
day23/sample.txt Normal file
View File

@@ -0,0 +1,23 @@
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#