Day 2, part 2
parent
e9d49de28d
commit
778342b2a2
|
@ -5,6 +5,7 @@ use std::io::prelude::*;
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub linear: i32,
|
pub linear: i32,
|
||||||
pub depth: i32,
|
pub depth: i32,
|
||||||
|
aim: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn track_position(filename: &str) -> io::Result<Position> {
|
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 {
|
let mut pos = Position {
|
||||||
linear: 0,
|
linear: 0,
|
||||||
depth: 0,
|
depth: 0,
|
||||||
|
aim: 0,
|
||||||
};
|
};
|
||||||
for line in buf.lines() {
|
for line in buf.lines() {
|
||||||
if let Ok(line) = line {
|
if let Ok(line) = line {
|
||||||
|
@ -23,12 +25,13 @@ pub fn track_position(filename: &str) -> io::Result<Position> {
|
||||||
match op {
|
match op {
|
||||||
"forward" => {
|
"forward" => {
|
||||||
pos.linear += value;
|
pos.linear += value;
|
||||||
|
pos.depth += pos.aim * value;
|
||||||
}
|
}
|
||||||
"down" => {
|
"down" => {
|
||||||
pos.depth += value;
|
pos.aim += value;
|
||||||
}
|
}
|
||||||
"up" => {
|
"up" => {
|
||||||
pos.depth -= value;
|
pos.aim -= value;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Invalid operation: {}", op);
|
eprintln!("Invalid operation: {}", op);
|
||||||
|
|
Reference in New Issue