diff --git a/src/main.rs b/src/main.rs index 6312093..8824b36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ fn main() { - // aoc_2018::tasks::day15::task1(); - aoc_2018::tasks::day15::task2(); + aoc_2018::tasks::day20::task1(); + // aoc_2018::tasks::day15::task2(); } diff --git a/src/tasks/day20.rs b/src/tasks/day20.rs new file mode 100644 index 0000000..6901aff --- /dev/null +++ b/src/tasks/day20.rs @@ -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 = 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, 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, 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) { + 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 { + 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 { + 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, +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index c4cadc0..f69e2c5 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -13,3 +13,4 @@ pub mod day12; pub mod day13; pub mod day14; pub mod day15; +pub mod day20;