aoc_omni/ruby/2025/2/lib/id_range.rb
2025-12-02 06:48:52 -05:00

42 lines
792 B
Ruby

class Integer
def invalid_id?
string = to_s
return false if string.length.odd?
string[..((string.length / 2) - 1)] == string[(string.length / 2)..]
end
def silly_invalid_id?
string = to_s
for pattern_length in 1.upto(string.length - 1)
next if string.length % pattern_length != 0
pattern = string[0..(pattern_length - 1)]
rest = string[pattern_length..]
return true if rest.gsub(pattern, "").empty?
end
false
end
end
class IdRange
attr_reader :range
def initialize(range)
@range = range
end
def self.for(chunk)
low, high = chunk.split("-").map(&:to_i)
new low..high
end
def invalid_ids
range.select(&:invalid_id?)
end
def silly_invalid_ids
range.select(&:silly_invalid_id?)
end
end