From 6b21ebf4ffff6ea5f5d24ea1443fce296ab4c9ae Mon Sep 17 00:00:00 2001 From: Ariejan de Vroom Date: Sat, 14 Dec 2024 08:13:12 +0100 Subject: [PATCH] Day 14 - Puzzle 2 Finally got it. --- lib/solutions/day_14.rb | 42 +---------------------------------- spec/solutions/day_14_spec.rb | 6 ----- 2 files changed, 1 insertion(+), 47 deletions(-) diff --git a/lib/solutions/day_14.rb b/lib/solutions/day_14.rb index 4830fb4..43f67c3 100644 --- a/lib/solutions/day_14.rb +++ b/lib/solutions/day_14.rb @@ -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 @@ -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 @@ -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| diff --git a/spec/solutions/day_14_spec.rb b/spec/solutions/day_14_spec.rb index ede3562..a2f8c5a 100644 --- a/spec/solutions/day_14_spec.rb +++ b/spec/solutions/day_14_spec.rb @@ -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