add more code for 81 (WIP)

This commit is contained in:
Dr. Matthias Ratajczak
2022-09-22 16:08:18 +02:00
parent 972294470c
commit 6944016d73
+43 -69
View File
@@ -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<Vec<u32>>,
}
type Coordinate = (usize, usize);
#[derive(Clone, Eq, PartialEq, Hash)]
enum Direction {
Down,
Right,
struct Matrix {
data: Vec<Vec<Node>>,
}
impl Matrix {
fn load(f: &Path) -> Self {
let data: Vec<Vec<u32>> = fs::read_to_string(f)
let mut data: Vec<Vec<_>> = fs::read_to_string(f)
.unwrap()
.split_whitespace()
.map(|line| {
line.split(',')
.into_iter()
.map(|number| number.parse::<u32>().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<Vec<Direction>> {
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<Coordinate>,
}
/// https://en.wikipedia.org/wiki/Heap%27s_algorithm
/// produces OOM, or is otherwise too slow
fn generate(length: usize, mut vec: Vec<Direction>) -> Vec<Vec<Direction>> {
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)
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.push(vec.clone());
c[i] += 1;
i = 1;
} else {
c[i] = 0;
i += 1;
}
}
result
#[derive(PartialEq, Eq, Hash)]
struct Node {
is_infinity: bool,
value: u32,
}
fn generate2(length: usize, mut vec: Vec<Direction>) -> Vec<Vec<Direction>> {
let orig = vec.clone();
let mut result = vec![vec.clone()];
for i in 1..=length {
// vec.remove
todo!()
impl From<u32> for Node {
fn from(value: u32) -> Self {
Node {
is_infinity: true,
value,
}
}
}
impl From<Node> for Rc<RefCell<Node>> {
fn from(node: Node) -> Self {
Rc::new(RefCell::new(node))
}
result
}