day 1 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-01 13:13:42 +01:00
parent 2f89da16ed
commit f77a97fd80
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 54 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 86 KiB

View File

@ -1,2 +1,2 @@
1:
stars: 1
stars: 2

View File

@ -0,0 +1,36 @@
#import "/src/utils.typ": *
#let solve(input) = {
let lines = input.split("\n")
let (l1, l2) = ((), ())
let reg = regex("^(\d+)\s+(\d+)$")
for line in lines {
let digits = line.match(reg)
l1.push(digits.captures.first())
l2.push(digits.captures.last())
}
let nums = (:)
for n in l1 {
if n not in nums.keys() {
nums.insert(n, (0, 0))
}
nums.at(n).first() += 1
}
for n in l2 {
if n in nums.keys() {
nums.at(n).last() += 1
}
}
let total = nums.pairs().map(((num, (a, b))) => int(num) * a * b).sum()
return total
}
#show-puzzle(
1,
solve,
example: 31
)

Binary file not shown.

View File

@ -15,25 +15,36 @@
#let check-example(day, func, target-result, suffix: none) = {
let result = (func)(read(get-example-path(day, suffix: suffix)))
/*assert(
result == target-result,
message: "Expected '" + repr(target-result) + "' got '" + repr(result) + "'"
)*/
let passes = (result == target-result)
let name = if suffix == none [Example] else [Example '#suffix']
box(
let badge = box(
inset: (x: 1.2em, y: 0.6em),
radius: 1.2em,
baseline: 35%,
fill: if passes {green.lighten(20%)} else {red.lighten(20%)},
if passes [#name passes] else [#name fails]
)
h(0.6em)
if not passes {
badge = box(
baseline: 35%,
grid(
columns: 2,
align: horizon,
)[
#badge
Expected '#repr(target-result)' got '#repr(result)'
]
)
}
[#badge #h(0.6em)]
}
#let show-result(result) = {
box(
inset: (x: 1.2em, y: 0.6em),
radius: 1.2em,
baseline: 35%,
fill: blue.lighten(20%),
text(fill: white)[Result: #result]
)