diff --git a/README.md b/README.md index e4d0aea..ff41e94 100644 --- a/README.md +++ b/README.md @@ -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) | 93.891 ms | 73.68 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) | 72.359 ms | 62.11 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) | diff --git a/src/day14.jl b/src/day14.jl index 405fc96..7ff547a 100644 --- a/src/day14.jl +++ b/src/day14.jl @@ -2,26 +2,18 @@ module Day14 using AdventOfCode2023 -@enum Space rounded_rock=1 cube_rock=2 empty=3 - -function to_space(c::Char) - c == '.' && return empty - c == 'O' && return rounded_rock - c == '#' && return cube_rock -end - function day14(input::String = readInput(joinpath(@__DIR__, "..", "data", "day14.txt"))) - data = map(x -> to_space(x[1]), reduce(vcat, permutedims.(map(x -> split(x, ""), split(input))))) + data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input))))) return [part1(data), part2(data)] end -function part1(data::Matrix{Space}) +function part1(data::Matrix{Char}) d = copy(data) tilt_north!(d) return score(d) end -function part2(data::Matrix{Space}) +function part2(data::Matrix{Char}) mat = copy(data) d = Dict{String,Int}() @@ -44,32 +36,32 @@ function part2(data::Matrix{Space}) return score(mat) end -function tilt_north!(mat::Matrix{Space}) +function tilt_north!(mat::Matrix{Char}) for col ∈ eachcol(mat) - cubes = findall(x -> x == cube_rock, col) + cubes = findall(x -> x == '#', col) pushfirst!(cubes, 0) push!(cubes, length(col) + 1) for i ∈ firstindex(cubes):lastindex(cubes)-1 c1, c2 = cubes[i], cubes[i+1] c2 - c1 == 1 && continue - nround = count(x -> x == rounded_rock, @view col[c1+1:c2-1]) - col[c1+1:c1+nround] .= rounded_rock - col[c1+nround+1:c2-1] .= empty + nround = count(x -> x == 'O', @view col[c1+1:c2-1]) + col[c1+1:c1+nround] .= 'O' + col[c1+nround+1:c2-1] .= '.' end end end -function score(mat::Matrix{Space}) +function score(mat::Matrix{Char}) s = 0 multi = size(mat,1) for row ∈ eachrow(mat) - s += count(x -> x == rounded_rock, row) * multi + s += count(x -> x == 'O', row) * multi multi -= 1 end return s end -function cycle!(mat::Matrix{Space}) +function cycle!(mat::Matrix{Char}) for _ ∈ 1:4 tilt_north!(mat) mat = rotr90(mat)