Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.2 kB · 54 lines
Ruby
at rust
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455#!/usr/bin/env ruby# frozen_string_literal: true
require_relative '../../../lib/aoc/intcode/cpu'
DEBUG = ENV.fetch('DEBUG', nil)
PATH = ARGV.first || 'input'
queues = []current_nat = nilany_sent = falsepart1 = falsepart2 = nillast_nat_y = nil
computers = 50.times.map do |i| AoC::Intcode::CPU.new.print_output!(false).load!(PATH).input!(i)end
until part2 if !any_sent && current_nat (queues[0] ||= Queue.new).enq current_nat nat_y = current_nat[1] part2 = nat_y if nat_y == last_nat_y last_nat_y = nat_y current_nat = nil end any_sent = false computers.each_with_index do |computer, address| queue = queues[address] ||= Queue.new if queue.empty? computer.input!(-1) else packet = queue.deq computer.input! packet[0] computer.input! packet[1] end computer.run! computer.output.each_slice(3) do |(target, x, y)| (queues[target] ||= Queue.new).enq [x, y] any_sent = true next unless target == 255 unless part1 puts "Part 1: #{y}" part1 = true end current_nat = [x, y] end computer.clear_output! endend
puts "Part 2: #{part2}"