Advertisement
Guest User

Advent of Code 2016 Day 8

a guest
Dec 7th, 2016
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.47 KB | None | 0 0
  1. extern crate regex;
  2. use regex::Regex;
  3. use std::collections::{HashMap, HashSet};  
  4. use std::io;
  5.  
  6.  
  7. fn read_line() -> String {
  8.     let mut input = String::new();
  9.     io::stdin().read_line(&mut input).unwrap();
  10.     input.trim_right().to_owned()
  11. }
  12.  
  13. fn parse_i32<'a, T: Into<&'a str>>(s: T) -> i32 {
  14.     s.into().parse().unwrap()
  15. }  
  16.  
  17. fn parse_usize<'a, T: Into<&'a str>>(s: T) -> usize {
  18.     s.into().parse().unwrap()
  19. }  
  20.  
  21. fn main() {
  22.     let mut sum = 0;
  23.     let rxrect = Regex::new(r"^rect (\d*)x(\d*)$").unwrap();
  24.     let rxrow = Regex::new(r"^rotate row y=(\d*) by (\d*)$").unwrap();
  25.     let rxcol = Regex::new(r"^rotate column x=(\d*) by (\d*)$").unwrap();
  26.    
  27.     const W: usize = 50;
  28.     const H: usize = 6;
  29.     let mut disp = vec![vec![false;W];H];
  30.  
  31.     loop {
  32.         let s = read_line();
  33.         if s.is_empty() { break };
  34.  
  35.         if rxrect.is_match(&s) {
  36.             let caps = rxrect.captures(&s).unwrap();
  37.             let cx = parse_usize(caps.at(1).unwrap());
  38.             let cy = parse_usize(caps.at(2).unwrap());
  39.             println!("{} {}", cx, cy);
  40.             for x in 0..cx {
  41.                 for y in 0..cy {
  42.                     disp[y][x] = true;
  43.                 }
  44.             }
  45.         } else if rxrow.is_match(&s) {
  46.             let caps = rxrow.captures(&s).unwrap();
  47.             let cy = parse_usize(caps.at(1).unwrap());
  48.             let cnt = parse_usize(caps.at(2).unwrap());
  49.             println!("{} {}", cy, cnt);
  50.             let v = disp[cy].clone();
  51.             for i in 0..W {
  52.                 disp[cy][(i+cnt)%W] = v[i];
  53.             }
  54.  
  55.         } else if rxcol.is_match(&s) {
  56.             let caps = rxcol.captures(&s).unwrap();
  57.             let cx = parse_usize(caps.at(1).unwrap());
  58.             let cnt = parse_usize(caps.at(2).unwrap());
  59.             println!("{} {}", cx, cnt);
  60.             let mut v = vec![];
  61.             for i in 0..H {
  62.                 v.push(disp[i][cx]);
  63.             }
  64.             for i in 0..H {
  65.                 disp[(i+cnt)%H][cx] = v[i];
  66.             }
  67.         } else {
  68.             panic!("Wrong input");
  69.         }
  70.         for y in 0..H {
  71.             for x in 0..W {
  72.                 print!("{}", if disp[y][x] {'X'} else {'.'});
  73.             }
  74.             println!("");
  75.         }
  76.         println!("");
  77.    
  78.     }
  79.     for y in 0..disp.len() {
  80.         for x in 0..disp[0].len() {
  81.             if disp[y][x] {
  82.                 sum += 1;
  83.             }
  84.         }
  85.     }
  86.     println!("{:?}", sum);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement