day20 format
This commit is contained in:
@@ -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<RealPoint>;
|
||||
fn set_label_partner(&mut self, point: 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 {
|
||||
@@ -73,7 +82,10 @@ impl PortalField {
|
||||
|
||||
impl Field for PortalField {
|
||||
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) {
|
||||
@@ -106,7 +118,11 @@ impl DimensionField {
|
||||
|
||||
impl Field for DimensionField {
|
||||
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 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<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)
|
||||
.into_iter()
|
||||
.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()
|
||||
.filter(|p| moc[p] == '.')
|
||||
.map(|p| (*p, field_factory(p)))
|
||||
|
||||
Reference in New Issue
Block a user