Clean up some formatting.
This commit is contained in:
@@ -20,7 +20,7 @@ impl FerryLocation {
|
|||||||
impl TryFrom<char> for FerryLocation {
|
impl TryFrom<char> for FerryLocation {
|
||||||
type Error = MapParseError;
|
type Error = MapParseError;
|
||||||
|
|
||||||
fn try_from(c: char) -> Result<Self,Self::Error> {
|
fn try_from(c: char) -> Result<Self, Self::Error> {
|
||||||
match c {
|
match c {
|
||||||
'.' => Ok(FerryLocation::Floor),
|
'.' => Ok(FerryLocation::Floor),
|
||||||
'L' => Ok(FerryLocation::EmptySeat),
|
'L' => Ok(FerryLocation::EmptySeat),
|
||||||
@@ -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)(¤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 {
|
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()?;
|
||||||
@@ -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 filename = env::args().nth(1).expect("No file argument given.");
|
||||||
let contents = fs::read_to_string(filename)?;
|
let contents = fs::read_to_string(filename)?;
|
||||||
let map = Map::<FerryLocation>::try_from(contents.as_str())?;
|
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() {
|
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
|
||||||
let view_evolving_map = EvolvingMap {
|
let view_evolving_map = EvolvingMap {
|
||||||
next_map: Some(map),
|
next_map: Some(map),
|
||||||
occupation_tolerance: 5,
|
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() {
|
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(())
|
||||||
|
|||||||
34
src/map.rs
34
src/map.rs
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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>,
|
underlying: &'a Map<X>,
|
||||||
x: usize,
|
x: usize,
|
||||||
y: 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);
|
type Item = (usize, usize, X);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
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> {
|
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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>) {
|
fn push_some<X>(vector: &mut Vec<X>, value: Option<X>) {
|
||||||
if let Some(val) = value {
|
if let Some(val) = value {
|
||||||
|
|||||||
Reference in New Issue
Block a user