aboutsummaryrefslogtreecommitdiffstats
path: root/y2021/src/d02.rs
blob: f844106fcaf7dcd1829afbf6a090caad46b92c6f (plain) (blame)
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
29
30
31
32
33
34
35
pub fn part1<T: Iterator<Item = String>>(e: T) -> usize {
    let (h, d) = e.fold((0, 0), |(h, d), l| {
        let (td, tv) = l.split_once(" ").unwrap();
        let tv = tv.parse::<usize>().unwrap();

        match td {
            "forward" => (h + tv, d),
            "down"    => (h, d + tv),
            "up"      => (h, d - tv),
                    _ => unreachable!(),
        }
    });

    h * d
}

#[cfg(test)]
mod tests {
    use super::*;

    static EX: [&str; 6] = [
        "forward 5",
        "down 5",
        "forward 8",
        "up 3",
        "down 8",
        "forward 2",
    ];

    #[test]
    fn test() {
        assert_eq!(150, part1(EX.iter().map(|x| x.to_string())));
    }
}