day 13 puzzle 2

This commit is contained in:
Louis Heredero 2024-12-13 13:19:44 +01:00
parent 6caa3516e6
commit ad4adc2372
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 75 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 123 KiB

View File

@ -23,4 +23,4 @@
12:
stars: 2
13:
stars: 1
stars: 2

View File

@ -0,0 +1,74 @@
#import "/src/utils.typ": *
#let parse-machine(lines) = {
let lines = lines.split("\n")
let match-a = lines.at(0).match(regex("Button A: X\\+(\d+), Y\\+(\d+)"))
let match-b = lines.at(1).match(regex("Button B: X\\+(\d+), Y\\+(\d+)"))
let match-p = lines.at(2).match(regex("Prize: X=(\d+), Y=(\d+)"))
return (
a: (
x: int(match-a.captures.first()),
y: int(match-a.captures.last()),
),
b: (
x: int(match-b.captures.first()),
y: int(match-b.captures.last()),
),
prize: (
x: int(match-p.captures.first()) + 10000000000000,
y: int(match-p.captures.last()) + 10000000000000,
)
)
}
#let mat-mul-v(mat, v) = {
return (
mat.at(0).at(0) * v.at(0) + mat.at(0).at(1) * v.at(1),
mat.at(1).at(0) * v.at(0) + mat.at(1).at(1) * v.at(1)
)
}
#let det(mat) = {
return mat.at(0).at(0) * mat.at(1).at(1) - mat.at(1).at(0) * mat.at(0).at(1)
}
#let solve(input) = {
let machines = input.split("\n\n")
machines = machines.map(parse-machine)
let total = 0
for m in machines {
let mat = (
(m.a.x, m.b.x),
(m.a.y, m.b.y)
)
let v = (m.prize.x, m.prize.y)
let mat2 = (
(mat.at(1).at(1), -mat.at(0).at(1)),
(-mat.at(1).at(0), mat.at(0).at(0)),
)
let (a, b) = mat-mul-v(mat2, v)
let d = det(mat)
// Check integer solution
if calc.rem(a, d) != 0 or calc.rem(b, d) != 0 {
continue
}
a = int(a / d)
b = int(b / d)
if a > 0 and b > 0 {
total += a * 3 + b
}
}
return total
}
#show-puzzle(
13, 2,
solve,
example: 875318608908
)

Binary file not shown.