Add D11P2

This commit is contained in:
kageru 2019-12-11 19:40:22 +01:00
parent 0942f432ed
commit 7ce5013eb3
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2

View File

@ -28,16 +28,16 @@ fn turn(dir: Direction, turn_value: i64) -> Direction {
fn mv(pos: (i64, i64), dir: &Direction) -> (i64, i64) {
match dir {
Direction::Up => (pos.0, pos.1 + 1),
Direction::Right => (pos.0 + 1, pos.1),
Direction::Left => (pos.0 - 1, pos.1),
Direction::Down => (pos.0, pos.1 - 1),
Direction::Up => (pos.0, pos.1 + 1),
Direction::Right => (pos.0 + 1, pos.1),
Direction::Left => (pos.0 - 1, pos.1),
Direction::Down => (pos.0, pos.1 - 1),
}
}
fn main() {
let mut positions= HashMap::new();
let mut pc = IntComputer::new(read_input(), 0, vec![0]);
let mut positions = HashMap::new();
let mut pc = IntComputer::new(read_input(), 0, vec![1]);
let mut pos = (0, 0);
let mut direction = Direction::Up;
while let IntComputerResult::Output(o) = pc.run() {
@ -47,5 +47,44 @@ fn main() {
pos = mv(pos, &direction);
pc.params.push(*positions.get(&pos).unwrap_or(&0));
}
//
println!("Part 1: {}", positions.len());
let x_max = positions
.clone()
.keys()
.into_iter()
.max_by_key(|k| k.0)
.unwrap()
.0;
let y_max = positions
.clone()
.keys()
.into_iter()
.max_by_key(|k| k.1)
.unwrap()
.1;
let x_min = positions
.clone()
.keys()
.into_iter()
.min_by_key(|k| k.0)
.unwrap()
.0;
let y_min = positions
.clone()
.keys()
.into_iter()
.min_by_key(|k| k.1)
.unwrap()
.1;
for y in (y_min..y_max+1).rev() {
println!(
"{}",
(x_min..x_max)
.map(|x| positions.get(&(x, y)).unwrap_or(&0).to_string())
.collect::<String>()
.replace('0', " ")
.replace('1', "")
);
}
}