Skip to content

Commit

Permalink
Day 14 - Puzzle 2
Browse files Browse the repository at this point in the history
Finally got it.
  • Loading branch information
ariejan committed Dec 14, 2024
1 parent f945b3c commit 6b21ebf
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 47 deletions.
42 changes: 1 addition & 41 deletions lib/solutions/day_14.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
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 @@ -36,12 +31,8 @@ def part_two(input, w = 101, h = 103)
moves = 0
loop do
coords = bots.map(&:p)
grid = to_grid(w, h, coords)
bounding_box = bounding_box(coords)

pattern = extract_pattern(grid, bounding_box)

if matches_christmas_tree?(pattern)
if coords.size == coords.uniq.size
puts "Found it after #{moves} moves"
display_bots(w, h, coords)
break
Expand All @@ -52,37 +43,6 @@ def part_two(input, w = 101, h = 103)
end
end

def to_grid(w, h, coords)
grid = Array.new(h) { Array.new(w, '.') }
coords.each do |x, y|
grid[y][x] = '#'
end
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)
grid = Array.new(h) { Array.new(w, '.') }
coords.each do |x, y|
Expand Down
6 changes: 0 additions & 6 deletions spec/solutions/day_14_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@
expect(subject.part_one(input, 11, 7)).to eq(12)
end
end

describe '#part_two' do
it 'calculates the correct solutions for part two' do
expect(subject.part_two(input)).to eq(0)
end
end
end

0 comments on commit 6b21ebf

Please sign in to comment.