From ae3a8c61211353a5a5e97137b2d33c71dd5e19a2 Mon Sep 17 00:00:00 2001 From: d2weber <29163905+d2weber@users.noreply.github.com> Date: Sun, 23 Apr 2023 08:25:46 +0200 Subject: [PATCH] Complete y2022:d03 --- src/y2021/d02/mod.rs | 31 +++++++++++++++++++++++++++++++ src/y2021/mod.rs | 1 + 2 files changed, 32 insertions(+) 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;