diff --git a/README.md b/README.md index 5ed47f1..e6b71fc 100644 --- a/README.md +++ b/README.md @@ -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 | \ No newline at end of file +| 12 | 3.904 ms | 4620| 2.33 MiB | +| 13 | 127.304 μs | 4162| 185.08 KiB | \ No newline at end of file diff --git a/inputs b/inputs index d6bb60d..75734f4 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit d6bb60d216b835104de191b952832f49da3b9c8e +Subproject commit 75734f4ba43c319d6214ae96e4ef4144a7faebc3 diff --git a/solutions/2024/src/AdventOfCode24.jl b/solutions/2024/src/AdventOfCode24.jl index eb1ce1a..5606172 100644 --- a/solutions/2024/src/AdventOfCode24.jl +++ b/solutions/2024/src/AdventOfCode24.jl @@ -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 diff --git a/solutions/2024/src/day13.jl b/solutions/2024/src/day13.jl new file mode 100644 index 0000000..c2e1cdf --- /dev/null +++ b/solutions/2024/src/day13.jl @@ -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 + diff --git a/solutions/2024/test/runtests.jl b/solutions/2024/test/runtests.jl index bcf64f9..6ea7840 100644 --- a/solutions/2024/test/runtests.jl +++ b/solutions/2024/test/runtests.jl @@ -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