Finished writing projecteuler 083 in a few minutes by basically copying 082

This commit is contained in:
2023-02-08 23:57:05 +01:00
parent f00e13bf26
commit 72f1368eda
4 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
use std::collections::HashMap;
use nom::{
bytes::streaming::tag,
character::complete::{self, multispace1},
multi::separated_list1,
IResult,
};
use pathfinding::prelude::dijkstra;
fn parse_input(input: &str) -> IResult<&str, Vec<Vec<u32>>> {
let (input, matrix) =
separated_list1(multispace1, separated_list1(tag(","), complete::u32))(input)?;
Ok((input, matrix))
}
fn main() {
println!("Hello, this is Patrick");
let input_text = include_str!("../matrix.txt");
let (_input, matrix) = parse_input(input_text).unwrap();
let l = matrix[0].len();
let mut points: HashMap<(i32, i32), u32> = HashMap::new();
for (row_index, row) in matrix.into_iter().enumerate() {
for (column_index, cell) in row.into_iter().enumerate() {
points.insert((column_index as i32, row_index as i32), cell);
}
}
let minimal_path: Option<(Vec<(i32, i32)>, u32)> = dijkstra(
&(i32::MAX, i32::MAX),
|&(column, row)| match (column, row) {
(i32::MAX, i32::MAX) => vec![(0, 0)]
.into_iter()
.filter_map(|coords| points.get_key_value(&coords))
.map(|(&coords, &cost)| (coords, cost))
.collect::<Vec<((i32, i32), u32)>>(),
(column, row) => vec![
(column, row - 1),
(column, row + 1),
(column + 1, row),
(column - 1, row),
]
.into_iter()
.filter_map(|coords| points.get_key_value(&coords))
.map(|(&coords, &cost)| (coords, cost))
.collect(),
},
|&(column, row)| column == l as i32 - 1 && row == l as i32 - 1,
);
println!("The minimal path is: {}", minimal_path.unwrap().1);
}