diff --git a/rust/2021/src/day_02.rs b/rust/2021/src/day_02.rs index 8e50bab..486d8bb 100644 --- a/rust/2021/src/day_02.rs +++ b/rust/2021/src/day_02.rs @@ -1,9 +1,47 @@ fn solve_part1(input: &str) -> i32 { - todo!() + let (horiz, _, depth) = solve(input); + horiz * depth } fn solve_part2(input: &str) -> i32 { - todo!() + let (horiz, depth, _) = solve(input); + horiz * depth +} + +fn solve(input: &str) -> (i32, i32, i32) { + let (mut horiz, mut depth, mut aim) = (0, 0, 0); + + for (dir, dist) in parse_input(input) { + match dir { + Some("forward") => { + horiz += dist; + depth += aim * dist; + } + Some("up") => aim -= dist, + Some("down") => aim += dist, + other => panic!("{:#?} is invalid", other), + } + } + + (horiz, depth, aim) +} + +fn parse_input(input: &str) -> Vec<(Option<&str>, i32)> { + input + .lines() + .map(|s| { + let mut it = s.split_whitespace(); + + let dir = it.next(); + let dist = it + .next() + .map(|s| s.parse().ok()) + .flatten() + .expect("parse &str as i32"); + + (dir, dist) + }) + .collect() } pub fn run(input: String) { @@ -15,4 +53,16 @@ pub fn run(input: String) { } #[cfg(test)] -mod tests {} +mod tests { + const PART_1_EXAMPLE_INPUT: &str = "forward 5\ndown 5\nforward 8\nup 3\ndown 8\nforward 2"; + + #[test] + fn part1() { + assert_eq!(super::solve_part1(PART_1_EXAMPLE_INPUT), 150); + } + + #[test] + fn part2() { + assert_eq!(super::solve_part2(PART_1_EXAMPLE_INPUT), 900); + } +} diff --git a/rust/2021/src/day_03.rs b/rust/2021/src/day_03.rs new file mode 100644 index 0000000..9c4c1ee --- /dev/null +++ b/rust/2021/src/day_03.rs @@ -0,0 +1,30 @@ +fn solve_part1(input: &str) -> i32 { + todo!(); +} + +fn solve_part2(input: &str) -> i32 { + todo!(); +} + +pub fn run(input: String) { + println!("--- Part 1 ---"); + println!("Answer: {}", solve_part1(&input)); + println!(); + println!("--- Part 2 ---"); + println!("Answer: {}", solve_part2(&input)) +} + +#[cfg(test)] +mod tests { + const EXAMPLE_INPUT: &str = todo!(); + + #[test] + fn part1() { + todo!(); + } + + #[test] + fn part2() { + todo!(); + } +} diff --git a/rust/2021/src/day_template.rs b/rust/2021/src/day_template.rs new file mode 100644 index 0000000..9c4c1ee --- /dev/null +++ b/rust/2021/src/day_template.rs @@ -0,0 +1,30 @@ +fn solve_part1(input: &str) -> i32 { + todo!(); +} + +fn solve_part2(input: &str) -> i32 { + todo!(); +} + +pub fn run(input: String) { + println!("--- Part 1 ---"); + println!("Answer: {}", solve_part1(&input)); + println!(); + println!("--- Part 2 ---"); + println!("Answer: {}", solve_part2(&input)) +} + +#[cfg(test)] +mod tests { + const EXAMPLE_INPUT: &str = todo!(); + + #[test] + fn part1() { + todo!(); + } + + #[test] + fn part2() { + todo!(); + } +} diff --git a/rust/2021/src/lib.rs b/rust/2021/src/lib.rs index 528d39d..bc1e2ab 100644 --- a/rust/2021/src/lib.rs +++ b/rust/2021/src/lib.rs @@ -1,6 +1,9 @@ mod day_01; mod day_02; +mod day_03; + +pub const INPUT_PATH: &str = "./input/2021/day3.txt"; pub fn run(input: String) { - day_02::run(input); + day_03::run(input); } diff --git a/rust/2021/src/main.rs b/rust/2021/src/main.rs index a7c7479..ab779d7 100644 --- a/rust/2021/src/main.rs +++ b/rust/2021/src/main.rs @@ -1,4 +1,6 @@ +use advent_of_code_2021::INPUT_PATH; + fn main() { - let input = std::fs::read_to_string("./input/2021/day2.txt").expect("open input file"); + let input = std::fs::read_to_string(INPUT_PATH).expect("open input file"); advent_of_code_2021::run(input); }