-
Notifications
You must be signed in to change notification settings - Fork 15
/
day02.rs
49 lines (45 loc) · 1.46 KB
/
day02.rs
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
//! # Dive!
//!
//! Both part 1 and part 2 rely on the [`fold`] method. This method comes in useful for a lot
//! of Advent of Code problems so is handy to know about. The input is parsed into a tuple enum
//! [`Sub`] for convenience.
//!
//! [`fold`]: Iterator::fold
use crate::util::iter::*;
use crate::util::parse::*;
#[derive(Clone, Copy)]
pub enum Sub {
Up(i32),
Down(i32),
Forward(i32),
}
pub fn parse(input: &str) -> Vec<Sub> {
let helper = |[a, b]: [&str; 2]| {
let amount = b.signed();
match a {
"up" => Sub::Up(amount),
"down" => Sub::Down(amount),
"forward" => Sub::Forward(amount),
_ => unreachable!(),
}
};
input.split_ascii_whitespace().chunk::<2>().map(helper).collect()
}
pub fn part1(input: &[Sub]) -> i32 {
let helper = |(position, depth), next| match next {
Sub::Up(n) => (position, depth - n),
Sub::Down(n) => (position, depth + n),
Sub::Forward(n) => (position + n, depth),
};
let (position, depth) = input.iter().copied().fold((0, 0), helper);
position * depth
}
pub fn part2(input: &[Sub]) -> i32 {
let helper = |(position, depth, aim), next| match next {
Sub::Up(n) => (position, depth, aim - n),
Sub::Down(n) => (position, depth, aim + n),
Sub::Forward(n) => (position + n, depth + aim * n, aim),
};
let (position, depth, _) = input.iter().copied().fold((0, 0, 0), helper);
position * depth
}