Compare commits

...

7 Commits

Author SHA1 Message Date
magical 67cd627138 day 6 part 2 2024-12-06 05:49:05 +00:00
magical c40379371e day 6 part 1 2024-12-06 05:19:41 +00:00
magical ba3639a098 day 5 cleanup 2024-12-05 08:11:46 +00:00
magical eeff5e846c day 5 part 2 2024-12-05 08:07:02 +00:00
magical ccc463cf17 day 5 part 1 2024-12-05 07:52:55 +00:00
magical 7f8e50b15f ignore executables 2024-12-04 07:29:50 +00:00
magical babad98e13 update cron.sh 2024-12-04 07:29:11 +00:00
15 changed files with 1888 additions and 2 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/cookie
/*/sol
/bin/

View File

@ -2,9 +2,9 @@
set -euo pipefail
# install me in your crontab!
# 0 5 1-25 12 * $HOME/adventofcode2023/cron.sh auto
# 0 5 1-25 12 * $HOME/adventofcode2024/cron.sh auto
year=2023
year=2024
root=~/adventofcode${year}
getinput() {

1370
day05/input 100644

File diff suppressed because it is too large Load Diff

28
day05/sample1.in 100644
View File

@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

3
day05/sample2.in 100644
View File

@ -0,0 +1,3 @@
75,47,<em>61</em>,53,29
97,61,<em>53</em>,29,13
75,<em>29</em>,13

77
day05/sol.py 100644
View File

@ -0,0 +1,77 @@
import functools
def parse(file):
rules = []
for line in file:
line = line.strip()
if line == "":
break
a, _, b = line.partition('|')
rules.append((a,b))
updates = []
for line in file:
line = line.strip()
updates.append(line.split(','))
return rules, updates
def solve(file):
rules, updates = parse(file)
rules = set(rules)
def isvalid(pages):
for i in range(len(pages)):
p = pages[i]
for g in pages[i+1:]:
if (g,p) in rules:
return False
return True
# part 1
t = 0
invalid = []
for pages in updates:
if isvalid(pages):
print(pages)
t += int(pages[len(pages)//2])
else:
invalid.append(pages)
print(t)
# part 2: select applicable rules and form a graph
# find the root node
# do a topological sort by doing a DFS of the graph
t = 0
for pages in invalid:
# select rules
rules2 = [(p,g) for p,g in rules if p in pages and g in pages]
#print(pages, rules2)
# find root node
S = set(p for p,g in rules2)
E = set(g for p,g in rules2)
top = next(iter(S-E))
# topological sort
l = []
seen = set()
def visit(node):
if node in seen: return
seen.add(node)
for p,g in rules2:
if p == node and g not in seen:
visit(g)
l.append(node)
visit(top)
l.reverse()
# sanity check
assert len(l) == len(pages)
assert isvalid(l)
print(l)
# add middle number to total
t += int(l[len(l)//2])
print(t)
solve(open("sample1.in"))
solve(open("input"))

2
day06/Makefile 100644
View File

@ -0,0 +1,2 @@
CFLAGS=-O2 -std=c99 -ggdb -Wall -Wextra
all: sol

130
day06/input 100644
View File

@ -0,0 +1,130 @@
....#.....#......#...............................#...........#.......#.....#................#...#....#....#................#..#.#.
...................................................................................#...........#............#.....................
..........#..............#.................................................#..............#.......................................
....................#...............#...#.....................#..............................#....#.............................#.
...............................##...............................................................#............#....................
................#...........#................#...............#....#.......#.............................#..........#...........#..
...........................#................................................#.##..................#.............#........#........
......#......#.....#..........#.......................#..........#.............#.....................#........#....#..............
..................................................#..................................#....#.........#...........#..............#..
..................................#..................#..........................................#.........#....................#..
#.................#....................#...........#....................................#......................#..................
.......#..........................................................................................................#...............
...............##.............................#...#.....................#..........................#.......................#......
...#.......................................................#..........................................#...................#..#....
.......#...#.......#.#.............#.....#.##.........................#...........................................................
#................#.........#.......................##............................................#...#.....#........#.......#.....
#......##..............................................................#......#....#....#....#.#...............#............#..#..
.....#...............................##.......................................#.......#......#............................#.....#.
.........#......#...............................#.....#.....#...................##......#.........................................
.#.................................................#...............#........#............................#........................
#.........#.........................................#.......................#..............#......................#...........#...
..........................................##.............................................................##..#.......#............
...#.........................#...........................................#........#...............................................
#..................#............#.................................................................#.#....................#....#..#
...................#.....................................#.#.........#.............#......#............................##.........
....#..............................................................##.......................................................#....#
.....................#.....#.............................................#.......................#..................#.............
...........#.......................................#................#.........#...............#.#...........#.....#.....#........#
.#..#.................#....#...............................#.................................................##...................
..#.....#......#..........#...................#..#..................................................#.......#...#.................
.............................................................#.......................#..........#.....#...................#.......
.......#.......................#.#....#...#.....##.......................#.................................................#......
.................#.....................#...............................................................#..#.......................
...#.................#.#..................................................##......................................#..#............
#........................................................#...#.........#................#.#.......................................
.................#........#.#........................#.#..................................#.......................................
..........#..#......#..................#.........................#................................#...............................
.............................................................................................#....................................
.....................#...........#........##.....#.........................#..............................#.....#....#............
..#........#.........#.....................................................#......................................................
...................................#...................#............#.............#...........................#...................
....#.........................................................#................................#......#...#..#....................
................................................................................#..#.......#..........................#....#......
#...........................................................................#........................#............................
..#..............#..........#...#..........................................................#......................................
.......................................................#.................#.#....#.................#..........#....#...............
..................................................................................................................................
.................#.........................................................#..........#.........#.........#.................#.....
....................#........#...................#................#...#....................#..............#................#......
...#.............##........................................#.........#.................#...........#.........#....................
................#............#...........#...............#.....................................#...........................#......
....................................................#...........#..............................#..........#...........#...........
.........#..............#................................#.............#......................................#.........#...#.....
..#..#.........................................#.............................#....................................................
.#............................##...#..........#............#..#.........................#......................#...#..............
........#.....#..............#..................................................#...#......................#....#..............#..
...............................................#.........#..................#............#.............................#..........
..................................................................................................#....#..........................
..............#..............................................................#....................................................
.....#....#.....................#......................#...........#...#..#.............#.......................#.................
.............................#...#..#...........................#............................................................#....
.............................................#...............................#...................................................#
..................................................................#......#....#.......................#.......#........#..........
..................#.#.........................................................................................##.......#........#.
.............................#.......................#.......................................##...................................
.....#.............................................................................................................#.#............
...............##.........#..#.....#...............#....................#.................................#.#....#................
................#......................................................................#...............#...................#......
.................#..............#.................................................................................................
............#...#...........................................................................................#......#..............
..#.....................................................................#.....#.#.#......#....#..........#........................
.#........#.............................#.....................#.....................#....#........................................
.......................................................................................................#..........................
...#........#..................................................#.........................................................#......#.
.......#.......#..#.................................................................................................#.............
..............................#..#..........................#..#..................#.............#.................................
..................#...#.............................................#...................#.......#...............#.................
...#.......#........#.....................................#...........#.................................................#.........
.#.............#....................................#........................................................##............#...#..
...............#.....#....................................................................................#....#................#.
....#...........................................................#.....#..............................................#............
.........#..........#...........................#.......................................#....#....................#...............
...............................................................#..........#......^...............................................#
#..........................#.....................#..............................#..............................#..................
...................#.....#......#...........#........#............#.......#.......#...............................................
.....#.....#.............................................................#..............#........#..........................#....#
............#...............................................................#.............#......................#................
.....#...............................................##........................................#.......#......#...................
.........................................#........................................................................................
..........................#...............................#............#............................#.....#.......................
...##.........#......#.....#...............................................................#........#.............................
......#...............#.#......................#.......#..............................#...#.........#.........#..#.........#......
....#......#....#........#.#.............................#......................................................#.....#...........
.....#.............#.....................#...#...............#....................................................................
......#............#............#.#............#....................#.............................................................
.............#..................................................#....................#.............#.......#.............#........
.............................................#......................................................................#.............
...............................#............................................................#................#...................#
............#.....#....................................................................................................#.#........
.........................#....................#...##.......#............#..#.................................#......#.............
....#......................#.....................#...........#.........................................#......#.#.................
..................................................................#..........................#......................#.............
.......................#......#..............................................................................#....................
.......#.................#...............................##...............................................#.............#.........
.#..............#.....................................................................................................#...........
.......##.................#................#.#...#......................................#...............#...#.....................
.............##......................................................#....................................#..........#............
....................................#............#............#............#.....................................#................
................#..#............................#..............................................#.....#...................#......#.
....................#....#......#..................#................#.............................................................
...............##...............#...#..........................#......#...............#..#................#.....#.............#...
...........#..........#..........#..#.........#....#...#......#..............#................#....#..#...........................
#.....................#..#...................................#...#..............................#......#.#.#......................
............##...................#..#.............................................................................................
..............................................#.#.......................#.................#....#..................#.......#.......
....#................#......#........................................................#........#..............#....................
.........#.#...............#............................###...........#...........................................................
.........#.................................................................................#......#...#..............#......#.....
....#....#......#.#.........#..........#...............#.........#............##.............#..#............................#....
....#..#...#.....#...........................#.#.......................................#...#.....................#............#..#
...........#............................#........#...#......#...............................................#...#.................
..#...............................................................#..............................................#.........#......
...................#.....................................#....................................#.#.#............#............#.....
.....#.................#...........................##.................#..#........................................................
...#.......#...................................#.............................#...........#......##.#...#.........#.#...........#..
.#.............#......#..#...#......................................................#..............#.............#.#..............
.......................#............#...................#.....................#.......................#.............#..#......#...
......................#..#.#.......#........#....#.............#.........#..............................#.#..........#........#...
.................#.............#..#..........#...#.........#..................................#..#.............#................#.
.#..................................#....#...........................................#..............................#...#.........

10
day06/sample1.in 100644
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

10
day06/sample2.in 100644
View File

@ -0,0 +1,10 @@
....#.....
....^....#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...

10
day06/sample3.in 100644
View File

@ -0,0 +1,10 @@
....#.....
........&gt;#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...

10
day06/sample4.in 100644
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#......v.
........#.
#.........
......#...

10
day06/sample5.in 100644
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#v..

10
day06/sample6.in 100644
View File

@ -0,0 +1,10 @@
....#.....
....XXXXX#
....X...X.
..#.X...X.
..XXXXX#X.
..X.X.X.X.
.#XXXXXXX.
.XXXXXXX#.
#XXXXXXX..
......#X..

214
day06/sol.c 100644
View File

@ -0,0 +1,214 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
struct grid {
char** A;
int x, y;
};
void* xrealloc(void* ptr, size_t size) {
if (size == 0) {
size = 1;
}
ptr = realloc(ptr, size);
if (ptr == NULL) {
fprintf(stderr, "fatal: alloc(%zd) failed", size);
exit(1);
}
return ptr;
}
int readfile(const char* filename, struct grid* out) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
exit(1);
}
int len = 0;
int cap = 100;
char* data = xrealloc(NULL, cap);
for (;;) {
if (len >= cap) {
cap *= 2;
data = xrealloc(data, cap);
}
int c = fgetc(fp);
if (c == -1) {
break;
}
data[len] = c;
len++;
}
int i, nl=0;
for (i = 0; i < len; i++) {
if (data[i] == '\n') {
nl++;
}
}
// split on newlines
char** lines = xrealloc(NULL, nl * sizeof(char*));
char* p = data;
int mincol = INT_MAX;
i = 0;
while (p != NULL && p < data+len) {
lines[i++] = p;
char* q = memchr(p, '\n', (data+len)-p);
if (!q) {
break; // TODO: final line?
}
int col = q - p;
if (mincol > col && col > 0) {
mincol = col;
}
p = q+1; // skip newline
}
out->A = lines;
out->x = mincol;
out->y = nl;
return 0;
}
inline bool inbounds(struct grid *g, int x, int y) {
return (0 <= x && x < g->x && 0 <= y && y < g->y);
}
struct step_r { int x, y, dx, dy; bool ok; };
struct step_r step(struct grid *g, int x, int y, int dx, int dy) {
struct step_r r = {0};
if (!inbounds(g, x, y)) {
return r;
}
// if there is nothing in front of you, step forward
if (!inbounds(g, x+dx, y+dy) || g->A[y+dy][x+dx] != '#') {
r.x = x+dx;
r.y = y+dy;
r.dx = dx;
r.dy = dy;
r.ok = true;
return r;
} else {
// if there is something in front of you, turn right
r.x = x;
r.y = y;
r.dx = -dy;
r.dy = dx;
r.ok = true;
return r;
}
}
struct point { int x, y; };
struct point startingPoint(struct grid *g) {
int x, y;
for (y = 0; y < g->y; y++)
for (x = 0; x < g->x; x++) {
if (g->A[y][x] == '^') {
struct point r = {x, y};
return r;
}
}
struct point r = {-1,-1};
return r;
}
int solve(struct grid *g) {
struct point start = startingPoint(g);
int x = start.x, y = start.y;
int count = 0;
int dx = 0, dy = -1; // up
while(inbounds(g, x, y)) {
if (g->A[y][x] != 'x') {
count++;
}
g->A[y][x] = 'x';
struct step_r r = step(g, x, y, dx, dy);
if (r.ok != true) {
break;
}
x = r.x;
y = r.y;
dx = r.dx;
dy = r.dy;
}
g->A[start.y][start.x] = '^';
printf("%d\n", count);
return 0;
}
// returns whether the chosen grid will cause the guard to get stuck in a loop
bool loops(struct grid *g, struct point start, uint16_t* state) {
int x = start.x;
int y = start.y;
int dx = 0, dy = -1; // up
int stride = g->x;
//printf("loops(%d,%d)\n", x,y);
while(inbounds(g, x, y)) {
uint16_t dir = 15;
if (dx == 1) { dir = 1; }
else if (dx == -1) { dir = 2; }
else if (dy == 1) { dir = 4; }
else if (dy == -1) { dir = 8; }
uint16_t *s = &state[y*stride + x];
if ((*s & dir) != 0) {
// revisited a state, loop!
return true;
}
*s |= dir;
struct step_r r = step(g, x, y, dx, dy);
if (r.ok != true) {
// step failed, no loop
return false;
}
x = r.x;
y = r.y;
dx = r.dx;
dy = r.dy;
}
// left the map, no loop
return false;
}
int solve2(struct grid *g) {
int count = 0;
int ox, oy; // obstacle coords
struct point p = startingPoint(g);
size_t state_size = g->x * g->y * sizeof(uint16_t);
uint16_t *state = xrealloc(NULL, state_size);
for (oy = 0; oy < g->y; oy++)
for (ox = 0; ox < g->x; ox++) {
int c = g->A[oy][ox];
if (c != '#' && c != '^') {
g->A[oy][ox] = '#';
memset(state, 0, state_size);
if (loops(g, p, state)) {
count++;
}
g->A[oy][ox] = c;
}
}
free(state);
printf("%d\n", count);
return 0;
}
int main() {
struct grid g;
//readfile("sample1.in", &g);
readfile("input", &g);
printf("%d %d\n", g.x, g.y);
solve(&g);
//printf("%s", g.A[0]);
solve2(&g);
}