Skip to content

Commit ccc13f6

Browse files
committed
regroup
1 parent f3ac8e1 commit ccc13f6

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

2023/day03/main1.rb

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,75 +22,48 @@ def neighbours(x, y, arr)
2222
SQUARE.map { |k, l| neighbour(x + k, y + l, arr) }.compact
2323
end
2424

25-
# stars 1
26-
class Stars1
27-
def initialize(arr)
28-
@arr = arr
29-
@current_str = ''
30-
@neighbours = []
31-
@result = 0
32-
end
25+
# main
26+
class Main
27+
attr_reader :stars1
3328

34-
def call
35-
@arr.each_with_index do |row, j|
36-
row.each_char.with_index do |state, i|
37-
update_current(state, i, j)
38-
check_current(row, state, i, j)
39-
end
40-
end
41-
@result
42-
end
43-
44-
def update_current(state, i, j)
45-
return unless state[/\d/]
46-
47-
@current_str += state
48-
@neighbours += neighbours(i, j, @arr)
49-
end
50-
51-
def check_current(row, state, i, _j)
52-
return unless @current_str.length.positive? && (row[i + 1].nil? || !state[/\d/])
53-
54-
@result += @current_str.to_i if @neighbours.any? { |v| v[:value][/[^\d.]/] }
55-
@current_str = ''
56-
@neighbours = []
57-
end
58-
end
59-
60-
# stars 2
61-
class Stars2
6229
def initialize(arr)
6330
@arr = arr
6431
@current_str = ''
6532
@neighbours = []
33+
@stars1 = 0
6634
@gears = {}
6735
end
6836

6937
def call
7038
loop
71-
@gears.filter { |_, v| v.length == 2 }.map { |_, v| v.reduce(&:*) }.sum
39+
self
7240
end
7341

7442
def loop
7543
@arr.each_with_index do |row, j|
7644
row.each_char.with_index do |state, i|
77-
update_current(state, i, j)
78-
check_current(row, state, i, j)
45+
update(state, i, j)
46+
checks(row, state, i, j)
7947
end
8048
end
8149
end
8250

83-
def update_current(state, i, j)
51+
def stars2
52+
@gears.filter { |_, v| v.length == 2 }.map { |_, v| v.reduce(&:*) }.sum
53+
end
54+
55+
def update(state, i, j)
8456
return unless state[/\d/]
8557

8658
@current_str += state
8759
@neighbours += neighbours(i, j, @arr)
8860
end
8961

90-
def check_current(row, state, i, _j)
62+
def checks(row, state, i, _j)
9163
return unless @current_str.length.positive? && (row[i + 1].nil? || !state[/\d/])
9264

9365
update_gears
66+
@stars1 += @current_str.to_i if @neighbours.any? { |v| v[:value][/[^\d.]/] }
9467
@current_str = ''
9568
@neighbours = []
9669
end
@@ -104,5 +77,5 @@ def update_gears
10477
end
10578
end
10679

107-
puts Stars1.new(arr1).call
108-
puts Stars2.new(arr1).call
80+
puts Main.new(arr1).call.stars1
81+
puts Main.new(arr1).call.stars2

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy