parent
d447e45d11
commit
5b874c234e
@ -1,27 +1,36 @@
|
||||
banks = ARGF.read.strip.split("\t").map(&:to_i)
|
||||
require "set"
|
||||
|
||||
history = { banks => true}
|
||||
count = 0
|
||||
seen = false
|
||||
loop do
|
||||
v, i = banks.map.with_index {|v,i| [v,i] }.max_by(&:first)
|
||||
banks[i] = 0
|
||||
i += 1
|
||||
while v > 0
|
||||
banks[i % banks.size] += 1
|
||||
v -= 1
|
||||
i += 1
|
||||
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
|
||||
count += 1
|
||||
if history.has_key?(banks)
|
||||
if seen
|
||||
puts count
|
||||
exit
|
||||
else
|
||||
history.clear
|
||||
seen = true
|
||||
count = 0
|
||||
end
|
||||
end
|
||||
history[banks] = true
|
||||
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
|
||||
|
Loading…
Reference in new issue