Skip to content

Commit

Permalink
Add 2023 day 10, part two
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Dec 10, 2023
1 parent ec88f85 commit cfeaf3f
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions 2023/day10/day10b
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,25 @@ class RouteFinder

def move
pos = @route.last
warn "looking at #{pos}"
[
{coords: {x: pos[:x] - 1, y: pos[:y]}, good_from: "S-7J", good_to: "S-LF"},
{coords: {x: pos[:x] + 1, y: pos[:y]}, good_from: "S-FL", good_to: "S-J7"},
{coords: {x: pos[:x], y: pos[:y] - 1}, good_from: "S|JL", good_to: "S|7F"},
{coords: {x: pos[:x], y: pos[:y] + 1}, good_from: "S|7F", good_to: "S|JL"}
].each do |next_pos|
warn "considering #{next_pos}"
next unless on_sketch?(next_pos[:coords])
warn "#{next_pos} is on sketch"

tile = @sketch[next_pos[:coords][:y]][next_pos[:coords][:x]]
warn "tile: #{tile}"

next unless next_pos[:good_from].include?(pos[:tile])
next unless next_pos[:good_to].include?(tile)

if tile == "S" && @route.length > 2
warn "looped around to start!"
@finished = true
break
end

warn "#{tile} could connect"
next if route.length > 1 && same_pos?(next_pos[:coords], route[-2])
warn "#{next_pos} is not moving backwards, is next!"

route << next_pos[:coords].merge({tile: tile})
break
Expand All @@ -51,12 +44,53 @@ class RouteFinder

def finished? = @finished

def filtered
@filtered ||= @sketch.map.with_index do |row, y|
row.map.with_index do |tile, x|
route_set.include?({x: x, y: y, tile: tile}) ? tile : "."
end.map { |tile| tile.tr("-|F7LJ", "─│┌┐└┘") }
end

# Replace "S" with correct pipe
prev_x = @route[-1][:x]
prev_y = @route[-1][:y]
next_x = @route[1][:x]
next_y = @route[1][:y]
@filtered[@route[0][:y]][@route[0][:x]] = case
when (next_x - prev_x).abs == 2 then "─"
when (next_y - prev_y).abs == 2 then "│"
when next_x == prev_x - 1 && next_y == prev_y - 1 then "┐"
when next_x == prev_x + 1 && next_y == prev_y - 1 then "┌"
when next_y == prev_y + 1 && next_x == prev_x + 1 then "┘"
when next_y == prev_y + 1 && next_x == prev_x - 1 then "└"
end

@filtered
end

def draw_route
filtered = @sketch.map.with_index do |row, y|
row.map.with_index { |tile, x| route.include?({x: x, y: y, tile: tile}) ? tile : "." }.join
puts filtered.map(&:join)
end

def count_enclosed
enclosed = 0
re = /│|┌─*┘|└─*┐/
filtered.each_with_index do |line, y|
line.each_with_index do |tile, x|
next if tile != "."
next if line[...x].all? { |tile| tile == "." }
next if line[x..].all? { |tile| tile == "." }
next unless line[x + 1..].join.scan(re).length.odd?

enclosed += 1
end
end

puts filtered
enclosed
end

private def route_set
@route_set ||= Set.new(@route)
end

private def on_sketch?(point)
Expand All @@ -73,4 +107,4 @@ file = File.open(ARGV[0])
finder = RouteFinder.new(file.readlines)
finder.move until finder.finished?

puts finder.route.length / 2
puts finder.count_enclosed

0 comments on commit cfeaf3f

Please sign in to comment.