Commit 7ec9f328 authored by Marcel Behrmann's avatar Marcel Behrmann

day(20): starting to solve

parent 2ab99fa4
......@@ -112,4 +112,4 @@ AE..#...#...#.#.#...#.......#.#
#.......#.................#.#.......#.......#.........#...#.#.........#...#.#.....#.#.......#.#...............#.#
###################################.#######.#######.#######.###.###########.#.#.#################################
B R M P K G M Z
O F S Q D Y T Z
O F S Q D Y T Z
\ No newline at end of file
......@@ -6,6 +6,7 @@ use std::fs;
use std::collections::HashMap;
use map::Pos;
use pathfinding::directed::astar;
use pathfinding::directed::dijkstra;
const INPUT_FILE: &str = "input.txt";
......@@ -14,26 +15,6 @@ type JumpTable = HashMap<Pos, Pos>;
fn main() {
let input = fs::read_to_string(INPUT_FILE).expect("Error reading input");
let input =
" A
A
#######.#########
#######.........#
#######.#######.#
#######.#######.#
#######.#######.#
##### B ###.#
BC...## C ###.#
##.## ###.#
##...DE F ###.#
##### G ###.#
#########.#####.#
DE..#######...###.#
#.#########.###.#
FG..#########.....#
###########.#####
Z
Z ";
let input = input.lines();
let input: Vec<&str> = input.collect::<Vec<&str>>();
......@@ -49,8 +30,9 @@ fn part_01(input: Vec<&str>) {
println!("Found start at: {:?}", start);
println!("Found goal at: {:?}", goal);
println!("Found {} jumps", network.len());
println!("Jumps: {:#?}", network);
//println!("Found {:?} jumps", network);
let pp = Pos{x: 2, y: 8};
let successors = |p: &Pos| {
p.successors(&network)
.iter()
......@@ -72,14 +54,16 @@ fn part_01(input: Vec<&str>) {
.collect::<Vec<(Pos, u32)>>()
};
let result = astar::astar(
//print_map(dimensions, &map, &network);
let result = dijkstra::dijkstra(
&start,
successors,
|p| p.distance(&goal),
//|p| p.distance(&goal),
|p| *p == goal
);
println!("Path: {:?}", result);
println!("Path: {:?}",result);
}
fn build_map(input: Vec<&str>) -> (Map, (i32, i32)) {
......@@ -111,7 +95,7 @@ fn build_map(input: Vec<&str>) -> (Map, (i32, i32)) {
x = 0;
}
(map, dimensions)
(map, (130, 130))
}
fn analyze_network(map: &Map, dimensions: &(i32, i32)) -> (Pos, Pos, JumpTable) {
......@@ -135,42 +119,48 @@ fn analyze_network(map: &Map, dimensions: &(i32, i32)) -> (Pos, Pos, JumpTable)
if let Some(c) = map.get(&p) {
match c {
'A' ..= 'Z' => (),
_ => continue,
_ => continue 'row,
}
for neighbour in &neighbours {
let t = p.clone() + neighbour.clone();
let tt = p.clone() + neighbour.clone() + neighbour.clone();
if let Some(cc) = map.get(&t) {
let ccc = map.get(&tt).or(Some(&'#')).unwrap();
if *ccc != '.'{
continue;
}
if cc == c {
let tt = p.clone() + neighbour.clone() + neighbour.clone();
// we need that check here so we can use the right portal stuff
if let Some(ccc) = map.get(&tt) {
if *ccc == '.' && *c == 'A' {
start = tt.clone();
continue 'row;
}
if *ccc == '.' && *c == 'Z' {
goal = tt.clone();
continue 'row;
}
// we need that check here so we can use the right portal stuff {
if *c == 'A' {
start = tt.clone();
continue 'row;
}
if *c == 'Z' {
goal = tt.clone();
continue 'row;
}
} else if *cc >= 'A' && *cc <= 'Z' {
let mut key = String::new();
// We have an error here, labels should always be red von top -> bottom, left -> right
key.push(*c);
key.push(*cc);
if let Some(ca) = cache.get(&key) {
let org = Pos{x, y};
table.insert(org.clone(), ca.clone());
table.insert(ca.clone(), org.clone());
table.insert(ca.clone(), tt.clone());
table.insert(tt.clone(), ca.clone());
continue 'row;
} else {
let org = Pos{x, y};
cache.insert(key, org);
println!("Inserting into cache: {} (new len: {}", key, cache.len());
cache.insert(key, tt);
continue 'row;
}
}
......@@ -180,11 +170,29 @@ fn analyze_network(map: &Map, dimensions: &(i32, i32)) -> (Pos, Pos, JumpTable)
}
}
println!("Jump table contains {} jumps", table.len());
//println!("{:#?}", cache);
(start, goal, table)
}
fn print_map(dimensions: (i32, i32), map: &Map, network: &JumpTable) {
for y in 0..dimensions.1 {
for x in 0..dimensions.0 {
if let Some(c) = map.get(&Pos{x, y}) {
if let Some(j) = network.get(&Pos{x, y}) {
print!("!");
} else {
print!("{}", c);
}
} else {
print!(" ");
}
}
println!();
}
}
#[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