diff --git a/Project.toml b/Project.toml index 882635f..f2a7af2 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/AdventOfCode2023.jl b/src/AdventOfCode2023.jl index bbfef12..40b6267 100644 --- a/src/AdventOfCode2023.jl +++ b/src/AdventOfCode2023.jl @@ -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) diff --git a/src/day24.jl b/src/day24.jl new file mode 100644 index 0000000..d506049 --- /dev/null +++ b/src/day24.jl @@ -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