Finished part 2 of day 2
This commit is contained in:
@@ -28,7 +28,7 @@ fn main(){
|
|||||||
|
|
||||||
let mut depth = 0;
|
let mut depth = 0;
|
||||||
let mut position = 0;
|
let mut position = 0;
|
||||||
for (direction, distance) in instructions {
|
for (direction, distance) in &instructions {
|
||||||
match direction.as_str() {
|
match direction.as_str() {
|
||||||
"forward" => position += distance,
|
"forward" => position += distance,
|
||||||
"up" => depth -= distance,
|
"up" => depth -= distance,
|
||||||
@@ -38,4 +38,27 @@ fn main(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
println!("Position of {} * depth of {} equals {}", position, depth, position * depth);
|
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);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user