Skip to content

Commit

Permalink
Add 2024, day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Dec 4, 2024
1 parent f11d754 commit 48d2731
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2024/day03/day03a
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby

memory = File.read(ARGV[0]).strip

res = memory.scan(/mul\(\d{1,3},\d{1,3}\)/).sum do |expr|
expr.scan(/\d+/).map(&:to_i).reduce(:*)
end

puts res
24 changes: 24 additions & 0 deletions 2024/day03/day03b
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

def evaluate(expr) = expr.scan(/\d+/).map(&:to_i).reduce(:*)

ENABLE = "do()"
DISABLE = "don't()"

memory = File.read(ARGV[0]).strip

total = 0
enabled = true

memory.scan(/mul\(\d{1,3},\d{1,3}\)|do\(\)|don't\(\)/).each do |expr|
case expr
when ENABLE
enabled = true
when DISABLE
enabled = false
else
total += evaluate(expr) if enabled
end
end

puts total

0 comments on commit 48d2731

Please sign in to comment.