implement FromIterator for Grid
This commit is contained in:
parent
49fac3e0a9
commit
804345629a
|
@ -9,38 +9,32 @@ fn read_input() -> String {
|
|||
}
|
||||
|
||||
fn parse_input(raw: &str) -> Parsed {
|
||||
// TODO: implement FromIterator for Grid
|
||||
Grid {
|
||||
fields: raw
|
||||
.lines()
|
||||
.enumerate()
|
||||
.flat_map(move |(y, l)| l.bytes().enumerate().map(move |(x, b)| ((x, y, 0).into(), b.into())))
|
||||
.collect(),
|
||||
}
|
||||
raw.lines()
|
||||
.enumerate()
|
||||
.flat_map(move |(y, l)| l.bytes().enumerate().map(move |(x, b)| ((x, y, 0).into(), b.into())))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_live_neighbors(p: &Position3D, grid: &Parsed) -> usize {
|
||||
p.neighbors().iter().filter(|&n| grid.get(*n) == Cell::Alive).count()
|
||||
}
|
||||
|
||||
fn make_step(mut input: Parsed) -> Parsed {
|
||||
fn make_step(input: Parsed) -> Parsed {
|
||||
let readonly = input.clone();
|
||||
Grid {
|
||||
fields: input
|
||||
.fields
|
||||
.keys()
|
||||
.flat_map(|p| p.neighbors())
|
||||
.map(|pos| {
|
||||
let cell = readonly.get(pos);
|
||||
let new = match (&cell, count_live_neighbors(&pos, &readonly)) {
|
||||
(Cell::Alive, 2..=3) => Cell::Alive,
|
||||
(Cell::Dead, 3) => Cell::Alive,
|
||||
_ => Cell::Dead,
|
||||
};
|
||||
(pos, new)
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
input
|
||||
.fields
|
||||
.keys()
|
||||
.flat_map(|p| p.neighbors())
|
||||
.map(|pos| {
|
||||
let cell = readonly.get(pos);
|
||||
let new = match (&cell, count_live_neighbors(&pos, &readonly)) {
|
||||
(Cell::Alive, 2..=3) => Cell::Alive,
|
||||
(Cell::Dead, 3) => Cell::Alive,
|
||||
_ => Cell::Dead,
|
||||
};
|
||||
(pos, new)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(parsed: &Parsed) -> usize {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use impl_ops::*;
|
||||
use itertools::{join, Itertools, iproduct};
|
||||
use itertools::{iproduct, join, Itertools};
|
||||
use std::{
|
||||
collections::HashMap, convert::TryInto, fmt::{self, Display, Formatter}, hash::{BuildHasher, Hash}, ops, ops::AddAssign
|
||||
};
|
||||
|
@ -84,6 +84,14 @@ impl<P: Position + Eq + Hash, T: Display + Default + Copy> Grid<P, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<P: Position + Hash + Eq, T: Display + Default> std::iter::FromIterator<(P, T)> for Grid<P, T> {
|
||||
fn from_iter<I: IntoIterator<Item = (P, T)>>(iter: I) -> Self {
|
||||
Grid {
|
||||
fields: iter.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Display + Default + Copy> Grid<Position2D, T> {
|
||||
fn draw_ascii(&self) -> String {
|
||||
draw_ascii(&self.fields)
|
||||
|
|
Loading…
Reference in New Issue
Block a user