Commit 9e9bb1e3 authored by Marcel Behrmann's avatar Marcel Behrmann

day(18): start solving

parent 85972bca
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day_18"
version = "0.1.0"
dependencies = [
"pathfinding 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "either"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "fixedbitset"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "indexmap"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itertools"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pathfinding"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fixedbitset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"indexmap 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
"checksum fixedbitset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d"
"checksum indexmap 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712d7b3ea5827fcb9d4fda14bf4da5f136f0db2ae9c8f4bd4e2d1c6fde4e6db2"
"checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
"checksum pathfinding 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3f9cd34a7aeaeec1c2de73ba797064beba632d36792b441095221f6dd878ae7"
......@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pathfinding = "2.0.1"
\ No newline at end of file
mod utils;
mod intvm;
use std::fs;
use std::collections::{HashMap, VecDeque, HashSet};
use pathfinding::directed::astar;
const INPUT_FILE: &str = "input.txt";
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
/*let input =
"########################
#[email protected]#
######################.#
#d.....................#
########################";
/* let input =
"#########
#[email protected]#
#########";*/
let input =
"########################
#...............b.C.D.f#
#.######################
#[email protected]#
########################";
let input =
"#################
#i.G..c...e..H.p#
########.########
#j.A..b...f..D.o#
########@########
#k.E..a...g..B.n#
########.########
#l.F..d...h..C.m#
#################"; /* /*
let input =
"########################
#@..............ac.GI.b#
###d#e#f################
###A#B#C################
###g#h#i################
########################";*/*/*/
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
......@@ -13,11 +53,142 @@ fn main() {
part_02(input.clone());
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Pos(i32, i32);
impl Pos {
fn successors(&self) -> Vec<(Pos, u32)> {
let &Pos(x, y) = self;
vec![Pos(x+1, y), Pos(x-1,y), Pos(x,y+1), Pos(x,y-1)]
.into_iter().map(|p| (p, 1)).collect()
}
fn distance(&self, a: &Self) -> u32 {
((self.0 - a.0).abs() + (self.1 - a.1).abs()) as u32
}
}
#[allow(unused_variables)]
fn part_01(input: Vec<&str>) {
let mut map: HashMap<Pos, char> = HashMap::new();
let mut x = 0;
let mut y = 0;
let mut start = Pos(-1, -1);
let mut keys: Vec<Pos> = Vec::new();
for line in input {
for c in line.chars() {
map.insert(Pos(x, y), c);
match c {
'.' => (),
'#' => (),
'@' => start = Pos(x, y),
_ => {
let cu = c as u8;
if cu > 96 {
keys.push(Pos(x, y));
}
}
};
x += 1;
}
y += 1;
x = 0;
}
println!("Cave contains {} keys", keys.len());
let steps_for_keys = find_keys(start.clone(), &map, keys.clone(), Vec::new(), 0);
println!("Needed {} Steps", steps_for_keys);
println!("Result of Part 1");
}
fn find_keys(pos: Pos, map: &HashMap<Pos, char>, keys: Vec<Pos>, available: Vec<char>, length: u32) -> u32 {
if keys.len() == 0 {
return length;
}
let successors = |p: &Pos| {
p.successors()
.iter()
.filter(|f| {
let p = map.get(&f.0);
match p {
Some(x) => {
match x {
'.' => true,
'#' => false,
_ => {
*x as u8 > 96 || *x == '@' || available.contains(x)
}
}
},
None => {
false
}
}
})
.map(|p| p.clone())
.collect::<Vec<(Pos, u32)>>()
};
let mut lowest = std::u32::MAX;
//println!("Looking for keys: {:?}, with open dors: {:?}", keys, available);
for key in &keys {
let goal = key;
let result = astar::astar(
&pos,
successors,
|p| p.distance(goal),
|p| *p == *goal
);
//println!("Found path: {:?}", result);
match result {
Some(path) => {
let mut new_keys = keys.clone();
let mut new_available = available.clone();
// Remove found key from list of keys
let pos = new_keys.iter().position(|x| *x == *key).unwrap();
new_keys.remove(pos);
// Add new key to available keys
new_available.push(((*map.get(goal).unwrap() as u8) - 32) as char);
//println!("We now have keys: {:?}", new_available);
let new_length = length + path.1;
if new_length > lowest {
continue;
}
let overall = find_keys(goal.clone(), &map, new_keys, new_available, new_length);
if overall < lowest {
lowest = overall;
}
},
None => ()
};
}
lowest
}
#[allow(unused_variables)]
fn part_02(input: Vec<&str>) {
println!("Result of Part 2");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment