結果

問題 No.1292 パタパタ三角形
ユーザー StrorkisStrorkis
提出日時 2020-11-20 22:00:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 3,020 bytes
コンパイル時間 3,627 ms
コンパイル使用メモリ 156,264 KB
実行使用メモリ 5,816 KB
最終ジャッジ日時 2023-09-30 19:15:33
合計ジャッジ時間 2,720 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 12 ms
4,380 KB
testcase_08 AC 12 ms
4,380 KB
testcase_09 AC 18 ms
5,816 KB
testcase_10 AC 20 ms
5,788 KB
testcase_11 AC 19 ms
5,748 KB
testcase_12 AC 19 ms
5,788 KB
testcase_13 AC 20 ms
5,800 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{BufRead, BufWriter, Write, StdinLock, StdoutLock};

fn input(stdin: &mut StdinLock) -> String {
    let mut input = String::new();
    stdin.read_line(&mut input).ok();
    input
}

macro_rules! read {
    ($stdin:expr, [$t:tt; $n:expr]) => {
        (0..$n).map(|_| read!($stdin, $t)).collect::<Vec<_>>()
    };
    ($stdin:expr, [$t:tt]) => {
        input($stdin)
            .split_whitespace()
            .map(|x| parse!(x, $t))
            .collect::<Vec<_>>()    
    };
    ($stdin:expr, ($($t:tt),*)) => {
        {
            let input = input($stdin);
            let mut iter = input.split_whitespace();    
            ( $( parse!(iter.next().unwrap(), $t) ),* )
        }
    };
    ($stdin:expr, $t:tt) => {
        parse!(input($stdin).trim(), $t)
    };
}

macro_rules! parse {
    ($s:expr, chars) => {
        $s.chars().collect::<Vec<_>>()
    };
    ($s:expr, usize1) => {
        parse!($s, usize) - 1
    };
    ($s:expr, $t:ty) => {
        $s.parse::<$t>().unwrap()
    }
}

trait ProconWrite {
    fn writeln<T: std::fmt::Display>(&mut self, x: T);
    fn writeln_iter<I: Iterator>(&mut self, iter: I)
    where
        I::Item: std::fmt::Display
    {
        self.writeln(
            iter.map(|x| x.to_string()).collect::<Vec<_>>().join(" ")
        );
    }
}
 
impl ProconWrite for BufWriter<StdoutLock<'_>> {
    fn writeln<T: std::fmt::Display>(&mut self, x: T) {
        writeln!(self, "{}", x).ok();
    }
}

fn solve(stdin: &mut StdinLock, writer: &mut BufWriter<StdoutLock>) {
    let s = read!(stdin, String);

    let f = |x: i32| {
        let res = x % 6;
        if res < 0 { res + 6 } else { res }
    };

    let (mut x, mut y): (i32, i32) = (0, 0);
    let mut set = std::collections::HashSet::new();
    set.insert((x, y));
    for c in s.chars() {
        if c == 'a' {
            match f(x) {
                0 => x -= 1,
                1 => if y % 2 == 0 { y += 1; } else { y -= 1; },
                2 => x += 1,
                3 => x -= 1,
                4 => if y % 2 == 0 { y -= 1; } else { y += 1; },
                _ => x += 1,
            }
        } else if c == 'b' {
            match f(x) {
                0 => x += 1,
                1 => x -= 1,
                2 => if y % 2 == 0 { y -= 1; } else { y += 1; },
                3 => x += 1,
                4 => x -= 1,
                _ => if y % 2 == 0 { y += 1; } else { y -= 1; },
            }
        } else {
            match f(x) {
                0 => if y % 2 == 0 { y -= 1; } else { y += 1; },
                1 => x += 1,
                2 => x -= 1,
                3 => if y % 2 == 0 { y += 1; } else { y -= 1; },
                4 => x += 1,
                _ => x -= 1,
            }
        }
        set.insert((x, y));
    }
    writer.writeln(set.len());
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin = stdin.lock();

    let stdout = std::io::stdout();
    let mut writer = BufWriter::new(stdout.lock());

    solve(&mut stdin, &mut writer);
}
0