blob: ea90532138674a6b0daa9436fd3c1df9d053f704 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
preamble = 25
def has_sum hash, sum
hash.keys.any? do |n|
hash[sum - n] > 0
end
end
def update array, hash, n
hash[n] += 1
hash[array.first] -= 1
array.shift
array.push n
end
lines = File.readlines('input.txt').map {|l| l.to_i}
current25 = lines.slice(0, preamble)
current25hash = Hash.new 0
current25.each do |n|
current25hash[n] += 1
end
invalid = 0
lines.slice(preamble, lines.size).each do |n|
if invalid == 0 && (!has_sum current25hash, n)
invalid = n
end
update current25, current25hash, n
end
puts invalid
|