diff options
author | Irene Knapp <ireneista@gmail.com> | 2021-12-01 21:20:09 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2021-12-01 21:20:09 -0800 |
commit | 7573a5ecb3efcc690182cfc9a3948e01df7b7442 (patch) | |
tree | bfd74f252683dfee60f0659204ef42be6421e8b5 /02/src | |
parent | aed6777886ca340e392a039150a3465e6436e0f8 (diff) |
02
Diffstat (limited to '02/src')
-rw-r--r-- | 02/src/main.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/02/src/main.rs b/02/src/main.rs new file mode 100644 index 0000000..92ba083 --- /dev/null +++ b/02/src/main.rs @@ -0,0 +1,70 @@ +use advent_lib::prelude::*; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + { + let mut depth = 0; + let mut horizontal = 0; + + for line in &input { + let mut words = line.split(' '); + let command = words.next().unwrap(); + let parameter = words.next().unwrap().parse::<i64>().unwrap(); + + match command { + "forward" => { + horizontal += parameter; + }, + "down" => { + depth += parameter; + }, + "up" => { + depth -= parameter; + }, + _ => { }, + } + } + + println!("{}", horizontal * depth); + } + + { + let mut aim = 0; + let mut depth = 0; + let mut horizontal = 0; + + for line in &input { + let mut words = line.split(' '); + let command = words.next().unwrap(); + let parameter = words.next().unwrap().parse::<i64>().unwrap(); + + match command { + "forward" => { + horizontal += parameter; + depth += aim * parameter; + }, + "down" => { + aim += parameter; + }, + "up" => { + aim -= parameter; + }, + _ => { }, + } + } + + println!("{}", horizontal * depth); + } + + Ok(()) +} + |