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
33
34
35
36
require "set"
class Day6
def initialize(banks)
@banks = banks
end
def each
return enum_for(__method__) unless block_given?
loop do
yield @banks.dup
value, i = @banks.map.with_index.max_by(&:first)
@banks[i] = 0
value.times do |offset|
@banks[(i+offset+1) % @banks.size] += 1
end
end
end
end
banks = ARGF.read.strip.split("\t").map(&:to_i)
day6 = Day6.new(banks).each.lazy
seen = Set.new
needle, i = day6.with_index.find {|config,_|
if seen.include?(config)
true
else
seen << config
false
end
}
p i
p day6.with_index.drop(1).find {|config,_| config == needle }.last