AtCoder Beginner Contest 256 B

https://atcoder.jp/contests/abc256/tasks/abc256_b

降順のRangeはこうやるんですね。

(0usize..4).rev()
// Batters
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// Bases ////////////////////

struct Bases {
    v: Vec<i32>
}

impl Bases {
    fn push(&mut self) {
        self.v[0] = 1
    }
    
    fn move_pieces(&mut self, a: usize) -> i32 {
        let p = ((4-a)..4).filter(|&i| self.v[i] == 1).count();
        for i in (0usize..4).rev() {
            if i >= 4 - a {
                self.v[i] = 0
            }
            else {
                self.v[i+a] = self.v[i];
                self.v[i] = 0
            }
        }
        return p as i32
    }
    
    fn create() -> Bases {
        let v  = vec![0, 0, 0, 0];
        Bases { v }
    }
}


//////////////////// process ////////////////////

fn main() {
    let _N: i32 = read();
    let A = read_vec();
    let mut bases = Bases::create();
    let mut P: i32 = 0;
    for &a in A.iter() {
        bases.push();
        P += bases.move_pieces(a)
    }
    println!("{}", P)
}