Skip to content

Commit

Permalink
Day 14 - Puzzle 2 - Attempt 2
Browse files Browse the repository at this point in the history
Try to detect the top
  • Loading branch information
ariejan committed Dec 14, 2024
1 parent ba776fe commit f945b3c
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions lib/solutions/day_14.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Day14
KNOWN_TREE_PATTERN = [
' # ',
' ### ',
' ##### '
]
def part_one(input, w = 101, h = 103)
quadrants = [0, 0, 0, 0]
wd = w / 2
Expand Down Expand Up @@ -28,28 +33,54 @@ def part_two(input, w = 101, h = 103)
.map { |line| line.scan(/(-?\d+)/).flatten }
.map { |values| Robot.new(values[0].to_i, values[1].to_i, values[2].to_i, values[3].to_i, w, h) }

moves = 1
moves = 0
loop do
bots.map(&:move!)
coords = bots.map(&:p)
grid = to_grid(w, h, coords)
bounding_box = bounding_box(coords)

pattern = extract_pattern(grid, bounding_box)

if all_semetrical?(w, h, coords)
puts "--- Move #{moves} ---"
display_bots(w, h, bots.map(&:p))
puts ''
if matches_christmas_tree?(pattern)
puts "Found it after #{moves} moves"
display_bots(w, h, coords)
break
end

bots.map(&:move!)
moves += 1
end
end

def all_semetrical?(w, h, coords)
def to_grid(w, h, coords)
grid = Array.new(h) { Array.new(w, '.') }
coords.each do |x, y|
grid[y][x] = '#'
end
grid.all? { |row| row == row.reverse }
grid
end

def bounding_box(coords)
x_values = coords.map { |x, _| x }
y_values = coords.map { |_, y| y }

[x_values.min, y_values.min, x_values.max, y_values.max]
end

def extract_pattern(grid, bounding_box)
x_min, y_min, x_max, y_max = bounding_box
pattern = []
(y_min..y_max).each do |y|
row = grid[y][x_min..x_max].join
pattern << row
end
pattern
end

def matches_christmas_tree?(pattern)
pattern[0].count('#') == 1 &&
(pattern[1].count('#') == 2 || pattern[1].count('#') == 3) &&
(pattern[1].count('#') == 2 || pattern[1].count('#') == 5)
end

def display_bots(w, h, coords)
Expand Down

0 comments on commit f945b3c

Please sign in to comment.