added visualization for day 4 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-04 11:31:59 +01:00
parent b45cc17e0d
commit 11277f0677
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 39 additions and 5 deletions

View File

@ -1,10 +1,11 @@
#import "/src/utils.typ": * #import "/src/utils.typ": *
#import "@preview/cetz:0.3.1": canvas, draw
#let check-xmas(lines, ox, oy) = { #let check-xmas(lines, ox, oy) = {
let w = lines.first().len() let w = lines.first().len()
let h = lines.len() let h = lines.len()
let total = 0 let dirs = ()
for dy in (-1, 0, 1) { for dy in (-1, 0, 1) {
for dx in (-1, 0, 1) { for dx in (-1, 0, 1) {
if dx == 0 and dy == 0 { if dx == 0 and dy == 0 {
@ -26,11 +27,11 @@
} }
} }
if buffer == "XMAS" { if buffer == "XMAS" {
total += 1 dirs.push((dx, dy))
} }
} }
} }
return total return dirs
} }
#let solve(input) = { #let solve(input) = {
@ -42,7 +43,7 @@
for y in range(h) { for y in range(h) {
for x in range(w) { for x in range(w) {
if lines.at(y).at(x) == "X" { if lines.at(y).at(x) == "X" {
total += check-xmas(lines, x, y) total += check-xmas(lines, x, y).len()
} }
} }
} }
@ -50,8 +51,41 @@
return total return total
} }
#let visualize(input) = {
let lines = input.split("\n")
let w = lines.first().len()
let h = lines.len()
canvas({
for y in range(h) {
for x in range(w) {
if lines.at(y).at(x) == "X" {
let key = str(x) + "-" + str(y)
let dirs = check-xmas(lines, x, y)
draw.on-layer(2, {
for (dx, dy) in dirs {
draw.line(
(x + dx * 0.2, y + dy * 0.2),
(x + dx * 2.8, y + dy * 2.8),
stroke: red,
fill: red,
mark: (end: ">")
)
}
})
}
draw.content(
(x, y),
lines.at(y).at(x)
)
}
}
})
}
#show-puzzle( #show-puzzle(
4, 1, 4, 1,
solve, solve,
example: 18 example: 18,
visualize: visualize
) )

Binary file not shown.