diff --git a/src/bin/ferry.rs b/src/bin/ferry.rs index c9317ce..d2247c0 100644 --- a/src/bin/ferry.rs +++ b/src/bin/ferry.rs @@ -20,7 +20,7 @@ impl FerryLocation { impl TryFrom for FerryLocation { type Error = MapParseError; - fn try_from(c: char) -> Result { + fn try_from(c: char) -> Result { match c { '.' => Ok(FerryLocation::Floor), 'L' => Ok(FerryLocation::EmptySeat), @@ -51,8 +51,8 @@ impl From> 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)(¤t_map, x, y).ok()?.iter().filter(|x| **x == FerryLocation::TakenSeat).count(); + let occupied_neighbors = (self.view)(¤t_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::::try_from(contents.as_str())?; @@ -96,20 +100,26 @@ 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(()) -} \ No newline at end of file +} diff --git a/src/errors.rs b/src/errors.rs index 6edabca..69d7be3 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -139,4 +139,4 @@ pub enum ExecutionError { pub enum MapOperationError { #[error("Out of bounds indexing map with ({0},{1})")] OutOfBounds(usize, usize), -} \ No newline at end of file +} diff --git a/src/map.rs b/src/map.rs index 7c94409..6cc12c2 100644 --- a/src/map.rs +++ b/src/map.rs @@ -62,12 +62,16 @@ impl Map { 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 Map { } 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,12 +98,16 @@ impl Map { self.adjacents_until(x, y, |_| true) } - pub fn adjacents_until(&self, x: usize, y: usize, f: fn(&X) -> bool) -> Result, MapOperationError> - { + pub fn adjacents_until( + &self, + x: usize, + y: usize, + f: fn(&X) -> bool, + ) -> Result, MapOperationError> { if y >= self.height { return Err(MapOperationError::OutOfBounds(x, y)); } - + if x >= self.width { return Err(MapOperationError::OutOfBounds(x, y)); } @@ -111,7 +122,6 @@ impl Map { 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 Map { } } -pub struct MapLocations<'a,X: Clone> { +pub struct MapLocations<'a, X: Clone> { underlying: &'a Map, 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 { @@ -173,16 +183,14 @@ impl> Map { impl PartialEq> for Map { 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 Eq for Map { } +impl Eq for Map {} fn push_some(vector: &mut Vec, value: Option) { if let Some(val) = value { vector.push(val); } -} \ No newline at end of file +}