This commit is contained in:
foosinn 2020-05-28 22:17:30 +02:00
parent 6f5e848c09
commit 81745013c9
3 changed files with 47 additions and 17 deletions

View file

@ -7,9 +7,6 @@ edition = "2018"
[dependencies] [dependencies]
ncurses = "5.99.0" ncurses = "5.99.0"
[profile.release]
debug = true
[[bin]] [[bin]]
name = "day1" name = "day1"
path = "src/day1/main.rs" path = "src/day1/main.rs"

View file

@ -1,4 +1,5 @@
use aoc2019::intcode; use aoc2019::intcode;
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
@ -24,32 +25,65 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("blocks: {}", blocks); println!("blocks: {}", blocks);
// part2 // part2
let mut computer = computer.clone_with_modified_program(vec![(0, 2)]);
ncurses::initscr(); ncurses::initscr();
ncurses::noecho(); ncurses::noecho();
loop { let mut computer = computer.clone_with_modified_program(vec![(0, 2)]);
while let Some(output) = &computer.run_until_input() { let mut paddlex = 0;
let mut score = 0;
while !computer.halted {
if let Some(output) = &computer.run_until_input() {
output.chunks(3).for_each(|c| { output.chunks(3).for_each(|c| {
let mut score = 0;
if let &[x, y, value] = c { if let &[x, y, value] = c {
if x == -1 && y == 0 { if x == -1 && y == 0 {
score = value; score = value;
} else { } else {
let tile = Tile::from(value); let tile = Tile::from(value);
if Tile::from(value) == Tile::HorizontalPaddle {
paddlex = x;
}
ncurses::mvprintw((y + 1) as i32, x as i32, tile.char()); ncurses::mvprintw((y + 1) as i32, x as i32, tile.char());
} }
ncurses::mvprintw(0, 0, &format!("score: {}", score));
} }
}); });
} }
ncurses::refresh(); let np = next_pos(&mut computer.clone());
let joystick = match ncurses::getch() { ncurses::mvprintw(
104 => -1, 0,
108 => 1, 0,
_ => 0, &format!(
"willx: {:0>2} paddlex: {:0>2} score: {:0>7}",
np, paddlex, score
),
);
let joystick = match np.cmp(&paddlex) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}; };
computer.push_input_and_step(joystick); computer.push_input_and_step(joystick);
ncurses::refresh();
} }
ncurses::endwin();
println!("score: {}", score);
Ok(())
}
fn next_pos(computer: &mut intcode::Computer) -> isize {
while !computer.halted {
if let Some(output) = computer.run_until_input() {
for c in output.chunks(3) {
if let &[x, y, value] = c {
if x == -1 && y == 0 {
// skip score
} else if Tile::from(value) == Tile::Ball && y == 23 {
return x;
}
}
}
}
computer.push_input_and_step(0);
}
0
} }
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]

View file

@ -131,7 +131,7 @@ pub struct Computer {
modes: Modes, modes: Modes,
inputs: VecDeque<isize>, inputs: VecDeque<isize>,
outputs: VecDeque<isize>, outputs: VecDeque<isize>,
halted: bool, pub halted: bool,
} }
impl Clone for Computer { impl Clone for Computer {
@ -183,7 +183,6 @@ impl Computer {
pub fn push_input_and_step(&mut self, input: isize) { pub fn push_input_and_step(&mut self, input: isize) {
self.inputs.push_back(input); self.inputs.push_back(input);
self.halted = false;
self.step() self.step()
} }
@ -250,7 +249,7 @@ impl Computer {
} }
fn step_until_input(&mut self) { fn step_until_input(&mut self) {
loop { while !self.halted {
let instruction = self.program[self.pos]; let instruction = self.program[self.pos];
let instruction = ModedOpcode::from(instruction); let instruction = ModedOpcode::from(instruction);
self.modes = Modes::from(&instruction); self.modes = Modes::from(&instruction);
@ -270,7 +269,7 @@ impl Computer {
} }
} }
fn step(&mut self) { pub fn step(&mut self) {
let instruction = self.program[self.pos]; let instruction = self.program[self.pos];
let instruction = ModedOpcode::from(instruction); let instruction = ModedOpcode::from(instruction);
self.modes = Modes::from(&instruction); self.modes = Modes::from(&instruction);