From cf6303a5bc1558ebdb7b467da38f74cd3ac3a9b1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 5 Dec 2022 19:46:06 +0100 Subject: Add 2022 --- 2021/day2/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2021/day2/src/main.rs (limited to '2021/day2/src') diff --git a/2021/day2/src/main.rs b/2021/day2/src/main.rs new file mode 100644 index 0000000..e0153a3 --- /dev/null +++ b/2021/day2/src/main.rs @@ -0,0 +1,34 @@ +use std::fs::File; +use std::io::prelude::*; +use std::io::BufReader; + +fn main() { + let file = File::open("input.txt").unwrap(); + let reader = BufReader::new(&file); + let tokens: Vec = reader.lines().map(|x| x.unwrap()).collect::>(); + let mut xpos: u32 = 0; + let mut ypos: u32 = 0; + let mut aim: u32 = 0; + + for line in tokens { + let command: Vec<&str> = line.split(" ").collect(); + let (action, value) = (command[0], command[1].parse::().unwrap()); + + match action { + "forward" => { + xpos += value; + ypos += aim * value; + } + "down" => { + // ypos += value; + aim += value; + } + "up" => { + // ypos -= value; + aim -= value; + } + _ => {} + } + } + println!("{}", ypos * xpos); +} -- cgit v1.2.3-18-g5258