From 7573a5ecb3efcc690182cfc9a3948e01df7b7442 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 1 Dec 2021 21:20:09 -0800 Subject: 02 --- 02/src/main.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 02/src/main.rs (limited to '02/src') 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::().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::().unwrap(); + + match command { + "forward" => { + horizontal += parameter; + depth += aim * parameter; + }, + "down" => { + aim += parameter; + }, + "up" => { + aim -= parameter; + }, + _ => { }, + } + } + + println!("{}", horizontal * depth); + } + + Ok(()) +} + -- cgit 1.4.1