mirror of
https://github.com/onyx-and-iris/aoc2025.git
synced 2025-12-07 22:57:50 +00:00
add day_02
This commit is contained in:
parent
c0152a39b2
commit
6fbf18f804
24
day_02/1.rb
Executable file
24
day_02/1.rb
Executable 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
34
day_02/2.rb
Executable 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
|
||||
Loading…
x
Reference in New Issue
Block a user