blob: 537a3841761d515ae3f09d263d010bd44cb7bdfe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/usr/bin/env ruby
data = File.read("data.txt")
valid = data.scan(/(mul\(\d+,\d+\)|don't\(\)|do\(\))/)
p valid
sum = 0;
act = true
ops = valid.map do |operation|
if operation[0] == "don't()"
act = false
0
elsif operation[0] == "do()"
act = true
0
elsif act
operation[0].gsub(/[^\d*,]/, "").to_s.split(",").map(&:to_i).reduce(&:*)
else
0
end
end
p ops.sum
|