This commit is contained in:
2016-12-08 10:32:46 +01:00
parent e6b66a4049
commit d0cf610046
4 changed files with 536 additions and 0 deletions

105
2016/08/Cargo.lock generated Normal file
View File

@@ -0,0 +1,105 @@
[root]
name = "08"
version = "0.1.0"
dependencies = [
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

8
2016/08/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "08"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]
lazy_static = "0.1"
regex = "0.1"

194
2016/08/input.txt Normal file
View File

@@ -0,0 +1,194 @@
rect 1x1
rotate row y=0 by 7
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 5
rect 1x1
rotate row y=0 by 3
rect 1x1
rotate row y=0 by 2
rect 1x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 7
rect 6x1
rotate row y=0 by 3
rect 2x1
rotate row y=0 by 2
rect 1x2
rotate row y=1 by 10
rotate row y=0 by 3
rotate column x=0 by 1
rect 2x1
rotate column x=20 by 1
rotate column x=15 by 1
rotate column x=5 by 1
rotate row y=1 by 5
rotate row y=0 by 2
rect 1x2
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 15
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 5
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate row y=2 by 10
rotate row y=0 by 10
rotate column x=8 by 1
rotate column x=5 by 1
rotate column x=0 by 1
rect 9x1
rotate column x=27 by 1
rotate row y=0 by 5
rotate column x=0 by 1
rect 4x1
rotate column x=42 by 1
rotate column x=40 by 1
rotate column x=22 by 1
rotate column x=17 by 1
rotate column x=12 by 1
rotate column x=7 by 1
rotate column x=2 by 1
rotate row y=3 by 10
rotate row y=2 by 5
rotate row y=1 by 3
rotate row y=0 by 10
rect 1x4
rotate column x=37 by 2
rotate row y=3 by 18
rotate row y=2 by 30
rotate row y=1 by 7
rotate row y=0 by 2
rotate column x=13 by 3
rotate column x=12 by 1
rotate column x=10 by 1
rotate column x=7 by 1
rotate column x=6 by 3
rotate column x=5 by 1
rotate column x=3 by 3
rotate column x=2 by 1
rotate column x=0 by 1
rect 14x1
rotate column x=38 by 3
rotate row y=3 by 12
rotate row y=2 by 10
rotate row y=0 by 10
rotate column x=7 by 1
rotate column x=5 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 9x1
rotate row y=4 by 20
rotate row y=3 by 25
rotate row y=2 by 10
rotate row y=0 by 15
rotate column x=12 by 1
rotate column x=10 by 1
rotate column x=8 by 3
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=0 by 1
rect 14x1
rotate column x=34 by 1
rotate row y=1 by 45
rotate column x=47 by 1
rotate column x=42 by 1
rotate column x=19 by 1
rotate column x=9 by 2
rotate row y=4 by 7
rotate row y=3 by 20
rotate row y=0 by 7
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=0 by 1
rect 6x1
rotate row y=4 by 8
rotate row y=3 by 5
rotate row y=1 by 5
rotate column x=5 by 1
rotate column x=4 by 1
rotate column x=3 by 2
rotate column x=2 by 1
rotate column x=1 by 3
rotate column x=0 by 1
rect 6x1
rotate column x=36 by 3
rotate column x=25 by 3
rotate column x=18 by 3
rotate column x=11 by 3
rotate column x=3 by 4
rotate row y=4 by 5
rotate row y=3 by 5
rotate row y=2 by 8
rotate row y=1 by 8
rotate row y=0 by 3
rotate column x=3 by 4
rotate column x=0 by 4
rect 4x4
rotate row y=4 by 10
rotate row y=3 by 20
rotate row y=1 by 10
rotate row y=0 by 10
rotate column x=8 by 1
rotate column x=7 by 1
rotate column x=6 by 1
rotate column x=5 by 1
rotate column x=3 by 1
rotate column x=2 by 1
rotate column x=1 by 1
rotate column x=0 by 1
rect 9x1
rotate row y=0 by 40
rotate column x=44 by 1
rotate column x=35 by 5
rotate column x=18 by 5
rotate column x=15 by 3
rotate column x=10 by 5
rotate row y=5 by 15
rotate row y=4 by 10
rotate row y=3 by 40
rotate row y=2 by 20
rotate row y=1 by 45
rotate row y=0 by 35
rotate column x=48 by 1
rotate column x=47 by 5
rotate column x=46 by 5
rotate column x=45 by 1
rotate column x=43 by 1
rotate column x=40 by 1
rotate column x=38 by 2
rotate column x=37 by 3
rotate column x=36 by 2
rotate column x=32 by 2
rotate column x=31 by 2
rotate column x=28 by 1
rotate column x=23 by 3
rotate column x=22 by 3
rotate column x=21 by 5
rotate column x=20 by 1
rotate column x=18 by 1
rotate column x=17 by 3
rotate column x=13 by 1
rotate column x=10 by 1
rotate column x=8 by 1
rotate column x=7 by 5
rotate column x=6 by 5
rotate column x=5 by 1
rotate column x=3 by 5
rotate column x=2 by 5
rotate column x=1 by 5

229
2016/08/src/main.rs Normal file
View File

