mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-07 22:57:50 +00:00
35 lines
584 B
Ruby
Executable File
35 lines
584 B
Ruby
Executable File
#!/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
|