From a78bb9d70afe05c8bfe02e042f5126d670980c83 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 10 Dec 2016 15:45:45 -0500 Subject: [PATCH] [2016][rust][8.x] --- 2016/input/day_08.txt | 173 ++++++++++++++++++++++++++++++++++++++++ 2016/rust/src/day_08.rs | 149 ++++++++++++++++++++++++++++++++++ 2016/rust/src/lib.rs | 2 + 2016/rust/src/main.rs | 2 +- 4 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 2016/input/day_08.txt create mode 100644 2016/rust/src/day_08.rs diff --git a/2016/input/day_08.txt b/2016/input/day_08.txt new file mode 100644 index 0000000..5603dd8 --- /dev/null +++ b/2016/input/day_08.txt @@ -0,0 +1,173 @@ +rect 1x1 +rotate row y=0 by 20 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 4 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 6 +rect 5x1 +rotate row y=0 by 2 +rect 1x3 +rotate row y=2 by 8 +rotate row y=0 by 8 +rotate column x=0 by 1 +rect 7x1 +rotate row y=2 by 24 +rotate row y=0 by 20 +rotate column x=5 by 1 +rotate column x=4 by 2 +rotate column x=2 by 2 +rotate column x=0 by 1 +rect 7x1 +rotate column x=34 by 2 +rotate column x=22 by 1 +rotate column x=15 by 1 +rotate row y=2 by 18 +rotate row y=0 by 12 +rotate column x=8 by 2 +rotate column x=7 by 1 +rotate column x=5 by 2 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 9x1 +rotate row y=3 by 28 +rotate row y=1 by 28 +rotate row y=0 by 20 +rotate column x=18 by 1 +rotate column x=15 by 1 +rotate column x=14 by 1 +rotate column x=13 by 1 +rotate column x=12 by 2 +rotate column x=10 by 3 +rotate column x=8 by 1 +rotate column x=7 by 2 +rotate column x=6 by 1 +rotate column x=5 by 1 +rotate column x=3 by 1 +rotate column x=2 by 2 +rotate column x=0 by 1 +rect 19x1 +rotate column x=34 by 2 +rotate column x=24 by 1 +rotate column x=23 by 1 +rotate column x=14 by 1 +rotate column x=9 by 2 +rotate column x=4 by 2 +rotate row y=3 by 5 +rotate row y=2 by 3 +rotate row y=1 by 7 +rotate row y=0 by 5 +rotate column x=0 by 2 +rect 3x2 +rotate column x=16 by 2 +rotate row y=3 by 27 +rotate row y=2 by 5 +rotate row y=0 by 20 +rotate column x=8 by 2 +rotate column x=7 by 1 +rotate column x=5 by 1 +rotate column x=3 by 3 +rotate column x=2 by 1 +rotate column x=1 by 2 +rotate column x=0 by 1 +rect 9x1 +rotate row y=4 by 42 +rotate row y=3 by 40 +rotate row y=1 by 30 +rotate row y=0 by 40 +rotate column x=37 by 2 +rotate column x=36 by 3 +rotate column x=35 by 1 +rotate column x=33 by 1 +rotate column x=32 by 1 +rotate column x=31 by 3 +rotate column x=30 by 1 +rotate column x=28 by 1 +rotate column x=27 by 1 +rotate column x=25 by 1 +rotate column x=23 by 3 +rotate column x=22 by 1 +rotate column x=21 by 1 +rotate column x=20 by 1 +rotate column x=18 by 1 +rotate column x=17 by 1 +rotate column x=16 by 3 +rotate column x=15 by 1 +rotate column x=13 by 1 +rotate column x=12 by 1 +rotate column x=11 by 2 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 2 +rotate column x=5 by 1 +rotate column x=3 by 3 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 39x1 +rotate column x=44 by 2 +rotate column x=42 by 2 +rotate column x=35 by 5 +rotate column x=34 by 2 +rotate column x=32 by 2 +rotate column x=29 by 2 +rotate column x=25 by 5 +rotate column x=24 by 2 +rotate column x=19 by 2 +rotate column x=15 by 4 +rotate column x=14 by 2 +rotate column x=12 by 3 +rotate column x=9 by 2 +rotate column x=5 by 5 +rotate column x=4 by 2 +rotate row y=5 by 5 +rotate row y=4 by 38 +rotate row y=3 by 10 +rotate row y=2 by 46 +rotate row y=1 by 10 +rotate column x=48 by 4 +rotate column x=47 by 3 +rotate column x=46 by 3 +rotate column x=45 by 1 +rotate column x=43 by 1 +rotate column x=37 by 5 +rotate column x=36 by 5 +rotate column x=35 by 4 +rotate column x=33 by 1 +rotate column x=32 by 5 +rotate column x=31 by 5 +rotate column x=28 by 5 +rotate column x=27 by 5 +rotate column x=26 by 3 +rotate column x=25 by 4 +rotate column x=23 by 1 +rotate column x=17 by 5 +rotate column x=16 by 5 +rotate column x=13 by 1 +rotate column x=12 by 5 +rotate column x=11 by 5 +rotate column x=3 by 1 +rotate column x=0 by 1 diff --git a/2016/rust/src/day_08.rs b/2016/rust/src/day_08.rs new file mode 100644 index 0000000..06952d3 --- /dev/null +++ b/2016/rust/src/day_08.rs @@ -0,0 +1,149 @@ +use std::collections::HashMap; +use std::fmt; + +use regex::Regex; + +use errors::*; + +pub fn solve(input: &str) -> Result { + let rect_re = Regex::new(r"rect (\d+)x(\d+)").unwrap(); + let rotate_re = Regex::new(r"rotate (column x|row y)=(\d+) by (\d+)").unwrap(); + + let mut screen = Screen::new(50, 6); + for line in input.lines() { + if let Some(caps) = rect_re.captures(line) { + screen.rect(caps[1].parse().unwrap(), caps[2].parse().unwrap()); + } else if let Some(caps) = rotate_re.captures(line) { + let index = caps[2].parse().unwrap(); + let count = caps[3].parse().unwrap(); + if &caps[1] == "column x" { + screen.rotate_col(index, count); + } else if &caps[1] == "row y" { + screen.rotate_row(index, count); + } + } else { + return Err(format!("unexpected input: '{}'", line).into()); + } + } + + Ok(format!("{}", screen.pixels.iter().filter(|&(_, &v)| v).count())) + // Ok(format!("{}", screen)) +} + +#[derive(Eq,Hash,PartialEq)] +struct Point(usize, usize); + +type Pixel = bool; + +pub struct Screen { + width: usize, + height: usize, + pixels: HashMap, +} + +impl Screen { + fn new(width: usize, height: usize) -> Self { + Screen { + width: width, + height: height, + pixels: HashMap::new(), + } + } + + fn rect(&mut self, x: usize, y: usize) { + for x in 0..x { + for y in 0..y { + self.pixels.insert(Point(x, y), true); + } + } + } + + fn rotate_col(&mut self, index: usize, count: usize) { + let count = count % self.height; + + let mut col = (0..self.height) + .map(|y| self.pixels.get(&Point(index, y)).cloned().unwrap_or(false)) + .collect::>(); + let col = Self::rotate(&mut col, count); + + for (y, &pixel) in col.iter().enumerate() { + self.pixels.insert(Point(index, y), pixel); + } + } + + fn rotate_row(&mut self, index: usize, count: usize) { + let count = count % self.width; + + let mut row = (0..self.width) + .map(|x| self.pixels.get(&Point(x, index)).cloned().unwrap_or(false)) + .collect::>(); + let row = Self::rotate(&mut row, count); + + for (x, &pixel) in row.iter().enumerate() { + self.pixels.insert(Point(x, index), pixel); + } + } + + fn rotate(v: &mut Vec, count: usize) -> Vec { + let count = v.len() - count - 1; + v[(count + 1)..].iter().chain(v[0...count].iter()).cloned().collect() + } +} + +impl fmt::Display for Screen { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = (0..self.height) + .map(|y| { + (0..self.width) + .map(|x| { + if self.pixels.get(&Point(x, y)).cloned().unwrap_or(false) { + '#' + } else { + '.' + } + }) + .collect::() + }) + .collect::>() + .join("\n"); + write!(f, "{}", s) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_screen() { + let mut screen = Screen::new(7, 3); + assert_eq!(format!("{}", screen), + "....... +....... +......."); + + screen.rect(3, 2); + assert_eq!(format!("{}", screen), + "###.... +###.... +......."); + + screen.rotate_col(1, 1); + assert_eq!(format!("{}", screen), + "#.#.... +###.... +.#....."); + + screen.rotate_row(0, 4); + assert_eq!(format!("{}", screen), + "....#.# +###.... +.#....."); + + screen.rotate_col(1, 1); + assert_eq!(format!("{}", screen), + ".#..#.# +#.#.... +.#....."); + } +} diff --git a/2016/rust/src/lib.rs b/2016/rust/src/lib.rs index 2e9fa82..3ff0012 100644 --- a/2016/rust/src/lib.rs +++ b/2016/rust/src/lib.rs @@ -1,3 +1,4 @@ +#![feature(inclusive_range_syntax)] #![recursion_limit = "1024"] #[macro_use] @@ -13,3 +14,4 @@ pub mod day_04; pub mod day_05; pub mod day_06; pub mod day_07; +pub mod day_08; diff --git a/2016/rust/src/main.rs b/2016/rust/src/main.rs index ab72f40..d984eee 100644 --- a/2016/rust/src/main.rs +++ b/2016/rust/src/main.rs @@ -10,7 +10,7 @@ fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).ok(); - let solution = day_07::solve(&input)?; + let solution = day_08::solve(&input)?; println!("{}", solution); Ok(())