Skip to main content

aoc/year2021/
day02.rs

1//! # Dive!
2//!
3//! Solves both parts at once, relying on the regular nature of the input.
4//! Each number is always a single digit.
5use crate::util::parse::*;
6
7type Input = (i32, i32);
8
9pub fn parse(input: &str) -> Input {
10    let mut slice = input.as_bytes();
11    let mut position = 0;
12    let mut depth = 0;
13    let mut aim = 0;
14
15    while !slice.is_empty() {
16        let amount = |index: usize| slice[index].to_decimal() as i32;
17
18        (slice, position, depth, aim) = match slice[0] {
19            b'u' => (&slice[5..], position, depth, aim - amount(3)),
20            b'd' => (&slice[7..], position, depth, aim + amount(5)),
21            b'f' => (&slice[10..], position + amount(8), depth + aim * amount(8), aim),
22            _ => unreachable!(),
23        }
24    }
25
26    (position * aim, position * depth)
27}
28
29pub fn part1(input: &Input) -> i32 {
30    input.0
31}
32
33pub fn part2(input: &Input) -> i32 {
34    input.1
35}