require "./intcode.cr" require "./intcode_fibers.cr" prog = File.read("day7.txt").chomp.split(",").map(&.to_i64) def chain_amplifiers(prog, perm, loop = false) chans = perm.map { |f| new_interpreter(f, prog) } chans.zip perm do |amp, freq| amp[0].send freq.to_i64 end chans[0][0].send 0 i = 0 history = Array(Int64).new(chans.size, 0) loop do j = (i+1) % chans.size break unless received = chans[i][1].receive history[i] = received chans[j][0].send received i = j break if i == 0 && !loop end history[4] end max_single = [0,1,2,3,4].permutations.max_of do |perm| chain_amplifiers(prog.clone, perm) end puts max_single max_cycled = [5,6,7,8,9].permutations.max_of do |perm| chain_amplifiers(prog.clone, perm, loop=true) end puts max_cycled