From 42fc5e6b3b5aad8584c0071c0f382d637c58d97b Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Thu, 18 Jan 2024 10:11:15 +0100 Subject: [PATCH] [Day 24] Use a simpler and more robust method --- README.md | 2 +- src/day24.jl | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index baad355..dc62827 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven | 21 | [:white_check_mark:](https://adventofcode.com/2023/day/21) | 9.675 ms | 7.19 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day21.jl) | | 22 | [:white_check_mark:](https://adventofcode.com/2023/day/22) | 790.712 ms | 631.26 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day22.jl) | | 23 | [:white_check_mark:](https://adventofcode.com/2023/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day23.jl) | -| 24 | [:white_check_mark:](https://adventofcode.com/2023/day/24) | 41.181 ms | 49.71 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day24.jl) | +| 24 | [:white_check_mark:](https://adventofcode.com/2023/day/24) | 43.214 ms | 49.77 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day24.jl) | | 25 | [:white_check_mark:](https://adventofcode.com/2023/day/25) | 69.476 ms | 62.03 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day25.jl) | diff --git a/src/day24.jl b/src/day24.jl index e8b8521..568c8f4 100644 --- a/src/day24.jl +++ b/src/day24.jl @@ -41,24 +41,38 @@ function part1(lines::Vector{Line}; leftbound::Int64=20_0000_000_000_000, rightb return c end -function part2(lines::Vector{Line}) - for lind=firstindex(lines):lastindex(lines)-3 - i, j, k = lind, lind + 1, lind + 2 - cij, sij = _coeffs_rhsscalar(lines[i], lines[j]) - cik, sik = _coeffs_rhsscalar(lines[i], lines[k]) - cjk, sjk = _coeffs_rhsscalar(lines[j], lines[k]) - A = vcat(cij', cik', cjk') - det(A) ≈ 0 && continue - rhs = [sij, sik, sjk] - w = round.(Int, A \ rhs) - A = hcat((lines[i].v - w)[1:2], (w - lines[j].v)[1:2]) - rhs = (lines[j].p - lines[i].p)[1:2] - t, _ = A \ rhs - return round.(Int, eval_at(Line(lines[i].p, lines[i].v - w), t)) |> sum - end +function _build_equation(li::Line, lj::Line, ind1::Int, ind2::Int) + lhs = zeros(Int, 6) + lhs[ind1] = li.v[ind2] - lj.v[ind2] + lhs[ind2] = lj.v[ind1] - li.v[ind1] + lhs[ind1 + 3] = lj.p[ind2] - li.p[ind2] + lhs[ind2 + 3] = li.p[ind1] - lj.p[ind1] + rhs = li.p[ind1] * li.v[ind2] + lj.p[ind2] * lj.v[ind1] - li.p[ind2] * li.v[ind1] - lj.p[ind1] * lj.v[ind2] + return lhs, rhs end -_coeffs_rhsscalar(l1::Line, l2::Line) = (float(l1.p) - l2.p) × (float(l1.v) - l2.v), dot(float(l1.p) - l2.p, float(l1.v) × l2.v) +function build_system(lines::Vector{Line}) + A = zeros(Rational{BigInt}, 6, 6) + rhs = zeros(Rational{BigInt}, 6) + baselineindex = 1 + objx = ( + (0, 1, 1, 2), + (0, 2, 1, 2), + (0, 1, 1, 3), + (0, 2, 1, 3), + (0, 1, 2, 3), + (0, 2, 2, 3) + ) + for i = 1:6 + A[i,:], rhs[i] = _build_equation(lines[baselineindex+objx[i][1]], lines[baselineindex+objx[i][2]], objx[i][3], objx[i][4]) + end + return A, rhs +end +function part2(lines::Vector{Line}) + A, rhs = build_system(lines) + x = A \ rhs + return round(Int64, sum(x[1:3])) +end end # module