Compare commits

..
3 Commits
Author SHA1 Message Date
attmin 0089f4337c Day 23 part 2 2022-10-03 09:04:41 +02:00
attmin c8cef9c0ce Extended IntCodeComputer. 2022-10-03 09:03:31 +02:00
attmin 420b6152fb day 23 part 1 2022-10-02 19:57:00 +02:00
6 changed files with 118 additions and 1 deletions

No files matched your search

+1
View File
@@ -0,0 +1 @@
3,62,1001,62,11,10,109,2245,105,1,0,1973,1674,1771,2107,1812,602,1474,1600,1540,1738,703,2014,1942,2047,2183,1272,1843,1707,765,1301,1639,839,2144,1505,899,2076,1148,965,1239,1439,1336,1569,1000,734,1373,1115,1208,932,870,1876,1402,571,637,1177,2214,1911,670,806,1031,1078,0,0,0,0,0,0,0,0,0,0,0,0,3,64,1008,64,-1,62,1006,62,88,1006,61,170,1105,1,73,3,65,20102,1,64,1,21001,66,0,2,21102,1,105,0,1106,0,436,1201,1,-1,64,1007,64,0,62,1005,62,73,7,64,67,62,1006,62,73,1002,64,2,133,1,133,68,133,102,1,0,62,1001,133,1,140,8,0,65,63,2,63,62,62,1005,62,73,1002,64,2,161,1,161,68,161,1101,1,0,0,1001,161,1,169,101,0,65,0,1102,1,1,61,1101,0,0,63,7,63,67,62,1006,62,203,1002,63,2,194,1,68,194,194,1006,0,73,1001,63,1,63,1106,0,178,21101,210,0,0,105,1,69,2102,1,1,70,1102,1,0,63,7,63,71,62,1006,62,250,1002,63,2,234,1,72,234,234,4,0,101,1,234,240,4,0,4,70,1001,63,1,63,1105,1,218,1105,1,73,109,4,21101,0,0,-3,21102,1,0,-2,20207,-2,67,-1,1206,-1,293,1202,-2,2,283,101,1,283,283,1,68,283,283,22001,0,-3,-3,21201,-2,1,-2,1106,0,263,22102,1,-3,-3,109,-4,2105,1,0,109,4,21101,0,1,-3,21102,0,1,-2,20207,-2,67,-1,1206,-1,342,1202,-2,2,332,101,1,332,332,1,68,332,332,22002,0,-3,-3,21201,-2,1,-2,1106,0,312,21202,-3,1,-3,109,-4,2105,1,0,109,1,101,1,68,358,21002,0,1,1,101,3,68,366,21002,0,1,2,21101,0,376,0,1106,0,436,22101,0,1,0,109,-1,2105,1,0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,17179869184,34359738368,68719476736,137438953472,274877906944,549755813888,1099511627776,2199023255552,4398046511104,8796093022208,17592186044416,35184372088832,70368744177664,140737488355328,281474976710656,562949953421312,1125899906842624,109,8,21202,-6,10,-5,22207,-7,-5,-5,1205,-5,521,21101,0,0,-4,21102,0,1,-3,21102,1,51,-2,21201,-2,-1,-2,1201,-2,385,471,20102,1,0,-1,21202,-3,2,-3,22207,-7,-1,-5,1205,-5,496,21201,-3,1,-3,22102,-1,-1,-5,22201,-7,-5,-7,22207,-3,-6,-5,1205,-5,515,22102,-1,-6,-5,22201,-3,-5,-3,22201,-1,-4,-4,1205,-2,461,1105,1,547,21101,-1,0,-4,21202,-6,-1,-6,21207,-7,0,-5,1205,-5,547,22201,-7,-6,-7,21201,-4,1,-4,1105,1,529,21201,-4,0,-7,109,-8,2105,1,0,109,1,101,1,68,563,21001,0,0,0,109,-1,2106,0,0,1102,1,101359,66,1101,0,1,67,1102,1,598,68,1101,556,0,69,1101,0,1,71,1102,600,1,72,1105,1,73,1,10531,16,562,1102,63391,1,66,1102,1,3,67,1101,0,629,68,1102,302,1,69,1102,1,1,71,1101,635,0,72,1105,1,73,0,0,0,0,0,0,40,104001,1101,28979,0,66,1101,0,1,67,1101,0,664,68,1102,556,1,69,1101,0,2,71,1102,1,666,72,1105,1,73,1,10,3,46798,18,126402,1101,0,3089,66,1101,2,0,67,1102,697,1,68,1102,1,302,69,1101,1,0,71,1102,1,701,72,1105,1,73,0,0,0,0,40,138668,1102,1,70079,66,1101,0,1,67,1102,1,730,68,1102,556,1,69,1102,1,1,71,1101,732,0,72,1105,1,73,1,13,30,73924,1101,0,20287,66,1101,1,0,67,1102,761,1,68,1101,0,556,69,1101,0,1,71,1101,763,0,72,1106,0,73,1,109,30,18481,1101,21067,0,66,1102,6,1,67,1102,1,792,68,1102,1,302,69,1102,1,1,71,1101,804,0,72,1105,1,73,0,0,0,0,0,0,0,0,0,0,0,0,1,60778,1101,8387,0,66,1101,1,0,67,1102,833,1,68,1101,556,0,69,1102,1,2,71,1102,835,1,72,1106,0,73,1,6653,2,616758,20,103534,1101,0,54881,66,1101,1,0,67,1101,0,866,68,1101,556,0,69,1102,1,1,71,1101,868,0,72,1105,1,73,1,4523,2,411172,1101,16693,0,66,1101,1,0,67,1102,897,1,68,1101,556,0,69,1102,1,0,71,1101,899,0,72,1106,0,73,1,1433,1101,95479,0,66,1101,0,2,67,1102,1,926,68,1102,1,302,69,1101,0,1,71,1102,930,1,72,1106,0,73,0,0,0,0,9,69302,1102,49663,1,66,1102,2,1,67,1102,959,1,68,1102,302,1,69,1101,0,1,71,1102,963,1,72,1105,1,73,0,0,0,0,46,6178,1102,1,2089,66,1101,3,0,67,1102,992,1,68,1101,302,0,69,1101,1,0,71,1102,998,1,72,1105,1,73,0,0,0,0,0,0,40,34667,1101,25793,0,66,1102,1,1,67,1101,1027,0,68,1101,556,0,69,1102,1,1,71,1101,1029,0,72,1106,0,73,1,1193,29,65551,1102,1,99257,66,1102,1,1,67,1101,1058,0,68,1101,0,556,69,1102,1,9,71,1101,1060,0,72,1106,0,73,1,1,16,281,24,190958,9,34651,11,83267,49,52534,29,196653,28,94726,35,68863,39,80783,1102,1,26267,66,1101,0,4,67,1101,0,1105,68,1101,302,0,69,1102,1,1,71,1101,1113,0,72,1105,1,73,0,0,0,0,0,0,0,0,7,173343,1102,1,68863,66,1102,1,2,67,1102,1,1142,68,1101,0,302,69,1102,1,1,71,1102,1,1146,72,1106,0,73,0,0,0,0,39,161566,1101,0,11057,66,1102,1,1,67,1101,1175,0,68,1101,0,556,69,1101,0,0,71,1102,1,1177,72,1105,1,73,1,1232,1102,1,103687,66,1102,1,1,67,1101,0,1204,68,1101,0,556,69,1102,1,1,71,1102,1,1206,72,1106,0,73,1,8,30,55443,1101,0,7759,66,1102,1,1,67,1102,1,1235,68,1101,0,556,69,1102,1,1,71,1101,1237,0,72,1106,0,73,1,125,3,70197,1101,47363,0,66,1102,1,2,67,1102,1266,1,68,1102,302,1,69,1102,1,1,71,1102,1270,1,72,1105,1,73,0,0,0,0,35,137726,1102,76579,1,66,1101,0,1,67,1101,0,1299,68,1102,556,1,69,1101,0,0,71,1101,1301,0,72,1105,1,73,1,1866,1101,0,13063,66,1102,1,1,67,1101,1328,0,68,1101,556,0,69,1101,0,3,71,1101,1330,0,72,1105,1,73,1,2,30,36962,18,21067,18,42134,1101,18481,0,66,1101,0,4,67,1102,1,1363,68,1101,302,0,69,1101,1,0,71,1101,0,1371,72,1105,1,73,0,0,0,0,0,0,0,0,7,231124,Line truncated
+1 -1
View File
@@ -3,5 +3,5 @@ extern crate core;
mod tasks; mod tasks;
fn main() { fn main() {
tasks::day20::run(); tasks::day23::run();
} }
+15
View File
@@ -217,6 +217,7 @@ impl IntCodeComputer {
op.params[0].store(&mut self.ram, *v, self.relative_base_offset); op.params[0].store(&mut self.ram, *v, self.relative_base_offset);
self.pc += 2; self.pc += 2;
} else { } else {
self.input_storage.clear();
return false; return false;
} }
} }
@@ -282,10 +283,24 @@ impl IntCodeComputer {
} }
} }
pub fn add_input(&mut self, input: &[i128]) {
for x in input {
self.input_storage.push(*x);
}
}
pub fn get_input(&self) -> &[i128] {
self.input_storage.as_slice()
}
pub fn clear_output(&mut self) { pub fn clear_output(&mut self) {
self.output_storage.clear(); 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] { pub fn get_output(&self) -> &[i128] {
self.output_storage.as_slice() self.output_storage.as_slice()
} }
+1
View File
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
#[allow(dead_code)]
pub fn run() { pub fn run() {
let input = std::fs::read_to_string("input/day20.txt").unwrap(); let input = std::fs::read_to_string("input/day20.txt").unwrap();
let maze = Maze::from(&input, PortalField::curried_factory); let maze = Maze::from(&input, PortalField::curried_factory);
+99
View File
@@ -0,0 +1,99 @@
use std::collections::VecDeque;
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 {
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!("Part 1: Finished with first y={} to 255", packet.y);
return;
}
computers[packet.destination].add_input(packet.to_input().as_slice());
}
}
}
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,
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]
}
}
+1
View File
@@ -20,4 +20,5 @@ pub mod day20;
pub mod day21; pub mod day21;
#[allow(dead_code)] #[allow(dead_code)]
pub mod day22; pub mod day22;
pub mod day23;
pub mod day24; pub mod day24;