summaryrefslogtreecommitdiffstats
path: root/src/bin/2021/day2.rs
blob: 5ae3596c14e8a12223ce4abb67f885fd45aecc61 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use advent_of_code::prelude::*;

pub enum Command {
    Forward(i64),
    Down(i64),
    Up(i64),
}

pub fn parse(fh: File) -> Result<impl Iterator<Item = Command>> {
    Ok(parse::raw_lines(fh).map(|line| {
        if let Some(n) = line.strip_prefix("forward ") {
            Command::Forward(n.parse().unwrap())
        } else if let Some(n) = line.strip_prefix("down ") {
            Command::Down(n.parse().unwrap())
        } else if let Some(n) = line.strip_prefix("up ") {
            Command::Up(n.parse().unwrap())
        } else {
            panic!("couldn't parse line: {}", line);
        }
    }))
}

pub fn part1(commands: impl Iterator<Item = Command>) -> Result<i64> {
    let mut horizontal = 0;
    let mut vertical = 0;
    for command in commands {
        match command {
            Command::Forward(n) => {
                horizontal += n;
            }
            Command::Down(n) => {
                vertical += n;
            }
            Command::Up(n) => {
                vertical -= n;
            }
        }
    }
    Ok(horizontal * vertical)
}

pub fn part2(commands: impl Iterator<Item = Command>) -> Result<i64> {
    let mut aim = 0;
    let mut horizontal = 0;
    let mut vertical = 0;
    for command in commands {
        match command {
            Command::Forward(n) => {
                horizontal += n;
                vertical += aim * n;
            }
            Command::Down(n) => {
                aim += n;
            }
            Command::Up(n) => {
                aim -= n;
            }
        }
    }
    Ok(horizontal * vertical)
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2021, 2).unwrap()).unwrap()).unwrap(),
        1694130
    );
    assert_eq!(
        part2(parse(parse::data(2021, 2).unwrap()).unwrap()).unwrap(),
        1698850445
    );
}