-
Notifications
You must be signed in to change notification settings - Fork 1
/
03-2.rb
executable file
·93 lines (76 loc) · 1.61 KB
/
03-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
require 'ostruct'
class Number
attr_accessor :left, :right, :up, :down, :value
def initialize(x, y)
@left = x
@right = x
@up = y
@down = y
@value = 0
end
def add_digit(digit, x, y)
@value = @value * 10 + digit.to_i
@right = x
end
def adjacent?(pos)
pos.x >= @left - 1 and
pos.x <= @right + 1 and
pos.y >= @up - 1 and
pos.y <= @down + 1
end
def to_s
"<#{self.class}: #{@value}, l: #{@left}, r: #{@right}, u: #{@up}, d: #{@down}>"
end
end
class Gear
attr_accessor :parts, :pos
def initialize(pos)
@pos = pos
@parts = []
end
def add_part(num)
@parts << num
end
def valid?
@parts.size == 2
end
def ratio
@parts[0].value * @parts[1].value
end
def to_s
"<#{self.class}: valid: #{valid?}, x: #{@pos.x}, y: #{@pos.y}, parts: #{@parts}>"
end
end
input = File.read('03.input').lines.map(&:strip).map(&:chars)
numbers = []
symbols = []
gears = []
input.each_with_index do |row, y|
current_number = nil
row.each_with_index do |char, x|
case char
when '.'
current_number = nil
when '0'..'9'
unless current_number
current_number = Number.new(x, y)
numbers << current_number
end
current_number.add_digit(char, x, y)
else
current_number = nil
pos = OpenStruct.new
pos.x = x
pos.y = y
symbols << pos
gears << Gear.new(pos) if char == '*'
end
end
end
gears.each do |gear|
numbers.each do |num|
gear.add_part num if num.adjacent? gear.pos
end
end
print gears.select(&:valid?).map(&:ratio).sum, "\n"