Compare commits

..

4 Commits

7 changed files with 179 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 121 KiB

View File

@ -17,4 +17,6 @@
9:
stars: 2
10:
stars: 2
11:
stars: 2

1
res/examples/day11.txt Normal file
View File

@ -0,0 +1 @@
125 17

View File

@ -1,4 +1,5 @@
#import "/src/utils.typ": *
#import "@preview/cetz:0.3.1": canvas, draw, matrix
#let offsets = (
(-1, 0),
@ -41,6 +42,38 @@
return tails.len()
}
#let get-paths(grid, w, h, ox, oy) = {
let paths = ()
let tails = ()
let in-grid = in-grid.with(w, h)
let to-visit = ((ox, oy),)
while to-visit.len() != 0 {
let (x, y) = to-visit.remove(0)
let v = grid.at(y).at(x)
for (dx, dy) in offsets {
let (x2, y2) = (x + dx, y + dy)
if not in-grid(x2, y2) {
continue
}
let v2 = grid.at(y2).at(x2)
if v2 == v + 1 {
let pos2 = (x2, y2)
if v2 == 9 {
if pos2 not in tails {
tails.push(pos2)
paths.push(((ox, oy), (x2, y2)))
}
} else {
to-visit.push(pos2)
}
}
}
}
return paths
}
#let solve(input) = {
let grid = input.split("\n").map(l => l.clusters().map(int))
@ -61,8 +94,50 @@
return total
}
#let visualize(input) = {
let grid = input.split("\n").map(l => l.clusters().map(int))
let w = grid.first().len()
let h = grid.len()
let count-paths = count-paths.with(grid, w, h)
let get-paths = get-paths.with(grid, w, h)
let total = 0
canvas({
let starts = ()
let c
for y in range(h) {
for x in range(w) {
c = grid.at(y).at(x)
draw.rect(
(x, -y),
(x + 1, -y - 1),
fill: black.lighten((9 - c) / 9 * 90% + 10%)
)
if c == 0 {
starts.push((x, y))
}
}
}
for (ox, oy) in starts {
let paths = get-paths(ox, oy)
for path in paths {
draw.line(
..path.map(
((x, y)) => (x + .5, -y - .5)
),
stroke: red,
mark: (end: ">")
)
}
}
})
}
#show-puzzle(
10, 1,
solve,
example: 36
example: 36,
visualize: visualize
)

64
src/day11/puzzle1.typ Normal file
View File

@ -0,0 +1,64 @@
#import "/src/utils.typ": *
#import "@preview/cetz:0.3.1": canvas
#import "@preview/cetz-plot:0.1.0": plot
#let process(rock) = {
if rock == 0 {
return (1,)
}
let rock-str = str(rock)
if calc.rem(rock-str.len(), 2) == 0 {
let hl = calc.div-euclid(rock-str.len(), 2)
return (
int(rock-str.slice(0, hl)),
int(rock-str.slice(hl))
)
}
return (rock * 2024,)
}
#let blink(rocks) = {
let new-rocks = ()
for rock in rocks {
new-rocks += process(rock)
}
return new-rocks
}
#let solve(input) = {
let rocks = input.split(" ").map(int)
for _ in range(25) {
rocks = blink(rocks)
}
return rocks.len()
}
#let visualize(input) = {
let rocks = input.split(" ").map(int)
let values = (rocks.len(),)
for _ in range(25) {
rocks = blink(rocks)
values.push(rocks.len())
}
canvas({
plot.plot(
{
plot.add(range(26).zip(values))
},
size: (6,6),
x-tick-step: 5,
y-tick-step: 10000,
x-label: "Blinks",
y-label: "Rocks"
)
})
}
#show-puzzle(
11, 1,
solve,
example: 55312,
visualize: visualize
)

36
src/day11/puzzle2.typ Normal file
View File

@ -0,0 +1,36 @@
#import "/src/utils.typ": *
#let process(rock, depth) = {
if depth == 0 {
return 1
}
if rock == 0 {
return process(1, depth - 1)
}
let rock-str = str(rock)
if calc.rem(rock-str.len(), 2) == 0 {
let hl = calc.div-euclid(rock-str.len(), 2)
let a = int(rock-str.slice(0, hl))
let b = int(rock-str.slice(hl))
return process(a, depth - 1) + process(b, depth - 1)
}
return process(rock * 2024, depth - 1)
}
#let solve(input) = {
let rocks = input.split(" ").map(int)
let total = 0
for rock in rocks {
total += process(rock, 75)
}
return total
}
#show-puzzle(
11, 2,
solve,
only-example: true
)
// Too long to recompile everytime
#show-result(228651922369703)

Binary file not shown.