wip
This commit is contained in:
parent
e16cafaa52
commit
41c99c2032
1 changed files with 133 additions and 0 deletions
133
src/day13/main.rs
Normal file
133
src/day13/main.rs
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
use aoc2019::intcode;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
use ncurses;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
let mut output = output.into_iter();
|
||||||
|
let xy = XY {
|
||||||
|
x: *output.next().unwrap(),
|
||||||
|
y: *output.next().unwrap(),
|
||||||
|
};
|
||||||
|
let tile = Tile::from(*output.next().unwrap());
|
||||||
|
grid.insert(xy, tile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// part 1
|
||||||
|
let blocks = grid.iter().filter(|(_, v)| **v == Tile::Block).count();
|
||||||
|
println!("blocks: {}", blocks);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
loop {
|
||||||
|
let key = ncurses::getch();
|
||||||
|
ncurses::clear();
|
||||||
|
let joystick = match key {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
//println!("{} {} {} {}", xmin, xmax, ymin, ymax);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq)]
|
||||||
|
enum Tile {
|
||||||
|
Empty,
|
||||||
|
Wall,
|
||||||
|
Block,
|
||||||
|
HorizontalPaddle,
|
||||||
|
Ball,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<isize> for Tile {
|
||||||
|
fn from(i: isize) -> Self {
|
||||||
|
match i {
|
||||||
|
0 => Tile::Empty,
|
||||||
|
1 => Tile::Wall,
|
||||||
|
2 => Tile::Block,
|
||||||
|
3 => Tile::HorizontalPaddle,
|
||||||
|
4 => Tile::Ball,
|
||||||
|
_ => unreachable!("there are only 5 tiles..."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tile {
|
||||||
|
fn char(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
Tile::Empty => " ",
|
||||||
|
Tile::Wall => "X",
|
||||||
|
Tile::Block => "O",
|
||||||
|
Tile::HorizontalPaddle => "-",
|
||||||
|
Tile::Ball => "*",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Eq, PartialEq, Hash)]
|
||||||
|
struct XY {
|
||||||
|
x: isize,
|
||||||
|
y: isize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<(isize, isize)> for XY {
|
||||||
|
fn from(xy: (isize, isize)) -> Self {
|
||||||
|
XY { x: xy.0, y: xy.1 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Grid = HashMap<XY, Tile>;
|
Loading…
Add table
Add a link
Reference in a new issue