feat: complete day 2
This commit is contained in:
parent
ca9eb632b8
commit
faf02e34fa
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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!();
|
||||
}
|
||||
}
|
|
@ -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!();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue