2022 day10/rust: cleanup

This commit is contained in:
Xiretza 2022-12-12 20:00:20 +01:00
parent 20981716c3
commit b5c941c684

View file

@ -1,6 +1,6 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
use std::io::{stdin, Read}; use std::io::stdin;
use petgraph::{ use petgraph::{
algo::k_shortest_path, algo::k_shortest_path,
@ -21,16 +21,14 @@ struct NodeData {
} }
fn main() { fn main() {
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
// graph of "downhill" edges - edge from A to B means that A is reachable from B // graph of "downhill" edges - edge from A to B means that A is reachable from B
let mut graph = Graph::<NodeData, (), _>::new(); let mut graph = Graph::<NodeData, (), _>::new();
let grid: Vec<Vec<NodeIndex>> = data let grid: Vec<Vec<NodeIndex>> = stdin()
.lines() .lines()
.map(|line| { .map(|line| {
line.chars() line.unwrap()
.chars()
.map(|c| { .map(|c| {
let (kind, c) = match c { let (kind, c) = match c {
'S' => (Some(NodeKind::Start), 'a'), 'S' => (Some(NodeKind::Start), 'a'),
@ -52,13 +50,14 @@ fn main() {
let x = i32::try_from(x).unwrap(); let x = i32::try_from(x).unwrap();
let y = i32::try_from(y).unwrap(); let y = i32::try_from(y).unwrap();
let neighbours = [(-1i32, 0), (0, -1), (1, 0), (0, 1)] let neighbours =
.into_iter() [(-1, 0), (0, -1), (1, 0), (0, 1)]
.filter_map(|(dx, dy)| { .into_iter()
let x = usize::try_from(x + dx).ok()?; .filter_map(|(dx, dy)| {
let y = usize::try_from(y + dy).ok()?; let x = usize::try_from(x + dx).ok()?;
grid.get(x)?.get(y) let y = usize::try_from(y + dy).ok()?;
}); grid.get(x)?.get(y)
});
for &neighbour_id in neighbours { for &neighbour_id in neighbours {
let neighbour = graph[neighbour_id]; let neighbour = graph[neighbour_id];