Added Solution for 2019 day 23

This commit is contained in:
Burnus 2023-04-01 18:16:39 +02:00
parent 15c201e045
commit dc03fbee9c
5 changed files with 134 additions and 4 deletions

View file

@ -188,10 +188,11 @@ pub mod intcode_processor {
/// cpu.run();
/// assert_eq!(cpu.get(4), 42);
/// ````
fn set_to_input(&mut self, dest: usize) {
let input = self.input.pop_front().expect("Input is empty");
self.set(dest, input);
fn set_to_input(&mut self/*, dest: usize*/) -> Option<isize> {
// let input = self.input.pop_front().expect("Input is empty");
// self.set(dest, input);
self.instr_ptr += 2;
self.input.pop_front()
}
/// Return DiagnosticCode(val) if the next instruction is Halt (opcode 99) and Output(val) otherwise.
@ -391,7 +392,13 @@ pub mod intcode_processor {
match instruction % 100 {
1 => self.add(params[0], params[1], params[2]),
2 => self.mul(params[0], params[1], params[2]),
3 => self.set_to_input(params[0]),
3 => {
if let Some(input) = self.set_to_input() {
self.set(params[0], input);
} else {
return OutputState::Halt;
}
},
4 => return self.ret(params[0]),
5 => self.jnz(params[0], params[1]),
6 => self.jiz(params[0], params[1]),

View file

@ -0,0 +1,9 @@
[package]
name = "day23_category_six"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
intcode_processor = { path = "../common/intcode_processor" }

View file

@ -0,0 +1,38 @@
The droids have finished repairing as much of the ship as they can. Their report indicates that this was a *Category 6* disaster - not because it was that bad, but because it destroyed the stockpile of [Category 6](https://en.wikipedia.org/wiki/Category_6_cable) network cables as well as most of the ship's network infrastructure.
You'll need to *rebuild the network from scratch*.
The computers on the network are standard [Intcode](9) computers that communicate by sending *packets* to each other. There are `50` of them in total, each running a copy of the same *Network Interface Controller* (NIC) software (your puzzle input). The computers have *network addresses* `0` through `49`; when each computer boots up, it will request its network address via a single input instruction. Be sure to give each computer a unique network address.
Once a computer has received its network address, it will begin doing work and communicating over the network by sending and receiving *packets*. All packets contain *two values* named `X` and `Y`. Packets sent to a computer are queued by the recipient and read in the order they are received.
To *send* a packet to another computer, the NIC will use *three output instructions* that provide the *destination address* of the packet followed by its `X` and `Y` values. For example, three output instructions that provide the values `10`, `20`, `30` would send a packet with `X=20` and `Y=30` to the computer with address `10`.
To *receive* a packet from another computer, the NIC will use an *input instruction*. If the incoming packet queue is *empty*, provide `-1`. Otherwise, provide the `X` value of the next packet; the computer will then use a second input instruction to receive the `Y` value for the same packet. Once both values of the packet are read in this way, the packet is removed from the queue.
Note that these input and output instructions never [block](https://en.wikipedia.org/wiki/Blocking_(computing)). Specifically, output instructions do not wait for the sent packet to be received - the computer might send multiple packets before receiving any. Similarly, input instructions do not wait for a packet to arrive - if no packet is waiting, input instructions should receive `-1`.
Boot up all `50` computers and attach them to your network. *What is the `Y` value of the first packet sent to address `255`?*
Your puzzle answer was `17949`.
\--- Part Two ---
----------
Packets sent to address `255` are handled by a device called a NAT (Not Always Transmitting). The NAT is responsible for managing power consumption of the network by blocking certain packets and watching for idle periods in the computers.
If a packet would be sent to address `255`, the NAT receives it instead. The NAT remembers only the *last* packet it receives; that is, the data in each packet it receives overwrites the NAT's packet memory with the new packet's `X` and `Y` values.
The NAT also monitors all computers on the network. If all computers have *empty incoming packet queues* and are *continuously trying to receive packets* without sending packets, the network is considered *idle*.
Once the network is idle, the NAT sends *only the last packet it received* to address `0`; this will cause the computers on the network to resume activity. In this way, the NAT can throttle power consumption of the network when the ship needs power in other areas.
Monitor packets released to the computer at address `0` by the NAT. *What is the first `Y` value delivered by the NAT to the computer at address `0` twice in a row?*
Your puzzle answer was `12326`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, you should [return to your Advent calendar](/2019) and try another puzzle.
If you still want to see it, you can [get your puzzle input](23/input).

View file

@ -0,0 +1,75 @@
use intcode_processor::intcode_processor::{Cpu, OutputState};
use std::{num::ParseIntError, collections::VecDeque};
pub fn run(input: &str) -> Result<(isize, isize), ParseIntError> {
let program = Cpu::try_with_memory_from_str(input)?;
let mut cpus = Vec::new();
for addr in 0..50 {
let mut cpu = program.clone();
cpu.set_input(addr);
cpu.set_input(-1);
cpus.push(cpu);
}
let mut first = 0;
let mut nat = (0, 0);
let mut last = 0;
let mut messages = vec![VecDeque::new(); 50];
loop {
let mut halted = 0;
for (id, cpu) in cpus.iter_mut().enumerate() {
while let Some((x, y)) = messages[id].pop_front() {
cpu.set_input(x);
cpu.set_input(y);
}
cpu.set_input(-1);
match cpu.run() {
OutputState::Halt => halted += 1,
OutputState::DiagnosticCode(i) => eprintln!("CPU {id} stopped with DiagnosticCode {i}"),
OutputState::Output(i) => {
let x = match cpu.run() {
OutputState::Halt => panic!("Received Halt instead of X"),
OutputState::DiagnosticCode(i) => panic!("CPU {id} stopped with DiagnosticCode {i}"),
OutputState::Output(i) => i,
};
let y = match cpu.run() {
OutputState::Halt => panic!("Received Halt instead of Y"),
OutputState::DiagnosticCode(i) => panic!("CPU {id} stopped with DiagnosticCode {i}"),
OutputState::Output(i) => i,
};
if i == 255 {
if first == 0 {
first = y;
}
nat = (x, y);
} else {
messages[i as usize].push_back((x, y));
}
}
}
}
if halted == 50 {
if last == nat.1 {
return Ok((first, last));
} else {
last = nat.1;
messages[0].push_back(nat);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), Ok((17949, 12326)));
}
}

File diff suppressed because one or more lines are too long