From d7d60acd454486b951ef7c4c16e4771e2065de2b Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Thu, 4 Jan 2024 02:44:11 +0100 Subject: [PATCH] [Day 16] Use for directions --- README.md | 2 +- src/day16.jl | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index f846de9..7f525d8 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven | 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) | 56.620 ms | 22.56 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) | +| 16 | [:white_check_mark:](https://adventofcode.com/2023/day/16) | 33.972 ms | 53.35 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) | | 18 | [:white_check_mark:](https://adventofcode.com/2023/day/18) | 630.330 μs | 484.14 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day18.jl) | | 19 | [:white_check_mark:](https://adventofcode.com/2023/day/19) | 2.702 ms | 1.85 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day19.jl) | diff --git a/src/day16.jl b/src/day16.jl index ea0920b..33ded8d 100644 --- a/src/day16.jl +++ b/src/day16.jl @@ -5,34 +5,34 @@ using AdventOfCode2023 struct Beam posx::Int posy::Int - dirx::Int - diry::Int + dirx::Int8 + diry::Int8 end function day16(input::String = readInput(joinpath(@__DIR__, "..", "data", "day16.txt"))) data = map(x -> x[1], reduce(vcat, permutedims.(map(x -> split(x, ""), split(input))))) - return [traverse_map(data, 1, 0, 0, 1), part2(data)] + return [traverse_map(data, 1, 0, Int8(0), Int8(1)), part2(data)] end function part2(data::Matrix{Char}) m = 0 for x ∈ 1:size(data, 1) - p = traverse_map(data, x, 0, 0, 1) + p = traverse_map(data, x, 0, Int8(0), Int8(1)) if p > m m = p end - p = traverse_map(data, x, size(data, 2) + 1, 0, -1) + p = traverse_map(data, x, size(data, 2) + 1, Int8(0), Int8(-1)) if p > m m = p end end for y ∈ 1:size(data, 2) - p = traverse_map(data, 0, y, 1, 0) + p = traverse_map(data, 0, y, Int8(1), Int8(0)) if p > m m = p end - p = traverse_map(data, size(data, 1) + 1, y, -1, 0) + p = traverse_map(data, size(data, 1) + 1, y, Int8(-1), Int8(0)) if p > m m = p end @@ -40,7 +40,7 @@ function part2(data::Matrix{Char}) return m end -function traverse_map(data::Matrix{Char}, posx::Int, posy::Int, dirx::Int, diry::Int) +function traverse_map(data::Matrix{Char}, posx::Int, posy::Int, dirx::Int8, diry::Int8) visited = zeros(Bool, size(data, 1), size(data, 2), 3, 3) beams = Vector{Beam}() cont = true @@ -72,12 +72,12 @@ function traverse_map(data::Matrix{Char}, posx::Int, posy::Int, dirx::Int, diry: elseif c == '|' if diry != 0 push!(beams, Beam(posx, posy, 1, 0)) - dirx, diry = -1, 0 + dirx, diry = Int8(-1), Int8(0) end elseif c == '-' if dirx != 0 push!(beams, Beam(posx, posy, 0, 1)) - dirx, diry = 0, -1 + dirx, diry = Int8(0), Int8(-1) end end end @@ -90,18 +90,18 @@ function traverse_map(data::Matrix{Char}, posx::Int, posy::Int, dirx::Int, diry: return count(energized) end -function _reflect_backslash(dirx::Int, diry::Int) - diry == 1 && return 1, 0 - dirx == -1 && return 0, -1 - diry == -1 && return -1, 0 - dirx == 1 && return 0, 1 +function _reflect_backslash(dirx::Int8, diry::Int8) + diry == 1 && return Int8(1), Int8(0) + dirx == -1 && return Int8(0), Int8(-1) + diry == -1 && return Int8(-1), Int8(0) + dirx == 1 && return Int8(0), Int8(1) end -function _reflect_slash(dirx::Int, diry::Int) - diry == 1 && return -1, 0 - dirx == 1 && return 0, -1 - dirx == -1 && return 0, 1 - diry == -1 && return 1, 0 +function _reflect_slash(dirx::Int8, diry::Int8) + diry == 1 && return Int8(-1), Int8(0) + dirx == 1 && return Int8(0), Int8(-1) + dirx == -1 && return Int8(0), Int8(1) + diry == -1 && return Int8(1), Int8(0) end