-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.jl
35 lines (31 loc) · 1013 Bytes
/
day19.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Day19
using AdventOfCode2017
function day19(input::String = readInput(joinpath(@__DIR__, "..", "data", "day19.txt")))
board = map(x -> x[1], reduce(vcat, permutedims.(split.(split(replace(input, " " => '#')), ""))))
start = findfirst(x -> x == '|', board[1,:])
current = CartesianIndex(1, start)
nsteps = 0
dir = CartesianIndex(1, 0)
collected = []
while true
nsteps += 1
if isletter(board[current])
push!(collected, board[current])
end
if board[current + dir] != '#'
current += dir
elseif board[current + left(dir)] != '#'
dir = left(dir)
current += dir
elseif board[current + right(dir)] != '#'
dir = right(dir)
current += dir
else
break
end
end
return [join(collected), nsteps]
end
left(ind::CartesianIndex) = CartesianIndex(-ind[2], ind[1])
right(ind::CartesianIndex) = CartesianIndex(ind[2], -ind[1])
end # module