summaryrefslogtreecommitdiff
path: root/k-th-symbol-in-grammar/src/main.rs
blob: 5aaa24c8159df4cb3659423d4c2bf855bd862efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    println!("Hello, world!");
    let tests = vec![
        (1,1,0),
        (2,1,0),
        (2,2,1),
        (30, 434991989, 0)
    ];
    for (n,k,o) in tests {
        let result = Solution::kth_grammar(n,k);
        println!("{n} {k} {result} {o}");
    }
}

struct Solution;

impl Solution {
    pub fn kth_grammar(n: i32, k: i32) -> i32 {
        let mut s: Vec<bool> = vec![false];
        for _ in 0..n {
            s = s.iter().flat_map(|x| match x {
                false => [false,true],
                true => [true,false]
            }).collect::<Vec<_>>();
        }
        s[(k - 1) as usize] as i32
    }
}