This commit is contained in:
foosinn 2020-05-27 13:09:11 +02:00
parent 41c99c2032
commit d42a9178da
2 changed files with 71 additions and 56 deletions

View file

@ -9,13 +9,13 @@ fn main() -> Result<(), Box<dyn Error>> {
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,64 +25,32 @@ fn main() -> Result<(), Box<dyn Error>> {
// 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;
ncurses::initscr();
ncurses::noecho();
loop {
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());
}
if xy.x > xmax {
xmax = xy.x
}
if xy.y < ymin {
ymin = xy.y;
}
if xy.y > ymax {
ymax = xy.y
ncurses::mvprintw(0, 0, &format!("score: {}", score));
}
});
ncurses::initscr();
}
ncurses::refresh();
let mut score: isize;
loop {
let key = ncurses::getch();
ncurses::clear();
let joystick = match key {
104 => 1,
108 => -1,
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;
computer.push_input_and_step(joystick);
}
}
}
ncurses::mvprintw(
0,
0,
&format!("{} {} {} {}", key, joystick, drawcount, itemdrawcount),
);
ncurses::refresh();
}
//println!("{} {} {} {}", xmin, xmax, ymin, ymax);
}
#[derive(Eq, PartialEq)]
enum Tile {

View file

@ -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<Vec<isize>> {
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<Vec<isize>> {
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<isize> {
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);