Skip to content

Commit

Permalink
[Day 14] Precompute positions of cube-shaped rocks
Browse files Browse the repository at this point in the history
goggle committed Jan 3, 2024
1 parent 675c46a commit f4305f0
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 11 | [:white_check_mark:](https://adventofcode.com/2023/day/11) | 4.728 ms | 1.71 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day11.jl) |
| 12 | [:white_check_mark:](https://adventofcode.com/2023/day/12) | 9.320 ms | 2.64 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day12.jl) |
| 13 | [:white_check_mark:](https://adventofcode.com/2023/day/13) | 2.202 ms | 3.18 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day13.jl) |
| 14 | [:white_check_mark:](https://adventofcode.com/2023/day/14) | 72.359 ms | 62.11 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day14.jl) |
| 14 | [:white_check_mark:](https://adventofcode.com/2023/day/14) | 36.220 ms | 28.00 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day14.jl) |
| 15 | [:white_check_mark:](https://adventofcode.com/2023/day/15) | 2.647 ms | 1.49 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day15.jl) |
| 16 | [:white_check_mark:](https://adventofcode.com/2023/day/16) | 65.902 ms | 53.79 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day16.jl) |
| 17 | [:white_check_mark:](https://adventofcode.com/2023/day/17) | 5.126 s | 467.29 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day17.jl) |
41 changes: 27 additions & 14 deletions src/day14.jl
Original file line number Diff line number Diff line change
@@ -4,23 +4,38 @@ using AdventOfCode2023

function day14(input::String = readInput(joinpath(@__DIR__, "..", "data", "day14.txt")))
data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
return [part1(data), part2(data)]

# Precompute the positions of the cube-shaped rockes, so that it must
# not be done in every tilting step again.
cubepos = Dict{Int,Dict{Int, Vector{Int}}}()
mat = data
for i = 0:3
cubepos[i] = Dict{Int,Vector{Int}}()
for (j, col) enumerate(eachcol(mat))
cubes = findall(x -> x == '#', col)
pushfirst!(cubes, 0)
push!(cubes, length(col) + 1)
cubepos[i][j] = cubes
mat = rotr90(mat)
end
end
return [part1(data, cubepos), part2(data, cubepos)]
end

function part1(data::Matrix{Char})
function part1(data::Matrix{Char}, cubepos::Dict{Int,Dict{Int, Vector{Int}}})
d = copy(data)
tilt_north!(d)
tilt_north!(d, 0, cubepos)
return score(d)
end

function part2(data::Matrix{Char})
function part2(data::Matrix{Char}, cubepos::Dict{Int,Dict{Int, Vector{Int}}})
mat = copy(data)

d = Dict{String,Int}()
d[join(mat)] = 0
first = last = 0
for n 1:1_000_000_000
mat = cycle!(mat)
mat = cycle!(mat, cubepos)
s = join(mat)
if haskey(d, s)
first = d[s]
@@ -31,16 +46,14 @@ function part2(data::Matrix{Char})
end
steps_left = mod(10^9 - last, last - first)
for _ 1:steps_left
mat = cycle!(mat)
mat = cycle!(mat, cubepos)
end
return score(mat)
end

function tilt_north!(mat::Matrix{Char})
for col eachcol(mat)
cubes = findall(x -> x == '#', col)
pushfirst!(cubes, 0)
push!(cubes, length(col) + 1)
function tilt_north!(mat::Matrix{Char}, orientation::Int, cubepos::Dict{Int,Dict{Int, Vector{Int}}})
for (j, col) enumerate(eachcol(mat))
cubes = cubepos[orientation][j]
for i firstindex(cubes):lastindex(cubes)-1
c1, c2 = cubes[i], cubes[i+1]
c2 - c1 == 1 && continue
@@ -61,9 +74,9 @@ function score(mat::Matrix{Char})
return s
end

function cycle!(mat::Matrix{Char})
for _ 1:4
tilt_north!(mat)
function cycle!(mat::Matrix{Char}, cubepos::Dict{Int,Dict{Int, Vector{Int}}})
for i 0:3
tilt_north!(mat, i, cubepos)
mat = rotr90(mat)
end
return mat

0 comments on commit f4305f0

Please sign in to comment.