Clean up D11
This commit is contained in:
parent
7ce5013eb3
commit
66f7b2bcc0
|
@ -8,3 +8,4 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
intcode = { path = "../intcode" }
|
intcode = { path = "../intcode" }
|
||||||
|
itertools = "0.8.2"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use intcode::*;
|
use intcode::*;
|
||||||
|
use itertools::join;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
enum Direction {
|
enum Direction {
|
||||||
|
@ -8,83 +9,96 @@ enum Direction {
|
||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn turn(dir: Direction, turn_value: i64) -> Direction {
|
struct Robot {
|
||||||
match turn_value {
|
direction: Direction,
|
||||||
0 => match dir {
|
position: (i64, i64),
|
||||||
Direction::Up => Direction::Left,
|
visited: HashMap<(i64, i64), i64>,
|
||||||
Direction::Right => Direction::Up,
|
}
|
||||||
Direction::Down => Direction::Right,
|
|
||||||
Direction::Left => Direction::Down,
|
impl Robot {
|
||||||
},
|
fn turn(&mut self, turn_value: i64) {
|
||||||
1 => match dir {
|
self.direction = match turn_value {
|
||||||
Direction::Up => Direction::Right,
|
0 => match self.direction {
|
||||||
Direction::Right => Direction::Down,
|
Direction::Up => Direction::Left,
|
||||||
Direction::Down => Direction::Left,
|
Direction::Right => Direction::Up,
|
||||||
Direction::Left => Direction::Up,
|
Direction::Down => Direction::Right,
|
||||||
},
|
Direction::Left => Direction::Down,
|
||||||
_ => unreachable!("Illegal turn value"),
|
},
|
||||||
|
1 => match self.direction {
|
||||||
|
Direction::Up => Direction::Right,
|
||||||
|
Direction::Right => Direction::Down,
|
||||||
|
Direction::Down => Direction::Left,
|
||||||
|
Direction::Left => Direction::Up,
|
||||||
|
},
|
||||||
|
_ => unreachable!("Illegal turn value"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mv(&mut self) {
|
||||||
|
let pos = self.position;
|
||||||
|
self.position = match self.direction {
|
||||||
|
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 paint(&mut self, color: i64) {
|
||||||
|
self.visited.insert(self.position, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_color(&self) -> i64 {
|
||||||
|
*self.visited.get(&self.position).unwrap_or(&0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mv(pos: (i64, i64), dir: &Direction) -> (i64, i64) {
|
#[rustfmt::skip]
|
||||||
match dir {
|
fn get_boundaries(positions: &HashMap<(i64, i64), i64>) -> (i64, i64, i64, i64) {
|
||||||
Direction::Up => (pos.0, pos.1 + 1),
|
let keys = positions.keys();
|
||||||
Direction::Right => (pos.0 + 1, pos.1),
|
let x_max = keys.clone().into_iter().max_by_key(|k| k.0).unwrap() .0;
|
||||||
Direction::Left => (pos.0 - 1, pos.1),
|
let y_max = keys.clone().into_iter().max_by_key(|k| k.1).unwrap() .1;
|
||||||
Direction::Down => (pos.0, pos.1 - 1),
|
let x_min = keys.clone().into_iter().min_by_key(|k| k.0).unwrap() .0;
|
||||||
}
|
let y_min = keys.clone().into_iter().min_by_key(|k| k.1).unwrap() .1;
|
||||||
|
(x_min, x_max, y_min, y_max)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn start_with_input(input: Vec<i64>, color: i64) -> Robot {
|
||||||
let mut positions = HashMap::new();
|
let mut robot = Robot {
|
||||||
let mut pc = IntComputer::new(read_input(), 0, vec![1]);
|
position: (0, 0),
|
||||||
let mut pos = (0, 0);
|
visited: HashMap::new(),
|
||||||
let mut direction = Direction::Up;
|
direction: Direction::Up,
|
||||||
|
};
|
||||||
|
let mut pc = IntComputer::new(input, 0, vec![color]);
|
||||||
while let IntComputerResult::Output(o) = pc.run() {
|
while let IntComputerResult::Output(o) = pc.run() {
|
||||||
positions.insert(pos, o);
|
robot.paint(o);
|
||||||
let turn_int = pc.run().unwrap();
|
let turn_int = pc.run().unwrap();
|
||||||
direction = turn(direction, turn_int);
|
robot.turn(turn_int);
|
||||||
pos = mv(pos, &direction);
|
robot.mv();
|
||||||
pc.params.push(*positions.get(&pos).unwrap_or(&0));
|
pc.params.push(robot.current_color());
|
||||||
}
|
}
|
||||||
//
|
robot
|
||||||
println!("Part 1: {}", positions.len());
|
}
|
||||||
let x_max = positions
|
|
||||||
.clone()
|
fn draw_ascii_text(coordinates: &HashMap<(i64, i64), i64>) -> String {
|
||||||
.keys()
|
let (x_min, x_max, y_min, y_max) = get_boundaries(&coordinates);
|
||||||
.into_iter()
|
join(
|
||||||
.max_by_key(|k| k.0)
|
(y_min..y_max + 1).rev().map(|y| {
|
||||||
.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)
|
(x_min..x_max)
|
||||||
.map(|x| positions.get(&(x, y)).unwrap_or(&0).to_string())
|
.map(|x| coordinates.get(&(x, y)).unwrap_or(&0).to_string())
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
.replace('0', " ")
|
.replace('0', " ")
|
||||||
.replace('1', "█")
|
.replace('1', "█")
|
||||||
);
|
}),
|
||||||
}
|
"\n",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = read_input();
|
||||||
|
let part1_robot = start_with_input(input.clone(), 0);
|
||||||
|
println!("Part 1: {}", part1_robot.visited.len());
|
||||||
|
|
||||||
|
let part2_robot = start_with_input(input.clone(), 1);
|
||||||
|
println!("Part 2:\n{}", draw_ascii_text(&part2_robot.visited));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user