From 1f6cb0bf5a75670edd4d719949f5358a4af44fb5 Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Wed, 3 Jan 2024 03:12:06 +0100 Subject: [PATCH] [Day 14] Using enum types instead of char makes performance worse (why Julia?) --- README.md | 2 +- src/day14.jl | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index dcfae48..e4d0aea 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) | 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) | diff --git a/src/day14.jl b/src/day14.jl index fcc9502..405fc96 100644 --- a/src/day14.jl +++ b/src/day14.jl @@ -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}() @@ -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)