Skip to content

Commit

Permalink
[Day 5] Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Dec 6, 2023
1 parent af03574 commit 9a3c5c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 2 | [:white_check_mark:](https://adventofcode.com/2023/day/2) | 1.050 ms | 350.39 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day02.jl) |
| 3 | [:white_check_mark:](https://adventofcode.com/2023/day/3) | 1.894 ms | 1.80 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day03.jl) |
| 4 | [:white_check_mark:](https://adventofcode.com/2023/day/4) | 2.745 ms | 1.40 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day04.jl) |
| 5 | [:white_check_mark:](https://adventofcode.com/2023/day/5) | 457.289 ms | 203.87 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day05.jl) |
| 5 | [:white_check_mark:](https://adventofcode.com/2023/day/5) | 456.675 ms | 249.62 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day05.jl) |
<!-- | 6 | [:white_check_mark:](https://adventofcode.com/2023/day/6) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day06.jl) | -->
<!-- | 7 | [:white_check_mark:](https://adventofcode.com/2023/day/7) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day07.jl) | -->
<!-- | 8 | [:white_check_mark:](https://adventofcode.com/2023/day/8) | | | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day08.jl) | -->
Expand Down
45 changes: 16 additions & 29 deletions src/day05.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function parse_input(input::AbstractString)
return seeds, data
end

function map_number(data::Dict{Tuple{String,String},Matrix{Int}}, source::String, destination::String, number::Int)
function perform_mapping(data::Dict{Tuple{String,String},Matrix{Int}}, source::String, destination::String, number::Int)
M = data[(source, destination)]
for row axes(M, 1)
if number M[row, 2]:M[row, 2]+M[row, 3]-1
Expand All @@ -34,65 +34,52 @@ function map_number(data::Dict{Tuple{String,String},Matrix{Int}}, source::String
return number
end

function map_ranges(data::Dict{Tuple{String,String},Matrix{Int}}, source::String, destination::String, numbers::Set{UnitRange{Int}})
function perform_mapping(data::Dict{Tuple{String,String},Matrix{Int}}, source::String, destination::String, numbers::Set{UnitRange{Int}})
M = data[(source, destination)]
newset = Set{UnitRange{Int}}()
while !isempty(numbers)
mapped = false
ran = pop!(numbers)
for row axes(M, 1)
inter = intersect(ran, M[row, 2]:M[row, 2]+M[row, 3]-1)
if length(inter) > 0
if !isempty(inter)
mapped = true
push!(newset, inter[1] - M[row,2] + M[row,1] : inter[end] - M[row,2] + M[row,1])
left = ran[1]:inter[1]-1
if length(left) > 0
push!(numbers, left)
end
isempty(left) || push!(numbers, left)
right = inter[end]+1:ran[end]
if length(right) > 0
push!(numbers, right)
end
isempty(right) || push!(numbers, right)
break
end
end
if !mapped
push!(newset, ran)
end
mapped || push!(newset, ran)
end
return newset
end

function part1(seeds::Vector{Int}, data::Dict{Tuple{String,String},Matrix{Int}})
locations = Vector{Int}()
locations = Set{Int}()
chain = ("seed", "soil", "fertilizer", "water", "light", "temperature", "humidity", "location")
for number seeds
number = map_number(data, "seed", "soil", number)
number = map_number(data, "soil", "fertilizer", number)
number = map_number(data, "fertilizer", "water", number)
number = map_number(data, "water", "light", number)
number = map_number(data, "light", "temperature", number)
number = map_number(data, "temperature", "humidity", number)
number = map_number(data, "humidity", "location", number)
for (src, dest) zip(chain[1:end-1], chain[2:end])
number = perform_mapping(data, src, dest, number)
end
push!(locations, number)
end
return minimum(locations)
end

function part2(seeds::Vector{Int}, data::Dict{Tuple{String,String},Matrix{Int}})
locations = Set{UnitRange{Int}}()
chain = ("seed", "soil", "fertilizer", "water", "light", "temperature", "humidity", "location")
seedstarts = seeds[1:2:end]
seedlengths = seeds[2:2:end]
for (ss, sl) zip(seedstarts, seedlengths)
numbers = map_ranges(data, "seed", "soil", Set([ss:ss+sl-1]))
numbers = map_ranges(data, "soil", "fertilizer", numbers)
numbers = map_ranges(data, "fertilizer", "water", numbers)
numbers = map_ranges(data, "water", "light", numbers)
numbers = map_ranges(data, "light", "temperature", numbers)
numbers = map_ranges(data, "temperature", "humidity", numbers)
numbers = map_ranges(data, "humidity", "location", numbers)
for loc numbers
push!(locations, loc)
numbers = Set([ss:ss+sl-1])
for (src, dest) zip(chain[1:end-1], chain[2:end])
numbers = perform_mapping(data, src, dest, numbers)
end
push!(locations, numbers...)
end
return minimum(x -> x[1], locations)
end
Expand Down

0 comments on commit 9a3c5c4

Please sign in to comment.