add day_02

This commit is contained in:
onyx-and-iris 2025-12-02 07:10:01 +00:00
parent c0152a39b2
commit 6fbf18f804
2 changed files with 58 additions and 0 deletions

24
day_02/1.rb Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
class Main
def run
@sum = 0
line = $stdin.gets.chomp
line.split(",").each do |range|
lo, hi = range.split("-")
(lo..hi).each do |num|
if num.length.even?
if num[0..(num.length / 2) - 1] == num[(num.length / 2)..]
@sum += num.to_i
end
end
end
end
puts @sum
end
end
Main.new.run

34
day_02/2.rb Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
class Main
def run
@sum = 0
line = $stdin.gets.chomp
line.split(",").each do |range|
lo, hi = range.split("-")
(lo..hi).each do |num|
(1..num.length / 2).each do |n|
if num.length % n != 0
next
end
parts = num.scan(/(\d{#{n}})/).map { _1[0] }
if all_equal? parts
@sum += num.to_i
break
end
end
end
end
puts @sum
end
def all_equal?(arr)
arr.uniq.size <= 1
end
end
Main.new.run