Day 23 part 2
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::tasks::day05::{IntCodeComputer, load_ram};
|
||||
use crate::tasks::day05::{IntCodeComputer, load_ram, RAM};
|
||||
|
||||
pub fn run() {
|
||||
let ram = load_ram("input/day23.txt");
|
||||
part1(ram.clone());
|
||||
part2(ram.clone());
|
||||
}
|
||||
|
||||
fn part1(ram: RAM) {
|
||||
let mut computers: Vec<IntCodeComputer> = (0..50).map(|address| IntCodeComputer::new(vec![address], ram.clone())).collect();
|
||||
let mut buffer: VecDeque<Packet> = VecDeque::new();
|
||||
loop {
|
||||
@@ -18,7 +23,7 @@ pub fn run() {
|
||||
|
||||
while let Some(packet) = buffer.pop_front() {
|
||||
if packet.destination == 255 {
|
||||
println!("Finished with first y={} to 255", packet.y);
|
||||
println!("Part 1: Finished with first y={} to 255", packet.y);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -27,6 +32,53 @@ pub fn run() {
|
||||
}
|
||||
}
|
||||
|
||||
fn part2(ram: RAM) {
|
||||
let mut computers: Vec<IntCodeComputer> = (0..50).map(|address| IntCodeComputer::new(vec![address], ram.clone())).collect();
|
||||
let mut buffer: VecDeque<Packet> = VecDeque::new();
|
||||
|
||||
let mut rounds_without_packets = 0;
|
||||
let mut nat_content: Option<Packet> = None;
|
||||
let mut pushed_to_first: Vec<Packet> = Vec::new();
|
||||
loop {
|
||||
for computer in &mut computers {
|
||||
if computer.get_input().is_empty() {
|
||||
computer.add_input(&[-1, -1, -1]);
|
||||
}
|
||||
computer.run_until_input_empty();
|
||||
while computer.get_output().len() >= 3 {
|
||||
buffer.push_back(Packet::from(&computer.get_output()[0..3]));
|
||||
computer.clear_n_output(3);
|
||||
}
|
||||
}
|
||||
|
||||
if buffer.is_empty() {
|
||||
rounds_without_packets += 1;
|
||||
}
|
||||
if let Some(ref energy_packet) = nat_content {
|
||||
if rounds_without_packets > 5 {
|
||||
if let Some(last) = pushed_to_first.last() {
|
||||
if last == energy_packet {
|
||||
println!("Part 2: Got the same packet twice. Y was {}", last.y);
|
||||
return;
|
||||
}
|
||||
}
|
||||
pushed_to_first.push(energy_packet.clone());
|
||||
buffer.push_back(energy_packet.clone());
|
||||
rounds_without_packets = 0;
|
||||
}
|
||||
}
|
||||
while let Some(packet) = buffer.pop_front() {
|
||||
if packet.destination == 255 {
|
||||
nat_content = Some(Packet { destination: 0, x: packet.x, y: packet.y });
|
||||
continue;
|
||||
}
|
||||
|
||||
computers[packet.destination].add_input(packet.to_input().as_slice());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct Packet {
|
||||
destination: usize,
|
||||
x: i128,
|
||||
|
||||
Reference in New Issue
Block a user