Skip to content

Commit cf9a347

Browse files
committed
Day 2 - Puzzle 2
1 parent 3a64a13 commit cf9a347

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

β€Žlib/solutions/day_03.rbβ€Ž

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
class Day03
22
REGEX_1 = /mul\((\d{1,3}),(\d{1,3})\)/
3+
REGEX_2 = /(mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\))/
4+
35
def part_one(input)
46
input.scan(REGEX_1).map do |match|
57
match.map(&:to_i).inject(:*)
68
end.sum
79
end
810

911
def part_two(input)
10-
0
12+
matches = input.scan(REGEX_2)
13+
result = 0
14+
enabled = true
15+
16+
matches.each do |match|
17+
instruction = match[0][0..2]
18+
case instruction
19+
when 'mul'
20+
result += match[1].to_i * match[2].to_i if enabled
21+
when 'do('
22+
enabled = true
23+
when 'don'
24+
enabled = false
25+
end
26+
end
27+
28+
result
1129
end
1230
end

β€Žspec/solutions/day_03_spec.rbβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
describe '#part_two' do
1414
it 'calculates the correct solutions for part two' do
15-
expect(subject.part_two(input)).to eq(0)
15+
input = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
16+
expect(subject.part_two(input)).to eq(48)
1617
end
1718
end
1819
end

0 commit comments

Comments
Β (0)