yay
This commit is contained in:
parent
6f5e848c09
commit
81745013c9
3 changed files with 47 additions and 17 deletions
|
@ -7,9 +7,6 @@ edition = "2018"
|
|||
[dependencies]
|
||||
ncurses = "5.99.0"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
||||
[[bin]]
|
||||
name = "day1"
|
||||
path = "src/day1/main.rs"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use aoc2019::intcode;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
|
@ -24,32 +25,65 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
println!("blocks: {}", blocks);
|
||||
|
||||
// part2
|
||||
let mut computer = computer.clone_with_modified_program(vec![(0, 2)]);
|
||||
ncurses::initscr();
|
||||
ncurses::noecho();
|
||||
loop {
|
||||
while let Some(output) = &computer.run_until_input() {
|
||||
let mut computer = computer.clone_with_modified_program(vec![(0, 2)]);
|
||||
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| {
|
||||
let mut score = 0;
|
||||
if let &[x, y, value] = c {
|
||||
if x == -1 && y == 0 {
|
||||
score = value;
|
||||
} else {
|
||||
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(0, 0, &format!("score: {}", score));
|
||||
}
|
||||
});
|
||||
}
|
||||
ncurses::refresh();
|
||||
let joystick = match ncurses::getch() {
|
||||
104 => -1,
|
||||
108 => 1,
|
||||
_ => 0,
|
||||
let np = next_pos(&mut computer.clone());
|
||||
ncurses::mvprintw(
|
||||
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);
|
||||
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)]
|
||||
|
|
|
@ -131,7 +131,7 @@ pub struct Computer {
|
|||
modes: Modes,
|
||||
inputs: VecDeque<isize>,
|
||||
outputs: VecDeque<isize>,
|
||||
halted: bool,
|
||||
pub halted: bool,
|
||||
}
|
||||
|
||||
impl Clone for Computer {
|
||||
|
@ -183,7 +183,6 @@ impl Computer {
|
|||
|
||||
pub fn push_input_and_step(&mut self, input: isize) {
|
||||
self.inputs.push_back(input);
|
||||
self.halted = false;
|
||||
self.step()
|
||||
}
|
||||
|
||||
|
@ -250,7 +249,7 @@ impl Computer {
|
|||
}
|
||||
|
||||
fn step_until_input(&mut self) {
|
||||
loop {
|
||||
while !self.halted {
|
||||
let instruction = self.program[self.pos];
|
||||
let instruction = ModedOpcode::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 = ModedOpcode::from(instruction);
|
||||
self.modes = Modes::from(&instruction);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue