Cleaner machine shutdown, day15a solution by not retracing too much.

This commit is contained in:
2019-12-19 20:36:32 -08:00
parent 9d44902037
commit 7f57849462
2 changed files with 52 additions and 16 deletions

View File

@@ -134,10 +134,17 @@ impl Computer {
} }
INPUT => { INPUT => {
let dest = self.read_dest(arg1mode, self.position + 1) as usize; let dest = self.read_dest(arg1mode, self.position + 1) as usize;
let val = input.recv().expect("Failed to get input!");
self.write(dest, val); match input.recv() {
self.position += 2; None => {
output.conclude();
self.done = true;
}
Some(val) => {
self.write(dest, val);
self.position += 2;
}
}
} }
OUTPUT => { OUTPUT => {
let arg1 = self.read_arg(arg1mode, self.position + 1); let arg1 = self.read_arg(arg1mode, self.position + 1);

View File

@@ -61,11 +61,37 @@ impl Path {
for dir in ALL_DIRECTIONS.iter() { for dir in ALL_DIRECTIONS.iter() {
let mut copy = self.steps.clone(); let mut copy = self.steps.clone();
copy.push(*dir); copy.push(*dir);
res.push(Path{ steps: copy }); let potential = Path{ steps: copy };
if !potential.loops() {
res.push(potential);
}
} }
res res
} }
fn loops(&self) -> bool {
let mut previous = vec![];
let mut x: i64 = 0;
let mut y: i64 = 0;
for step in self.steps.iter() {
match step {
Direction::North => y -= 1,
Direction::South => y += 1,
Direction::East => x += 1,
Direction::West => x -= 1,
}
if previous.contains(&(x, y)) {
return true;
}
previous.push((x, y));
}
false
}
} }
struct RepairSearch { struct RepairSearch {
@@ -97,23 +123,26 @@ impl RepairSearch {
} }
fn try_path(&mut self, path: &Path) -> MoveResult { fn try_path(&mut self, path: &Path) -> MoveResult {
let ( mysend, mut corecv) = channel(); let (mut mysend, mut corecv) = channel();
let (mut cosend, mut myrecv) = channel(); let (mut cosend, mut myrecv) = channel();
let my_computer = self.computer.clone(); let my_computer = self.computer.clone();
for step in path.steps.iter() {
mysend.send(step.encode());
}
thread::spawn(move || my_computer.clone().run(&mut corecv, &mut cosend));
let mut last_response = MoveResult::Done; let mut last_response = MoveResult::Done;
for response in myrecv.take(path.steps.len()) { thread::spawn(move || my_computer.clone().run(&mut corecv, &mut cosend));
last_response = MoveResult::new(response); for step in path.steps.iter() {
if last_response == MoveResult::HitWall { mysend.send(step.encode());
break match myrecv.recv() {
None =>
return last_response,
Some(response) => {
last_response = MoveResult::new(response);
if last_response == MoveResult::HitWall {
break
}
}
} }
} }
mysend.conclude();
last_response last_response
} }
@@ -144,5 +173,5 @@ impl RepairSearch {
#[test] #[test]
fn day15() { fn day15() {
let mut day15a = RepairSearch::new("inputs/day15"); let mut day15a = RepairSearch::new("inputs/day15");
assert_eq!(0, day15a.run_search()); assert_eq!(298, day15a.run_search());
} }