Skip to content

Commit

Permalink
[Day 14] Using enum types instead of char makes performance worse (wh…
Browse files Browse the repository at this point in the history
…y Julia?)
  • Loading branch information
goggle committed Jan 3, 2024
1 parent 9420c89 commit 1f6cb0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 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) | 68.515 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) | 93.891 ms | 73.68 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
29 changes: 18 additions & 11 deletions src/day14.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ 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 -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
data = map(x -> to_space(x[1]), reduce(vcat, permutedims.(map(x -> split(x, ""), split(input)))))
return [part1(data), part2(data)]
end

function part1(data::Matrix{Char})
function part1(data::Matrix{Space})
d = copy(data)
tilt_north!(d)
return score(d)
end

function part2(data::Matrix{Char})
function part2(data::Matrix{Space})
mat = copy(data)

d = Dict{String,Int}()
Expand All @@ -37,32 +44,32 @@ function part2(data::Matrix{Char})
return score(mat)
end

function tilt_north!(mat::Matrix{Char})
function tilt_north!(mat::Matrix{Space})
for col eachcol(mat)
cubes = findall(x -> x == '#', col)
cubes = findall(x -> x == cube_rock, 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 == 'O', @view col[c1+1:c2-1])
col[c1+1:c1+nround] .= 'O'
col[c1+nround+1:c2-1] .= '.'
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
end
end
end

function score(mat::Matrix{Char})
function score(mat::Matrix{Space})
s = 0
multi = size(mat,1)
for row eachrow(mat)
s += count(x -> x == 'O', row) * multi
s += count(x -> x == rounded_rock, row) * multi
multi -= 1
end
return s
end

function cycle!(mat::Matrix{Char})
function cycle!(mat::Matrix{Space})
for _ 1:4
tilt_north!(mat)
mat = rotr90(mat)
Expand Down

0 comments on commit 1f6cb0b

Please sign in to comment.