Finished Dec 15
This commit is contained in:
3
december_15/example2.txt
Normal file
3
december_15/example2.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
19999
|
||||
19111
|
||||
11191
|
||||
@@ -1,11 +1,16 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader};
|
||||
use std::io::prelude::*;
|
||||
|
||||
const RADIX: u32 = 10;
|
||||
|
||||
static mut shortest: u32 = 99999;
|
||||
struct Node {
|
||||
distance: u32,
|
||||
previous: Option<(usize, usize)>,
|
||||
neigbours: Vec<(usize, usize)>,
|
||||
visited: bool
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let file = File::open("./input.txt").expect("Read failed");
|
||||
@@ -22,41 +27,86 @@ fn main(){
|
||||
weight_vec.push(temp_vec);
|
||||
}
|
||||
|
||||
let mut weight_vec_big_temp = Vec::new();
|
||||
for line in 0..weight_vec.len() {
|
||||
let mut new_line_vec = Vec::new();
|
||||
for i in 0..weight_vec.len() * 5 {
|
||||
let coordinate_from = i % weight_vec[line].len();
|
||||
let add_val = i / weight_vec[line].len();
|
||||
let mut new_value = weight_vec[line][coordinate_from] + add_val as u32;
|
||||
if new_value > 9 {
|
||||
new_value -= 9;
|
||||
}
|
||||
new_line_vec.push(new_value);
|
||||
}
|
||||
weight_vec_big_temp.push(new_line_vec);
|
||||
}
|
||||
|
||||
let mut final_vector = Vec::new();
|
||||
|
||||
for line in 0..weight_vec_big_temp.len() * 5 {
|
||||
let add_val = line / weight_vec_big_temp.len();
|
||||
let mut temp_vec = Vec::new();
|
||||
for i in 0..weight_vec_big_temp[0].len() {
|
||||
let mut new_value = weight_vec_big_temp[line % weight_vec_big_temp.len()][i] + add_val as u32;
|
||||
if new_value > 9 {
|
||||
new_value -= 9;
|
||||
}
|
||||
temp_vec.push(new_value)
|
||||
}
|
||||
final_vector.push(temp_vec);
|
||||
}
|
||||
|
||||
println!("Finished generating Vec");
|
||||
weight_vec = final_vector;
|
||||
|
||||
let height = weight_vec.len();
|
||||
let width = weight_vec[0].len();
|
||||
|
||||
let visited: Vec<(usize, usize)> = Vec::new();
|
||||
let mut graph = HashMap::new();
|
||||
|
||||
let out = get_shortest_weight(&weight_vec, 0, 0, 0, width, height, &visited);
|
||||
|
||||
|
||||
|
||||
println!("");
|
||||
}
|
||||
|
||||
fn get_shortest_weight(weight_vec: &Vec<Vec<u32>>, x: usize, y: usize, current_weight: u32, width: usize, height: usize, visited: &Vec<(usize, usize)>) {
|
||||
let mut visited_local = visited.to_owned();
|
||||
|
||||
unsafe {
|
||||
if current_weight > shortest {
|
||||
return;
|
||||
for x in 0..width {
|
||||
for y in 0..height {
|
||||
let mut neighbours = Vec::new();
|
||||
if x > 0 { neighbours.push((x-1,y)) }
|
||||
if x < width - 1 { neighbours.push((x+1,y)) }
|
||||
if y > 0 { neighbours.push((x,y-1)) }
|
||||
if y < height - 1 { neighbours.push((x,y+1)) }
|
||||
graph.insert((x,y) , Node{distance: u32::MAX, previous: None, neigbours: neighbours, visited: false});
|
||||
}
|
||||
}
|
||||
if x == width - 1 && y == height - 1 {
|
||||
unsafe {shortest = current_weight;
|
||||
println!("Current length: {}, shortest now: {}", current_weight, shortest);}
|
||||
return;
|
||||
}
|
||||
|
||||
if visited_local.contains(&(x,y)) {
|
||||
return;
|
||||
}
|
||||
visited_local.push((x,y));
|
||||
|
||||
let temp_weight = current_weight + weight_vec[y][x];
|
||||
|
||||
if x > 0 { get_shortest_weight(&weight_vec, x - 1, y, temp_weight, width, height, &visited_local); }
|
||||
if x < width - 1 { get_shortest_weight(&weight_vec, x + 1, y, temp_weight, width, height, &visited_local); }
|
||||
if y > 0 { get_shortest_weight(&weight_vec, x, y-1, temp_weight, width, height, &visited_local); }
|
||||
if y < height - 1 { get_shortest_weight(&weight_vec, x, y+1, temp_weight, width, height, &visited_local); }
|
||||
let default_node = Node { distance: 0, previous: None, neigbours: Vec::new(), visited: false };
|
||||
|
||||
let start_node = (0,0);
|
||||
let end_node = (width-1, height-1);
|
||||
|
||||
graph.entry(start_node).or_insert(default_node).distance = 0;
|
||||
|
||||
loop {
|
||||
let active_node = &graph.iter().filter(|x| x.1.visited == false).min_by_key(|x| x.1.distance).expect("").0.to_owned();
|
||||
|
||||
if active_node == &end_node {
|
||||
break;
|
||||
}
|
||||
let distance = graph.get(active_node).expect("PANIK").distance;
|
||||
//graph.entry(active_node.to_owned()).or_insert(Node { distance: 0, previous: None, neigbours: Vec::new(), visited: false }).visited = true;
|
||||
graph.get_mut(active_node).unwrap().visited = true;
|
||||
|
||||
for neighbour in graph.get(active_node).expect("PANIK").neigbours.clone() {
|
||||
|
||||
let old_distance = graph.get(&neighbour).expect("PANIK").distance;
|
||||
let new_distance = distance + weight_vec[neighbour.1][neighbour.0];
|
||||
if new_distance < old_distance.to_owned() {
|
||||
graph.get_mut(&neighbour).unwrap().distance = new_distance;
|
||||
graph.get_mut(&neighbour).unwrap().previous = Some(active_node.to_owned());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let distance = graph.get(&end_node).unwrap().distance;
|
||||
|
||||
println!("{}", distance);
|
||||
}
|
||||
Reference in New Issue
Block a user