From 778342b2a296feebaca9ce543648d62810faf7b6 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 2 Dec 2021 23:10:43 -0600 Subject: [PATCH] Day 2, part 2 --- src/pilot.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pilot.rs b/src/pilot.rs index 7925cf6..177b401 100644 --- a/src/pilot.rs +++ b/src/pilot.rs @@ -5,6 +5,7 @@ use std::io::prelude::*; pub struct Position { pub linear: i32, pub depth: i32, + aim: i32, } pub fn track_position(filename: &str) -> io::Result { @@ -13,6 +14,7 @@ pub fn track_position(filename: &str) -> io::Result { let mut pos = Position { linear: 0, depth: 0, + aim: 0, }; for line in buf.lines() { if let Ok(line) = line { @@ -23,12 +25,13 @@ pub fn track_position(filename: &str) -> io::Result { match op { "forward" => { pos.linear += value; + pos.depth += pos.aim * value; } "down" => { - pos.depth += value; + pos.aim += value; } "up" => { - pos.depth -= value; + pos.aim -= value; } _ => { eprintln!("Invalid operation: {}", op);