diff --git a/src/y2021/d02/mod.rs b/src/y2021/d02/mod.rs index 1afc82e..6a1edf5 100644 --- a/src/y2021/d02/mod.rs +++ b/src/y2021/d02/mod.rs @@ -29,3 +29,34 @@ pub mod part1 { assert_eq!(solution(INPUT), 1427868); } } +pub mod part2 { + #[cfg(test)] + use super::*; + + pub fn solution(s: &str) -> i32 { + let mut pos = 0; + let mut depth = 0; + let mut aim = 0; + s.lines().for_each(|l| { + if let Some(v) = l.strip_prefix("up ") { + aim -= v.parse::().unwrap(); + } else if let Some(v) = l.strip_prefix("down ") { + aim += v.parse::().unwrap(); + } else { + let x = l.strip_prefix("forward ").unwrap().parse::().unwrap(); + pos += x; + depth += x * aim; + } + }); + pos * depth + } + + #[test] + fn sample() { + assert_eq!(solution(SAMPLE), 900); + } + #[test] + fn actual() { + assert_eq!(solution(INPUT), 1568138742); + } +} diff --git a/src/y2021/mod.rs b/src/y2021/mod.rs index 55e411a..5a4a47d 100644 --- a/src/y2021/mod.rs +++ b/src/y2021/mod.rs @@ -1,2 +1,3 @@ pub mod d01; pub mod d02; +pub mod d03;