day 19 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-20 09:53:58 +01:00
parent 5921a90c73
commit 0950e76d8d
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 47 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 129 KiB

After

Width:  |  Height:  |  Size: 129 KiB

View File

@ -35,4 +35,4 @@
18:
stars: 2
19:
stars: 1
stars: 2

View File

@ -0,0 +1,46 @@
#import "/src/utils.typ": *
#let solve(input) = {
let (towels, targets) = input.split("\n\n")
towels = towels.split(", ")
let by-initial = (:)
for towel in towels {
let initial = towel.first()
if initial not in by-initial {
by-initial.insert(initial, ())
}
by-initial.at(initial).push(towel)
}
let count(target) = {
let initial = target.first()
if initial not in by-initial {
return 0
}
let cnt = 0
for towel in by-initial.at(initial) {
if towel == target {
cnt += 1
} else if target.starts-with(towel) {
cnt += count(target.slice(towel.len()))
}
}
return cnt
}
let total = 0
for target in targets.split("\n") {
total += count(target)
}
return total
}
#show-puzzle(
19, 2,
solve,
example: 16,
only-example: true
)
#show-result(632423618484345)

Binary file not shown.