Complete day 2

This commit is contained in:
2021-12-02 00:18:55 -05:00
parent b905c5b69b
commit 51251ea83e
5 changed files with 1097 additions and 0 deletions

1000
crates/day02/src/input/input.txt Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

73
crates/day02/src/main.rs Normal file
View File

@@ -0,0 +1,73 @@
use anyhow::Result;
use common::instrument;
const INPUT: &str = include_str!("input/input.txt");
fn solve_part1(input: &str) -> Result<i32> {
let lines = input.trim().lines();
let mut horizontal = 0;
let mut depth = 0;
for line in lines {
let mut parts = line.split(" ");
let command = parts.next().unwrap();
let value = parts.next().unwrap().parse::<i32>()?;
match command {
"forward" => horizontal += value,
"up" => depth -= value,
"down" => depth += value,
_ => panic!("Unknown command: {}", command),
}
}
Ok(horizontal * depth)
}
fn solve_part2(input: &str) -> Result<i32> {
let lines: Vec<&str> = input.trim().lines().collect();
let mut horizontal = 0;
let mut depth = 0;
let mut aim = 0;
for line in lines {
let mut parts = line.split(" ");
let command = parts.next().unwrap();
let value = parts.next().unwrap().parse::<i32>()?;
match command {
"forward" => {
horizontal += value;
depth += aim * value;
}
"up" => aim -= value,
"down" => aim += value,
_ => panic!("Unknown command: {}", command),
}
}
Ok(horizontal * depth)
}
fn main() {
instrument!(solve_part1(INPUT).unwrap(), solve_part2(INPUT).unwrap());
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_INPUT: &str = include_str!("input/test.txt");
#[test]
fn solves_part1() {
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 150);
}
#[test]
fn solves_part2() {
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 900);
}
}