wip
This commit is contained in:
parent
41c99c2032
commit
d42a9178da
2 changed files with 71 additions and 56 deletions
|
@ -9,13 +9,13 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let f = File::open("input13")?;
|
let f = File::open("input13")?;
|
||||||
let mut computer = intcode::Computer::from(f);
|
let mut computer = intcode::Computer::from(f);
|
||||||
let mut grid: Grid = HashMap::new();
|
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 mut output = output.into_iter();
|
||||||
let xy = XY {
|
let xy = XY {
|
||||||
x: *output.next().unwrap(),
|
x: output.next().unwrap(),
|
||||||
y: *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);
|
grid.insert(xy, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,63 +25,31 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
// part2
|
// part2
|
||||||
let mut computer = computer.clone_with_modified_program(vec![(0, 2)]);
|
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::initscr();
|
||||||
ncurses::refresh();
|
ncurses::noecho();
|
||||||
let mut score: isize;
|
|
||||||
loop {
|
loop {
|
||||||
let key = ncurses::getch();
|
while let Some(output) = &computer.run_until_input() {
|
||||||
ncurses::clear();
|
output.chunks(3).for_each(|c| {
|
||||||
let joystick = match key {
|
let mut score = 0;
|
||||||
104 => 1,
|
if let &[x, y, value] = c {
|
||||||
108 => -1,
|
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,
|
_ => 0,
|
||||||
};
|
};
|
||||||
let mut drawcount = 0;
|
computer.push_input_and_step(joystick);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
//println!("{} {} {} {}", xmin, xmax, ymin, ymax);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq)]
|
#[derive(Eq, PartialEq)]
|
||||||
|
|
|
@ -181,6 +181,12 @@ impl Computer {
|
||||||
self.outputs.pop_front()
|
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 {
|
pub fn clone_with_modified_program(&self, u: Vec<(usize, isize)>) -> Computer {
|
||||||
let mut computer = self.clone();
|
let mut computer = self.clone();
|
||||||
for (pos, val) in u {
|
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> {
|
pub fn run_until_output(&mut self) -> Option<isize> {
|
||||||
loop {
|
loop {
|
||||||
self.step();
|
self.step();
|
||||||
|
@ -223,6 +249,27 @@ impl Computer {
|
||||||
self.run_until_output()
|
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) {
|
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);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue