Skip to content

Instantly share code, notes, and snippets.

@social-anthrax
Created December 2, 2021 13:00
Show Gist options
  • Save social-anthrax/ee6f096de7ff524a6a0299d991b9eb7e to your computer and use it in GitHub Desktop.
Save social-anthrax/ee6f096de7ff524a6a0299d991b9eb7e to your computer and use it in GitHub Desktop.
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