Compare commits

...

2 Commits

Author SHA1 Message Date
ad4adc2372
day 13 puzzle 2 2024-12-13 13:19:44 +01:00
6caa3516e6
day 13 puzzle 1 2024-12-13 12:27:51 +01:00
6 changed files with 177 additions and 0 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 123 KiB

View File

@ -21,4 +21,6 @@
11: 11:
stars: 2 stars: 2
12: 12:
stars: 2
13:
stars: 2 stars: 2

15
res/examples/day13.txt Normal file
View File

@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279

86
src/day13/puzzle1.typ Normal file
View File

@ -0,0 +1,86 @@
#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()),
y: int(match-p.captures.last()),
)
)
}
#let in-line(v1, v2) = {
let f1 = v2.x / v1.x
let f2 = v2.y / v1.y
return f1 == f2
}
#let solve(input) = {
let machines = input.split("\n\n")
machines = machines.map(parse-machine)
let total = 0
for m in machines {
let totals = ()
let are-inline = in-line(m.a, m.b)
for b in range(101) {
let bx = b * m.b.x
let by = b * m.b.y
let rx = m.prize.x - bx
let ry = m.prize.y - by
if rx < 0 or ry < 0 {
break
}
let rax = calc.rem(
rx,
m.a.x
)
let ray = calc.rem(
ry,
m.a.y
)
if rax != 0 or ray != 0 {
continue
}
let a1 = calc.div-euclid(
rx,
m.a.x
)
let a2 = calc.div-euclid(
ry,
m.a.y
)
if a1 != a2 or a1 > 100 {
continue
}
totals.push(b + a1 * 3)
if not are-inline {
break
}
}
if totals.len() != 0 {
total += calc.min(..totals)
}
}
return total
}
#show-puzzle(
13, 1,
solve,
example: 480
)

74
src/day13/puzzle2.typ Normal file
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.