Skip to content

Commit

Permalink
Complete y2022:d03
Browse files Browse the repository at this point in the history
  • Loading branch information
d2weber committed Apr 23, 2023
1 parent 9a37ee4 commit ae3a8c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/y2021/d02/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32>().unwrap();
} else if let Some(v) = l.strip_prefix("down ") {
aim += v.parse::<i32>().unwrap();
} else {
let x = l.strip_prefix("forward ").unwrap().parse::<i32>().unwrap();
pos += x;
depth += x * aim;
}
});
pos * depth
}

#[test]
fn sample() {
assert_eq!(solution(SAMPLE), 900);
}
#[test]
fn actual() {
assert_eq!(solution(INPUT), 1568138742);
}
}
1 change: 1 addition & 0 deletions src/y2021/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod d01;
pub mod d02;
pub mod d03;

0 comments on commit ae3a8c6

Please sign in to comment.