Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused_imports)]
- #![allow(dead_code)]
- 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_usize<'a, T: Into<&'a str>>(s: T) -> usize {
- s.into().parse().unwrap()
- }
- fn decompress(s: &str) -> usize {
- let mut out = 0;
- let mut cur_slice = s;
- loop {
- match cur_slice.find('(') {
- Some(pos) => {
- out += pos;
- cur_slice = &cur_slice[pos..];
- match cur_slice.find(')') {
- Some(pos) => {
- let enc = &cur_slice[1..pos];
- cur_slice = &cur_slice[pos+1..];
- let xpos = enc.find('x').unwrap();
- let cnt = parse_usize(&enc[0..xpos]);
- let rep = parse_usize(&enc[xpos+1..]);
- let replen = decompress(&cur_slice[0..cnt]);
- out += rep * replen;
- cur_slice = &cur_slice[cnt..];
- },
- _ => unreachable!(),
- }
- },
- _ => {
- out += cur_slice.len();
- break;
- },
- }
- }
- out
- }
- fn main() {
- let mut sum = 0;
- let mut input = "".to_string();
- loop {
- let line = read_line();
- if line.is_empty() { break };
- input.push_str(&line[..]);
- };
- input.replace(char::is_whitespace,"");
- let out = decompress(&input[..]);
- println!("{:?}", out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement