Skip to content

Commit

Permalink
[Day 5] Improve performance by using Vector instead of Set
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Jan 2, 2024
1 parent 72c04e8 commit 8d80b24
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 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) | 456.675 ms | 249.62 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) | 71.969 ms | 198.09 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) | 7.953 μs | 7.44 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day06.jl) |
| 7 | [:white_check_mark:](https://adventofcode.com/2023/day/7) | 13.584 ms | 18.21 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day07.jl) |
| 8 | [:white_check_mark:](https://adventofcode.com/2023/day/8) | 11.879 ms | 556.42 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day08.jl) |
Expand Down
12 changes: 6 additions & 6 deletions src/day05.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ function perform_mapping(data::Dict{Tuple{String,String},Matrix{Int}}, source::S
return number
end

function perform_mapping(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::Vector{UnitRange{Int}})
M = data[(source, destination)]
newset = Set{UnitRange{Int}}()
new = 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 !isempty(inter)
mapped = true
push!(newset, inter[1] - M[row,2] + M[row,1] : inter[end] - M[row,2] + M[row,1])
push!(new, inter[1] - M[row,2] + M[row,1] : inter[end] - M[row,2] + M[row,1])
left = ran[1]:inter[1]-1
isempty(left) || push!(numbers, left)
right = inter[end]+1:ran[end]
isempty(right) || push!(numbers, right)
break
end
end
mapped || push!(newset, ran)
mapped || push!(new, ran)
end
return newset
return new
end

function part1(seeds::Vector{Int}, data::Dict{Tuple{String,String},Matrix{Int}})
Expand All @@ -75,7 +75,7 @@ function part2(seeds::Vector{Int}, data::Dict{Tuple{String,String},Matrix{Int}})
seedstarts = seeds[1:2:end]
seedlengths = seeds[2:2:end]
for (ss, sl) zip(seedstarts, seedlengths)
numbers = Set([ss:ss+sl-1])
numbers = [ss:ss+sl-1]
for (src, dest) zip(chain[1:end-1], chain[2:end])
numbers = perform_mapping(data, src, dest, numbers)
end
Expand Down

0 comments on commit 8d80b24

Please sign in to comment.