Skip to content

Commit

Permalink
Add solution for day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikSchnack committed Dec 9, 2024
1 parent 24859b6 commit e17757e
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inputs
2 changes: 1 addition & 1 deletion solutions/2024/src/AdventOfCode24.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module AdventOfCode24
readInput(day::Int) = AdventOfCode.readInput(day, year)
export readInput

solvedDays = [1, 2, 3, 4, 5, 6, 7, 8]
solvedDays = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Include the source files:
for day in solvedDays
Expand Down
140 changes: 140 additions & 0 deletions solutions/2024/src/day09.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module Day09
using ..AdventOfCode24

"""
day09()
Solves the two puzzles of day 09.
"""

function day09(input::String = readInput(09))
disk = [parse(Int, m) for m in input]

maxid = floor(Int, length(disk)/2)
ids = Dict{Int, Int}(k-1 => m for (k,m) in enumerate(disk[1:2:end]) )
gaps = Dict{Int, Vector{Int}}( k-1 => [m] for (k,m) in enumerate(disk[2:2:end]) )
gaps[maxid] = [0]

ids1 = deepcopy(ids)
gaps1 = deepcopy(gaps)

d = Int[]
d1 = Int[]
for k in 0:maxid
fill_id!(d, ids, k)
fill_id!(d1, ids1, k)

fill_gaps0!(d, ids, gaps, k)
fill_gaps1!(d1, ids1, gaps1, k)
end

s0 = checksum(d)
s1 = checksum(d1)

return [s0,s1]
end

function checksum(d::Vector{Int})
s = 0
for (k,m) in enumerate(d)
(m >= 0) && (s += (k-1) * m)
end

return s
end

function fill_id!(d::Vector{Int}, ids::Dict{Int, Int}, k::Int)

!haskey(ids, k) && return

for _ in 1:ids[k]
push!(d, k)
end

delete!(ids, k)

end

function fill_gaps1!(d::Vector{Int}, ids::Dict{Int, Int}, gaps::Dict{Int, Vector{Int}}, k::Int)

for g in gaps[k]
l = g

while true
l == 0 && break
l_old = l

for k in maxid:-1:1
!haskey(ids, k) && continue

if l >= ids[k]

l = l-ids[k]

for _ in 1:ids[k]
push!(d, k)
end

pushfirst!(gaps[k], ids[k])

delete!(ids, k)

break
end

end

if l == l_old
for _ in 1:l
push!(d, -1)
end

break
end
end

end


end

function fill_gaps0!(d::Vector{Int}, ids::Dict{Int, Int}, gaps::Dict{Int, Vector{Int}}, k::Int)

for g in gaps[k]
l = g
while l > 0 && !isempty(ids)

for k in maxid:-1:1
!haskey(ids, k) && continue

if l >= ids[k]

l = l-ids[k]

for _ in 1:ids[k]
push!(d, k)
end

delete!(ids, k)

break
else

for _ in 1:l
push!(d, k)
end

ids[k] -= l
l = 0
break
end
end

end

end

end

end

4 changes: 4 additions & 0 deletions solutions/2024/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ end
@testset "Day 08" begin
@test AdventOfCode24.Day08.day08() == [390, 1246]
end

@testset "Day 09" begin
@test AdventOfCode24.Day09.day09() == [6378826667552, 6413328569890]
end

0 comments on commit e17757e

Please sign in to comment.