dustin
/
aoc2021
Archived
1
0
Fork 0

Day 2, part 2

master
Dustin 2021-12-02 23:10:43 -06:00
parent e9d49de28d
commit 778342b2a2
1 changed files with 5 additions and 2 deletions

View File

@ -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);