day 5 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-05 08:54:20 +01:00
parent 4fb226448f
commit 54d93d6a1b
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 64 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

@ -7,4 +7,4 @@
4:
stars: 2
5:
stars: 1
stars: 2

View File

@ -0,0 +1,63 @@
#import "/src/utils.typ": *
#let make-rules-dict(rules) = {
let dict = (:)
for rule in rules {
let (a, b) = rule
if a not in dict {
dict.insert(a, ())
}
dict.at(a).push(b)
}
return dict
}
/// Bubble sort
#let fix-update(dict, update) = {
let update = update
let has-changed = false
let changed = true
while changed {
changed = false
for i in range(update.len() - 1) {
let a = str(update.at(i))
let b = str(update.at(i + 1))
if a in dict.at(b, default: ()) {
update.at(i) = int(b)
update.at(i + 1) = int(a)
changed = true
has-changed = true
}
}
}
return (has-changed, update)
}
#let solve(input) = {
let (rules, updates) = input.split("\n\n")
rules = rules.split("\n").map(l => l.split("|"))
updates = updates.split("\n").map(l => l.split(",").map(int))
let total = 0
let rules-dict = make-rules-dict(rules)
for update in updates {
let (has-changed, update) = fix-update(rules-dict, update)
if has-changed {
total += update.at(calc.div-euclid(update.len(), 2))
}
}
return total
}
#show-puzzle(
5, 2,
solve,
example: 123
)

Binary file not shown.