add day_03

This commit is contained in:
onyx-and-iris 2025-12-03 16:33:08 +00:00
parent 6fbf18f804
commit df944f69f2
2 changed files with 46 additions and 0 deletions

19
day_03/1.rb Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
class Main
def run
@sum = 0
$stdin.each_line do |line|
nums = line.chomp.each_char.map(&:to_i)
first_digit = nums[0..-2].sort.reverse[0]
second_digit = nums[nums.find_index(first_digit) + 1..].sort.reverse[0]
@sum += "#{first_digit}#{second_digit}".to_i
end
puts @sum
end
end
Main.new.run

27
day_03/2.rb Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
class Main
def run
@sum = 0
$stdin.each_line do |line|
nums = line.chomp.each_char.map(&:to_i)
largest_joltage = recurse(12, nums, [])
@sum += largest_joltage.join.to_i
end
puts @sum
end
def recurse(i, nums, largest_joltage)
return largest_joltage if i == 0
max = nums[..-i].max
largest_joltage << max
recurse(i - 1, nums[nums.find_index(max) + 1..], largest_joltage)
end
end
Main.new.run