day 2
This commit is contained in:
parent
78f3b7cf23
commit
bbc116017a
File diff suppressed because it is too large
Load Diff
|
@ -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"))));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
pub mod day01;
|
pub mod day01;
|
||||||
|
pub mod day02;
|
Loading…
Reference in New Issue