Skip to content

Commit

Permalink
Day 18 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregawoods committed Dec 18, 2023
1 parent bc69447 commit 77ac1a0
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/days/18.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def neighbors
Point.new(x + 1, y),
Point.new(x, y - 1),
Point.new(x, y + 1)
].reject { |p| p.x.negative? || p.y.negative? }
]
end
end

GridInfo = Data.define(:min_x, :max_x, :min_y, :max_y) do
def valid?(x, y)
x >= min_x && x <= max_x && y >= min_y && y < max_y
x >= min_x && x <= max_x && y >= min_y && y <= max_y
end

def size
Expand Down Expand Up @@ -82,19 +82,38 @@ def part1(input)
end
end

outside.to_a.each do |point|
check_neighbors(point, trench, outside, info)
points_to_check = outside.map(&:neighbors).flatten.uniq

loop do
new_points_to_check = []

points_to_check.each do |p|
if !trench.include?(p) && !outside.include?(p) && info.valid?(p.x, p.y)
outside.add(p)
new_points_to_check += p.neighbors
end
end

points_to_check = new_points_to_check

break if points_to_check.empty?
end

info.size - outside.size
end

def check_neighbors(point, trench, outside, info)
point.neighbors.each do |p|
next if trench.include?(p) || outside.include?(p) || !info.valid?(p.x, p.y)

outside.add(p)
check_neighbors(p, trench, outside, info)
def draw_grid(info, trench, outside)
(info.min_x..info.max_x).each do |x|
(info.min_y..info.max_y).each do |y|
if trench.include?(Point.new(x, y))
print '#'.red
elsif outside.include?(Point.new(x, y))
print 'O'.blue
else
print '.'
end
end
print "\n"
end
end

Expand Down

0 comments on commit 77ac1a0

Please sign in to comment.