From 8d80b2468214c68659d4cfe7abe6bf8c1f2df025 Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Tue, 2 Jan 2024 03:50:05 +0100 Subject: [PATCH] [Day 5] Improve performance by using Vector instead of Set --- README.md | 2 +- src/day05.jl | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 241bfce..2af45f4 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/src/day05.jl b/src/day05.jl index 7a9d8e0..06e622e 100644 --- a/src/day05.jl +++ b/src/day05.jl @@ -34,9 +34,9 @@ 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) @@ -44,7 +44,7 @@ function perform_mapping(data::Dict{Tuple{String,String},Matrix{Int}}, source::S 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] @@ -52,9 +52,9 @@ function perform_mapping(data::Dict{Tuple{String,String},Matrix{Int}}, source::S 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}}) @@ -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