day 23 part 1

This commit is contained in:
2022-10-02 19:57:00 +02:00
parent 79ea3c29d0
commit 420b6152fb
6 changed files with 61 additions and 1 deletions

1
input/day23.txt Normal file

File diff suppressed because one or more lines are too long

View File

@@ -3,5 +3,5 @@ extern crate core;
mod tasks;
fn main() {
tasks::day20::run();
tasks::day23::run();
}

View File

@@ -282,10 +282,20 @@ impl IntCodeComputer {
}
}
pub fn add_input(&mut self, input: &[i128]) {
for x in input {
self.input_storage.push(*x);
}
}
pub fn clear_output(&mut self) {
self.output_storage.clear();
}
pub fn clear_n_output(&mut self, n: usize) {
self.output_storage = Vec::from(self.output_storage.split_at(n).1);
}
pub fn get_output(&self) -> &[i128] {
self.output_storage.as_slice()
}

View File

@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet, VecDeque};
#[allow(dead_code)]
pub fn run() {
let input = std::fs::read_to_string("input/day20.txt").unwrap();
let maze = Maze::from(&input, PortalField::curried_factory);

47
src/tasks/day23.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::collections::VecDeque;
use crate::tasks::day05::{IntCodeComputer, load_ram};
pub fn run() {
let ram = load_ram("input/day23.txt");
let mut computers: Vec<IntCodeComputer> = (0..50).map(|address| IntCodeComputer::new(vec![address], ram.clone())).collect();
let mut buffer: VecDeque<Packet> = VecDeque::new();
loop {
for computer in &mut computers {
computer.add_input(&[-1]);
computer.run_until_input_empty();
if computer.get_output().len() >= 3 {
buffer.push_back(Packet::from(&computer.get_output()[0..3]));
computer.clear_n_output(3);
}
}
while let Some(packet) = buffer.pop_front() {
if packet.destination == 255 {
println!("Finished with first y={} to 255", packet.y);
return;
}
computers[packet.destination].add_input(packet.to_input().as_slice());
}
}
}
struct Packet {
destination: usize,
x: i128,
y: i128,
}
impl Packet {
fn from(slice: &[i128]) -> Packet {
if slice.len() != 3 {
panic!("not enough for a packet")
}
Packet { destination: slice[0] as usize, x: slice[1], y: slice[2] }
}
fn to_input(&self) -> Vec<i128> {
vec![self.x, self.y]
}
}

View File

@@ -20,4 +20,5 @@ pub mod day20;
pub mod day21;
#[allow(dead_code)]
pub mod day22;
pub mod day23;
pub mod day24;