~swaits/aoc2021

282ed1aa2e8827fe5a19206c5654db1e97b2f29d — Stephen Waits 2 years ago b380ecb
day2 solution
4 files changed, 1069 insertions(+), 0 deletions(-)

A data/day02-test.txt
A data/day02.txt
A src/day02.rs
M src/main.rs
A data/day02-test.txt => data/day02-test.txt +6 -0
@@ 0,0 1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

A data/day02.txt => data/day02.txt +1000 -0
@@ 0,0 1,1000 @@
forward 2
down 9
up 6
forward 1
down 5
down 7
down 9
forward 9
down 8
up 7
forward 2
up 6
forward 4
down 5
down 9
up 1
down 9
forward 8
forward 6
forward 6
forward 5
forward 9
up 3
up 5
forward 1
down 4
down 7
forward 2
up 3
down 8
forward 1
down 2
forward 3
up 1
up 1
up 7
forward 5
up 8
forward 8
forward 8
down 6
forward 1
forward 5
forward 4
forward 6
forward 5
down 6
down 9
forward 9
down 8
forward 6
down 5
forward 9
up 3
up 1
down 8
down 7
down 9
forward 7
down 8
down 9
down 5
down 3
forward 1
forward 6
down 1
forward 9
down 5
forward 7
up 2
down 8
forward 1
down 4
down 9
down 4
up 5
forward 4
forward 6
forward 1
down 3
forward 1
down 6
up 5
up 4
forward 6
forward 1
forward 1
down 2
up 4
up 3
up 2
up 6
down 6
forward 1
down 8
forward 1
up 6
forward 7
down 5
forward 4
forward 6
down 4
forward 4
down 4
down 4
forward 2
forward 8
down 5
down 1
down 8
up 5
up 8
down 5
forward 4
down 6
up 7
forward 2
down 3
forward 2
forward 2
down 9
down 3
up 6
forward 8
up 2
up 9
forward 4
down 1
down 5
forward 4
down 2
down 3
forward 5
down 4
forward 7
up 4
forward 6
up 8
forward 1
up 9
down 4
forward 2
down 1
forward 7
down 3
down 2
forward 5
down 3
down 9
down 9
up 5
forward 2
down 8
up 9
forward 4
down 3
forward 3
forward 6
up 2
forward 3
down 1
down 1
down 1
forward 7
forward 4
forward 7
down 5
down 6
down 2
forward 6
down 3
up 6
forward 4
down 8
up 1
forward 8
down 2
down 5
forward 4
down 9
forward 2
forward 2
down 3
forward 3
down 1
forward 2
down 7
forward 3
forward 9
up 9
forward 6
forward 2
down 1
down 5
forward 6
forward 6
down 3
up 3
forward 9
down 7
down 2
down 4
down 7
forward 5
up 4
forward 8
down 5
forward 7
down 7
up 7
down 8
forward 9
up 5
forward 1
down 2
forward 5
down 9
forward 3
down 5
forward 8
forward 3
up 5
down 2
up 3
forward 2
up 1
up 5
down 8
forward 2
down 5
up 4
up 5
up 2
forward 9
forward 6
down 9
up 9
forward 6
forward 4
forward 3
forward 7
up 1
down 2
down 6
down 1
forward 8
down 1
forward 6
down 8
forward 8
down 7
down 6
down 5
forward 2
up 8
up 6
up 5
down 1
forward 1
down 1
down 5
forward 7
forward 3
down 1
forward 5
forward 5
forward 8
down 1
up 2
down 6
up 6
forward 6
forward 6
down 3
forward 9
up 4
forward 4
down 6
up 1
forward 6
down 2
down 5
down 2
down 6
up 5
down 1
down 1
forward 3
forward 7
forward 3
up 2
down 8
down 4
down 1
down 5
down 1
down 9
forward 6
down 6
down 4
down 6
down 8
forward 4
down 6
down 7
forward 8
down 4
up 4
down 1
forward 1
forward 4
forward 1
up 9
down 7
forward 7
down 4
forward 1
up 4
forward 4
down 5
down 7
forward 5
forward 7
forward 1
forward 1
forward 9
forward 9
up 3
forward 4
down 2
forward 9
up 8
forward 3
up 5
down 3
down 8
forward 8
down 6
forward 1
down 6
down 6
up 9
down 2
forward 8
up 9
down 7
up 9
up 8
up 1
forward 6
forward 9
down 2
forward 8
down 1
up 4
forward 4
forward 7
up 2
forward 4
down 5
forward 3
down 2
down 7
down 4
down 2
up 5
down 5
down 5
down 4
up 1
forward 7
down 6
forward 5
forward 1
down 4
up 9
down 5
forward 7
forward 5
down 6
down 3
down 9
down 1
forward 6
up 2
down 7
down 3
down 6
up 3
down 4
down 4
forward 9
down 3
forward 2
down 9
down 8
up 4
down 2
forward 2
down 5
down 4
down 4
down 2
forward 6
down 3
forward 1
down 4
forward 7
down 5
up 4
down 6
forward 8
down 6
forward 2
forward 4
forward 5
forward 7
forward 4
forward 5
down 8
down 7
forward 3
forward 5
up 7
forward 1
down 4
forward 5
forward 4
forward 4
down 5
down 8
forward 8
down 1
down 1
down 5
up 5
forward 6
down 6
forward 3
forward 4
forward 7
forward 4
down 8
forward 2
down 4
forward 4
down 1
up 2
forward 6
up 1
down 7
down 9
forward 7
forward 2
up 3
down 2
down 9
down 5
up 7
forward 1
forward 8
down 8
up 3
down 3
forward 9
up 4
down 5
up 5
down 1
up 8
forward 9
down 3
up 6
forward 6
forward 1
down 1
forward 9
down 8
forward 8
down 6
up 9
down 4
up 3
up 9
forward 2
down 2
down 2
forward 3
down 2
forward 5
forward 4
up 8
forward 9
up 7
forward 2
down 5
down 6
forward 8
up 7
forward 4
forward 3
up 5
down 8
forward 3
up 2
down 3
forward 6
down 9
down 2
down 6
down 2
forward 7
forward 5
forward 7
down 8
forward 2
down 2
forward 8
up 8
forward 4
forward 3
up 5
down 3
forward 3
up 8
up 7
down 4
down 1
forward 2
down 1
up 6
up 4
down 3
up 1
forward 7
forward 7
forward 7
forward 8
down 1
forward 5
down 6
forward 9
forward 7
forward 7
down 4
up 4
down 6
down 9
up 4
up 2
up 6
forward 4
up 4
up 6
down 2
forward 4
down 9
forward 9
forward 9
down 1
forward 7
down 2
down 7
down 8
down 8
down 9
up 9
down 5
forward 5
forward 7
forward 4
down 7
forward 8
forward 1
down 8
up 9
down 7
forward 9
forward 4
forward 8
down 9
forward 4
down 3
forward 3
down 1
down 1
down 2
up 5
down 2
down 1
down 8
forward 3
up 2
forward 7
down 3
down 8
down 1
forward 4
forward 7
down 5
forward 6
down 6
down 2
forward 6
down 3
up 4
down 7
forward 7
up 1
up 9
down 1
down 2
down 8
down 7
up 1
forward 7
down 2
forward 4
forward 6
forward 9
down 6
forward 2
up 8
down 2
up 2
up 5
down 8
up 6
down 9
forward 6
down 8
down 6
down 1
up 7
up 6
down 8
forward 2
up 7
forward 5
forward 7
forward 7
up 5
forward 2
down 9
up 2
up 8
up 2
down 3
down 7
forward 9
down 3
up 9
forward 8
up 8
forward 4
forward 8
forward 6
up 1
down 3
up 1
down 1
forward 2
forward 1
forward 4
forward 7
up 8
down 9
up 2
down 7
forward 4
down 3
forward 4
forward 2
down 9
forward 8
forward 5
forward 3
down 6
forward 4
forward 4
forward 9
forward 4
up 5
down 7
up 6
forward 5
down 5
forward 4
down 5
forward 7
forward 3
forward 5
down 5
forward 4
down 5
up 4
down 8
up 3
down 3
up 5
forward 4
forward 5
down 6
forward 6
forward 1
forward 8
down 6
down 9
up 5
forward 2
forward 8
up 6
down 6
forward 2
down 8
forward 7
forward 7
down 5
forward 5
forward 8
forward 1
down 4
down 2
down 5
up 4
forward 3
forward 5
down 4
down 7
down 4
up 9
up 6
forward 1
down 8
up 8
up 9
forward 2
forward 1
down 6
forward 6
down 4
forward 7
up 2
up 1
forward 4
down 1
forward 8
forward 3
up 7
up 5
down 1
forward 8
forward 6
up 6
forward 9
down 5
down 9
forward 2
down 3
up 1
up 7
down 1
forward 8
up 9
down 1
down 5
down 7
down 5
down 5
down 5
up 9
forward 9
forward 7
forward 4
forward 6
down 5
down 3
forward 9
forward 1
down 1
down 8
up 4
down 9
forward 9
up 1
down 5
forward 8
up 6
forward 3
down 6
up 8
down 7
forward 3
forward 6
down 7
forward 6
forward 4
forward 4
down 4
forward 6
forward 5
down 6
forward 6
down 7
forward 6
forward 3
up 4
up 2
up 6
down 2
down 8
forward 5
forward 1
up 4
forward 7
forward 9
up 6
down 7
down 3
up 5
forward 5
down 8
up 1
down 1
down 3
down 2
down 1
forward 5
down 3
down 5
forward 7
forward 9
down 3
forward 7
forward 5
forward 4
forward 2
forward 7
forward 8
forward 6
down 8
forward 5
forward 6
forward 6
down 8
down 2
forward 4
down 7
forward 6
down 7
down 4
forward 6
up 6
forward 4
forward 9
forward 2
forward 3
forward 1
down 8
down 3
forward 4
up 3
forward 7
forward 1
down 7
down 8
forward 1
up 8
forward 8
up 8
down 5
forward 6
down 8
down 4
down 9
up 1
down 3
forward 6
down 6
forward 7
forward 3
down 6
down 6
forward 4
down 4
down 1
down 8
forward 2
forward 8
forward 8
down 6
forward 9
down 9
down 5
down 5
forward 7
down 1
forward 1
down 1
down 6
down 1
forward 1
up 6
up 9
forward 5
down 6
forward 8
forward 6
down 7
forward 1
forward 4
forward 9
forward 2
forward 4
down 2
forward 1
forward 8
down 1
down 1
forward 4
down 5
down 3
down 9
down 2
up 8
down 7
down 1
down 9
forward 2
forward 2
up 3
forward 3
down 3
forward 5
forward 9
down 7
up 7
down 9
forward 3
forward 7
down 1
forward 8
down 8
forward 1
down 8
down 6
forward 2
down 3
down 1
down 8
forward 3
up 5
down 7
up 2
up 8
forward 5
up 7
down 6
up 7
down 9
forward 5
up 4
forward 9
down 5
up 7
down 2
up 2
up 7
forward 5
down 6
forward 4
down 4
down 3
forward 2
up 2
down 5
forward 8
down 3
up 7
down 1
down 7
forward 7
forward 4
forward 7
down 2
down 9
down 6
down 9
down 2
down 9
down 7
down 5
forward 4
up 5
up 7
forward 2
forward 7
down 3
down 3
forward 4

