File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed
Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,14 @@ def part_one(input)
1313 @antinodes . flatten . count ( true )
1414 end
1515
16- def part_two ( _input )
17- 0
16+ def part_two ( input )
17+ parse_input ( input )
18+
19+ @nodes . each_pair do |_node , coords |
20+ find_antinodes2 ( coords )
21+ end
22+
23+ @antinodes . flatten . count ( true )
1824 end
1925
2026 def mark_antinode ( x , y )
@@ -25,7 +31,7 @@ def mark_antinode(x, y)
2531
2632 if x < 0 || x >= @w || y < 0 || y >= @h
2733 # puts '- OUT'
28- return
34+ return false
2935 end
3036
3137 # puts ' - OK'
@@ -43,6 +49,29 @@ def find_antinodes(coords)
4349 end
4450 end
4551
52+ def find_antinodes2 ( coords )
53+ # Loop through all coordinate pemutations
54+ coords . permutation ( 2 ) . to_a . each do |a , b |
55+ # Mark this node as antinode
56+ mark_antinode ( a [ 0 ] , a [ 1 ] )
57+
58+ dx = b [ 0 ] - a [ 0 ]
59+ dy = b [ 1 ] - a [ 1 ]
60+
61+ ox = a [ 0 ]
62+ oy = a [ 1 ]
63+
64+ multiplier = 1
65+
66+ loop do
67+ in_grid = mark_antinode ( ox - ( dx * multiplier ) , oy - ( dy * multiplier ) )
68+ break unless in_grid
69+
70+ multiplier += 1
71+ end
72+ end
73+ end
74+
4675 def parse_input ( input )
4776 # Parse grid
4877 @grid = input . split ( "\n " ) . map ( &:chars )
Original file line number Diff line number Diff line change 1212
1313 describe '#part_two' do
1414 it 'calculates the correct solutions for part two' do
15- expect ( subject . part_two ( input ) ) . to eq ( 0 )
15+ expect ( subject . part_two ( input ) ) . to eq ( 34 )
1616 end
1717 end
1818end
You canβt perform that action at this time.
0 commit comments