58 lines
1.9 KiB
Rust
58 lines
1.9 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::fs::File;
|
|
use std::io::{BufReader};
|
|
use std::io::prelude::*;
|
|
|
|
const RADIX: u32 = 10;
|
|
|
|
static mut shortest: u32 = 60000;
|
|
|
|
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 visited_local.contains(&(x,y)) || current_weight > shortest || visited_local.len() > 1000{
|
|
//println!("Breaking at {}, {}, {}", current_weight, visited_local.len(), shortest);
|
|
return;
|
|
}
|
|
}
|
|
if x == width - 1 && y == height - 1 {
|
|
unsafe {shortest = current_weight;
|
|
println!("Current length: {}, shortest now: {}", current_weight, shortest);}
|
|
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); }
|
|
} |