Add D13P2

This commit is contained in:
kageru 2019-12-13 19:29:22 +01:00
parent 23043afc32
commit 3f6ff50094
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 54 additions and 4 deletions

View File

@ -8,3 +8,5 @@ edition = "2018"
[dependencies]
intcode = { path = "../intcode" }
grid = { path = "../grid" }
itertools = "0.8.2"

View File

@ -1,7 +1,21 @@
use grid::*;
use intcode::*;
use std::collections::HashMap;
mod tile;
use std::cmp::Ordering;
use tile::Tile;
fn int(ord: Ordering) -> i64 {
match ord {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
fn main() {
let part1 = IntComputer::new(read_input(), 0, vec![])
let mut input = read_input();
let part1 = IntComputer::new(input.clone(), 0, vec![])
.get_all_outputs()
.into_iter()
.skip(2)
@ -9,4 +23,38 @@ fn main() {
.filter(|s| s == &2)
.count();
println!("Part 1: {}", part1);
input[0] = 2;
let (mut paddle_pos, mut ball_pos) = (0, 0);
let mut ic = IntComputer::new(input, 0, vec![]);
let mut outputs = Vec::with_capacity(3);
let mut field: HashMap<Position2D, Tile> = HashMap::new();
let mut score = 0;
loop {
match ic.step() {
IntComputerResult::Output(o) => outputs.push(o),
IntComputerResult::Halt => break,
IntComputerResult::Continue => (),
};
if outputs.len() == 3 {
let pos: Position2D = (outputs[0], outputs[1]).into();
if pos.x == -1 {
score = outputs[2];
outputs.clear();
continue;
}
let tile = outputs[2].into();
match tile {
Tile::Ball => ball_pos = pos.x,
Tile::Paddle => paddle_pos = pos.x,
_ => (),
};
field.insert(pos, tile);
outputs.clear();
ic.params = vec![int(ball_pos.cmp(&paddle_pos))];
//println!("{}", draw_ascii(&field, Tile::Empty));
}
}
println!("Part 2: {}", score);
}

View File

@ -3,8 +3,8 @@ use std::collections::HashMap;
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
pub struct Position2D {
x: i64,
y: i64,
pub x: i64,
pub y: i64,
}
struct Boundaries {
@ -27,7 +27,7 @@ pub fn draw_ascii<T: std::fmt::Display>(coordinates: &HashMap<Position2D, T>, de
let b = get_boundaries(&coordinates.keys().collect::<Vec<_>>());
join(
(b.y_min..=b.y_max).rev().map(|y| {
(b.x_min..b.x_max)
(b.x_min..=b.x_max)
.map(|x| {
coordinates
.get(&(x, y).into())