-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_1.rb
47 lines (37 loc) · 770 Bytes
/
day_1.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
sum = 0
File.open('input/day_1.txt').each do |line|
digits = line.scan(/(\d)\w*(\d)/).join("")
if digits.empty?
digit = line.scan(/(\d)/).join("")
digits = digit + digit
end
sum += digits.to_i
end
puts sum
sum = 0
mapper = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9
}
matcher = "(\\d|one|two|three|four|five|six|seven|eight|nine)"
File.open('input/day_1.txt').each do |line|
puts line
digits = line.scan(Regexp.new("#{matcher}\\w*#{matcher}"))
if digits.empty?
digit = line.scan(Regexp.new matcher).first.first
digits = [[digit, digit].first]
end
digits = digits.first.map do |d|
mapper[d.to_sym] || d
end.join("")
puts digits
sum += digits.to_i
end
puts sum