advent_of_code/2024_3.rb
Code:
# Advent of Code 2024, Day 3: Mull It Over
# https://adventofcode.com/2024/day/3
require_relative './helpers.rb'
def parse_mul(data)
data.split("mul").map { |a|
a.scan(/^\(\d+,\d+\)/).map { |m|
m[1..-2].split(",").map(&:to_i).inject(:*)
}
}.flatten
end
def part_one(data)
parse_mul(data).sum
end
def part_two(data)
parse_mul(data.split("do()").map { |d| d.split("don't()")[0] }.join).sum
end
def solve(data)
data = data.join
[part_one(data), part_two(data)]
end
puts solve(get_dataset(year: 2024, day: 3, type: 'example'))
puts solve(get_dataset(year: 2024, day: 3, type: 'example_2'))
puts solve(get_dataset(year: 2024, day: 3, type: 'input'))
Output:
161
161
161
48
156388521
75920122