Day 15
This commit is contained in:
parent
290789d5f5
commit
ea1bbcc089
4 changed files with 246 additions and 6 deletions
|
|
@ -1,6 +1,8 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
pub enum Direction {
|
||||
North,
|
||||
South,
|
||||
|
|
@ -12,12 +14,25 @@ impl Direction {
|
|||
pub fn all_variants() -> Vec<Self> {
|
||||
vec![Self::North, Self::East, Self::West, Self::South]
|
||||
}
|
||||
|
||||
pub fn opposite(self) -> Self {
|
||||
match self {
|
||||
Self::North => Self::South,
|
||||
Self::East => Self::West,
|
||||
Self::West => Self::East,
|
||||
Self::South => Self::North,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_parallel(self, other: Self) -> bool {
|
||||
self == other || self == other.opposite()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Map<T>
|
||||
where
|
||||
T: Copy,
|
||||
T: Copy + Display,
|
||||
{
|
||||
pub map: Vec<Vec<T>>,
|
||||
}
|
||||
|
|
@ -36,7 +51,7 @@ impl Map<char> {
|
|||
|
||||
impl<T> Map<T>
|
||||
where
|
||||
T: Copy,
|
||||
T: Copy + Display,
|
||||
{
|
||||
pub fn from_2d_vec(map: Vec<Vec<T>>) -> Self {
|
||||
Self { map }
|
||||
|
|
@ -69,13 +84,23 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, x: usize, y: usize) -> Option<T> {
|
||||
Some(*self.map.get(y)?.get(x)?)
|
||||
}
|
||||
|
||||
pub fn travel_get(&self, x: usize, y: usize, direction: Direction) -> Option<T> {
|
||||
let (nx, ny) = self.travel(x, y, direction)?;
|
||||
self.get(nx, ny)
|
||||
}
|
||||
|
||||
pub fn get(&self, x: usize, y: usize) -> Option<T> {
|
||||
Some(*self.map.get(y)?.get(x)?)
|
||||
pub fn travel_and_get(
|
||||
&self,
|
||||
x: usize,
|
||||
y: usize,
|
||||
direction: Direction,
|
||||
) -> Option<(usize, usize, T)> {
|
||||
let (nx, ny) = self.travel(x, y, direction)?;
|
||||
Some((nx, ny, self.get(nx, ny)?))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: usize, y: usize, value: T) {
|
||||
|
|
@ -98,6 +123,10 @@ where
|
|||
self.map.iter().flat_map(|row| row.iter())
|
||||
}
|
||||
|
||||
pub fn iter_rows(&self) -> impl Iterator<Item = &Vec<T>> {
|
||||
self.map.iter()
|
||||
}
|
||||
|
||||
pub fn coordinates(&self) -> impl Iterator<Item = (usize, usize)> {
|
||||
(0..self.width()).cartesian_product(0..self.height())
|
||||
}
|
||||
|
|
@ -106,3 +135,21 @@ where
|
|||
self.coordinates().map(|(x, y)| (x, y, &self.map[y][x]))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Display for Map<T>
|
||||
where
|
||||
T: Copy + Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut final_string = "".to_string();
|
||||
for row in self.map.iter() {
|
||||
let s = row.iter().fold("".to_string(), |mut acc, e| {
|
||||
acc.push_str(&format!("{}", e));
|
||||
acc
|
||||
});
|
||||
final_string.push_str("\n");
|
||||
final_string.push_str(&s);
|
||||
}
|
||||
f.write_str(&final_string)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue