diff --git a/2020/src/bin/day17.rs b/2020/src/bin/day17.rs index f7efcc2..c9945d3 100644 --- a/2020/src/bin/day17.rs +++ b/2020/src/bin/day17.rs @@ -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 { diff --git a/2020/src/grid.rs b/2020/src/grid.rs index 898287b..d28a1a4 100644 --- a/2020/src/grid.rs +++ b/2020/src/grid.rs @@ -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 Grid { } } +impl std::iter::FromIterator<(P, T)> for Grid { + fn from_iter>(iter: I) -> Self { + Grid { + fields: iter.into_iter().collect(), + } + } +} + impl Grid { fn draw_ascii(&self) -> String { draw_ascii(&self.fields)