Compare commits

...

3 Commits

Author SHA1 Message Date
1f0389071c
day 8 puzzle 1 2024-12-08 09:58:28 +01:00
6374452e24
added visualization for day 5 puzzle 1 2024-12-08 02:26:21 +01:00
da56d30760
added visualization for day 3 puzzle 1 2024-12-08 02:10:50 +01:00
8 changed files with 161 additions and 4 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

After

Width:  |  Height:  |  Size: 114 KiB

View File

@ -11,4 +11,6 @@
6:
stars: 2
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..
............
............

View File

@ -1,7 +1,8 @@
#import "/src/utils.typ": *
#let reg = regex("mul\((\d{1,3}),(\d{1,3})\)")
#let solve(input) = {
let matches = input.matches(regex("mul\((\d{1,3}),(\d{1,3})\)"))
let matches = input.matches(reg)
let total = matches.map(m => {
m.captures.map(int)
.product()
@ -9,8 +10,24 @@
return total
}
#let visualize(input) = {
[
#set text(size: 1.2em)
#show reg: it => {
let m = it.text.match(reg)
let v = m.captures.map(int).product()
math.underbrace(
highlight(fill: red, raw(it.text)),
text(size: 1.5em, str(v))
)
}
#raw(input)
]
}
#show-puzzle(
3, 1,
solve,
example: ("1": 161)
example: ("1": 161),
visualize: visualize
)

View File

@ -1,4 +1,5 @@
#import "/src/utils.typ": *
#import "@preview/cetz:0.3.1": canvas, draw
#let make-rules-dict(rules) = {
let dict = (:)
@ -45,8 +46,82 @@
return total
}
#let visualize(input) = {
let (rules, updates) = input.split("\n\n")
rules = rules.split("\n").map(l => l.split("|").map(int))
updates = updates.split("\n").map(l => l.split(",").map(int))
let total = 0
//let rules-dict = make-rules-dict(rules)
let diags = ()
for update in updates {
let diag = canvas(length: 3em, {
for (x, n) in update.enumerate() {
draw.circle(
(x, 0),
radius: 0.4,
name: str(x)
)
draw.content(
(x, 0),
str(n)
)
}
let flip = false
let c = 1
for (a, b) in rules {
let i = update.position(n => n == a)
let j = update.position(n => n == b)
if i == none or j == none {
continue
}
let anchor = if flip {".south"} else {".north"}
let pt-i = str(i) + anchor
let pt-j = str(j) + anchor
let col = if j < i {red} else {green}
draw.arc-through(
pt-i,
(
rel: (0, if flip {-c / 10} else {c / 10}),
to: (pt-i, 50%, pt-j)
),
pt-j,
mark: (end: ">", fill: col),
stroke: col
)
flip = not flip
c += 1
}
})
diags.push(diag)
/*if is-update-valid(rules-dict, update) {
total += update.at(calc.div-euclid(update.len(), 2))
}*/
}
diags.last() = grid.cell(
colspan: 2 - calc.rem(diags.len() - 1, 2),
diags.last()
)
grid(
columns: 2,
stroke: (paint: black, dash: "dashed"),
align: center + horizon,
inset: 0.4em,
..diags
)
}
#show-puzzle(
5, 1,
solve,
example: 143
example: 143,
visualize: visualize
)

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.