This commit is contained in:
kageru 2019-12-18 16:32:23 +01:00
parent f14828637f
commit 4a28ac8e8e
4 changed files with 186 additions and 0 deletions

11
2019/18/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "day18"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
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"

81
2019/18/input Normal file
View File

@ -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.#
#################################################################################

11
2019/18/notes Normal file
View File

@ -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<Key, Set<Key>>)
dead end detection -> also done?
//implement pathfinding (maybe dijkstra-like?)

83
2019/18/src/main.rs Normal file
View File

@ -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<Position2D, char> = 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::<Vec<_>>())
.collect();
}
fn visit_neighbors(position: Position2D, steps: usize, distances: &mut HashMap<Position2D, usize>, dependencies: &mut HashMap<char, HashSet<char>>, mut doors: HashSet<char>) {
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<Position2D, usize>, HashMap<char, HashSet<char>>) {
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());
}