Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate regex;
- use regex::Regex;
- use std::collections::{HashMap, HashSet};
- use std::io;
- fn read_line() -> String {
- let mut input = String::new();
- io::stdin().read_line(&mut input).unwrap();
- input.trim_right().to_owned()
- }
- fn parse_i32<'a, T: Into<&'a str>>(s: T) -> i32 {
- s.into().parse().unwrap()
- }
- fn parse_usize<'a, T: Into<&'a str>>(s: T) -> usize {
- s.into().parse().unwrap()
- }
- fn main() {
- let mut sum = 0;
- let rxrect = Regex::new(r"^rect (\d*)x(\d*)$").unwrap();
- let rxrow = Regex::new(r"^rotate row y=(\d*) by (\d*)$").unwrap();
- let rxcol = Regex::new(r"^rotate column x=(\d*) by (\d*)$").unwrap();
- const W: usize = 50;
- const H: usize = 6;
- let mut disp = vec![vec![false;W];H];
- loop {
- let s = read_line();
- if s.is_empty() { break };
- if rxrect.is_match(&s) {
- let caps = rxrect.captures(&s).unwrap();
- let cx = parse_usize(caps.at(1).unwrap());
- let cy = parse_usize(caps.at(2).unwrap());
- println!("{} {}", cx, cy);
- for x in 0..cx {
- for y in 0..cy {
- disp[y][x] = true;
- }
- }
- } else if rxrow.is_match(&s) {
- let caps = rxrow.captures(&s).unwrap();
- let cy = parse_usize(caps.at(1).unwrap());
- let cnt = parse_usize(caps.at(2).unwrap());
- println!("{} {}", cy, cnt);
- let v = disp[cy].clone();
- for i in 0..W {
- disp[cy][(i+cnt)%W] = v[i];
- }
- } else if rxcol.is_match(&s) {
- let caps = rxcol.captures(&s).unwrap();
- let cx = parse_usize(caps.at(1).unwrap());
- let cnt = parse_usize(caps.at(2).unwrap());
- println!("{} {}", cx, cnt);
- let mut v = vec![];
- for i in 0..H {
- v.push(disp[i][cx]);
- }
- for i in 0..H {
- disp[(i+cnt)%H][cx] = v[i];
- }
- } else {
- panic!("Wrong input");
- }
- for y in 0..H {
- for x in 0..W {
- print!("{}", if disp[y][x] {'X'} else {'.'});
- }
- println!("");
- }
- println!("");
- }
- for y in 0..disp.len() {
- for x in 0..disp[0].len() {
- if disp[y][x] {
- sum += 1;
- }
- }
- }
- println!("{:?}", sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement