This commit is contained in:
2021-12-15 11:45:11 +01:00
parent 10bd7c4997
commit 16c6f4bcc3
4 changed files with 180 additions and 0 deletions

62
december_15/src/main.rs Normal file
View File

@@ -0,0 +1,62 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
const RADIX: u32 = 10;
static mut shortest: u32 = 99999;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut weight_vec = Vec::new();
for line in reader.lines() {
let mut temp_vec = Vec::new();
for symbol in line.unwrap().chars() {
let symbol_as_int = symbol.to_digit(RADIX).unwrap();
temp_vec.push(symbol_as_int)
}
weight_vec.push(temp_vec);
}
let height = weight_vec.len();
let width = weight_vec[0].len();
let visited: Vec<(usize, usize)> = Vec::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;
}
}
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); }
}