From 2b884fab65f28313f726e96a06ae225f0606d1ad Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Thu, 2 Dec 2021 08:50:56 +0100 Subject: [PATCH] Day 2 --- input/day02 | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/day02.rs | 102 +++++ src/main.rs | 4 +- 3 files changed, 1105 insertions(+), 1 deletion(-) create mode 100644 input/day02 create mode 100644 src/day02.rs diff --git a/input/day02 b/input/day02 new file mode 100644 index 0000000..e8377df --- /dev/null +++ b/input/day02 @@ -0,0 +1,1000 @@ +forward 1 +down 9 +down 4 +forward 4 +down 2 +down 7 +up 9 +down 5 +forward 9 +forward 9 +down 2 +forward 7 +forward 9 +down 5 +down 2 +up 9 +down 7 +forward 6 +down 6 +down 6 +up 1 +down 2 +forward 8 +forward 8 +forward 9 +up 1 +forward 4 +up 1 +forward 3 +forward 5 +up 3 +up 7 +down 4 +down 1 +forward 3 +forward 4 +up 3 +forward 9 +down 2 +forward 5 +up 2 +up 1 +up 5 +forward 1 +up 9 +forward 6 +up 4 +down 4 +forward 3 +up 7 +down 1 +down 1 +down 8 +forward 3 +forward 1 +up 7 +up 1 +down 4 +up 6 +down 6 +forward 8 +down 3 +down 9 +down 6 +up 8 +forward 8 +down 9 +up 7 +down 7 +forward 8 +down 8 +up 6 +down 7 +down 4 +up 7 +forward 7 +forward 7 +forward 2 +forward 5 +down 5 +down 9 +down 8 +down 5 +up 3 +down 3 +down 6 +down 6 +forward 1 +down 2 +forward 2 +down 5 +up 2 +forward 5 +down 1 +down 3 +forward 4 +down 2 +forward 8 +down 6 +forward 3 +forward 3 +forward 9 +down 9 +down 4 +down 8 +down 7 +down 3 +up 6 +down 7 +forward 9 +up 9 +down 8 +forward 9 +forward 8 +down 2 +up 4 +forward 8 +up 7 +down 2 +forward 6 +forward 7 +down 5 +up 4 +down 3 +down 3 +down 5 +down 2 +down 5 +forward 8 +forward 8 +down 5 +up 4 +down 6 +forward 5 +up 6 +up 5 +down 4 +forward 7 +forward 5 +down 7 +up 9 +up 4 +up 9 +forward 8 +down 8 +down 8 +down 2 +down 8 +down 8 +up 6 +forward 2 +down 9 +forward 5 +forward 4 +down 8 +up 5 +forward 3 +forward 1 +down 6 +forward 9 +forward 2 +forward 1 +up 5 +down 2 +forward 5 +up 6 +up 6 +forward 9 +up 3 +forward 2 +forward 9 +down 9 +down 5 +up 4 +up 7 +up 8 +forward 4 +forward 3 +down 7 +down 5 +forward 5 +down 6 +forward 9 +up 4 +up 6 +down 2 +forward 6 +forward 7 +up 9 +up 5 +down 4 +up 5 +down 2 +forward 4 +up 9 +down 1 +forward 7 +down 9 +forward 5 +down 5 +down 2 +up 4 +up 8 +down 8 +down 7 +up 7 +down 1 +forward 5 +forward 8 +down 2 +down 1 +forward 8 +down 7 +up 6 +forward 9 +forward 4 +down 8 +forward 1 +up 8 +forward 3 +up 4 +forward 9 +down 6 +forward 5 +forward 1 +forward 8 +forward 5 +forward 8 +down 4 +forward 8 +forward 2 +forward 8 +forward 3 +down 3 +forward 6 +up 6 +up 9 +down 4 +down 6 +forward 6 +down 9 +up 2 +down 8 +down 6 +down 1 +forward 1 +down 2 +down 3 +forward 5 +forward 6 +down 5 +up 7 +up 2 +forward 2 +down 4 +down 4 +forward 3 +down 4 +up 4 +forward 1 +down 4 +forward 9 +forward 7 +forward 6 +down 3 +down 4 +forward 3 +forward 3 +forward 3 +up 6 +forward 4 +forward 7 +forward 3 +forward 5 +forward 7 +forward 4 +down 1 +up 2 +up 7 +forward 4 +down 2 +forward 2 +up 7 +down 7 +forward 5 +forward 9 +down 8 +forward 5 +forward 5 +down 9 +forward 1 +forward 6 +down 2 +down 3 +down 1 +down 7 +down 3 +up 3 +down 3 +forward 5 +down 4 +forward 6 +forward 2 +down 4 +forward 4 +up 5 +up 3 +forward 2 +forward 5 +down 7 +up 9 +up 5 +down 3 +forward 8 +forward 4 +down 8 +up 4 +forward 8 +forward 7 +forward 1 +down 2 +down 6 +up 7 +forward 9 +down 2 +down 4 +down 3 +down 8 +up 4 +down 2 +up 3 +forward 1 +down 2 +up 6 +down 9 +up 3 +down 1 +down 1 +forward 4 +down 2 +forward 3 +forward 5 +forward 7 +down 7 +up 3 +up 5 +down 1 +down 2 +forward 4 +down 1 +down 1 +down 7 +up 4 +forward 3 +down 3 +forward 2 +down 3 +up 7 +down 1 +up 5 +down 7 +forward 4 +up 2 +forward 5 +up 4 +down 4 +down 3 +forward 1 +up 9 +down 5 +forward 9 +forward 1 +down 7 +down 2 +down 4 +down 2 +forward 3 +down 1 +down 2 +up 9 +forward 2 +down 8 +forward 1 +forward 3 +down 5 +down 4 +forward 2 +forward 8 +forward 9 +down 9 +down 6 +up 3 +forward 1 +down 6 +down 7 +up 3 +forward 9 +down 2 +up 4 +forward 3 +forward 6 +forward 6 +down 7 +forward 7 +forward 6 +down 7 +up 9 +forward 7 +down 1 +down 8 +down 2 +down 2 +down 9 +down 6 +forward 1 +down 9 +forward 9 +forward 8 +forward 6 +forward 9 +down 5 +forward 9 +forward 4 +forward 4 +down 6 +forward 6 +forward 5 +forward 7 +down 7 +down 9 +forward 4 +down 9 +up 5 +forward 6 +down 6 +forward 9 +up 7 +forward 6 +up 6 +up 9 +forward 9 +up 4 +down 3 +up 9 +down 8 +down 2 +forward 4 +down 8 +down 2 +forward 2 +down 5 +up 3 +down 1 +forward 4 +down 3 +forward 3 +down 9 +forward 6 +forward 6 +down 9 +forward 6 +forward 1 +down 5 +up 1 +forward 3 +forward 1 +forward 5 +down 8 +forward 7 +forward 2 +up 3 +forward 8 +forward 1 +forward 7 +down 1 +down 4 +forward 7 +forward 9 +forward 4 +down 2 +forward 5 +forward 3 +forward 9 +forward 3 +up 3 +forward 9 +down 3 +forward 9 +forward 3 +forward 7 +down 5 +forward 6 +down 6 +forward 5 +forward 1 +down 1 +forward 8 +down 3 +up 1 +down 6 +forward 7 +up 5 +up 7 +down 3 +forward 8 +up 8 +down 4 +up 6 +up 7 +forward 1 +forward 2 +forward 7 +down 1 +forward 4 +down 8 +up 6 +up 7 +up 4 +down 7 +down 8 +down 6 +forward 5 +forward 9 +down 7 +forward 8 +down 9 +down 9 +forward 5 +forward 2 +up 4 +down 2 +forward 3 +down 2 +forward 1 +down 7 +down 6 +forward 5 +up 2 +forward 4 +down 4 +forward 5 +forward 7 +up 5 +down 7 +forward 9 +forward 3 +forward 6 +up 8 +down 9 +forward 1 +up 8 +forward 3 +down 6 +down 1 +down 3 +down 4 +up 2 +up 3 +forward 8 +up 7 +down 2 +forward 1 +down 6 +forward 1 +down 8 +forward 3 +forward 5 +forward 5 +up 5 +forward 4 +forward 6 +down 8 +up 7 +forward 8 +up 6 +forward 5 +down 4 +forward 8 +down 3 +forward 7 +forward 9 +forward 8 +up 9 +forward 4 +up 5 +up 8 +down 8 +forward 1 +down 1 +forward 3 +forward 9 +down 9 +up 2 +forward 5 +down 1 +down 6 +forward 2 +forward 2 +down 2 +down 8 +forward 5 +up 2 +up 2 +forward 3 +down 6 +up 2 +down 2 +down 1 +up 9 +forward 6 +up 4 +forward 4 +down 2 +forward 8 +up 9 +down 1 +forward 9 +up 3 +up 7 +forward 5 +down 9 +down 2 +down 5 +up 4 +up 8 +up 2 +down 1 +up 4 +forward 8 +forward 2 +down 8 +forward 9 +forward 1 +down 5 +forward 3 +down 6 +forward 3 +up 2 +down 9 +down 6 +down 4 +down 9 +down 8 +forward 2 +forward 7 +up 4 +forward 8 +up 9 +up 6 +forward 2 +down 1 +down 5 +down 5 +down 3 +up 9 +up 9 +forward 9 +forward 5 +down 1 +down 3 +down 8 +down 4 +forward 5 +up 4 +down 2 +down 5 +down 4 +down 8 +down 2 +forward 9 +forward 7 +up 9 +forward 8 +down 9 +down 2 +forward 5 +down 9 +forward 7 +down 2 +down 9 +forward 4 +up 7 +down 9 +forward 6 +forward 2 +down 1 +down 9 +down 3 +down 7 +down 8 +down 5 +down 9 +down 1 +up 9 +down 7 +forward 7 +up 4 +down 2 +down 4 +forward 6 +forward 8 +forward 1 +forward 2 +up 2 +up 2 +forward 9 +down 9 +forward 8 +forward 6 +forward 5 +down 2 +forward 7 +up 3 +up 8 +forward 1 +forward 5 +down 7 +down 7 +forward 5 +down 1 +up 3 +up 8 +forward 5 +forward 7 +forward 3 +down 6 +up 7 +forward 8 +down 5 +up 3 +forward 7 +down 2 +down 1 +down 1 +forward 1 +down 1 +down 8 +down 3 +up 1 +forward 8 +forward 9 +forward 5 +forward 8 +down 2 +forward 3 +down 5 +up 9 +down 9 +down 5 +forward 1 +up 9 +up 4 +up 7 +up 9 +up 4 +down 2 +forward 6 +forward 4 +up 5 +up 6 +down 8 +down 6 +down 6 +up 3 +forward 3 +down 9 +down 5 +down 5 +forward 7 +down 6 +forward 7 +down 9 +down 8 +down 3 +down 7 +down 4 +down 1 +forward 6 +up 7 +down 1 +forward 7 +down 2 +forward 4 +down 3 +forward 6 +up 7 +forward 3 +down 3 +up 6 +down 2 +down 4 +down 8 +forward 6 +down 4 +forward 7 +down 2 +forward 9 +down 3 +down 6 +down 3 +down 6 +forward 4 +down 8 +up 5 +down 6 +forward 6 +down 6 +forward 9 +up 8 +down 7 +up 1 +forward 4 +up 3 +forward 9 +up 6 +forward 4 +up 8 +forward 2 +down 7 +down 7 +forward 1 +up 9 +up 7 +forward 3 +down 1 +up 5 +forward 3 +forward 1 +forward 3 +forward 7 +down 6 +down 1 +forward 9 +up 1 +up 1 +down 8 +down 7 +forward 1 +up 8 +up 6 +down 8 +forward 1 +up 3 +down 7 +forward 7 +down 7 +forward 4 +forward 6 +up 2 +down 9 +forward 9 +down 4 +up 2 +forward 7 +forward 4 +up 8 +up 5 +down 8 +forward 7 +forward 6 +down 1 +up 5 +up 5 +down 3 +forward 7 +forward 1 +forward 7 +forward 1 +down 3 +forward 3 +forward 4 +forward 6 +down 3 +up 4 +up 5 +down 8 +up 3 +forward 4 +forward 6 +forward 8 +forward 4 +forward 7 +down 9 +up 3 +down 1 +up 4 +forward 8 +forward 5 +forward 4 +forward 1 +down 2 +down 7 +down 6 +forward 4 +forward 2 +up 2 +down 2 +forward 2 +forward 2 +up 6 +forward 6 +forward 1 +forward 8 +up 7 +forward 4 +forward 2 +down 2 +down 1 +forward 4 +down 4 +up 7 +up 2 +up 3 +down 5 +forward 2 +forward 9 +down 2 +down 9 +down 4 +down 8 +up 9 +up 5 +up 2 +forward 6 +down 6 +forward 9 +forward 7 +forward 8 +forward 4 +up 9 +forward 9 +down 8 +down 2 +forward 5 +down 8 +down 8 +forward 8 +forward 6 +forward 7 +down 8 +forward 1 +forward 6 +down 4 +forward 1 +down 3 +forward 1 +down 8 +forward 2 +down 4 +down 6 +down 4 +up 9 +forward 6 +up 3 +down 7 +forward 3 +down 8 +forward 6 +forward 3 +down 1 +forward 1 +up 6 +down 1 +down 2 +forward 5 +down 3 +up 5 +forward 6 +forward 7 +forward 2 +forward 9 +down 7 +up 4 +forward 6 +forward 8 +down 6 +down 7 +down 4 +down 5 +down 6 +down 3 +up 1 +forward 2 +forward 3 +down 1 +down 7 +up 8 +forward 1 +forward 6 +up 3 +down 1 +up 6 +down 2 +up 1 +up 7 +forward 3 +up 8 +forward 7 +forward 5 +forward 5 \ No newline at end of file diff --git a/src/day02.rs b/src/day02.rs new file mode 100644 index 0000000..cad3a0c --- /dev/null +++ b/src/day02.rs @@ -0,0 +1,102 @@ +use crate::Day; + +pub struct Day02(Vec); + +enum Instruction { + Forward(i32), + Down(i32), + Up(i32), +} + +impl Day for Day02 { + fn init(content: String) -> Self where Self: Sized { + let lines = content.split("\n").map(str_to_instr).collect::>(); + Day02(lines) + } + + fn part1(&self) -> String { + let mut depth = 0; + let mut fw = 0; + for i in &self.0 { + match i { + Instruction::Forward(n) => fw += n, + Instruction::Down(n) => depth += n, + Instruction::Up(n) => depth -= n, + } + } + format!("{}", depth * fw) + } + + fn part2(&self) -> String { + let mut depth = 0; + let mut fw = 0; + let mut aim = 0; + for i in &self.0 { + match i { + Instruction::Forward(n) => { + fw += n; + depth += aim * n; + } + Instruction::Down(n) => { + aim += n; + } + Instruction::Up(n) => { + aim -= n; + } + } + } + format!("{}", depth * fw) + } +} + +fn str_to_instr(s: &str) -> Instruction { + let v = s.split(' ').collect::>(); + let name = v[0]; + let height = v[1].parse().expect("This should be an int :p"); + match name { + "forward" => Instruction::Forward(height), + "up" => Instruction::Up(height), + "down" => Instruction::Down(height), + _ => panic!("How did this happen???") + } +} + +#[cfg(test)] +mod tests { + use std::fs::read_to_string; + use crate::day02::Day02; + use crate::day::Day; + + const D: &str = r"forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2"; + + #[test] + fn part1_test() { + let d2 = Day02::init(String::from(D)); + assert_eq!("150", d2.part1()) + } + + #[test] + fn part2_test() { + let d2 = Day02::init(String::from(D)); + assert_eq!("900", d2.part2()) + } + + #[test] + pub fn part1_real() { + let f = read_to_string("./input/day02").expect("Could not load input"); + let d = Day02::init(f); + assert_eq!("1524750", d.part1()) + } + + #[test] + pub fn part2_real() { + let f = read_to_string("./input/day02").expect("Could not load input"); + let d = Day02::init(f); + assert_eq!("1592426537", d.part2()) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 24e676e..cfa220c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,12 @@ use std::fs::read_to_string; use chrono::Datelike; use clap::{App, Arg}; use crate::day01::Day01; +use crate::day02::Day02; use crate::day::Day; mod day; mod day01; +mod day02; fn load_input(day: &str) -> String { read_to_string(format!("./input/day{}", day)).expect("Could not load input") @@ -40,7 +42,7 @@ fn main() { .multiple(true)) .get_matches(); - let days: Vec> = vec![Box::new(Day01::init(load_input("01")))]; + let days: Vec> = vec![Box::new(Day01::init(load_input("01"))), Box::new(Day02::init(load_input("02")))]; let _verbosity = matches.occurrences_of("v"); if matches.is_present("all") {