-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3part1.rb
39 lines (39 loc) · 878 Bytes
/
day3part1.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
file = File.open "day3in.txt"
y = 0
numbers = []
symbols = {}
file.each_line do |l|
x = 0
cur_num = nil
l.then { |line| line.chomp.split "" }.each do |c|
x += 1
if c.match? /[[:digit:]]/
if cur_num == nil
cur_num = { val: c.to_i, x: x, y: y, length: 1, adj_symbol: false }
numbers.push cur_num
else
cur_num[:val] = cur_num[:val] * 10 + c.to_i
cur_num[:length] += 1
end
else
cur_num = nil
symbols["#{x},#{y}"] = { val: c } unless c == "."
end
end
y += 1
end
file.close
numbers.each do |num|
x = num[:x] - 1
y = num[:y] - 1
3.times do |y_offset|
(num[:length] + 2).times do |x_offset|
num[:adj_symbol] = true if symbols["#{x + x_offset},#{y + y_offset}"] != nil
end
end
end
sum = numbers.inject(0) do |sum, num|
sum += num[:val] if num[:adj_symbol]
sum
end
puts sum