diff --git a/src/euler81/src/main.rs b/src/euler81/src/main.rs index d5cfcb5..9d1eab2 100644 --- a/src/euler81/src/main.rs +++ b/src/euler81/src/main.rs @@ -1,108 +1,82 @@ +use std::cell::RefCell; +use std::collections::HashSet; use std::fs; +use std::iter::Cycle; use std::path::Path; +use std::rc::Rc; const LENGTH: usize = 80; -struct Matrix { - data: Vec>, -} +type Coordinate = (usize, usize); -#[derive(Clone, Eq, PartialEq, Hash)] -enum Direction { - Down, - Right, +struct Matrix { + data: Vec>, } impl Matrix { fn load(f: &Path) -> Self { - let data: Vec> = fs::read_to_string(f) + let mut data: Vec> = fs::read_to_string(f) .unwrap() .split_whitespace() .map(|line| { line.split(',') .into_iter() .map(|number| number.parse::().unwrap()) + .map(Node::from) .collect() }) .collect(); + data[0][0].is_infinity = false; assert_eq!(data.len(), LENGTH); assert!(data.iter().all(|subvec| subvec.len() == LENGTH)); Self { data } } - - fn calculate_sum_along_route(&self, route: &[Direction]) -> u32 { - assert_eq!(route.len() / 2, LENGTH - 1); - let mut i = 0; - let mut j = 0; - let mut sum = self.data[i][j]; - for step in route { - match step { - Direction::Down => { - i += 1; - } - Direction::Right => { - j += 1; - } - } - sum += self.data[i][j]; - } - sum - } } fn main() { let matrix = Matrix::load(Path::new("p081_matrix.txt")); - let permutations = calculate_permutations(LENGTH - 1); - assert_eq!(permutations.len(), (LENGTH - 1).pow(2)); - let mut sums = vec![]; - - for route in permutations.into_iter() { - let sum = matrix.calculate_sum_along_route(&route); - sums.push(sum); - } - sums.sort_unstable(); - println!("{}", sums.last().unwrap()); + let dijkstra = Dijkstra::new(matrix); } -fn calculate_permutations(length: usize) -> Vec> { - let mut base = vec![Direction::Down; length]; - base.append(&mut vec![Direction::Right; length]); - generate2(length, base) +struct Dijkstra { + initial: Coordinate, + matrix: Matrix, + unvisited: Vec, } -/// https://en.wikipedia.org/wiki/Heap%27s_algorithm -/// produces OOM, or is otherwise too slow -fn generate(length: usize, mut vec: Vec) -> Vec> { - let mut result = vec![vec.clone()]; - let mut c = vec![0; length]; - - let mut i = 1; - while i < length { - if c[i] < i { - if i % 2 == 0 { - vec.swap(0, i); - } else { - vec.swap(c[i], i) - } - result.push(vec.clone()); - - c[i] += 1; - i = 1; - } else { - c[i] = 0; - i += 1; +impl Dijkstra { + fn new(matrix: Matrix) -> Self { + let initial = (0, 0); + let unvisited: Vec<_> = (0..LENGTH) + .flat_map(|x| (0..LENGTH).zip([x].into_iter().cycle())) + .filter(|value| *value != (0, 0)) + .collect(); + assert_eq!(unvisited.len(), LENGTH * LENGTH - 1); + Self { + initial, + matrix, + unvisited, } } - - result } -fn generate2(length: usize, mut vec: Vec) -> Vec> { - let orig = vec.clone(); - let mut result = vec![vec.clone()]; - for i in 1..=length { - // vec.remove - todo!() +#[derive(PartialEq, Eq, Hash)] +struct Node { + is_infinity: bool, + value: u32, +} + +impl From for Node { + fn from(value: u32) -> Self { + Node { + is_infinity: true, + value, + } + } +} + +impl From for Rc> { + fn from(node: Node) -> Self { + Rc::new(RefCell::new(node)) } - result }