You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2020/ruby/day_09.rb

19 lines
346 B

N = 25
data = ARGF.read.scan(/\d+/).map(&:to_i)
invalid = data.each_cons(N+1).filter_map {|range|
n = range.pop
range.combination(2).map(&:sum).include?(n) ? nil : n
}.first
weakness = (0..).lazy.flat_map {|start|
(2..data.size-start-1).map {|len|
data[start, len]
}
}.find {|range|
range.sum == invalid
}.minmax.sum
p weakness