day20 format

This commit is contained in:
2022-10-02 17:49:00 +02:00
parent e87d31c941
commit 79ea3c29d0

View File

@@ -44,13 +44,20 @@ struct RealPoint {
impl RealPoint { impl RealPoint {
fn map_point(&self) -> MapPoint { fn map_point(&self) -> MapPoint {
MapPoint { x: self.x, y: self.y } MapPoint {
x: self.x,
y: self.y,
}
} }
} }
impl RealPoint { impl RealPoint {
fn from(map_point: MapPoint, level: usize) -> Self { 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<RealPoint>; fn neighbors(&self, at_level: usize) -> Vec<RealPoint>;
fn set_label_partner(&mut self, point: MapPoint); fn set_label_partner(&mut self, point: MapPoint);
fn set_neighbors(&mut self, neighbors: Vec<MapPoint>); fn set_neighbors(&mut self, neighbors: Vec<MapPoint>);
fn curried_factory(width: C, height: C) -> FieldFactory where Self: Sized; fn curried_factory(width: C, height: C) -> FieldFactory
where
Self: Sized;
} }
struct PortalField { struct PortalField {
@@ -73,7 +82,10 @@ impl PortalField {
impl Field for PortalField { impl Field for PortalField {
fn neighbors(&self, _: usize) -> Vec<RealPoint> { fn neighbors(&self, _: usize) -> Vec<RealPoint> {
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) { fn set_label_partner(&mut self, point: MapPoint) {
@@ -106,7 +118,11 @@ impl DimensionField {
impl Field for DimensionField { impl Field for DimensionField {
fn neighbors(&self, at_level: usize) -> Vec<RealPoint> { fn neighbors(&self, at_level: usize) -> Vec<RealPoint> {
let mut result: Vec<RealPoint> = self.neighbors.iter().map(|n| RealPoint::from(*n, at_level)).collect(); let mut result: Vec<RealPoint> = self
.neighbors
.iter()
.map(|n| RealPoint::from(*n, at_level))
.collect();
if let Some(destination) = self.jump { if let Some(destination) = self.jump {
if self.is_inward() { if self.is_inward() {
result.push(RealPoint::from(destination, at_level + 1)); result.push(RealPoint::from(destination, at_level + 1));
@@ -127,7 +143,15 @@ impl Field for DimensionField {
} }
fn curried_factory(width: C, height: C) -> FieldFactory { 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 start = labels["AA"][0];
let finish = labels["ZZ"][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 { fn shortest_path(&self) -> usize {
@@ -163,12 +191,20 @@ impl Maze {
continue; continue;
} }
map.get_mut(&points[0]).unwrap().set_label_partner(points[1]); map.get_mut(&points[0])
map.get_mut(&points[1]).unwrap().set_label_partner(points[0]); .unwrap()
.set_label_partner(points[1]);
map.get_mut(&points[1])
.unwrap()
.set_label_partner(points[0]);
} }
} }
fn labels(moc: &HashMap<MapPoint, char>, width: C, height: C) -> HashMap<String, Vec<MapPoint>> { fn labels(
moc: &HashMap<MapPoint, char>,
width: C,
height: C,
) -> HashMap<String, Vec<MapPoint>> {
let horizontal: Vec<(String, MapPoint)> = (0..width - 2) let horizontal: Vec<(String, MapPoint)> = (0..width - 2)
.into_iter() .into_iter()
.flat_map(|x| { .flat_map(|x| {
@@ -240,7 +276,10 @@ impl Maze {
}) })
} }
fn create_map_of_free_spots(moc: &HashMap<MapPoint, char>, field_factory: FieldFactory) -> FieldMap { fn create_map_of_free_spots(
moc: &HashMap<MapPoint, char>,
field_factory: FieldFactory,
) -> FieldMap {
moc.keys() moc.keys()
.filter(|p| moc[p] == '.') .filter(|p| moc[p] == '.')
.map(|p| (*p, field_factory(p))) .map(|p| (*p, field_factory(p)))