Finished part 2 of day 2

This commit is contained in:
2021-12-11 23:48:23 +01:00
parent 677e28a60a
commit 75a1991223

View File

@@ -28,7 +28,7 @@ fn main(){
let mut depth = 0;
let mut position = 0;
for (direction, distance) in instructions {
for (direction, distance) in &instructions {
match direction.as_str() {
"forward" => position += distance,
"up" => depth -= distance,
@@ -38,4 +38,27 @@ fn main(){
}
println!("Position of {} * depth of {} equals {}", position, depth, position * depth);
// Now for the second part of day 2
// So instead of just adding to the depth immediately through the up and down commands, we now use
// a so-called aim
depth = 0;
position = 0;
let mut aim = 0;
for (direction, distance) in &instructions {
match direction.as_str() {
"forward" => {
depth += aim * distance;
position += distance
},
"up" => aim -= distance,
"down" => aim += distance,
s => println!("Stumbled upon unknown input: {}", s),
}
}
println!("Position of {} * depth of {} equals {}", position, depth, position * depth);
}