day 1 puzzle 2
This commit is contained in:
parent
2f89da16ed
commit
f77a97fd80
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 86 KiB |
@ -1,2 +1,2 @@
|
||||
1:
|
||||
stars: 1
|
||||
stars: 2
|
@ -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
|
||||
)
|
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
@ -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]
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user