11class 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 |
0 commit comments