Skip to content

Commit

Permalink
Day 2 - Puzzle 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ariejan committed Dec 3, 2024
1 parent 3a64a13 commit cf9a347
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/solutions/day_03.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
class Day03
REGEX_1 = /mul\((\d{1,3}),(\d{1,3})\)/
REGEX_2 = /(mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\))/

def part_one(input)
input.scan(REGEX_1).map do |match|
match.map(&:to_i).inject(:*)
end.sum
end

def part_two(input)
0
matches = input.scan(REGEX_2)
result = 0
enabled = true

matches.each do |match|
instruction = match[0][0..2]
case instruction
when 'mul'
result += match[1].to_i * match[2].to_i if enabled
when 'do('
enabled = true
when 'don'
enabled = false
end
end

result
end
end
3 changes: 2 additions & 1 deletion spec/solutions/day_03_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

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

0 comments on commit cf9a347

Please sign in to comment.