day 8 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-08 09:58:28 +01:00
parent 6374452e24
commit 1f0389071c
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 66 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

After

Width:  |  Height:  |  Size: 114 KiB

View File

@ -11,4 +11,6 @@
6: 6:
stars: 2 stars: 2
7: 7:
stars: 2 stars: 2
8:
stars: 1

12
res/examples/day8.txt Normal file
View File

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

51
src/day8/puzzle1.typ Normal file
View File

@ -0,0 +1,51 @@
#import "/src/utils.typ": *
#let in-grid(w, h, x, y) = {
return 0 <= x and x < w and 0 <= y and y < h
}
#let solve(input) = {
let by-freq = (:)
let antinodes = ()
let grid = input.split("\n").map(l => l.clusters())
let w = grid.first().len()
let h = grid.len()
let in-grid = in-grid.with(w, h)
for y in range(h) {
for x in range(w) {
let c = grid.at(y).at(x)
if c == "." {
continue
}
if c not in by-freq {
by-freq.insert(c, ())
}
for (x2, y2) in by-freq.at(c) {
let (dx, dy) = (x2 - x, y2 - y)
let node1 = (x - dx, y - dy)
let node2 = (x2 + dx, y2 + dy)
if in-grid(..node1) and node1 not in antinodes {
antinodes.push(node1)
}
if in-grid(..node2) and node2 not in antinodes {
antinodes.push(node2)
}
}
by-freq.at(c).push((x, y))
}
}
return antinodes.len()
}
#show-puzzle(
8, 1,
solve,
example: 14
)

0
src/day8/puzzle2.typ Normal file
View File

Binary file not shown.