diff --git a/progress.png b/progress.png index 58a4d11..9660044 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 9684d46..89cd022 100644 --- a/progress.yaml +++ b/progress.yaml @@ -23,4 +23,4 @@ 12: stars: 2 13: - stars: 1 \ No newline at end of file + stars: 2 \ No newline at end of file diff --git a/src/day13/puzzle2.typ b/src/day13/puzzle2.typ index e69de29..3ab1f8a 100644 --- a/src/day13/puzzle2.typ +++ b/src/day13/puzzle2.typ @@ -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 +) \ No newline at end of file diff --git a/src/main.pdf b/src/main.pdf index dafcee8..0b91fb5 100644 Binary files a/src/main.pdf and b/src/main.pdf differ