Skip to content

Commit

Permalink
[Day 10] Simplify and improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Jan 2, 2024
1 parent 4be75cc commit 9420c89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 7 | [:white_check_mark:](https://adventofcode.com/2023/day/7) | 11.831 ms | 15.28 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day07.jl) |
| 8 | [:white_check_mark:](https://adventofcode.com/2023/day/8) | 11.879 ms | 556.42 KiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day08.jl) |
| 9 | [:white_check_mark:](https://adventofcode.com/2023/day/9) | 1.408 ms | 1.72 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day09.jl) |
| 10 | [:white_check_mark:](https://adventofcode.com/2023/day/10) | 43.997 ms | 8.82 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day10.jl) |
| 10 | [:white_check_mark:](https://adventofcode.com/2023/day/10) | 7.801 ms | 7.78 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day10.jl) |
| 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) |
Expand Down
53 changes: 15 additions & 38 deletions src/day10.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,20 @@ function part1(data::Matrix{Char}, spos::CartesianIndex{2})
return maximum(distance), x2positions
end

const global d = Dict(
'|' => ((1, 0), (-1, 0)),
'-' => ((0, 1), (0, -1)),
'F' => ((1, 0), (0, 1)),
'7' => ((1, 0), (0, -1)),
'J' => ((-1, 0), (0, -1)),
'L' => ((-1, 0), (0, 1))
)
function push_next_positions!(positions::Vector{CartesianIndex{2}}, x2pos::Vector{CartesianIndex{2}}, dmap::Matrix{Int}, cpos::CartesianIndex, c::Char)
if c == '|'
if dmap[cpos[1] + 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, 1, 0)
elseif dmap[cpos[1] - 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, -1, 0)
end
elseif c == '-'
if dmap[cpos[1], cpos[2] + 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, 1)
elseif dmap[cpos[1], cpos[2] - 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, -1)
end
elseif c == 'F'
if dmap[cpos[1] + 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, 1, 0)
elseif dmap[cpos[1], cpos[2] + 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, 1)
end
elseif c == '7'
if dmap[cpos[1] + 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, 1, 0)
elseif dmap[cpos[1], cpos[2] - 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, -1)
end
elseif c == 'J'
if dmap[cpos[1] - 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, -1, 0)
elseif dmap[cpos[1], cpos[2] - 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, -1)
end
elseif c == 'L'
if dmap[cpos[1] - 1, cpos[2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, -1, 0)
elseif dmap[cpos[1], cpos[2] + 1] == -1
_push_position_x2pos!(positions, x2pos, cpos, 0, 1)
end

if dmap[cpos[1] + d[c][1][1], cpos[2] + d[c][1][2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, d[c][1][1], d[c][1][2])
elseif dmap[cpos[1] + d[c][2][1], cpos[2] + d[c][2][2]] == -1
_push_position_x2pos!(positions, x2pos, cpos, d[c][2][1], d[c][2][2])
end
end

Expand All @@ -90,9 +67,9 @@ function part2(x2pos::Vector{CartesianIndex{2}}, osize::Tuple{Int,Int})
end

function flood!(maze::Matrix{Int8})
positions = Set{CartesianIndex{2}}([CartesianIndex(1,1)])
positions = CartesianIndex{2}[CartesianIndex(1,1)]
while !isempty(positions)
pos = pop!(positions)
pos = popfirst!(positions)
maze[pos] = 2
nextidx = [CartesianIndex(pos[1] + 1, pos[2]),
CartesianIndex(pos[1] - 1, pos[2]),
Expand Down

0 comments on commit 9420c89

Please sign in to comment.