Skip to content

Commit

Permalink
[Day 24] Add solution for part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Dec 26, 2023
1 parent fb34d0b commit 6b0dd54
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.0"
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

Expand Down
2 changes: 1 addition & 1 deletion src/AdventOfCode2023.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module AdventOfCode2023
using BenchmarkTools
using Printf

solvedDays = 1:23
solvedDays = 1:24
# Include the source files:
for day in solvedDays
ds = @sprintf("%02d", day)
Expand Down
43 changes: 43 additions & 0 deletions src/day24.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Day24

using AdventOfCode2023
using LinearAlgebra

struct Hailstone
p::Vector{Int}
v::Vector{Int}
end


function day24(input::String = readInput(joinpath(@__DIR__, "..", "data", "day24.txt")))
hailstones = parse_input(input)
part1(hailstones)
end

function parse_input(input::AbstractString)
data = Hailstone[]
for line eachsplit(rstrip(input), "\n")
left, right = strip.(split(line, "@"))
push!(data, Hailstone(parse.(Int, split(left, ",")), parse.(Int, split(right, ","))))
end
return data
end

function part1(data::Vector{Hailstone}; leftbound::Int64=20_0000_000_000_000, rightbound::Int64=400_000_000_000_000)
c = 0
for i firstindex(data):lastindex(data)
for j i + 1: lastindex(data)
A = hcat(data[i].v[1:2], -data[j].v[1:2])
rhs = data[j].p[1:2] - data[i].p[1:2]
A[1] * A[4] - A[2] * A[3] == 0 && continue # parallel
t, s = A \ rhs
x, y = data[i].p[1:2] + t * data[i].v[1:2]
if leftbound <= x <= rightbound && leftbound <= y <= rightbound && t >= 0 && s >= 0
c += 1
end
end
end
return c
end

end # module

0 comments on commit 6b0dd54

Please sign in to comment.