Created
December 2, 2021 13:00
-
-
Save social-anthrax/ee6f096de7ff524a6a0299d991b9eb7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::task_handler::get_task; | |
pub fn task2_1() -> String { | |
let input = get_task(2); | |
let mut depth = 0; | |
let mut horizontal = 0; | |
for line in input.lines() { | |
match line.split(' ').collect::<Vec<&str>>()[..] { | |
["forward", x] => horizontal = horizontal + x.parse::<i32>().unwrap(), | |
["down", x] => depth = depth + x.parse::<i32>().unwrap(), | |
["up", x] => depth = depth - x.parse::<i32>().unwrap(), | |
_ => panic!("Unrecognised instruction"), | |
} | |
} | |
return (depth * horizontal).to_string(); | |
} | |
pub fn task2_2() -> String { | |
let input = get_task(2); | |
let mut depth = 0; | |
let mut horizontal = 0; | |
let mut aim = 0; | |
for line in input.lines() { | |
match line.split(' ').collect::<Vec<&str>>()[..] { | |
["forward", x] => { | |
horizontal = horizontal + x.parse::<i32>().unwrap(); | |
depth = depth + (aim * x.parse::<i32>().unwrap()); | |
} | |
["down", x] => aim = aim + x.parse::<i32>().unwrap(), | |
["up", x] => aim = aim - x.parse::<i32>().unwrap(), | |
_ => panic!("Unrecognised instruction"), | |
} | |
} | |
return (depth * horizontal).to_string(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment