diff --git a/advent_of_code/2.rs b/advent_of_code/2.rs index f8b800b..c7fbab1 100644 --- a/advent_of_code/2.rs +++ b/advent_of_code/2.rs @@ -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); } \ No newline at end of file