Skip to content

Commit

Permalink
[Day 14] Revert to using
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Jan 3, 2024
1 parent 1f6cb0b commit 71589cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
30 changes: 11 additions & 19 deletions src/day14.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}()
Expand All @@ -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)
Expand Down

0 comments on commit 71589cb

Please sign in to comment.