From d42a9178dab00d399b851cbedc8433b4b7a3e47a Mon Sep 17 00:00:00 2001 From: foosinn Date: Wed, 27 May 2020 13:09:11 +0200 Subject: [PATCH] wip --- src/day13/main.rs | 80 ++++++++++++++--------------------------------- src/intcode.rs | 47 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 56 deletions(-) diff --git a/src/day13/main.rs b/src/day13/main.rs index 7ae3008..840e5f9 100644 --- a/src/day13/main.rs +++ b/src/day13/main.rs @@ -9,13 +9,13 @@ fn main() -> Result<(), Box> { let f = File::open("input13")?; let mut computer = intcode::Computer::from(f); let mut grid: Grid = HashMap::new(); - while let Some(output) = &computer.run_until_n_output(3) { + while let Some(output) = computer.run_until_n_output(3) { let mut output = output.into_iter(); let xy = XY { - x: *output.next().unwrap(), - y: *output.next().unwrap(), + x: output.next().unwrap(), + y: output.next().unwrap(), }; - let tile = Tile::from(*output.next().unwrap()); + let tile = Tile::from(output.next().unwrap()); grid.insert(xy, tile); } @@ -25,63 +25,31 @@ fn main() -> Result<(), Box> { // part2 let mut computer = computer.clone_with_modified_program(vec![(0, 2)]); - let mut xmin = 0; - let mut xmax = 0; - let mut ymin = 0; - let mut ymax = 0; - grid.iter().for_each(|(xy, _)| { - if xy.x < xmin { - xmin = xy.x; - } - if xy.x > xmax { - xmax = xy.x - } - if xy.y < ymin { - ymin = xy.y; - } - if xy.y > ymax { - ymax = xy.y - } - }); ncurses::initscr(); - ncurses::refresh(); - let mut score: isize; + ncurses::noecho(); loop { - let key = ncurses::getch(); - ncurses::clear(); - let joystick = match key { - 104 => 1, - 108 => -1, + while let Some(output) = &computer.run_until_input() { + output.chunks(3).for_each(|c| { + let mut score = 0; + if let &[x, y, value] = c { + if x == -1 && y == 0 { + score = value; + } else { + let tile = Tile::from(value); + ncurses::mvprintw((y + 1) as i32, x as i32, tile.char()); + } + ncurses::mvprintw(0, 0, &format!("score: {}", score)); + } + }); + } + ncurses::refresh(); + let joystick = match ncurses::getch() { + 104 => -1, + 108 => 1, _ => 0, }; - let mut drawcount = 0; - let mut itemdrawcount = 0; - 'computer: while let Some(output) = &computer.run_until_n_output(3) { - drawcount += 1; - computer.push_input(joystick); - let mut output = output.into_iter(); - let x = *output.next().unwrap(); - let y = *output.next().unwrap(); - if x == -1 && y == 0 { - score = *output.next().unwrap(); - } else { - let tile = Tile::from(*output.next().unwrap()); - ncurses::mvprintw(y as i32, x as i32, tile.char()); - if tile == Tile::HorizontalPaddle { - itemdrawcount = drawcount; - break 'computer; - } - } - } - - ncurses::mvprintw( - 0, - 0, - &format!("{} {} {} {}", key, joystick, drawcount, itemdrawcount), - ); - ncurses::refresh(); + computer.push_input_and_step(joystick); } - //println!("{} {} {} {}", xmin, xmax, ymin, ymax); } #[derive(Eq, PartialEq)] diff --git a/src/intcode.rs b/src/intcode.rs index 7befcc2..b1b96e8 100644 --- a/src/intcode.rs +++ b/src/intcode.rs @@ -181,6 +181,12 @@ impl Computer { self.outputs.pop_front() } + pub fn push_input_and_step(&mut self, input: isize) { + self.inputs.push_back(input); + self.halted = false; + self.step() + } + pub fn clone_with_modified_program(&self, u: Vec<(usize, isize)>) -> Computer { let mut computer = self.clone(); for (pos, val) in u { @@ -201,6 +207,26 @@ impl Computer { } } + pub fn run_until_input(&mut self) -> Option> { + self.step_until_input(); + if self.outputs.len() > 0 { + return Some(self.outputs.split_off(0).into_iter().collect()); + } + return None; + } + + pub fn run_until_n_output(&mut self, n: usize) -> Option> { + loop { + self.step(); + if self.outputs.len() >= n { + return Some(self.outputs.split_off(0).into_iter().collect()); + } + if self.halted { + return None; + } + } + } + pub fn run_until_output(&mut self) -> Option { loop { self.step(); @@ -223,6 +249,27 @@ impl Computer { self.run_until_output() } + fn step_until_input(&mut self) { + loop { + let instruction = self.program[self.pos]; + let instruction = ModedOpcode::from(instruction); + self.modes = Modes::from(&instruction); + let advance = match Opcode::from(&instruction) { + Opcode::Add => self.add(), + Opcode::Multiply => self.multiply(), + Opcode::Input => return, + Opcode::Output => self.output(), + Opcode::JumpIfTrue => self.jump_if_true(), + Opcode::JumpIfFalse => self.jump_if_false(), + Opcode::LessThan => self.less_than(), + Opcode::Equals => self.equals(), + Opcode::RelativeAdj => self.relative_adj(), + Opcode::Halt => self.halt(), + }; + self.pos += advance; + } + } + fn step(&mut self) { let instruction = self.program[self.pos]; let instruction = ModedOpcode::from(instruction);