Update Day 17 to the new machine.

This commit is contained in:
2019-12-23 16:11:16 -08:00
parent 869d5d5236
commit 48f18afb0a
2 changed files with 45 additions and 39 deletions

View File

@@ -8,6 +8,7 @@ mod orbits;
mod repair; mod repair;
#[cfg(test)] #[cfg(test)]
mod robot; mod robot;
#[cfg(test)]
mod scaffold; mod scaffold;
mod wiremap; mod wiremap;

View File

@@ -1,8 +1,4 @@
use crate::endchannel::channel; use crate::machine::{Computer, RunResult};
use crate::machine::Computer;
use std::cmp::min;
use std::fmt;
use std::thread;
struct ScaffoldMap { struct ScaffoldMap {
data: Vec<Tile>, data: Vec<Tile>,
@@ -12,32 +8,47 @@ struct ScaffoldMap {
impl ScaffoldMap { impl ScaffoldMap {
fn new(intcode: &str) -> ScaffoldMap { fn new(intcode: &str) -> ScaffoldMap {
let mut computer = Computer::load(intcode, 0); let mut computer = Computer::load(intcode);
let (mut mysend, mut corecv) = channel();
let (mut cosend, mut myrecv) = channel();
thread::spawn(move || computer.run(&mut corecv, &mut cosend) );
let mut data = Vec::new(); let mut data = Vec::new();
let mut width = 0; let mut width = 0;
let mut height = 0; let mut height = 0;
let mut got_width = false; let mut got_width = false;
while let Some(c64) = myrecv.next() {
let c = c64 as u8 as char;
if c == '\n' { loop {
height += 1; match computer.run() {
got_width = true; RunResult::Halted(_) => {
continue; height -= 1;
assert_eq!(height, (data.len() / width));
return ScaffoldMap{
data,
width, height
}
}
RunResult::Continue(next) =>
computer = next,
RunResult::Input(_) =>
panic!("Don't know how to deal with input!"),
RunResult::Output(o, next) => {
let c = o as u8 as char;
computer = next;
if c == '\n' {
height += 1;
got_width = true;
continue;
}
if !got_width {
width += 1;
}
data.push(Tile::from(c));
}
} }
if !got_width {
width += 1;
}
data.push(Tile::from(c));
} }
height -= 1;
assert_eq!(height, (data.len() / width));
ScaffoldMap{ data, width, height }
} }
fn get(&self, x: usize, y: usize) -> Tile { fn get(&self, x: usize, y: usize) -> Tile {
@@ -70,8 +81,6 @@ impl ScaffoldMap {
} }
fn print(&self) { fn print(&self) {
let mut i = 0;
println!("Scaffold is {} x {}", self.width, self.height); println!("Scaffold is {} x {}", self.width, self.height);
for y in 0..self.height { for y in 0..self.height {
for x in 0..self.width { for x in 0..self.width {
@@ -427,23 +436,19 @@ fn day17b() {
for (a, b, c) in triples { for (a, b, c) in triples {
for answer in answers(&path, &a, &b, &c).iter() { for answer in answers(&path, &a, &b, &c).iter() {
assert_eq!(answer.to_path(), path); assert_eq!(answer.to_path(), path);
let mut comp = Computer::load("inputs/day17", 0); let mut comp = Computer::load("inputs/day17");
let (mut mysend, mut corecv) = channel(); let mut inputs = answer.to_inputs();
let (mut cosend, mut myrecv) = channel();
assert_eq!(comp.read(0), 1); assert_eq!(comp.read(0), 1);
comp.write(0, 2); comp.write(0, 2);
thread::spawn(move || comp.run(&mut corecv, &mut cosend) ); inputs.push('n' as u8 as i64);
let inputs = answer.to_inputs(); inputs.push('\n' as u8 as i64);
println!("Going to send: {:?}", inputs); let results = comp.standard_run(&inputs);
for x in answer.to_inputs() { mysend.send(x); } for x in results.iter() {
mysend.send('n' as u8 as i64); if *x < 256 {
mysend.send('\n' as u8 as i64); print!("{}", *x as u8 as char);
for x in myrecv {
if x < 256 {
print!("{}", x as u8 as char);
} else { } else {
assert_eq!(768115, x); assert_eq!(768115, *x);
} }
} }
} }