diff --git a/progress.png b/progress.png index 50fc9fe..58a4d11 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index 9b41b60..9684d46 100644 --- a/progress.yaml +++ b/progress.yaml @@ -21,4 +21,6 @@ 11: stars: 2 12: - stars: 2 \ No newline at end of file + stars: 2 +13: + stars: 1 \ No newline at end of file diff --git a/res/examples/day13.txt b/res/examples/day13.txt new file mode 100644 index 0000000..444a287 --- /dev/null +++ b/res/examples/day13.txt @@ -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 \ No newline at end of file diff --git a/src/day13/puzzle1.typ b/src/day13/puzzle1.typ new file mode 100644 index 0000000..d872278 --- /dev/null +++ b/src/day13/puzzle1.typ @@ -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 +) \ No newline at end of file diff --git a/src/day13/puzzle2.typ b/src/day13/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index 9b754c8..dafcee8 100644 Binary files a/src/main.pdf and b/src/main.pdf differ