Skip to content

Commit

Permalink
[Day 16] Use for directions
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Jan 4, 2024
1 parent ee71df5 commit d7d60ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
40 changes: 20 additions & 20 deletions src/day16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ 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
end
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
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit d7d60ac

Please sign in to comment.