Day 2, part 2
parent
e9d49de28d
commit
778342b2a2
|
@ -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<Position> {
|
||||
|
@ -13,6 +14,7 @@ pub fn track_position(filename: &str) -> io::Result<Position> {
|
|||
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<Position> {
|
|||
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);
|
||||
|
|
Reference in New Issue