diff --git a/Cargo.toml b/Cargo.toml index af323ed..99f2756 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,13 @@ [package] -name = "executor" -version = "0.0.0" +name = "advent-of-code-2021" +version = "0.1.0" authors = ["Anthony Cicchetti "] edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -clap = "2.33.3" +clap = "2.34.0" day_01 = { path = "./day_01" } -day_02 = { path = "./day_02"} +day_02 = { path = "./day_02" } [workspace] members = [ diff --git a/data/day_2_input.txt b/data/day_2_input.txt new file mode 100644 index 0000000..c9f4cd4 --- /dev/null +++ b/data/day_2_input.txt @@ -0,0 +1,1000 @@ +forward 6 +forward 9 +down 9 +down 7 +forward 8 +down 4 +forward 7 +forward 3 +up 5 +down 7 +down 3 +down 2 +down 1 +down 2 +down 1 +forward 5 +down 9 +up 7 +forward 6 +forward 3 +down 2 +forward 2 +down 4 +down 3 +forward 9 +up 4 +down 7 +forward 5 +down 4 +down 4 +forward 8 +up 6 +forward 4 +forward 6 +down 8 +forward 6 +up 9 +down 6 +down 8 +forward 7 +down 8 +up 1 +forward 5 +up 8 +forward 9 +down 7 +down 9 +up 3 +forward 9 +forward 7 +down 1 +down 5 +forward 6 +up 3 +down 2 +forward 8 +forward 5 +forward 9 +down 5 +down 5 +forward 7 +forward 9 +forward 8 +forward 9 +up 4 +up 8 +forward 2 +up 6 +forward 2 +down 8 +forward 4 +forward 6 +down 1 +forward 3 +down 7 +down 8 +forward 9 +forward 5 +down 8 +forward 1 +forward 8 +down 8 +up 1 +down 9 +forward 6 +down 1 +down 8 +up 6 +up 8 +down 7 +forward 8 +down 3 +forward 2 +up 2 +forward 1 +forward 7 +down 5 +down 7 +forward 5 +down 7 +forward 5 +forward 4 +up 8 +down 5 +forward 8 +forward 6 +up 4 +up 1 +down 1 +down 8 +down 1 +up 1 +down 4 +down 4 +up 3 +down 6 +forward 7 +up 8 +up 1 +forward 7 +forward 6 +down 9 +forward 3 +down 9 +down 5 +forward 5 +forward 1 +forward 5 +down 4 +forward 4 +up 5 +down 8 +down 2 +forward 2 +down 8 +up 4 +down 3 +forward 6 +up 7 +forward 2 +up 5 +up 9 +forward 6 +down 1 +down 1 +up 4 +down 7 +up 2 +forward 1 +down 5 +down 4 +down 5 +forward 4 +down 3 +up 8 +forward 7 +down 2 +up 6 +forward 4 +forward 8 +forward 8 +up 7 +down 2 +down 7 +down 9 +forward 8 +down 9 +down 5 +forward 8 +forward 6 +up 4 +up 7 +down 7 +forward 1 +up 2 +forward 1 +down 6 +down 1 +forward 9 +forward 8 +up 4 +forward 9 +up 7 +forward 8 +forward 1 +down 6 +up 3 +forward 2 +down 6 +up 3 +up 5 +forward 6 +up 5 +down 9 +forward 4 +up 5 +down 3 +forward 5 +forward 2 +up 6 +up 1 +up 4 +forward 9 +down 5 +up 2 +forward 9 +up 5 +down 6 +up 9 +down 6 +down 7 +forward 6 +forward 3 +down 2 +forward 3 +down 1 +down 9 +down 2 +down 2 +forward 8 +up 9 +forward 4 +forward 6 +forward 5 +forward 9 +forward 4 +up 6 +down 8 +down 8 +down 3 +forward 1 +forward 7 +up 3 +forward 2 +down 9 +up 7 +forward 5 +down 1 +forward 9 +up 8 +forward 7 +forward 1 +down 7 +forward 8 +down 7 +down 6 +forward 5 +forward 7 +up 9 +up 4 +forward 2 +down 1 +forward 3 +down 5 +forward 2 +forward 2 +forward 6 +up 1 +up 9 +down 1 +down 7 +up 6 +down 6 +down 7 +forward 3 +forward 7 +forward 9 +forward 9 +down 6 +down 8 +forward 9 +up 5 +forward 5 +down 6 +down 4 +forward 1 +up 9 +forward 6 +up 4 +up 7 +forward 5 +down 7 +down 6 +forward 2 +down 4 +forward 2 +down 1 +forward 8 +down 3 +down 8 +down 7 +down 6 +forward 3 +forward 1 +down 8 +up 4 +down 8 +forward 7 +down 2 +forward 3 +forward 2 +forward 4 +forward 2 +forward 8 +forward 2 +up 1 +down 6 +down 9 +down 3 +forward 8 +down 3 +forward 8 +forward 3 +forward 5 +down 6 +forward 6 +forward 2 +down 7 +forward 8 +down 1 +forward 7 +forward 6 +up 9 +forward 2 +up 4 +up 6 +forward 4 +forward 5 +forward 6 +forward 7 +down 4 +forward 9 +forward 1 +down 8 +down 5 +down 9 +up 5 +forward 9 +forward 6 +forward 7 +down 5 +forward 9 +down 4 +down 8 +forward 9 +down 5 +down 5 +down 6 +forward 8 +forward 9 +down 5 +down 7 +forward 8 +forward 5 +forward 9 +up 4 +down 3 +down 3 +forward 5 +down 7 +down 3 +up 7 +forward 6 +up 7 +down 9 +forward 6 +forward 8 +up 8 +down 9 +down 6 +up 3 +forward 6 +up 3 +down 4 +down 6 +forward 5 +down 5 +forward 1 +down 5 +forward 2 +up 3 +up 9 +down 9 +up 1 +down 3 +down 4 +forward 5 +up 4 +down 2 +forward 1 +forward 7 +up 9 +up 9 +down 2 +down 8 +forward 5 +down 4 +up 2 +forward 9 +down 3 +up 6 +down 3 +forward 1 +down 7 +down 7 +down 7 +forward 1 +forward 6 +down 5 +up 7 +forward 2 +up 6 +down 5 +up 3 +down 5 +up 1 +down 9 +up 2 +down 7 +up 5 +down 6 +up 1 +forward 5 +down 2 +down 1 +up 2 +forward 5 +forward 8 +up 7 +up 5 +forward 9 +forward 9 +forward 5 +forward 8 +down 4 +forward 8 +down 1 +up 3 +down 1 +down 9 +up 5 +down 7 +down 8 +down 4 +down 3 +forward 6 +down 5 +down 7 +forward 6 +down 3 +down 1 +down 5 +forward 4 +up 8 +down 6 +forward 1 +forward 1 +down 4 +down 8 +forward 9 +forward 3 +forward 6 +down 5 +down 7 +down 4 +up 1 +forward 2 +forward 4 +forward 8 +up 2 +up 9 +up 3 +down 8 +forward 4 +down 7 +forward 7 +down 5 +up 3 +up 6 +down 5 +forward 2 +down 8 +up 1 +down 4 +down 4 +forward 1 +down 6 +down 1 +up 2 +forward 2 +down 8 +down 7 +forward 5 +forward 2 +up 4 +forward 6 +down 4 +down 6 +down 2 +up 5 +down 5 +forward 7 +forward 2 +down 2 +down 7 +forward 1 +forward 1 +forward 4 +down 3 +forward 3 +up 4 +up 6 +down 3 +forward 4 +down 5 +down 6 +down 1 +forward 3 +up 3 +down 5 +down 5 +down 5 +forward 5 +forward 5 +forward 2 +up 3 +down 6 +down 6 +down 2 +down 5 +forward 3 +forward 9 +down 9 +forward 6 +up 4 +down 8 +up 7 +forward 1 +forward 4 +forward 9 +down 6 +up 1 +up 1 +up 4 +up 6 +forward 7 +down 5 +forward 8 +forward 7 +up 1 +up 6 +down 7 +down 8 +down 4 +down 8 +up 8 +down 7 +up 1 +forward 9 +forward 6 +forward 2 +forward 7 +down 9 +down 9 +down 7 +forward 4 +forward 8 +forward 8 +up 3 +down 7 +down 7 +down 3 +forward 5 +forward 3 +down 3 +down 6 +forward 2 +down 6 +up 7 +forward 2 +up 5 +down 7 +forward 4 +down 7 +forward 6 +up 6 +up 6 +forward 6 +forward 3 +up 1 +forward 2 +forward 6 +up 1 +down 9 +forward 8 +forward 6 +down 8 +down 3 +up 5 +forward 7 +forward 2 +up 4 +up 3 +forward 4 +down 3 +down 5 +down 5 +down 2 +down 7 +up 2 +down 2 +up 7 +down 1 +forward 6 +forward 5 +forward 4 +down 6 +down 1 +down 5 +down 6 +down 3 +down 9 +down 2 +down 7 +forward 7 +up 7 +down 3 +down 3 +forward 7 +up 5 +down 9 +forward 9 +down 2 +up 4 +up 9 +up 9 +down 1 +up 6 +down 1 +forward 7 +down 6 +forward 9 +down 9 +down 4 +down 1 +forward 6 +down 2 +forward 6 +up 8 +forward 1 +up 1 +up 5 +down 6 +down 8 +down 8 +forward 5 +forward 4 +forward 3 +up 8 +up 9 +down 4 +forward 4 +forward 4 +down 9 +forward 9 +down 7 +forward 7 +up 4 +forward 2 +down 8 +forward 1 +down 7 +forward 5 +forward 1 +down 8 +up 1 +up 1 +down 2 +down 4 +down 7 +down 3 +forward 7 +down 3 +up 3 +forward 8 +up 6 +forward 7 +forward 6 +down 1 +down 8 +forward 7 +up 1 +forward 2 +down 6 +down 1 +forward 4 +forward 2 +down 9 +up 8 +down 9 +down 2 +down 3 +down 5 +down 3 +down 8 +down 1 +down 1 +up 5 +forward 5 +down 2 +down 5 +down 5 +up 4 +up 5 +up 6 +up 7 +down 5 +forward 4 +up 6 +up 3 +down 5 +forward 9 +down 5 +down 3 +down 8 +forward 6 +forward 4 +forward 4 +forward 4 +forward 9 +forward 3 +up 3 +forward 7 +down 6 +down 7 +down 9 +down 5 +up 3 +forward 9 +forward 7 +forward 2 +down 4 +up 4 +down 7 +down 9 +down 6 +down 8 +forward 4 +forward 9 +down 4 +forward 9 +forward 3 +forward 9 +down 8 +forward 6 +down 6 +up 8 +forward 9 +forward 5 +forward 6 +down 5 +up 1 +forward 2 +up 5 +up 8 +down 7 +down 9 +forward 6 +forward 7 +forward 2 +down 8 +down 5 +forward 5 +down 6 +forward 2 +forward 7 +down 6 +up 5 +down 4 +down 7 +up 9 +up 4 +down 1 +forward 4 +up 6 +forward 2 +forward 8 +forward 9 +down 5 +forward 1 +forward 1 +down 6 +up 9 +forward 7 +down 9 +forward 4 +forward 2 +forward 6 +forward 6 +forward 7 +up 6 +down 8 +up 8 +down 7 +forward 6 +up 2 +forward 6 +down 1 +down 5 +down 6 +up 8 +forward 5 +up 5 +forward 1 +down 9 +down 1 +up 4 +down 2 +up 8 +up 2 +down 3 +up 4 +down 3 +up 5 +down 4 +down 2 +up 9 +forward 2 +forward 8 +down 5 +down 3 +forward 4 +forward 3 +forward 3 +down 6 +down 8 +forward 6 +down 1 +forward 6 +down 8 +down 7 +down 7 +forward 6 +forward 5 +up 8 +forward 7 +up 6 +down 7 +forward 7 +down 8 +down 8 +forward 7 +up 4 +forward 4 +forward 1 +up 1 +down 5 +up 6 +up 6 +up 7 +down 1 +down 2 +up 3 +down 3 +forward 6 +down 7 +down 9 +down 1 +forward 2 +forward 6 +down 9 +up 2 +forward 6 +up 7 +forward 5 +down 6 +down 5 +forward 2 +down 9 +forward 8 +up 7 +down 9 +down 7 +forward 8 +forward 7 +down 6 +down 7 +forward 1 +down 9 +up 6 +up 8 +down 4 +down 2 +down 4 +down 2 +down 5 +forward 9 +down 3 +forward 3 +forward 1 +down 2 +down 4 +down 1 +up 3 +up 7 +down 5 +down 8 +forward 6 +up 6 +down 2 +down 3 +forward 3 +up 4 +up 3 +down 6 +down 3 +forward 4 +forward 7 +forward 3 +up 7 +forward 3 +down 8 +forward 7 +down 3 +forward 8 +forward 2 +forward 4 +down 5 +forward 3 +down 8 +up 2 +forward 5 +up 7 +forward 2 +down 2 +down 5 +up 1 +up 7 +down 4 +up 9 +down 4 +forward 3 +forward 1 +up 9 +forward 1 +forward 3 +forward 1 +forward 5 +forward 2 +forward 6 +up 6 +down 1 +forward 1 +forward 6 +forward 6 +forward 5 +down 6 +forward 8 +down 4 +forward 3 +forward 4 +forward 1 +forward 9 +down 4 +forward 5 +down 2 +down 3 +forward 7 +forward 2 +forward 1 +forward 5 +down 9 +up 3 +up 7 +forward 3 +forward 2 +down 2 +up 7 +forward 9 +up 4 +up 2 +up 1 +down 5 +down 4 +forward 2 +down 6 +down 5 +down 6 +forward 6 +down 9 +down 1 +forward 2 +forward 7 +forward 4 +down 5 +down 6 +down 2 +forward 7 +down 3 +forward 5 \ No newline at end of file diff --git a/day_01/Cargo.toml b/day_01/Cargo.toml index 0c9064d..3a755b8 100644 --- a/day_01/Cargo.toml +++ b/day_01/Cargo.toml @@ -1,8 +1,7 @@ [package] name = "day_01" version = "0.1.0" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +edition = "2021" [dependencies] -itertools = "*" +itertools = "0.10.1" \ No newline at end of file diff --git a/day_02/Cargo.toml b/day_02/Cargo.toml index dd58d48..ea0cea1 100644 --- a/day_02/Cargo.toml +++ b/day_02/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "day_02" version = "0.1.0" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] \ No newline at end of file +[dependencies] +peg = "0.7.0" +derive_more = "0.99.17" \ No newline at end of file diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs index f25beb9..0e130da 100644 --- a/day_02/src/lib.rs +++ b/day_02/src/lib.rs @@ -1,11 +1,59 @@ -enum Direction { +use std::fmt::{Debug, Formatter}; + +peg::parser!{ + grammar input_parser() for str { + rule number() -> usize + = n:$(['0'..='9']+) {? n.parse().or(Err("u32"))} + + rule forward() -> Direction + = "forward " b:number() { Direction::Forward(b) } + + rule down() -> Direction + = "down " b:number() { Direction::Down(b) } + + rule up() -> Direction + = "up " b:number() { Direction::Up(b) } + + rule direction() -> Direction + = up() / down() / forward() + + pub(crate) rule directions() -> Vec + = a:direction() ** "\n" + } +} + +#[derive(Debug)] +pub(crate) enum Direction { Forward(usize), Down(usize), Up(usize), } -pub fn input_parse(input: &str) -> Vec { - vec![] +pub(crate) struct Submarine { + horizontal: usize, + depth: usize, +} + +impl Submarine { + fn new() -> Self { + Submarine { + horizontal: 0, + depth: 0, + } + } + fn move_in_direction(&mut self, direction: &Direction) -> () { + match direction { + Direction::Forward(x) => { + self.horizontal += x; + }, + Direction::Up(y) => self.depth -= y, + Direction::Down(y) => self.depth += y, + } + } +} + +pub(crate) fn input_parse(input: &str) -> Vec { + input_parser::directions(input).unwrap() } ///Now, you need to figure out how to pilot this thing. @@ -46,5 +94,42 @@ pub fn input_parse(input: &str) -> Vec { /// /// Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? pub fn part1(input: &str) -> usize { - 0 + let directions = input_parse(input); + let mut p = Submarine::new(); + directions.iter().for_each(|direction| p.move_in_direction(direction)); + p.horizontal * p.depth } + +///--- Part Two --- +/// +/// Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated. +/// +/// In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at 0. The commands also mean something entirely different than you first thought: +/// +/// ```text +/// down X increases your aim by X units. +/// up X decreases your aim by X units. +/// forward X does two things: +/// It increases your horizontal position by X units. +/// It increases your depth by your aim multiplied by X. +/// ``` +/// +/// Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction. +/// +/// Now, the above example does something different: +/// +/// ```text +/// forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change. +/// down 5 adds 5 to your aim, resulting in a value of 5. +/// forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40. +/// up 3 decreases your aim by 3, resulting in a value of 2. +/// down 8 adds 8 to your aim, resulting in a value of 10. +/// forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60. +/// ``` +/// +/// After following these new instructions, you would have a horizontal position of 15 and a depth of 60. (Multiplying these produces 900.) +/// +/// Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? +pub fn part2(input: &str) -> usize { + 0 +} \ No newline at end of file