diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-12-05 19:46:06 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-12-05 19:46:06 +0100 |
commit | cf6303a5bc1558ebdb7b467da38f74cd3ac3a9b1 (patch) | |
tree | 6292cb239a8cf114179c6e7c8b3015840dfbae6e /2021/day2/src | |
parent | 6e10cc2773fcaff64902b13f44443da014f38be7 (diff) |
Add 2022
Diffstat (limited to '2021/day2/src')
-rw-r--r-- | 2021/day2/src/main.rs | 34 |
1 files changed, 34 insertions, 0 deletions
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<String> = reader.lines().map(|x| x.unwrap()).collect::<Vec<String>>(); + 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::<u32>().unwrap()); + + match action { + "forward" => { + xpos += value; + ypos += aim * value; + } + "down" => { + // ypos += value; + aim += value; + } + "up" => { + // ypos -= value; + aim -= value; + } + _ => {} + } + } + println!("{}", ypos * xpos); +} |