Clean up some formatting.

This commit is contained in:
2020-12-11 10:39:25 -08:00
parent 806f86214b
commit 3e31c00505
3 changed files with 43 additions and 25 deletions

View File

@@ -20,7 +20,7 @@ impl FerryLocation {
impl TryFrom<char> for FerryLocation {
type Error = MapParseError;
fn try_from(c: char) -> Result<Self,Self::Error> {
fn try_from(c: char) -> Result<Self, Self::Error> {
match c {
'.' => Ok(FerryLocation::Floor),
'L' => Ok(FerryLocation::EmptySeat),
@@ -51,8 +51,8 @@ impl From<Map<FerryLocation>> for EvolvingMap {
EvolvingMap {
next_map: Some(start_map),
occupation_tolerance: 4,
view: |m, x, y| { m.adjacents(x, y) },
}
view: |m, x, y| m.adjacents(x, y),
}
}
}
@@ -64,7 +64,11 @@ impl Iterator for EvolvingMap {
let mut next_map = current_map.clone();
for (x, y, value) in current_map.locations() {
let occupied_neighbors = (self.view)(&current_map, x, y).ok()?.iter().filter(|x| **x == FerryLocation::TakenSeat).count();
let occupied_neighbors = (self.view)(&current_map, x, y)
.ok()?
.iter()
.filter(|x| **x == FerryLocation::TakenSeat)
.count();
match value {
FerryLocation::EmptySeat if occupied_neighbors == 0 => {
next_map.set(x, y, FerryLocation::TakenSeat).ok()?;
@@ -86,7 +90,7 @@ impl Iterator for EvolvingMap {
}
}
fn main() -> Result<(),TopLevelError> {
fn main() -> Result<(), TopLevelError> {
let filename = env::args().nth(1).expect("No file argument given.");
let contents = fs::read_to_string(filename)?;
let map = Map::<FerryLocation>::try_from(contents.as_str())?;
@@ -96,19 +100,25 @@ fn main() -> Result<(),TopLevelError> {
for (idx, step) in base_evolving_map.enumerate() {
println!("Base map, step #{}", idx);
step.print();
println!("# of occupied seats: {}\n", step.count(FerryLocation::TakenSeat));
println!(
"# of occupied seats: {}\n",
step.count(FerryLocation::TakenSeat)
);
}
// part 2
let view_evolving_map = EvolvingMap {
next_map: Some(map),
occupation_tolerance: 5,
view: |m,x,y| m.adjacents_until(x, y, FerryLocation::is_seat),
view: |m, x, y| m.adjacents_until(x, y, FerryLocation::is_seat),
};
for (idx, step) in view_evolving_map.enumerate() {
println!("Extended map, step #{}", idx);
step.print();
println!("# of occupied seats: {}\n", step.count(FerryLocation::TakenSeat));
println!(
"# of occupied seats: {}\n",
step.count(FerryLocation::TakenSeat)
);
}
Ok(())

View File

@@ -62,12 +62,16 @@ impl<X: Clone> Map<X> {
let mut sy = (y as isize) + rise;
loop {
if sx < 0 { return None; }
if sy < 0 { return None; }
if sx < 0 {
return None;
}
if sy < 0 {
return None;
}
let entry = self.at_unwrapped(sx as usize, sy as usize)?;
if f(&entry) {
return Some(entry)
return Some(entry);
}
sx += run;
@@ -76,7 +80,10 @@ impl<X: Clone> Map<X> {
}
pub fn set(&mut self, x: usize, y: usize, value: X) -> Result<(), MapOperationError> {
let row = self.data.get_mut(y).ok_or(MapOperationError::OutOfBounds(x, y))?;
let row = self
.data
.get_mut(y)
.ok_or(MapOperationError::OutOfBounds(x, y))?;
if x >= self.width {
return Err(MapOperationError::OutOfBounds(x, y));
@@ -91,8 +98,12 @@ impl<X: Clone> Map<X> {
self.adjacents_until(x, y, |_| true)
}
pub fn adjacents_until(&self, x: usize, y: usize, f: fn(&X) -> bool) -> Result<Vec<X>, MapOperationError>
{
pub fn adjacents_until(
&self,
x: usize,
y: usize,
f: fn(&X) -> bool,
) -> Result<Vec<X>, MapOperationError> {
if y >= self.height {
return Err(MapOperationError::OutOfBounds(x, y));
}
@@ -111,7 +122,6 @@ impl<X: Clone> Map<X> {
push_some(&mut results, self.view(f, x, y, 1, 0));
push_some(&mut results, self.view(f, x, y, 1, 1));
Ok(results)
}
@@ -130,13 +140,13 @@ impl<X: Clone + PartialEq> Map<X> {
}
}
pub struct MapLocations<'a,X: Clone> {
pub struct MapLocations<'a, X: Clone> {
underlying: &'a Map<X>,
x: usize,
y: usize,
}
impl<'a, X: Clone> Iterator for MapLocations<'a,X> {
impl<'a, X: Clone> Iterator for MapLocations<'a, X> {
type Item = (usize, usize, X);
fn next(&mut self) -> Option<Self::Item> {
@@ -173,13 +183,11 @@ impl<X: Clone + Into<char>> Map<X> {
impl<X: Clone + PartialEq> PartialEq<Map<X>> for Map<X> {
fn eq(&self, other: &Self) -> bool {
self.height == other.height &&
self.width == other.width &&
self.data == other.data
self.height == other.height && self.width == other.width && self.data == other.data
}
}
impl<X: Clone + PartialEq + Eq> Eq for Map<X> { }
impl<X: Clone + PartialEq + Eq> Eq for Map<X> {}
fn push_some<X>(vector: &mut Vec<X>, value: Option<X>) {
if let Some(val) = value {