day 13 puzzle 1

This commit is contained in:
Louis Heredero 2024-12-13 12:27:51 +01:00
parent c23ff2ea4b
commit 6caa3516e6
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 104 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 122 KiB

View File

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

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
)

0
src/day13/puzzle2.typ Normal file
View File

Binary file not shown.