-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [Julia] Day 10 based on @carstenbauer's solution
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
..F7. | ||
.FJ|. | ||
SJ.L7 | ||
|F--J | ||
LJ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FF7FSF7F7F7F7F7F---7 | ||
L|LJ||||||||||||F--J | ||
FL-7LJLJ||||||LJL-77 | ||
F--JF--7||LJLJ7F7FJ- | ||
L---JF-JLJ.||-FJLJJ7 | ||
|F|F-JF---7F7-L7L|7| | ||
|FFJF7L7F-JF7|JL---7 | ||
7-L-JL7||F7|L7F-7F7| | ||
L.L7LFJ|||||FJL7||LJ | ||
L7JLJL-JLJLJL--JLJ.L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
6890 | ||
453 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const CI = CartesianIndex{2} | ||
|
||
# Matrix coord: up down right left | ||
const U, D, R, L = CI(-1, 0), CI(1, 0), CI(0, 1), CI(0, -1) | ||
const FOUR_DIRS = (U, D, L, R) | ||
|
||
const CONNECTIONS = Dict( | ||
'|' => (U, D), | ||
'-' => (L, R), | ||
'L' => (U, R), | ||
'J' => (U, L), | ||
'7' => (L, D), | ||
'F' => (R, D), | ||
) | ||
|
||
function in_out(pipe, in_dir) | ||
E1, E2 = CONNECTIONS[pipe] | ||
if in_dir == E1 | ||
E2 | ||
elseif in_dir == E2 | ||
E1 | ||
else | ||
nothing | ||
end | ||
end | ||
|
||
function step(M, here, here_dir) | ||
next = here + here_dir | ||
next_pipe = M[next] | ||
if next_pipe == 'S' | ||
return next, next | ||
end | ||
next_dir = in_out(next_pipe, -here_dir) | ||
return next, next_dir | ||
end | ||
|
||
function impl1(M, pos, dir) | ||
res = 0 | ||
loop_pipes = [pos] | ||
while true | ||
pos, dir = step(M, pos, dir) | ||
push!(loop_pipes, pos) | ||
res += 1 | ||
M[pos] == 'S' && break | ||
end | ||
return res ÷ 2, loop_pipes | ||
end | ||
|
||
function main(path) | ||
M = stack(readlines(path); dims=1) | ||
pos = findfirst(==('S'), M)::CI | ||
dir_idx = findall(FOUR_DIRS) do dir | ||
!isassigned(M, pos + dir) && return false | ||
sym = M[pos+dir] | ||
!haskey(CONNECTIONS, sym) && return false | ||
out_dir = in_out(sym, -dir) | ||
!isnothing(out_dir) | ||
end | ||
dirs = FOUR_DIRS[dir_idx] | ||
p1, main_loop_pipes = impl1(M, pos, dirs[begin]) | ||
println(p1) | ||
|
||
# replace 'S' with real pipe | ||
M[pos] = findfirst(CONNECTIONS) do v | ||
Set(Tuple(v)) == Set(Tuple(dirs)) | ||
end | ||
# clear out non-pipe map with ' ' | ||
for c in CartesianIndices(M) | ||
if c ∉ main_loop_pipes | ||
M[c] = ' ' | ||
end | ||
end | ||
|
||
for col in eachcol(M) | ||
inside = false | ||
path_start = area_start = 0 | ||
if first(col) != ' ' | ||
path_start = 1 | ||
else | ||
area_start = 1 | ||
end | ||
for (i, c) in enumerate(col) | ||
# maintain state | ||
c in (' ', '|') && continue | ||
|
||
prevc = get(col, i - 1, '.') | ||
if prevc == ' ' # area -> path | ||
col[area_start:i-1] .= inside ? 'I' : 'O' | ||
area_start = nothing | ||
path_start = i | ||
end | ||
|
||
nextc = get(col, i + 1, '.') | ||
if nextc == ' ' # path -> area | ||
if string(col[path_start], c) ∉ ("FL", "7J") | ||
inside = !inside | ||
end | ||
area_start = i + 1 | ||
path_start = nothing | ||
elseif c in ('L', 'J', '-') # path -> new path | ||
if string(col[path_start], c) ∉ ("FL", "7J") | ||
inside = !inside | ||
end | ||
path_start = i + 1 | ||
end | ||
end | ||
end | ||
println(count(==('I'), M)) | ||
|
||
end | ||
|
||
(abspath(PROGRAM_FILE) == @__FILE__) && (main(ARGS[1])) |