mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-08 11:47:47 +00:00
add day_03
This commit is contained in:
parent
6fbf18f804
commit
df944f69f2
19
day_03/1.rb
Executable file
19
day_03/1.rb
Executable 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
27
day_03/2.rb
Executable 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
|
||||||
Loading…
x
Reference in New Issue
Block a user