diff --git a/src/main.rs b/src/main.rs index 384468d..188599c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ #![feature(test)] + +use std::fmt::{Display, Write}; extern crate test; const INPUTS: [&'static str; 2] = [ @@ -12,6 +14,15 @@ enum NodeType { Empty, } +impl Display for NodeType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + NodeType::Dot => f.write_char('\u{2588}'), + NodeType::Empty => f.write_char('.'), + } + } +} + impl NodeType { #[inline] const fn or(&self, rhs: &NodeType) -> NodeType { @@ -71,7 +82,7 @@ fn parse_input(input: &'static str) -> (Vec, Vec) { (points, folds) } -fn solution((points, folds): (Vec, Vec)) -> u64 { +fn solution((points, folds): (Vec, Vec)) -> Vec> { let mut y_max = 0; let mut x_max = 0; for point in points.iter() { @@ -94,6 +105,8 @@ fn solution((points, folds): (Vec, Vec)) -> u64 { let mirror = std::mem::replace(&mut grid[j][n - i - 1], NodeType::Empty); grid[j][i] = grid[j][i].or(&mirror); } + + grid[j] = grid[j].iter().take(position).cloned().collect(); } } Fold::Y(position) => { @@ -104,28 +117,25 @@ fn solution((points, folds): (Vec, Vec)) -> u64 { grid[j][i] = grid[j][i].or(&mirror); } } + + grid = grid.into_iter().take(position).collect(); } } - - let mut sum = 0; - for row in grid { - sum += row.into_iter().fold(0, |a, x| match x { - NodeType::Dot => a + 1, - NodeType::Empty => a, - }) - } - - return sum; } - 0 + grid } fn main() { for input in INPUTS { let input = parse_input(input); - let result = solution(input); - println!("Result {}", result); + let grid = solution(input); + for row in grid.iter() { + for c in row { + print!("{}", c); + } + println!("") + } } }