mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-07 06:37:49 +00:00
54 lines
958 B
Ruby
Executable File
54 lines
958 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
class Main
|
|
def run
|
|
problems = Hash.new(0)
|
|
$stdin.each_line do |line|
|
|
arr = line.chomp.split.map do |part|
|
|
if /^\d+$/.match?(part)
|
|
part.to_i
|
|
else
|
|
part.to_sym
|
|
end
|
|
end.compact
|
|
|
|
arr.each_with_index do |part, idx|
|
|
problems[idx] = [] unless problems.key?(idx)
|
|
problems[idx] << part
|
|
end
|
|
end
|
|
|
|
@sum = 0
|
|
problems.each do |_, parts|
|
|
@sum += recurse(parts[..-2], parts.last)
|
|
end
|
|
|
|
puts @sum
|
|
end
|
|
|
|
def recurse(nums, op)
|
|
return nums.first if nums.size == 1
|
|
|
|
num1, num2 = nums.shift(2)
|
|
|
|
case op
|
|
when :+
|
|
nums.unshift(num1 + num2)
|
|
when :-
|
|
nums.unshift(num1 - num2)
|
|
when :*
|
|
nums.unshift(num1 * num2)
|
|
when :/
|
|
if num2 == 0
|
|
raise "Division by zero"
|
|
end
|
|
nums.unshift(num1 / num2)
|
|
end
|
|
|
|
recurse(nums, op)
|
|
end
|
|
end
|
|
|
|
Main.new.run
|