Skip to content

Commit 42fc5e6

Browse files
committed
[Day 24] Use a simpler and more robust method
1 parent b82253d commit 42fc5e6

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
3333
| 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) |
3434
| 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) |
3535
| 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) |
36-
| 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) |
36+
| 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) |
3737
| 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) |
3838

3939

src/day24.jl

+30-16
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,38 @@ function part1(lines::Vector{Line}; leftbound::Int64=20_0000_000_000_000, rightb
4141
return c
4242
end
4343

44-
function part2(lines::Vector{Line})
45-
for lind=firstindex(lines):lastindex(lines)-3
46-
i, j, k = lind, lind + 1, lind + 2
47-
cij, sij = _coeffs_rhsscalar(lines[i], lines[j])
48-
cik, sik = _coeffs_rhsscalar(lines[i], lines[k])
49-
cjk, sjk = _coeffs_rhsscalar(lines[j], lines[k])
50-
A = vcat(cij', cik', cjk')
51-
det(A) 0 && continue
52-
rhs = [sij, sik, sjk]
53-
w = round.(Int, A \ rhs)
54-
A = hcat((lines[i].v - w)[1:2], (w - lines[j].v)[1:2])
55-
rhs = (lines[j].p - lines[i].p)[1:2]
56-
t, _ = A \ rhs
57-
return round.(Int, eval_at(Line(lines[i].p, lines[i].v - w), t)) |> sum
58-
end
44+
function _build_equation(li::Line, lj::Line, ind1::Int, ind2::Int)
45+
lhs = zeros(Int, 6)
46+
lhs[ind1] = li.v[ind2] - lj.v[ind2]
47+
lhs[ind2] = lj.v[ind1] - li.v[ind1]
48+
lhs[ind1 + 3] = lj.p[ind2] - li.p[ind2]
49+
lhs[ind2 + 3] = li.p[ind1] - lj.p[ind1]
50+
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]
51+
return lhs, rhs
5952
end
6053

61-
_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)
54+
function build_system(lines::Vector{Line})
55+
A = zeros(Rational{BigInt}, 6, 6)
56+
rhs = zeros(Rational{BigInt}, 6)
57+
baselineindex = 1
58+
objx = (
59+
(0, 1, 1, 2),
60+
(0, 2, 1, 2),
61+
(0, 1, 1, 3),
62+
(0, 2, 1, 3),
63+
(0, 1, 2, 3),
64+
(0, 2, 2, 3)
65+
)
66+
for i = 1:6
67+
A[i,:], rhs[i] = _build_equation(lines[baselineindex+objx[i][1]], lines[baselineindex+objx[i][2]], objx[i][3], objx[i][4])
68+
end
69+
return A, rhs
70+
end
6271

72+
function part2(lines::Vector{Line})
73+
A, rhs = build_system(lines)
74+
x = A \ rhs
75+
return round(Int64, sum(x[1:3]))
76+
end
6377

6478
end # module

0 commit comments

Comments
 (0)