Skip to content

Commit

Permalink
[Day 21] Little improvement by changing to matrix of Int8 (previous b…
Browse files Browse the repository at this point in the history
…enchmark was on another machine)
  • Loading branch information
goggle committed Jan 6, 2024
1 parent 0c4a8ec commit 257375f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 18 | [:white_check_mark:](https://adventofcode.com/2023/day/18) | 630.330 μs | 484.14 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day18.jl) |
| 19 | [:white_check_mark:](https://adventofcode.com/2023/day/19) | 2.559 ms | 1.79 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day19.jl) |
| 20 | [:white_check_mark:](https://adventofcode.com/2023/day/20) | 65.710 ms | 28.76 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day20.jl) |
| 21 | [:white_check_mark:](https://adventofcode.com/2023/day/21) | 73.583 ms | 187.11 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day21.jl) |
| 21 | [:white_check_mark:](https://adventofcode.com/2023/day/21) | 146.688 ms | 185.76 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day21.jl) |
| 22 | [:white_check_mark:](https://adventofcode.com/2023/day/22) | 790.712 ms | 631.26 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day22.jl) |
| 23 | [:white_check_mark:](https://adventofcode.com/2023/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day23.jl) |
| 24 | [:white_check_mark:](https://adventofcode.com/2023/day/24) | 41.181 ms | 49.71 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day24.jl) |
Expand Down
57 changes: 29 additions & 28 deletions src/day21.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,42 @@ using AdventOfCode2023
function day21(input::String = readInput(joinpath(@__DIR__, "..", "data", "day21.txt")))
data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
start = findfirst(x -> x == 'S', data)
data[start] = '.'

return [part1(data, start), part2(data, start)]
M = zeros(Int8, size(data))
M[data .== '#'] .= -1
return [part1(M, start), part2(M, start)]
end

function part1(data::Matrix{Char}, start::CartesianIndex{2}; nsteps::Int = 64)
data = copy(data)
data[start] = 'O'
function part1(M::Matrix{Int8}, start::CartesianIndex{2}; nsteps::Int = 64)
M = copy(M)
M[start] = 1
for _ 1:nsteps
step!(data)
step!(M)
end
return findall(x -> x == 'O', data) |> length
return sum(x -> x > 0, M)
end

function part2(data::Matrix{Char}, start::CartesianIndex{2})
sdata = copy(data)
sdata[start] = 'O'
M = [data data data data data;
data data data data data;
data data sdata data data;
data data data data data;
data data data data data]
function part2(M::Matrix{Int8}, start::CartesianIndex{2})
MM = copy(M)
MM[start] = 1
bigM = [M M M M M ;
M M M M M ;
M M MM M M ;
M M M M M ;
M M M M M ]

reachables = Int[]
for _ 1:65
step!(M)
step!(bigM)
end
push!(reachables, findall(x -> x == 'O', M) |> length)
push!(reachables, findall(x -> x == 1, bigM) |> length)
for _ 1:131
step!(M)
step!(bigM)
end
push!(reachables, findall(x -> x == 'O', M) |> length)
push!(reachables, findall(x -> x == 1, bigM) |> length)
for _ 1:131
step!(M)
step!(bigM)
end
push!(reachables, findall(x -> x == 'O', M) |> length)
push!(reachables, findall(x -> x == 1, bigM) |> length)

# Some explanations:
#
Expand Down Expand Up @@ -68,14 +69,14 @@ function part2(data::Matrix{Char}, start::CartesianIndex{2})
return Int(p(Int64(202300)))
end

function step!(data)
positions = findall(x -> x == 'O', data)
data[positions] .= '.'
dirs = CartesianIndex.((1,0,-1,0),(0,1,0,-1))
function step!(data::Matrix{Int8})
positions = findall(x -> x == 1, data)
data[positions] .= 0
dirs = CartesianIndex.((1,0,-1,0), (0,1,0,-1))
for pos positions
for dir dirs
if data[pos + dir] != '#'
data[pos + dir] = 'O'
if data[pos + dir] != -1
data[pos + dir] = 1
end
end
end
Expand Down

0 comments on commit 257375f

Please sign in to comment.