From e659170d7e45c3482fe72426578cf76e115fb65e Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Thu, 30 Dec 2021 23:05:14 +0100 Subject: [PATCH] day02 task2 - still poor but working :sweat: --- day02/src/main.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/day02/src/main.rs b/day02/src/main.rs index d267694..e134604 100644 --- a/day02/src/main.rs +++ b/day02/src/main.rs @@ -3,8 +3,9 @@ use std::io::{self, BufRead}; use std::path::Path; fn main() { - let mut depth = 0; - let mut pos_h = 0; + let mut depth: i32 = 0; + let mut pos_h: i32 = 0; + let mut aim: i32 = 0; if let Ok(lines) = read_lines("./data/input") { for line in lines { if let Ok(str) = line { @@ -12,21 +13,27 @@ fn main() { let direction = words[0]; let number = words[1].parse::().unwrap(); - match direction { - "down" => depth = depth + number, - "up" => depth = depth - number, - "forward" => pos_h = pos_h + number, - _ => eprintln!("UNKNOWN!!!!!"), - + if direction == "down" { + aim += number; + } else if direction == "up" { + aim -= number; + } else if direction == "forward" { + depth += number * aim; + pos_h += number; + } else { + eprintln!("UNKNOWN!!!!!") } } } } - println!("{}", depth * pos_h); + let result = depth * pos_h; + println!("{}", result); } fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) }