From 28223a1b34ac2764debd0b4dcd1157aa17dbf73b Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Fri, 3 Dec 2021 01:24:33 +0200 Subject: [PATCH] feat: solve day 2 part 1 --- input.txt => input1.txt | 0 input2.txt | 1000 +++++++++++++++++++++++++++++++++++++++ src/day2.rs | 74 +++ src/main.rs | 18 +- 4 files changed, 1086 insertions(+), 6 deletions(-) rename input.txt => input1.txt (100%) create mode 100644 input2.txt create mode 100644 src/day2.rs diff --git a/input.txt b/input1.txt similarity index 100% rename from input.txt rename to input1.txt diff --git a/input2.txt b/input2.txt new file mode 100644 index 0000000..9e714f9 --- /dev/null +++ b/input2.txt @@ -0,0 +1,1000 @@ +forward 2 +down 8 +down 1 +up 7 +forward 4 +down 4 +down 3 +forward 2 +forward 5 +forward 2 +forward 7 +forward 8 +down 7 +forward 6 +forward 1 +down 8 +down 8 +up 9 +down 9 +forward 1 +up 1 +forward 6 +forward 7 +down 7 +forward 4 +forward 2 +forward 1 +forward 3 +forward 8 +forward 4 +up 6 +down 2 +forward 4 +down 3 +up 3 +up 2 +up 3 +forward 8 +down 5 +down 8 +down 5 +down 4 +down 2 +up 1 +forward 6 +forward 6 +forward 8 +up 5 +down 5 +forward 8 +forward 7 +down 9 +down 7 +down 6 +down 3 +forward 3 +up 7 +forward 2 +up 6 +forward 7 +forward 9 +down 9 +forward 3 +down 2 +down 2 +down 7 +down 7 +forward 8 +down 7 +forward 9 +up 7 +down 4 +down 8 +forward 2 +forward 2 +up 1 +forward 8 +down 5 +forward 8 +forward 4 +up 1 +forward 2 +forward 2 +forward 2 +down 9 +down 7 +down 9 +forward 9 +forward 4 +up 7 +down 4 +forward 9 +forward 8 +up 5 +up 1 +down 1 +down 9 +down 6 +up 8 +forward 2 +forward 7 +up 7 +forward 7 +forward 6 +down 6 +forward 8 +up 6 +forward 7 +down 7 +forward 4 +forward 9 +up 9 +up 8 +forward 8 +forward 3 +up 1 +up 4 +down 4 +up 9 +up 8 +forward 6 +down 2 +down 2 +up 4 +forward 4 +forward 1 +down 8 +forward 6 +down 5 +forward 6 +down 8 +up 1 +forward 1 +up 9 +down 8 +up 3 +up 9 +forward 9 +forward 2 +down 2 +up 6 +up 6 +forward 8 +up 3 +down 3 +forward 5 +up 2 +forward 1 +forward 1 +forward 8 +down 4 +forward 8 +forward 7 +down 8 +forward 7 +down 7 +down 2 +down 1 +down 7 +up 9 +down 5 +up 1 +forward 7 +down 5 +down 9 +down 2 +down 8 +down 4 +forward 2 +forward 2 +forward 1 +down 6 +up 7 +forward 2 +down 1 +down 5 +forward 3 +forward 8 +down 4 +up 2 +up 9 +up 7 +forward 7 +forward 4 +up 8 +up 3 +up 4 +forward 6 +down 7 +forward 7 +up 6 +down 9 +up 6 +forward 4 +up 3 +down 3 +up 6 +down 9 +down 6 +forward 7 +forward 9 +forward 2 +down 6 +up 3 +up 9 +forward 9 +forward 8 +up 4 +up 5 +forward 6 +down 5 +up 4 +up 9 +down 8 +forward 5 +up 5 +forward 7 +forward 6 +down 8 +down 5 +down 2 +up 5 +down 8 +forward 7 +forward 7 +up 6 +forward 3 +down 8 +forward 5 +forward 8 +down 7 +forward 6 +down 2 +forward 2 +forward 8 +down 4 +forward 5 +forward 7 +forward 5 +forward 8 +forward 9 +forward 6 +down 6 +up 7 +down 9 +forward 7 +forward 1 +up 2 +forward 8 +down 1 +up 9 +forward 7 +down 2 +up 2 +forward 5 +down 4 +down 3 +up 8 +up 6 +forward 3 +up 4 +forward 3 +forward 1 +forward 1 +up 9 +down 2 +down 9 +up 4 +forward 4 +forward 4 +forward 2 +forward 5 +forward 9 +forward 7 +up 4 +up 5 +down 5 +forward 9 +down 1 +forward 2 +down 6 +forward 9 +down 4 +down 2 +forward 2 +up 7 +forward 7 +forward 8 +up 9 +forward 3 +up 1 +down 5 +forward 5 +down 1 +up 5 +forward 4 +forward 5 +up 3 +down 4 +up 6 +up 1 +down 2 +forward 6 +down 8 +up 9 +down 7 +forward 9 +down 8 +forward 3 +forward 8 +down 1 +down 7 +forward 6 +up 6 +down 1 +down 5 +forward 6 +down 7 +down 2 +forward 6 +forward 2 +forward 8 +forward 7 +forward 2 +down 7 +up 2 +down 7 +forward 8 +forward 8 +forward 2 +forward 4 +down 1 +down 6 +down 1 +down 4 +down 4 +down 2 +down 7 +up 5 +up 5 +down 1 +forward 3 +up 1 +down 3 +forward 9 +forward 4 +forward 7 +down 4 +down 4 +down 2 +forward 2 +forward 1 +forward 9 +down 1 +down 4 +down 1 +forward 4 +up 8 +forward 3 +down 6 +forward 5 +forward 9 +forward 1 +up 8 +down 7 +down 8 +forward 4 +down 4 +up 5 +down 3 +forward 3 +down 6 +down 1 +down 9 +forward 8 +up 5 +down 7 +up 7 +forward 3 +up 5 +up 7 +down 4 +up 2 +down 2 +down 8 +up 6 +down 4 +up 1 +down 8 +down 4 +forward 7 +down 4 +up 3 +down 9 +forward 3 +up 4 +up 1 +up 5 +down 4 +forward 4 +forward 4 +down 2 +down 3 +down 4 +forward 2 +down 9 +down 9 +down 6 +forward 5 +forward 7 +down 3 +forward 2 +up 8 +down 6 +down 8 +down 2 +up 9 +down 1 +forward 8 +forward 1 +forward 8 +up 4 +up 1 +down 8 +up 2 +forward 5 +down 3 +forward 5 +forward 4 +forward 1 +down 9 +forward 2 +forward 5 +forward 4 +forward 3 +down 7 +up 9 +forward 7 +up 8 +forward 3 +forward 3 +down 5 +forward 7 +forward 5 +down 4 +forward 6 +down 6 +up 3 +down 3 +forward 7 +forward 8 +up 3 +up 8 +forward 8 +up 4 +forward 5 +up 4 +down 7 +forward 4 +down 8 +down 6 +forward 7 +down 4 +up 7 +down 6 +up 5 +down 2 +forward 7 +down 7 +up 3 +up 3 +down 3 +down 5 +forward 3 +forward 5 +forward 8 +down 3 +down 8 +forward 3 +down 1 +forward 4 +up 5 +forward 8 +up 1 +up 4 +down 4 +forward 1 +forward 6 +up 7 +up 1 +up 7 +down 5 +forward 8 +down 1 +forward 1 +forward 6 +up 9 +up 6 +down 4 +forward 5 +down 1 +forward 7 +down 8 +up 3 +down 9 +down 1 +up 7 +up 6 +forward 8 +down 9 +down 4 +forward 4 +up 7 +down 3 +forward 5 +forward 8 +up 5 +down 7 +up 2 +forward 3 +forward 3 +forward 4 +down 1 +forward 5 +forward 7 +up 7 +forward 7 +forward 1 +forward 3 +forward 9 +down 3 +forward 5 +down 9 +down 7 +down 4 +down 8 +up 3 +down 1 +up 5 +down 5 +forward 2 +down 6 +down 5 +forward 2 +forward 6 +down 6 +up 6 +down 8 +up 1 +forward 4 +forward 2 +forward 1 +up 1 +forward 5 +forward 3 +forward 7 +down 9 +forward 3 +down 9 +up 5 +down 7 +down 2 +down 3 +up 5 +up 3 +down 2 +forward 4 +forward 1 +down 3 +up 4 +down 8 +down 1 +forward 6 +down 3 +forward 9 +down 7 +down 3 +down 4 +down 5 +up 1 +forward 8 +forward 2 +up 3 +up 7 +up 1 +forward 3 +forward 9 +up 5 +forward 6 +down 8 +down 9 +down 5 +forward 6 +forward 3 +forward 8 +forward 7 +forward 9 +forward 2 +up 9 +forward 8 +down 2 +down 6 +down 9 +down 5 +forward 1 +down 7 +forward 2 +down 4 +down 1 +up 1 +down 2 +forward 5 +forward 8 +down 9 +up 6 +forward 8 +forward 3 +up 6 +up 2 +forward 8 +down 8 +up 1 +down 6 +down 2 +down 6 +forward 5 +forward 6 +down 4 +forward 1 +down 4 +up 7 +forward 4 +up 1 +forward 9 +forward 7 +up 3 +down 7 +down 4 +forward 3 +forward 8 +forward 6 +forward 6 +up 1 +up 2 +up 8 +down 7 +forward 8 +down 5 +forward 6 +down 1 +down 9 +down 2 +forward 6 +up 3 +down 5 +down 6 +forward 1 +forward 2 +down 3 +forward 8 +forward 1 +up 5 +down 8 +down 4 +up 9 +up 2 +forward 7 +forward 9 +up 8 +up 6 +forward 1 +down 7 +up 1 +down 3 +forward 2 +forward 3 +down 3 +down 2 +forward 3 +down 7 +forward 3 +forward 7 +forward 7 +down 8 +down 4 +forward 3 +forward 4 +down 7 +down 9 +down 4 +down 2 +forward 7 +up 8 +down 4 +down 3 +forward 9 +down 5 +up 6 +up 2 +down 5 +down 6 +forward 2 +forward 8 +down 1 +forward 6 +up 7 +down 6 +forward 4 +down 2 +down 5 +down 9 +forward 7 +up 4 +forward 9 +up 7 +down 4 +down 6 +up 9 +forward 1 +up 7 +down 5 +forward 3 +forward 3 +down 7 +down 1 +down 7 +down 7 +down 1 +forward 8 +forward 9 +forward 8 +down 9 +down 8 +down 5 +down 3 +forward 4 +forward 1 +down 1 +forward 1 +down 7 +forward 7 +forward 3 +down 8 +forward 3 +forward 9 +forward 8 +down 7 +forward 8 +down 2 +up 5 +forward 7 +forward 4 +down 2 +up 6 +up 8 +forward 7 +down 9 +up 3 +forward 4 +up 9 +up 5 +up 5 +up 6 +down 4 +down 5 +up 5 +forward 7 +forward 6 +down 4 +forward 5 +forward 4 +up 7 +forward 8 +down 2 +forward 1 +down 9 +down 8 +forward 7 +down 7 +down 1 +forward 9 +down 7 +forward 6 +down 2 +up 9 +forward 1 +up 9 +down 3 +up 9 +down 1 +forward 1 +down 1 +up 6 +down 8 +up 2 +down 3 +forward 1 +down 4 +up 5 +down 5 +down 4 +forward 5 +forward 4 +down 9 +up 7 +down 7 +forward 7 +forward 6 +forward 8 +down 8 +forward 8 +down 1 +down 1 +down 8 +down 2 +up 2 +up 1 +forward 5 +down 1 +up 5 +up 2 +down 6 +up 8 +forward 5 +down 8 +down 1 +up 5 +down 1 +forward 4 +down 6 +down 4 +forward 2 +forward 2 +down 1 +up 4 +up 8 +down 6 +down 2 +forward 5 +forward 8 +forward 7 +down 5 +down 7 +down 3 +forward 6 +down 3 +down 3 +forward 6 +forward 6 +forward 6 +up 7 +forward 1 +down 5 +down 2 +up 8 +forward 6 +down 7 +down 6 +forward 1 +up 5 +down 4 +up 9 +forward 3 +up 3 +forward 9 +forward 9 +forward 7 +forward 5 +down 9 +forward 1 +forward 6 +up 8 +down 7 +forward 9 +forward 5 +up 4 +down 8 +forward 8 +forward 4 +down 9 +up 2 +forward 5 +forward 8 +down 8 +down 9 +down 9 +forward 4 +forward 8 +down 5 +down 5 +forward 5 +forward 5 +up 9 +up 7 +forward 3 +up 4 +down 8 +up 6 +up 6 +down 4 +down 3 +forward 2 +forward 9 +down 4 +down 2 +forward 4 +up 9 +forward 1 +down 8 +down 9 +down 9 +down 3 +forward 1 +down 5 +up 9 +forward 6 +up 4 +forward 2 +forward 2 +forward 4 +down 9 +up 5 +up 1 +down 6 +forward 7 +down 8 +forward 4 +forward 9 +up 9 +up 4 +down 5 +down 3 +forward 2 +down 8 +down 6 +forward 3 +down 2 +forward 6 +up 2 +forward 6 +down 7 +up 4 +forward 1 +forward 4 +up 4 +forward 6 +forward 8 +down 7 +down 6 +up 7 +down 2 +down 4 +down 5 +forward 1 +up 4 +forward 8 +forward 6 +down 8 +up 5 +up 2 +up 9 +up 5 +forward 6 +down 4 +up 3 +down 8 +down 6 +down 2 +up 3 +up 5 +down 1 +forward 9 +up 8 +up 2 +down 3 +forward 6 +down 1 +forward 5 +down 3 +up 1 +up 2 +down 5 +down 7 +forward 8 +down 8 +up 9 +forward 3 +down 8 +down 8 +forward 1 +down 4 +down 4 +forward 3 +up 6 +down 3 +down 7 +down 7 +up 1 +forward 3 +forward 2 diff --git a/src/day2.rs b/src/day2.rs new file mode 100644 index 0000000..a4d4b84 --- /dev/null +++ b/src/day2.rs @@ -0,0 +1,74 @@ +use std::fs::File; +use std::io::{prelude::*, self}; +use std::num::ParseIntError; + +#[derive(Debug)] +pub enum InputFromFileError { + ParseCommandError, + ParseIntError(ParseIntError), + IoError(io::Error), +} + +pub enum Command { + Forward, + Down, + Up +} + +pub struct CommandLine(Command, u32); + +fn parse_line(line: &str) -> Result { + let parts: Vec<&str> = line.split(' ').collect(); + let command = match parts[0] { + "up" => Ok(Command::Up), + "down" => Ok(Command::Down), + "forward" => Ok(Command::Forward), + _ => Err(InputFromFileError::ParseCommandError) + }?; + let amount = parts[1].parse().map_err(InputFromFileError::ParseIntError)?; + Ok(CommandLine(command, amount)) +} + +pub fn input_from_file(filename: &str) -> Result, InputFromFileError> { + let mut file = File::open(filename).map_err(InputFromFileError::IoError)?; + + let mut contents = String::new(); + file.read_to_string(&mut contents).map_err(InputFromFileError::IoError)?; + + contents.split_terminator('\n') + .map(parse_line) + .collect() +} + +pub fn part1(commands: &[CommandLine]) -> u32 { + let mut depth = 0; + let mut horizontal = 0; + for command in commands { + match command.0 { + Command::Up => depth -= command.1, + Command::Down => depth += command.1, + Command::Forward => horizontal += command.1, + } + } + return depth * horizontal; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_example() { + let commands = [ + CommandLine(Command::Forward, 5), + CommandLine(Command::Down, 5), + CommandLine(Command::Forward, 8), + CommandLine(Command::Up, 3), + CommandLine(Command::Down, 8), + CommandLine(Command::Forward, 2) + ]; + let result = part1(&commands); + assert_eq!(result, 150); + } +} + diff --git a/src/main.rs b/src/main.rs index a65299e..f35762e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,18 @@ mod day1; +mod day2; fn main() { - let input_filename = "input.txt"; - let input = day1::input_from_file(input_filename) - .expect("Failed to read input.txt"); - let result1 = day1::part1(&input); - let result2 = day1::part2(&input); + let input_filename = "input2.txt"; + let input = day2::input_from_file(input_filename) + .expect("Failed to read input2.txt"); + + // let result1 = day1::part1(&input); + // let result2 = day1::part2(&input); + + let result1 = day2::part1(&input); + // let result2 = day2::part2(&input); + println!("Part 1 result: {}", result1); - println!("Part 2 result: {}", result2); + // println!("Part 2 result: {}", result2); }