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 {
|
fn parse_input(raw: &str) -> Parsed {
|
||||||
// TODO: implement FromIterator for Grid
|
raw.lines()
|
||||||
Grid {
|
.enumerate()
|
||||||
fields: raw
|
.flat_map(move |(y, l)| l.bytes().enumerate().map(move |(x, b)| ((x, y, 0).into(), b.into())))
|
||||||
.lines()
|
.collect()
|
||||||
.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 {
|
fn count_live_neighbors(p: &Position3D, grid: &Parsed) -> usize {
|
||||||
p.neighbors().iter().filter(|&n| grid.get(*n) == Cell::Alive).count()
|
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();
|
let readonly = input.clone();
|
||||||
Grid {
|
input
|
||||||
fields: input
|
.fields
|
||||||
.fields
|
.keys()
|
||||||
.keys()
|
.flat_map(|p| p.neighbors())
|
||||||
.flat_map(|p| p.neighbors())
|
.map(|pos| {
|
||||||
.map(|pos| {
|
let cell = readonly.get(pos);
|
||||||
let cell = readonly.get(pos);
|
let new = match (&cell, count_live_neighbors(&pos, &readonly)) {
|
||||||
let new = match (&cell, count_live_neighbors(&pos, &readonly)) {
|
(Cell::Alive, 2..=3) => Cell::Alive,
|
||||||
(Cell::Alive, 2..=3) => Cell::Alive,
|
(Cell::Dead, 3) => Cell::Alive,
|
||||||
(Cell::Dead, 3) => Cell::Alive,
|
_ => Cell::Dead,
|
||||||
_ => Cell::Dead,
|
};
|
||||||
};
|
(pos, new)
|
||||||
(pos, new)
|
})
|
||||||
})
|
.collect()
|
||||||
.collect(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(parsed: &Parsed) -> usize {
|
fn part1(parsed: &Parsed) -> usize {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use impl_ops::*;
|
use impl_ops::*;
|
||||||
use itertools::{join, Itertools, iproduct};
|
use itertools::{iproduct, join, Itertools};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap, convert::TryInto, fmt::{self, Display, Formatter}, hash::{BuildHasher, Hash}, ops, ops::AddAssign
|
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> {
|
impl<T: Display + Default + Copy> Grid<Position2D, T> {
|
||||||
fn draw_ascii(&self) -> String {
|
fn draw_ascii(&self) -> String {
|
||||||
draw_ascii(&self.fields)
|
draw_ascii(&self.fields)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user