@@ -0,0 +1,229 @@
#[macro_use] extern crate lazy_static;
extern crate regex;
use std::str::FromStr;
use std::io::{self, BufRead};
use std::fmt;
struct Screen {
screen: Vec<Vec<bool>>
}
impl Screen {
fn new(width: usize, height: usize) -> Self {
Screen {
screen: vec![vec![false; width]; height]
}
}
fn set(&mut self, display: &str) {
for (y, line) in display.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
self.screen[y][x] = match c {
'#' => true,
'.' => false,
_ => panic!("invalid char!")
};
}
}
}
fn get(&self) -> String {
self.screen.iter()
.map(|row| {
row.iter()
.map(|c| if *c { '#' } else { '.' })
.collect::<String>()
})
.collect::<Vec<_>>()
.join("\n")
}
fn fill_rect(&mut self, width: usize, height: usize) {
for y in 0..height {
for x in 0..width {
self.screen[y][x] = true;
}
}
}
fn rotate_column(&mut self, column: usize, step: usize) {
let swaps = self.screen.len();
for _ in 0..step {
for y in (1..swaps).rev() {
let tmp = self.screen[y - 1][column];
self.screen[y - 1][column] = self.screen[y][column];
self.screen[y][column] = tmp;
}
}
}
fn rotate_row(&mut self, row: usize, step: usize) {
let mut vec = &mut self.screen[row];
let swaps = vec.len();
for _ in 0..step {
for x in (1..swaps).rev() {
vec.swap(x, x - 1);
}
}
}
}
impl fmt::Display for Screen {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Debug, PartialEq)]
enum Instruction {
Rect { x: usize, y: usize },
RotateColumn { x: usize, step: usize },
RotateRow { y: usize, step: usize }
}
impl FromStr for Instruction {
type Err = ();
fn from_str(s: &str) -> Result<Instruction, ()> {
use regex::Regex;
lazy_static! {
static ref RECT: Regex = Regex::new(r"^rect (\d+)x(\d+)$").unwrap();
static ref R_COL: Regex = Regex::new(r"^rotate column x=(\d+) by (\d+)$").unwrap();
static ref R_ROW: Regex = Regex::new(r"^rotate row y=(\d+) by (\d+)$").unwrap();
}
if let Some(caps) = RECT.captures(s) {
let x = caps.at(1).unwrap().parse::<usize>().unwrap();
let y = caps.at(2).unwrap().parse::<usize>().unwrap();
return Ok(Instruction::Rect { x: x, y: y });
}
if let Some(caps) = R_COL.captures(s) {
let x = caps.at(1).unwrap().parse::<usize>().unwrap();
let step = caps.at(2).unwrap().parse::<usize>().unwrap();
return Ok(Instruction::RotateColumn { x: x, step: step });
}
if let Some(caps) = R_ROW.captures(s) {
let y = caps.at(1).unwrap().parse::<usize>().unwrap();
let step = caps.at(2).unwrap().parse::<usize>().unwrap();
return Ok(Instruction::RotateRow { y: y, step: step });
}
Err(())
}
}
fn main() {
let stdin = io::stdin();
let instructions = stdin.lock().lines()
.filter_map(|line| line.ok())
.filter_map(|line| Instruction::from_str(&line).ok());
let mut screen = Screen::new(50, 6);
for instruction in instructions {
use Instruction::*;
match instruction {
Rect { x, y } => screen.fill_rect(x, y),
RotateColumn { x, step } => screen.rotate_column(x, step),
RotateRow { y, step } => screen.rotate_row(y, step)
}
}
let count = screen.get().chars()
.fold(0, |acc, c| if c == '#' { acc + 1 } else { acc });
println!("{}", screen);
println!("lit={}", count);
}
#[cfg(test)]
mod tests {
use std::fmt::Write;
use std::str::FromStr;
use super::{Screen, Instruction};
#[test]
fn test_rect() {
let mut screen = Screen::new(7, 3);
screen.fill_rect(3, 2);
let mut w = String::new();
write!(&mut w, "{}", screen).unwrap();
assert_eq!(w, "###....\n\
###....\n\
.......");
}
#[test]
fn test_rotate_column() {
let mut screen = Screen::new(7, 3);
screen.set("###....\n\
###....\n\
.......");
screen.rotate_column(1, 1);
let mut w = String::new();
write!(&mut w, "{}", screen).unwrap();
assert_eq!(w, "#.#....\n\
###....\n\
.#.....");
}
#[test]
fn test_rotate_row() {
let mut screen = Screen::new(7, 3);
screen.set("#.#....\n\
###....\n\
.#.....");
screen.rotate_row(0, 4);
let mut w = String::new();
write!(&mut w, "{}", screen).unwrap();
assert_eq!(w, "....#.#\n\
###....\n\
.#.....");
}
#[test]
fn test_parse_rect() {
assert_eq!(Ok(Instruction::Rect { x: 1, y: 1 }), Instruction::from_str("rect 1x1"));
}
#[test]
fn test_parse_rotate_column() {
assert_eq!(Ok(Instruction::RotateColumn { x: 0, step: 1 }), Instruction::from_str("rotate column x=0 by 1"));
}
#[test]
fn test_parse_rotate_row() {
assert_eq!(Ok(Instruction::RotateRow { y: 2, step: 15 }), Instruction::from_str("rotate row y=2 by 15"));
}
}