Skip to content

Commit

Permalink
Add solution for day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikSchnack committed Dec 13, 2024
1 parent 6649bb1 commit 8b133c8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
| 09 | 414.915 ms | 55001| 7.39 MiB |
| 10 | 360.287 μs | 932| 154.28 KiB |
| 11 | 12.206 ms | 85| 817.98 KiB |
| 12 | 3.904 ms | 4620| 2.33 MiB |
| 12 | 3.904 ms | 4620| 2.33 MiB |
| 13 | 127.304 μs | 4162| 185.08 KiB |
2 changes: 1 addition & 1 deletion inputs
2 changes: 1 addition & 1 deletion solutions/2024/src/AdventOfCode24.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module AdventOfCode24
readInput(day::Int) = AdventOfCode.readInput(day, year)
export readInput

solvedDays = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
solvedDays = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

# Include the source files:
for day in solvedDays
Expand Down
47 changes: 47 additions & 0 deletions solutions/2024/src/day13.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Day13
using ..AdventOfCode24

"""
day13()
Solves the two puzzles of day 13.
"""

function day13(input::String = readInput(13))
r = r"""Button A: X\+(\d+), Y\+(\d+)
Button B: X\+(\d+), Y\+(\d+)
Prize: X=(\d+), Y=(\d+)"""

s0 = 0
s1 = 0

for m in eachmatch(r, input)

a11, a21, a12, a22, b1, b2 = parse.(Int, m.captures)

s0 += solve_matrix(a11, a21, a12, a22, b1, b2)

b1 += 10000000000000
b2 += 10000000000000
s1 += solve_matrix(a11, a21, a12, a22, b1, b2)
end

return [s0, s1]
end



function solve_matrix(a11::Int, a21::Int, a12::Int, a22::Int, b1::Int, b2::Int)
det = a11 * a22 - a12 * a21

c1, r1 = divrem(a22 * b1 - a12 * b2, det)
c2, r2 = divrem(a11 * b2 - a21 * b1, det)

(r1 != 0 || r2 != 0) && return 0

return 3 * Int(c1) + Int(c2)
end


end

4 changes: 4 additions & 0 deletions solutions/2024/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ end
@testset "Day 12" begin
@test AdventOfCode24.Day12.day12() == [1352976, 808796]
end

@testset "Day 13" begin
@test AdventOfCode24.Day13.day13() == [32067, 92871736253789]
end

0 comments on commit 8b133c8

Please sign in to comment.