From 79ea3c29d020f992aabe90bf8afe95526b830b7b Mon Sep 17 00:00:00 2001 From: Johannes Date: Sun, 2 Oct 2022 17:49:00 +0200 Subject: [PATCH] day20 format --- src/tasks/day20.rs | 61 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/src/tasks/day20.rs b/src/tasks/day20.rs index e29eea0..2b4b035 100644 --- a/src/tasks/day20.rs +++ b/src/tasks/day20.rs @@ -44,13 +44,20 @@ struct RealPoint { impl RealPoint { fn map_point(&self) -> MapPoint { - MapPoint { x: self.x, y: self.y } + MapPoint { + x: self.x, + y: self.y, + } } } impl RealPoint { fn from(map_point: MapPoint, level: usize) -> Self { - RealPoint { x: map_point.x, y: map_point.y, level } + RealPoint { + x: map_point.x, + y: map_point.y, + level, + } } } @@ -58,7 +65,9 @@ trait Field { fn neighbors(&self, at_level: usize) -> Vec; fn set_label_partner(&mut self, point: MapPoint); fn set_neighbors(&mut self, neighbors: Vec); - fn curried_factory(width: C, height: C) -> FieldFactory where Self: Sized; + fn curried_factory(width: C, height: C) -> FieldFactory + where + Self: Sized; } struct PortalField { @@ -73,7 +82,10 @@ impl PortalField { impl Field for PortalField { fn neighbors(&self, _: usize) -> Vec { - self.neighbors.iter().map(|p| RealPoint::from(*p, 0)).collect() + self.neighbors + .iter() + .map(|p| RealPoint::from(*p, 0)) + .collect() } fn set_label_partner(&mut self, point: MapPoint) { @@ -106,7 +118,11 @@ impl DimensionField { impl Field for DimensionField { fn neighbors(&self, at_level: usize) -> Vec { - let mut result: Vec = self.neighbors.iter().map(|n| RealPoint::from(*n, at_level)).collect(); + let mut result: Vec = self + .neighbors + .iter() + .map(|n| RealPoint::from(*n, at_level)) + .collect(); if let Some(destination) = self.jump { if self.is_inward() { result.push(RealPoint::from(destination, at_level + 1)); @@ -127,7 +143,15 @@ impl Field for DimensionField { } fn curried_factory(width: C, height: C) -> FieldFactory { - Box::new(move |point: &MapPoint| Box::new(Self { point: *point, neighbors: vec![], jump: None, map_width: width, map_height: height })) + Box::new(move |point: &MapPoint| { + Box::new(Self { + point: *point, + neighbors: vec![], + jump: None, + map_width: width, + map_height: height, + }) + }) } } @@ -150,7 +174,11 @@ impl Maze { let start = labels["AA"][0]; let finish = labels["ZZ"][0]; - Maze { map, start: RealPoint::from(start, 0), finish: RealPoint::from(finish, 0) } + Maze { + map, + start: RealPoint::from(start, 0), + finish: RealPoint::from(finish, 0), + } } fn shortest_path(&self) -> usize { @@ -163,12 +191,20 @@ impl Maze { continue; } - map.get_mut(&points[0]).unwrap().set_label_partner(points[1]); - map.get_mut(&points[1]).unwrap().set_label_partner(points[0]); + map.get_mut(&points[0]) + .unwrap() + .set_label_partner(points[1]); + map.get_mut(&points[1]) + .unwrap() + .set_label_partner(points[0]); } } - fn labels(moc: &HashMap, width: C, height: C) -> HashMap> { + fn labels( + moc: &HashMap, + width: C, + height: C, + ) -> HashMap> { let horizontal: Vec<(String, MapPoint)> = (0..width - 2) .into_iter() .flat_map(|x| { @@ -240,7 +276,10 @@ impl Maze { }) } - fn create_map_of_free_spots(moc: &HashMap, field_factory: FieldFactory) -> FieldMap { + fn create_map_of_free_spots( + moc: &HashMap, + field_factory: FieldFactory, + ) -> FieldMap { moc.keys() .filter(|p| moc[p] == '.') .map(|p| (*p, field_factory(p)))