diff --git a/2019/13/Cargo.toml b/2019/13/Cargo.toml index 9cdd69f..5235924 100644 --- a/2019/13/Cargo.toml +++ b/2019/13/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" [dependencies] intcode = { path = "../intcode" } +grid = { path = "../grid" } +itertools = "0.8.2" diff --git a/2019/13/src/main.rs b/2019/13/src/main.rs index 4f24586..910e773 100644 --- a/2019/13/src/main.rs +++ b/2019/13/src/main.rs @@ -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 = 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); } diff --git a/2019/grid/src/lib.rs b/2019/grid/src/lib.rs index 50e1b66..6e23ff5 100644 --- a/2019/grid/src/lib.rs +++ b/2019/grid/src/lib.rs @@ -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(coordinates: &HashMap, de let b = get_boundaries(&coordinates.keys().collect::>()); 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())