This commit is contained in:
Dan Ballard 2021-12-04 23:00:54 -08:00
parent 78f3b7cf23
commit bbc116017a
3 changed files with 1039 additions and 1 deletions

1000
input/day02.txt Normal file

File diff suppressed because it is too large Load Diff

37
src/day02.rs Normal file
View File

@ -0,0 +1,37 @@
fn parse_input(input: &str) -> Vec<(&str, i64)> {
input.lines().map(|l| l.split(" ").collect()).map ( |v: Vec<&str>| (v[0], v[1].parse::<i64>().expect("err parsing int"))).collect()
}
fn plot(input: Vec<(&str, i64)>) -> i64 {
input.iter().fold(vec![0,0], |acc: Vec<i64>, v| match v.0 {
"up" => vec!(acc[0] - v.1, acc[1]),
"down" => vec!(acc[0] + v.1, acc[1]),
"forward" => vec!(acc[0], acc[1] + v.1),
_ => {println!("unhandled dir {}", v.0); acc},
}).iter().product::<i64>()
}
fn plot_aim(input: Vec<(&str, i64)>) -> i64 {
let v = input.iter().fold(vec![0,0,0], |acc: Vec<i64>, v| match v.0 {
"up" => vec!(acc[0], acc[1], acc[2] - v.1),
"down" => vec!(acc[0], acc[1], acc[2] + v.1),
"forward" => vec!(acc[0] + v.1, acc[1] + (v.1 * acc[2]), acc[2]),
_ => {println!("unhandled dir {}", v.0); acc},
});
v[0] * v[1]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part_1(){
println!("Part 1 Solution: {}", plot(parse_input( include_str!("../input/day02.txt"))));
}
#[test]
fn part_2(){
println!("Part 2 Solution: {}", plot_aim(parse_input( include_str!("../input/day02.txt"))));
}
}

View File

@ -1 +1,2 @@
pub mod day01;
pub mod day01;
pub mod day02;