From 5f574cf344f579787f3ba6a65831a8e80532acbd Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 6 Dec 2021 22:19:46 +0100 Subject: Add day2 --- day2/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day2/src/main.rs (limited to 'day2/src') diff --git a/day2/src/main.rs b/day2/src/main.rs new file mode 100644 index 0000000..e0153a3 --- /dev/null +++ b/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