Add D13P2
This commit is contained in:
parent
23043afc32
commit
3f6ff50094
@ -8,3 +8,5 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
intcode = { path = "../intcode" }
|
intcode = { path = "../intcode" }
|
||||||
|
grid = { path = "../grid" }
|
||||||
|
itertools = "0.8.2"
|
||||||
|
@ -1,7 +1,21 @@
|
|||||||
|
use grid::*;
|
||||||
use intcode::*;
|
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() {
|
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()
|
.get_all_outputs()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.skip(2)
|
.skip(2)
|
||||||
@ -9,4 +23,38 @@ fn main() {
|
|||||||
.filter(|s| s == &2)
|
.filter(|s| s == &2)
|
||||||
.count();
|
.count();
|
||||||
println!("Part 1: {}", part1);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
|
#[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)]
|
||||||
pub struct Position2D {
|
pub struct Position2D {
|
||||||
x: i64,
|
pub x: i64,
|
||||||
y: i64,
|
pub y: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Boundaries {
|
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<_>>());
|
let b = get_boundaries(&coordinates.keys().collect::<Vec<_>>());
|
||||||
join(
|
join(
|
||||||
(b.y_min..=b.y_max).rev().map(|y| {
|
(b.y_min..=b.y_max).rev().map(|y| {
|
||||||
(b.x_min..b.x_max)
|
(b.x_min..=b.x_max)
|
||||||
.map(|x| {
|
.map(|x| {
|
||||||
coordinates
|
coordinates
|
||||||
.get(&(x, y).into())
|
.get(&(x, y).into())
|
||||||
|
Loading…
Reference in New Issue
Block a user