A src/day02.rs => src/day02.rs +61 -0
@@ 0,0 1,61 @@
use crate::utils::read_file;
use anyhow::Result;

fn parse_line(s: &str) -> (&str, i32) {
    let parsed: Vec<&str> = s.splitn(2, ' ').collect();
    let instruction = parsed[0];
    let quantity = parsed[1].parse::<i32>().unwrap();
    (instruction, quantity)
}

fn follow_course(s: &str) -> i32 {
    let (mut pos, mut depth) = (0, 0);
    s.lines().for_each(|line| match parse_line(line) {
        ("forward", d) => pos += d,
        ("down", d) => depth += d,
        ("up", d) => depth -= d,
        _ => unreachable!(),
    });
    pos * depth
}

fn follow_course_better(s: &str) -> i32 {
    let (mut pos, mut depth, mut aim) = (0, 0, 0);
    s.lines().for_each(|line| match parse_line(line) {
        ("forward", d) => {
            pos += d;
            depth += aim * d;
        }
        ("down", d) => aim += d,
        ("up", d) => aim -= d,
        _ => unreachable!(),
    });
    pos * depth
}

pub(crate) fn run() -> Result<(usize, usize)> {
    let input = read_file("data/day02.txt")?;
    Ok((
        follow_course(&input) as usize,
        follow_course_better(&input) as usize,
    ))
}

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

    #[test]
    fn test_example() {
        let input = read_file("data/day02-test.txt").unwrap();
        assert_eq!(follow_course(&input), 150);
        assert_eq!(follow_course_better(&input), 900);
    }

    #[test]
    fn test_my_data() {
        let input = read_file("data/day02.txt").unwrap();
        assert_eq!(follow_course(&input), 2070300);
        assert_eq!(follow_course_better(&input), 2078985210);
    }
}

M src/main.rs => src/main.rs +2 -0
@@ 1,9 1,11 @@
use anyhow::Result;

mod day01;
mod day02;
mod utils;

fn main() -> Result<()> {
    println!("day01: {:?}", day01::run()?);
    println!("day02: {:?}", day02::run()?);
    Ok(())
}