day 6 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-06 07:55:12 +01:00
parent 54d93d6a1b
commit eeebb6e721
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 92 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

@ -8,3 +8,5 @@
stars: 2 stars: 2
5: 5:
stars: 2 stars: 2
6:
stars: 1

10
res/examples/day6.txt Normal file
View File

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

79
src/day6/puzzle1.typ Normal file
View File

@ -0,0 +1,79 @@
#import "/src/utils.typ": *
#let offsets = (
(0, -1),
(1, 0),
(0, 1),
(-1, 0)
)
#let in-grid(x, y, w, h) = {
return 0 <= x and x < w and 0 <= y and y < h
}
#let solve(input) = {
let grid = input.split("\n").map(l => l.clusters())
let w = grid.first().len()
let h = grid.len()
let ox = 0
let oy = 0
let found-start = false
for y in range(h) {
for x in range(h) {
if grid.at(y).at(x) == "^" {
ox = x
oy = y
found-start = true
break
}
}
if found-start {
break
}
}
grid.at(oy).at(ox) = "v"
let x = ox
let y = oy
let dir = 0
let count = 1
let (dx, dy) = offsets.at(dir)
while in-grid(x, y, w, h) {
if grid.at(y).at(x) != "v" {
grid.at(y).at(x) = "v"
count += 1
}
let x2 = x + dx
let y2 = y + dy
if not in-grid(x2, y2, w, h) {
break
}
for _ in range(4) {
let next = grid.at(y2).at(x2)
if next == "#" {
dir = calc.rem(dir + 1, 4)
(dx, dy) = offsets.at(dir)
x2 = x + dx
y2 = y + dy
} else {
break
}
}
x = x2
y = y2
}
return count
}
#show-puzzle(
6, 1,
solve,
example: 41
)

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

Binary file not shown.