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

@@ -51,7 +51,7 @@ impl From<Map<FerryLocation>> for EvolvingMap {
EvolvingMap { EvolvingMap {
next_map: Some(start_map), next_map: Some(start_map),
occupation_tolerance: 4, 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(); let mut next_map = current_map.clone();
for (x, y, value) in current_map.locations() { 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 { match value {
FerryLocation::EmptySeat if occupied_neighbors == 0 => { FerryLocation::EmptySeat if occupied_neighbors == 0 => {
next_map.set(x, y, FerryLocation::TakenSeat).ok()?; next_map.set(x, y, FerryLocation::TakenSeat).ok()?;
@@ -96,7 +100,10 @@ fn main() -> Result<(),TopLevelError> {
for (idx, step) in base_evolving_map.enumerate() { for (idx, step) in base_evolving_map.enumerate() {
println!("Base map, step #{}", idx); println!("Base map, step #{}", idx);
step.print(); step.print();
println!("# of occupied seats: {}\n", step.count(FerryLocation::TakenSeat)); println!(
"# of occupied seats: {}\n",
step.count(FerryLocation::TakenSeat)
);
} }
// part 2 // part 2
@@ -108,7 +115,10 @@ fn main() -> Result<(),TopLevelError> {
for (idx, step) in view_evolving_map.enumerate() { for (idx, step) in view_evolving_map.enumerate() {
println!("Extended map, step #{}", idx); println!("Extended map, step #{}", idx);
step.print(); step.print();
println!("# of occupied seats: {}\n", step.count(FerryLocation::TakenSeat)); println!(
"# of occupied seats: {}\n",
step.count(FerryLocation::TakenSeat)
);
} }
Ok(()) Ok(())

View File

@@ -62,12 +62,16 @@ impl<X: Clone> Map<X> {
let mut sy = (y as isize) + rise; let mut sy = (y as isize) + rise;
loop { loop {
if sx < 0 { return None; } if sx < 0 {
if sy < 0 { return None; } return None;
}
if sy < 0 {
return None;
}
let entry = self.at_unwrapped(sx as usize, sy as usize)?; let entry = self.at_unwrapped(sx as usize, sy as usize)?;
if f(&entry) { if f(&entry) {
return Some(entry) return Some(entry);
} }
sx += run; sx += run;
@@ -76,7 +80,10 @@ impl<X: Clone> Map<X> {
} }
pub fn set(&mut self, x: usize, y: usize, value: X) -> Result<(), MapOperationError> { 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 { if x >= self.width {
return Err(MapOperationError::OutOfBounds(x, y)); return Err(MapOperationError::OutOfBounds(x, y));
@@ -91,8 +98,12 @@ impl<X: Clone> Map<X> {
self.adjacents_until(x, y, |_| true) 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 { if y >= self.height {
return Err(MapOperationError::OutOfBounds(x, y)); 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, 0));
push_some(&mut results, self.view(f, x, y, 1, 1)); push_some(&mut results, self.view(f, x, y, 1, 1));
Ok(results) Ok(results)
} }
@@ -173,9 +183,7 @@ impl<X: Clone + Into<char>> Map<X> {
impl<X: Clone + PartialEq> PartialEq<Map<X>> for Map<X> { impl<X: Clone + PartialEq> PartialEq<Map<X>> for Map<X> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.height == other.height && self.height == other.height && self.width == other.width && self.data == other.data
self.width == other.width &&
self.data == other.data
} }
} }