diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-12-06 22:19:46 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-12-06 22:19:46 +0100 |
commit | 5f574cf344f579787f3ba6a65831a8e80532acbd (patch) | |
tree | 2cc5bb3bd99859f941114709bfbbde69c10f8eba /day2/src | |
parent | f754fb2b193bfd17a7af51fae32f0805a1f16d0b (diff) |
Add day2
Diffstat (limited to 'day2/src')
-rw-r--r-- | day2/src/main.rs | 34 |
1 files changed, 34 insertions, 0 deletions
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<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); +} |