advent_of_code/2024_7.rb
Code:
# Advent of Code 2024, Day 7: Bridge Repair
# https://adventofcode.com/2024/day/7
require_relative './helpers.rb'
def part_one(data)
data.map { |t, ops|
v = [ops[0]]
ops.tail.each { |n| v = v.flat_map { |e| [e + n, e * n] } }
v.include?(t) ? t : 0
}.sum
end
def part_two(data)
data.map { |t, ops|
v = [ops[0]]
ops.tail.each { |n| v = v.flat_map { |e| [e + n, e * n, (e.to_s + n.to_s).to_i] } }
v.include?(t) ? t : 0
}.sum
end
def solve(data)
data.map! { |l| l.split(': ').as { |a, b| [a.to_i, b.split(' ').map(&:to_i)] } }
[part_one(data), part_two(data)]
end
puts solve(get_dataset(year: 2024, day: 7, type: 'example'))
puts solve(get_dataset(year: 2024, day: 7, type: 'input'))
Output:
3749
11387
1985268524462
150077710195188