Skip to content

Commit 6b21ebf

Browse files
committed
Day 14 - Puzzle 2
Finally got it.
1 parent f945b3c commit 6b21ebf

File tree

2 files changed

+1
-47
lines changed

2 files changed

+1
-47
lines changed

β€Žlib/solutions/day_14.rbβ€Ž

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
class Day14
2-
KNOWN_TREE_PATTERN = [
3-
' # ',
4-
' ### ',
5-
' ##### '
6-
]
72
def part_one(input, w = 101, h = 103)
83
quadrants = [0, 0, 0, 0]
94
wd = w / 2
@@ -36,12 +31,8 @@ def part_two(input, w = 101, h = 103)
3631
moves = 0
3732
loop do
3833
coords = bots.map(&:p)
39-
grid = to_grid(w, h, coords)
40-
bounding_box = bounding_box(coords)
4134

42-
pattern = extract_pattern(grid, bounding_box)
43-
44-
if matches_christmas_tree?(pattern)
35+
if coords.size == coords.uniq.size
4536
puts "Found it after #{moves} moves"
4637
display_bots(w, h, coords)
4738
break
@@ -52,37 +43,6 @@ def part_two(input, w = 101, h = 103)
5243
end
5344
end
5445

55-
def to_grid(w, h, coords)
56-
grid = Array.new(h) { Array.new(w, '.') }
57-
coords.each do |x, y|
58-
grid[y][x] = '#'
59-
end
60-
grid
61-
end
62-
63-
def bounding_box(coords)
64-
x_values = coords.map { |x, _| x }
65-
y_values = coords.map { |_, y| y }
66-
67-
[x_values.min, y_values.min, x_values.max, y_values.max]
68-
end
69-
70-
def extract_pattern(grid, bounding_box)
71-
x_min, y_min, x_max, y_max = bounding_box
72-
pattern = []
73-
(y_min..y_max).each do |y|
74-
row = grid[y][x_min..x_max].join
75-
pattern << row
76-
end
77-
pattern
78-
end
79-
80-
def matches_christmas_tree?(pattern)
81-
pattern[0].count('#') == 1 &&
82-
(pattern[1].count('#') == 2 || pattern[1].count('#') == 3) &&
83-
(pattern[1].count('#') == 2 || pattern[1].count('#') == 5)
84-
end
85-
8646
def display_bots(w, h, coords)
8747
grid = Array.new(h) { Array.new(w, '.') }
8848
coords.each do |x, y|

β€Žspec/solutions/day_14_spec.rbβ€Ž

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,4 @@
3535
expect(subject.part_one(input, 11, 7)).to eq(12)
3636
end
3737
end
38-
39-
describe '#part_two' do
40-
it 'calculates the correct solutions for part two' do
41-
expect(subject.part_two(input)).to eq(0)
42-
end
43-
end
4438
end

0 commit comments

Comments
Β (0)