day20 WIP
This commit is contained in:
129
src/tasks/day20.rs
Normal file
129
src/tasks/day20.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use self::Tile::*;
|
||||
#[allow(unused_imports)]
|
||||
use crate::utils;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn task1() {
|
||||
// let input = utils::read_file("input/day20.txt");
|
||||
let input = "^ENWWW(NEEE|SSE(EE|N))$".to_string();
|
||||
let input = &input[1..];
|
||||
let mut map: HashMap<Point, Tile> = HashMap::new();
|
||||
add_default_neighbors_for_room(&mut map, Point(0, 0));
|
||||
parse_input(&mut map, &input, Point(0, 0));
|
||||
|
||||
print_map(&map);
|
||||
}
|
||||
|
||||
fn parse_input(map: &mut HashMap<Point, Tile>, input: &str, position: Point) {
|
||||
let mut position = position;
|
||||
let mut input = input;
|
||||
while let Some(c) = input.chars().next() {
|
||||
match c {
|
||||
'(' => return,
|
||||
'N' => {
|
||||
position = Point(position.0, position.1 - 2);
|
||||
add_default_neighbors_for_room(map, position);
|
||||
map.insert(Point(position.0, position.1 + 1), Door);
|
||||
input = &input[1..];
|
||||
}
|
||||
'E' => {
|
||||
position = Point(position.0 + 2, position.1);
|
||||
add_default_neighbors_for_room(map, position);
|
||||
map.insert(Point(position.0 - 1, position.1), Door);
|
||||
input = &input[1..];
|
||||
}
|
||||
'S' => {
|
||||
position = Point(position.0, position.1 + 2);
|
||||
add_default_neighbors_for_room(map, position);
|
||||
map.insert(Point(position.0, position.1 - 1), Door);
|
||||
input = &input[1..];
|
||||
}
|
||||
'W' => {
|
||||
position = Point(position.0 - 2, position.1);
|
||||
add_default_neighbors_for_room(map, position);
|
||||
map.insert(Point(position.0 + 1, position.1), Door);
|
||||
input = &input[1..];
|
||||
}
|
||||
'$' => return,
|
||||
c => panic!(
|
||||
"Stumbled upon a '{}' when reading input where it shouldn't be",
|
||||
c
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_default_neighbors_for_room(map: &mut HashMap<Point, Tile>, position: Point) {
|
||||
map.insert(position, Room);
|
||||
for p in position.diagonals() {
|
||||
map.insert(p, Wall);
|
||||
}
|
||||
for p in position.neighbors() {
|
||||
if !map.contains_key(&p) {
|
||||
map.insert(p, Unknown);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_map(map: &HashMap<Point, Tile>) {
|
||||
let xmin = map.keys().min_by_key(|it| it.0).unwrap().0;
|
||||
let xmax = map.keys().max_by_key(|it| it.0).unwrap().0;
|
||||
let ymin = map.keys().min_by_key(|it| it.1).unwrap().1;
|
||||
let ymax = map.keys().max_by_key(|it| it.1).unwrap().1;
|
||||
|
||||
for y in ymin..=ymax {
|
||||
for x in xmin..=xmax {
|
||||
if x == 0 && y == 0 {
|
||||
print!("X")
|
||||
} else {
|
||||
print!(
|
||||
"{}",
|
||||
match map.get(&Point(x, y)) {
|
||||
None => " ",
|
||||
Some(Wall) => "#",
|
||||
Some(Room) => ".",
|
||||
Some(Unknown) => "?",
|
||||
Some(Door) => {
|
||||
if y % 2 == 0 {
|
||||
"|"
|
||||
} else {
|
||||
"-"
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone, Copy, Debug)]
|
||||
struct Point(i32, i32);
|
||||
|
||||
impl Point {
|
||||
fn diagonals(&self) -> Vec<Point> {
|
||||
vec![
|
||||
Point(self.0 - 1, self.1 - 1),
|
||||
Point(self.0 - 1, self.1 + 1),
|
||||
Point(self.0 + 1, self.1 - 1),
|
||||
Point(self.0 + 1, self.1 + 1),
|
||||
]
|
||||
}
|
||||
|
||||
fn neighbors(&self) -> Vec<Point> {
|
||||
vec![
|
||||
Point(self.0 - 1, self.1),
|
||||
Point(self.0 + 1, self.1),
|
||||
Point(self.0, self.1 - 1),
|
||||
Point(self.0, self.1 + 1),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
enum Tile {
|
||||
Unknown,
|
||||
Room,
|
||||
Wall,
|
||||
Door,
|
||||
}
|
||||
@@ -13,3 +13,4 @@ pub mod day12;
|
||||
pub mod day13;
|
||||
pub mod day14;
|
||||
pub mod day15;
|
||||
pub mod day20;
|
||||
|
||||
Reference in New Issue
Block a user