diff --git a/2019/18/Cargo.toml b/2019/18/Cargo.toml new file mode 100644 index 0000000..96923f8 --- /dev/null +++ b/2019/18/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day18" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +grid = { path = "../grid" } +lazy_static = "1.3.0" diff --git a/2019/18/input b/2019/18/input new file mode 100644 index 0000000..3278a5d --- /dev/null +++ b/2019/18/input @@ -0,0 +1,81 @@ +################################################################################# +#.........#...#....h....Q.......#.......#.#...........#...#...#.................# +#.###.###.#.#.###.#############.#.###T#.#.#.#####.###.#.#.#.#.#####.###########.# +#..a#.#.#...#.#...#.#...#.....#.#.#.#.#.#.......#.#...#.#.#.#.....#.#.....#.....# +#####.#.#####.#.###.#.#.#.#.###.#.#.#.###.#######.#.#####.#.#####.#.#.###.#.###.# +#...#.#.......#.#.....#...#.#...#...#...#.#...#...#.#.....#.#.#...#...#...#.#...# +#.#.#.#.#######.#.#########.#.###.#####.###.#.#.###.#.#####.#.#.#######L###.#.### +#.#...#.#.......#.#.......#...#.#.#.....#...#...#.....#.......#.....#.....#.#...# +#.#####.#.#######.#####.#######.#.#.###.#.#.###########.#######.###.#####.#.###.# +#...#.#.#...#.........#...#.....#.#...#.#.#.#...#.......#.....#...#...#...#.#...# +#.#.#.#.###.#########.###.#.#.###.###.#.#.#.#.#.#.#######.###.#######.#.###.##### +#.#.#.#.#...#...#...#...#...#.......#.#.#.#.#.#...#.#.....#.#.......#g#.#.#.....# +###.#.#.#.###.#.#.#.###.###########.#.###.###.#####.#.#####.#######.#.#.#.#####.# +#...#.....#...#z..#.#.#...........#.#...#.#...#.....#.#.........#...#.#.....#...# +#.#########.#######.#.#.#########.#.###.#.#.#####.#.#.#####.###.#.###.#####.#.### +#...#.......#.#...#.#...#...#.....#.#...#.#...#...#.#.#...#...#.......#...Z.#...# +#.#.#.#######.#.#.#.#####.#.###.#####.#.#.###.#.###.#.#.#.#####################.# +#.#.#...#.#.....#.#....j..#...#.....#.#.#...#...#.#.#...#.#.........#.........#.# +###.###.#.#.#####.###########.#####.#.#.###.#####.#.#####.#.#####.#.#.#######.#.# +#...#...#...#.....#.........#.....#...#.#.......#.#...#.#...#...#.#.#.#.......#.# +#K###.#######.###.#.#############.#####.#.#####.#.###.#.#####.#.###.#.#.#######.# +#.....#.......#.....#.#.......#..e#...#.#...#.......#.#.....#.#.....#.#.#.......# +#########.#.#########.#.#####.#.###.###.#.#.#######.#.#.###.#########.#.###.##### +#....n....#.#...#...#...#.....#.#.....#.#.#.....#.#.#.#.#...#.........#.....#...# +#.###.#####.#.#.#.#.#####.#####.#.###.#.#.#####.#.#.#.#.#.#####.#######.#####.#.# +#.#...#.....#.#...#d....#.#.....#...#...#.#...#.#...#...#.....#.....#...#.....#.# +###.###.#####.#####.###.#.#.#.#####.#####.#.#.#.#.###########.#.###.#####.#####.# +#...#.......#.#.....#.#.#.#.#.#.#...#...#...#.#.#...........#.#...#...#.......#.# +#.###########.#.#####.#.#.#.#.#.#.#####.#######.#############.###.###.#.#######.# +#.....#.......#.....#...#.#.#.#.#.....#.#.....#.......#.......#.....#...#...#...# +#.###.#.###########.#.###.###.#.#####.#.#.###.#######.#.#################.#.#.#.# +#...#...#...#.....#.#.#.#u....#...#...#.#.#..v........#.#...........#.....#r..#.# +###.#####.#.#.#.###.#.#.#######.#.#.###.#.###.#########.#######.###.#.#########.# +#...#.....#...#...#.#.......#...#.#.....#...#.#...#.....#.......#.D.#.......#...# +#.###############.#.#######.#.###.#####.#.#.###.#.#.#####.#######.###.#####.##### +#.......#.......#.#.#.........#...#.....#.#.....#.#.#...#...M.#.#.#.......#.....# +#######.#.#####.#.#.#.#############.#####.#######.#.#.#.#####.#.#.#####.#######.# +#.#.....#.....#...#m#.#.....#...#...#...#.#.....#.#...#.#.....#.#.....#.#.......# +#.#.#########.###.#.###.###.#.#.#.#####.#.###.#.#.#####.#.#####.#####.###.#####.# +#.............#...#.V.....#...#...............#.#.........#...............#.....# +#######################################.@.####################################### +#.....................#.............................#...#.......#.....#.#.......# +#.#####.#.###########.###.###.#########.#.#########.###.#.###.#.###.#.#.#.#####.# +#...#.#.#.#...#.#...#...#.#...#.......#.#.#...#.....#...#.#.#.#.....#...#.....#.# +###.#.#.###.#.#.#.#.###.###.###.###.###.#.#.#.#.#####.###.#.#.#############.###.# +#.....#.....#.#...#.........#.....#.....#.#.#...#.......#...#...#...#.......#...# +#.#########.#.#.#########.#######.#######.###.###.#####.###.###.#.#.#.#######.#.# +#.#.......#.#.#...#...#...#...#...#...#.#...#...#.#...#.....#.#...#.#...#....k#.# +#.#.#####.###.#####.#.#####.#.#.###.#.#.###.###.#.#.#.#######.#####.#.###.#####.# +#.#.#...#.#...#...O.#...#...#.#.....#.#.#...#...#...#.......#....o#...#...#.....# +#.#.#.#.#.#.###.#######.#B###.#######.#.#.###.#############.#####.#####.###.##### +#.#...#.#.#...#...#...#.#.#.#.#.....#.#.#.#...........#.....#...#.#...#.#...#...# +#####.#.#.###.###.#.#.#.#.#.#.#####.#.#.#.###########.#####.#.#.#.#.#.#.###.###.# +#...#.#.#.....#.#.#.#.#...#.#.#...#...#.#.#...#.......#...#...#.#...#.#...#...#.# +#.#.###.#####.#.#.#.#.#####.#.#.#.#.###.#.#.#.#####.###.#.#####U###.#.###.###.#.# +#.#.....#...#.#.#c..#...#.#...#.#.#...#.#...#.#...#.#...#...#.....#.#...#.#...#.# +#.#######.###.#.#######C#.#.###E#.###.#.#####.#.#.#.###.###.#####.###.#.#.#.###.# +#.#...#...#...#...#...#.#.#.#...#.#.....#.....#.#.#.....#.......#.....#.#.#...#.# +#.#.#.###.#.###.#.#.#.#.#.#.###.#.#######.###.#.#.#######.#####.#####.###.###.#.# +#...#...#.......#...#.#.#.....#.#.#.....#...#.#.#...#...#.#...#.#...#.#...#.#...# +#.#####.###############.#####.#.#.#.###J#.#.###.###.###.#.#.#.#.#.#.#.#.###.###.# +#.#...#...#.......I.#.#p..#.....#...#.#.#.#...#.#.#...#...#.#.#.#.#..b#.#.......# +#.#.#.###.#.#######.#.#.#.###########.#.#.###.#.#.###.#####.###.#.#####.#.#####.# +#...#.#...#.#.......#.#.#.....#.......#.#.#...#.#...#.......#.#t#.#...#.#.#...#.# +#.#####.###.#.#####.#.###.###.###.###.#.###.###.#.###########.#.###.#.#.###.#.#.# +#.#.....#...#y..#...#...#...#.P.#...#...#...#...#.#.....#.....#.....#.#...#.#.#.# +#.#.#####.#####.#.#####W###.###.###.#####.###.###.#.#.###.#.#.#######.#.#.#.#.### +#.#.....#i..#.#.#.#.....#...#...#.#...#.#.....#.....#...#.#.#.#.....#.#.#...#...# +#R#####.###F#.#.###.#########.###.###.#.###############.#.#.#.#N#.###.#########.# +#.#...#...#...#...#.#..s......#...#...#.#...............#.#.#.#.#.....#...#.....# +###.#.###.###.###.#.#####.#######.#.###.#.#############.#.#.###.#######.#.#.###.# +#...#.#.....#.#.#.#.#.....#.......#.#...#.....#.......#.#.#.#...#...#...#...#...# +#.###.#.###.#.#.#Y#.#.#####.#.###.#.#.#.#.###.#######.#.#.#.#.###.#.#.#######.### +#.#.#.#.#...#.#.#..w#...#.#.#...#.#...#.#.#.#.......#...#.#...#...#.#...#...#...# +#.#.#.#.#####.#.#######.#A#.###.#######.#.#.#######.#####.#####.###.###H#.#####.# +#.#.....#...#......f..S.#.#.#.#.......#.#...#...#.#.#...#...#...#.#.#...#.....#.# +#.#######.#.#############.#.#.#######.#.###.#.#.#.#.#.#.###.#.###.#.#.###.#.#.#.# +#...#..x#.#.......#.....#...#...#..l..#.#.#.#.#.#.#...#...#.#.#...#...#.#.#.#.#.# +#G#.#.#.#.#####.###.#.###.#####.#.#####.#.#.#.#.#.#######.#.#.#.#.#####.#.#.###.# +#.#...#.......#.....#...........#.......#.....#.........#..q..#.#.........#...X.# +################################################################################# diff --git a/2019/18/notes b/2019/18/notes new file mode 100644 index 0000000..2ff8dd0 --- /dev/null +++ b/2019/18/notes @@ -0,0 +1,11 @@ +priority: +(closest) actually dead end with keys -> (closest) key for reachable door +alphabetical as tiebreaker + +for scan: set of visited positions + +tasks: +//scan entire map -> create dependencies (Map>) +dead end detection -> also done? +//implement pathfinding (maybe dijkstra-like?) + diff --git a/2019/18/src/main.rs b/2019/18/src/main.rs new file mode 100644 index 0000000..887ba72 --- /dev/null +++ b/2019/18/src/main.rs @@ -0,0 +1,83 @@ +use grid::*; +use std::char; +use std::collections::{HashMap,HashSet}; +use std::io::{self, BufRead}; +#[macro_use] +extern crate lazy_static; + +lazy_static! { + static ref MAP: HashMap = io::stdin() + .lock() + .lines() + .enumerate() + .flat_map(move |(y, l)| l + .unwrap() + .to_owned() + .chars() + .enumerate() + .map(move |(x, c)| ((x, y).into(), c)) + .collect::>()) + .collect(); +} + +fn visit_neighbors(position: Position2D, steps: usize, distances: &mut HashMap, dependencies: &mut HashMap>, mut doors: HashSet) { + let mut unvisited = Vec::new(); + // TODO: fix ownership and make this a single filter().inspect().for_each() + for (_, p) in &position.neighbors() { + match MAP.get(p) { + Some(&'#') => (), + Some(n) if n.is_alphanumeric() => { + if n.is_lowercase() { + dependencies.insert(*n, doors.clone()); + } + if n.is_uppercase() { + doors.insert(*n); + } + if distances.get(p).is_none() { + unvisited.push(p.to_owned()); + } + }, + _ => { + if distances.get(p).is_none() { + unvisited.push(p.to_owned()); + } + }, + } + if MAP.get(p) != Some(&'#') && distances.get(p).is_none() { + unvisited.push(p.to_owned()); + } + } + /* + unvisited.into_iter() + .inspect(|p| { distances.insert(p.to_owned(), steps+1);}) + .for_each(|p| visit_neighbors(p.to_owned(), steps+1, distances)); + */ + for p in &unvisited { + distances.insert(p.to_owned(), steps+1); + } + for p in &unvisited { + visit_neighbors(p.to_owned(), steps+1, distances, dependencies, doors.clone()); + } +} + +fn traverse(start: Position2D) -> (HashMap, HashMap>) { + let mut distances = HashMap::new(); + let mut dependencies = HashMap::new(); + distances.insert(start, 0usize); + visit_neighbors(start, 0, &mut distances, &mut dependencies, HashSet::new()); + (distances, dependencies) +} + +fn main() { + //println!("{:?}", MAP.keys()); + println!("{}", draw_ascii(&MAP, ' ')); + let start = MAP.keys().find(|p| MAP.get(&p) == Some(&'@')).unwrap(); + let v = MAP.keys().find(|p| MAP.get(&p) == Some(&'v')).unwrap(); + println!("Start: {:?}", start); + let (distances, dependencies) = traverse(start.to_owned()); + println!("{:?}", distances); + println!("{:?}", distances.get(&v)); + println!("{:?}", distances.len()); + println!("{:?}", dependencies.get(&'m')); + println!("{}", dependencies.len()); +}