From b4872144212b6d2e0f1f003bfa18114c8800fbfa Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 3 Dec 2021 11:01:48 -0800 Subject: [PATCH] Implementation of d02 in rust --- d02/Cargo.lock | 7 + d02/Cargo.toml | 8 + d02/input.txt | 1000 +++++++++++++++++++++++++++++++++++++++++++ d02/input_short.txt | 6 + d02/src/main.rs | 131 ++++++ 5 files changed, 1152 insertions(+) create mode 100644 d02/Cargo.lock create mode 100644 d02/Cargo.toml create mode 100644 d02/input.txt create mode 100644 d02/input_short.txt create mode 100644 d02/src/main.rs diff --git a/d02/Cargo.lock b/d02/Cargo.lock new file mode 100644 index 0000000..dda390c --- /dev/null +++ b/d02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "d02" +version = "0.1.0" diff --git a/d02/Cargo.toml b/d02/Cargo.toml new file mode 100644 index 0000000..9935f1c --- /dev/null +++ b/d02/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d02" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d02/input.txt b/d02/input.txt new file mode 100644 index 0000000..83b7ae1 --- /dev/null +++ b/d02/input.txt @@ -0,0 +1,1000 @@ +forward 5 +down 8 +down 6 +down 7 +down 8 +forward 7 +down 3 +up 6 +forward 6 +down 2 +forward 5 +down 6 +up 3 +down 4 +forward 4 +down 6 +down 1 +up 5 +forward 5 +down 1 +down 7 +up 2 +down 7 +forward 1 +forward 6 +down 1 +up 1 +up 4 +forward 3 +forward 6 +forward 1 +forward 4 +up 3 +forward 1 +forward 4 +down 9 +forward 4 +forward 8 +up 8 +forward 5 +up 4 +up 3 +down 8 +forward 5 +down 4 +forward 1 +forward 7 +down 1 +forward 8 +down 4 +forward 2 +forward 7 +forward 9 +up 4 +down 3 +forward 7 +forward 6 +down 8 +forward 2 +forward 5 +forward 4 +down 6 +forward 6 +up 5 +down 3 +down 6 +down 5 +down 7 +down 8 +up 5 +down 5 +forward 5 +forward 4 +up 3 +down 7 +down 3 +forward 4 +down 2 +forward 4 +forward 3 +forward 4 +forward 9 +forward 6 +forward 8 +up 8 +down 8 +up 5 +down 4 +down 8 +up 7 +up 8 +down 6 +down 3 +forward 2 +forward 7 +up 1 +up 2 +forward 2 +down 7 +down 1 +up 9 +forward 6 +forward 4 +down 2 +up 6 +down 2 +down 1 +down 3 +up 6 +down 1 +down 8 +forward 7 +up 8 +forward 5 +forward 8 +down 8 +forward 6 +forward 8 +down 3 +down 4 +down 6 +up 2 +forward 6 +up 9 +forward 4 +forward 8 +up 4 +down 8 +forward 8 +down 8 +down 4 +down 5 +forward 7 +down 6 +down 6 +up 2 +up 1 +forward 7 +forward 8 +forward 4 +forward 9 +down 7 +forward 4 +up 5 +down 3 +up 4 +down 9 +down 2 +down 8 +forward 3 +forward 5 +forward 7 +forward 9 +forward 5 +forward 8 +forward 6 +forward 4 +forward 6 +forward 7 +forward 2 +down 1 +down 8 +down 4 +down 5 +down 6 +up 3 +up 2 +forward 4 +down 4 +forward 7 +up 6 +up 9 +down 1 +down 3 +down 1 +up 3 +up 1 +down 2 +up 5 +forward 1 +down 7 +forward 9 +down 4 +up 4 +down 6 +down 3 +forward 4 +up 6 +up 4 +forward 1 +up 7 +down 1 +down 7 +down 7 +forward 9 +down 3 +down 3 +forward 6 +down 2 +forward 7 +up 4 +up 8 +down 8 +forward 7 +forward 6 +down 7 +forward 5 +up 6 +up 6 +down 9 +up 6 +up 2 +forward 9 +forward 1 +up 5 +up 3 +down 9 +up 8 +down 7 +up 7 +forward 5 +down 7 +down 4 +forward 2 +forward 3 +forward 5 +down 1 +up 6 +down 6 +up 6 +down 8 +down 3 +down 4 +forward 9 +down 3 +forward 3 +up 1 +down 2 +forward 8 +down 7 +up 9 +forward 1 +down 3 +forward 1 +forward 8 +down 3 +forward 8 +forward 6 +down 1 +down 9 +forward 2 +down 1 +down 6 +up 1 +up 7 +down 9 +forward 6 +forward 5 +forward 2 +up 6 +down 6 +forward 6 +up 3 +down 7 +down 8 +forward 5 +down 7 +forward 8 +down 8 +forward 4 +down 6 +forward 4 +down 7 +up 5 +down 5 +down 5 +down 4 +down 3 +forward 8 +forward 1 +down 8 +down 2 +forward 3 +forward 7 +forward 3 +down 5 +down 6 +down 8 +down 6 +forward 9 +forward 4 +forward 8 +down 5 +down 7 +forward 4 +up 5 +down 8 +up 6 +up 7 +down 6 +down 8 +forward 3 +up 6 +forward 7 +down 4 +up 1 +up 8 +forward 3 +down 6 +down 1 +forward 7 +down 1 +down 9 +forward 6 +down 4 +forward 3 +forward 1 +down 5 +down 9 +down 9 +down 5 +down 8 +down 7 +forward 1 +forward 5 +down 2 +forward 2 +forward 1 +down 8 +forward 6 +down 3 +forward 4 +up 2 +up 8 +forward 7 +forward 4 +down 8 +up 6 +forward 3 +up 1 +up 2 +forward 5 +forward 9 +down 5 +forward 2 +forward 5 +up 6 +down 1 +down 1 +down 6 +forward 6 +down 7 +forward 5 +forward 8 +down 7 +down 5 +forward 9 +forward 1 +up 6 +down 7 +forward 1 +forward 4 +down 5 +down 6 +up 3 +up 8 +up 5 +down 8 +down 8 +down 6 +down 2 +down 3 +down 9 +forward 8 +forward 7 +forward 7 +up 5 +down 5 +forward 9 +up 8 +up 5 +forward 1 +down 9 +down 9 +forward 9 +forward 4 +forward 6 +up 9 +up 5 +up 3 +down 9 +up 7 +up 1 +down 3 +down 9 +down 7 +forward 6 +down 7 +forward 7 +forward 8 +down 2 +forward 5 +up 1 +down 6 +up 9 +forward 5 +up 9 +down 2 +down 3 +forward 5 +down 9 +forward 9 +forward 2 +forward 8 +down 1 +forward 8 +up 1 +forward 3 +up 1 +down 1 +forward 9 +down 2 +forward 2 +up 1 +up 8 +down 2 +down 7 +down 5 +up 2 +up 6 +down 9 +down 7 +down 7 +up 6 +up 8 +down 7 +forward 5 +down 4 +down 5 +up 8 +up 6 +down 6 +forward 6 +up 6 +down 1 +down 1 +down 1 +forward 1 +down 8 +down 4 +down 5 +down 2 +down 5 +up 8 +up 8 +down 3 +down 6 +down 1 +forward 6 +forward 5 +forward 1 +down 3 +down 4 +up 9 +down 3 +up 8 +forward 5 +down 5 +forward 2 +down 8 +down 2 +up 1 +forward 7 +up 8 +forward 7 +down 3 +down 1 +down 3 +forward 4 +down 5 +down 8 +forward 8 +forward 3 +forward 7 +down 7 +forward 4 +down 1 +forward 3 +up 2 +down 7 +down 1 +forward 4 +forward 7 +down 3 +down 1 +forward 4 +down 3 +forward 2 +up 9 +down 5 +down 9 +forward 5 +up 5 +down 3 +up 6 +up 8 +down 7 +down 3 +down 9 +forward 6 +forward 8 +forward 3 +down 6 +up 8 +forward 8 +forward 9 +down 4 +down 1 +forward 2 +down 2 +up 2 +down 5 +down 1 +down 3 +forward 4 +down 3 +up 8 +up 6 +up 5 +down 4 +forward 3 +up 6 +forward 6 +forward 2 +down 8 +down 5 +forward 3 +up 1 +forward 5 +forward 9 +forward 5 +down 5 +forward 3 +forward 6 +forward 5 +forward 3 +down 1 +down 1 +down 1 +down 9 +forward 8 +forward 2 +forward 4 +forward 8 +down 1 +up 8 +down 1 +down 6 +down 5 +up 8 +down 4 +forward 8 +forward 6 +down 6 +forward 2 +forward 7 +forward 2 +up 7 +forward 4 +up 1 +up 8 +down 3 +down 2 +down 3 +up 7 +down 9 +up 5 +down 1 +down 3 +up 5 +down 6 +up 9 +down 4 +down 7 +down 6 +down 4 +forward 5 +forward 6 +down 8 +forward 3 +forward 8 +up 5 +up 6 +up 8 +forward 8 +forward 1 +down 6 +forward 3 +forward 3 +forward 6 +down 3 +down 2 +forward 5 +down 5 +forward 6 +down 3 +down 9 +down 8 +down 6 +down 6 +forward 1 +up 5 +down 9 +forward 3 +forward 3 +down 2 +forward 8 +forward 3 +forward 2 +forward 5 +down 4 +down 1 +up 2 +down 1 +down 1 +forward 5 +down 7 +up 7 +down 9 +down 8 +down 6 +forward 3 +forward 5 +down 3 +down 6 +up 3 +up 2 +up 8 +down 3 +up 3 +down 6 +forward 7 +forward 4 +up 5 +forward 1 +up 3 +forward 8 +down 2 +down 5 +down 2 +forward 4 +forward 4 +down 4 +up 8 +down 1 +up 2 +forward 2 +forward 9 +forward 4 +down 3 +down 7 +forward 1 +down 2 +forward 8 +down 8 +forward 3 +down 7 +forward 9 +forward 6 +up 1 +forward 3 +up 2 +up 3 +forward 6 +down 8 +up 9 +down 2 +down 9 +down 6 +down 4 +forward 5 +forward 3 +up 7 +forward 7 +up 7 +up 6 +down 7 +down 2 +up 7 +down 5 +up 9 +forward 3 +up 6 +up 6 +up 6 +up 1 +forward 5 +forward 5 +down 8 +forward 6 +forward 7 +down 3 +down 4 +down 2 +down 4 +down 1 +forward 7 +down 7 +down 5 +forward 8 +up 6 +up 8 +forward 8 +forward 2 +forward 4 +down 6 +down 4 +down 2 +down 3 +forward 8 +forward 6 +down 3 +forward 7 +forward 4 +up 8 +down 9 +forward 5 +up 5 +up 5 +up 7 +forward 3 +up 1 +down 2 +forward 5 +forward 5 +up 1 +forward 4 +down 6 +up 5 +up 3 +forward 9 +down 9 +down 6 +down 1 +down 2 +down 4 +down 7 +forward 3 +up 5 +forward 2 +down 3 +forward 7 +up 8 +up 3 +forward 6 +up 7 +up 1 +up 2 +down 5 +forward 5 +down 3 +down 5 +down 6 +up 1 +down 2 +up 1 +forward 3 +down 3 +down 4 +down 6 +down 1 +down 3 +forward 9 +forward 1 +down 1 +up 3 +forward 4 +forward 7 +forward 4 +down 2 +forward 6 +forward 2 +forward 7 +down 9 +forward 8 +forward 3 +up 8 +down 9 +up 8 +forward 5 +forward 9 +down 4 +forward 1 +up 9 +forward 2 +down 6 +up 3 +forward 1 +forward 3 +forward 8 +down 7 +down 3 +down 5 +down 2 +down 2 +forward 4 +forward 1 +down 2 +up 8 +down 2 +forward 3 +down 2 +down 6 +down 1 +up 1 +down 7 +down 3 +forward 3 +forward 1 +forward 9 +down 9 +down 2 +up 1 +forward 9 +up 2 +down 2 +forward 3 +down 4 +forward 9 +forward 5 +up 5 +forward 2 +up 3 +forward 8 +down 3 +forward 5 +forward 5 +down 8 +up 9 +forward 7 +up 2 +up 2 +up 1 +up 7 +down 8 +forward 9 +forward 9 +up 6 +down 5 +forward 7 +down 9 +down 8 +down 5 +down 3 +down 2 +forward 6 +down 7 +forward 3 +up 5 +forward 1 +up 7 +forward 3 +down 5 +down 9 +down 8 +forward 2 +up 4 +forward 7 +forward 5 +forward 8 +forward 7 +up 7 +forward 4 +up 7 +down 9 +forward 1 +forward 3 +down 3 +forward 4 +down 3 +forward 3 +down 5 +down 1 +forward 6 +down 4 +down 3 +down 2 +up 1 +down 1 +down 6 +down 6 +forward 9 +down 5 +forward 1 +up 4 +forward 7 +down 8 +forward 1 +forward 9 +forward 7 +down 1 +down 3 +up 2 +down 5 +up 6 +forward 2 +up 2 +down 7 +down 9 +forward 3 +up 5 +up 7 +down 4 +forward 6 +down 8 +forward 7 +up 1 +up 4 +forward 4 +down 9 +forward 9 +forward 9 +down 3 +forward 5 +forward 1 +down 3 +down 8 +forward 7 +down 4 +forward 3 +down 3 +forward 8 +forward 2 +forward 6 +up 9 +forward 2 +down 9 +forward 2 +down 1 +forward 9 +up 1 +up 4 +up 1 +down 1 +forward 4 +up 9 +up 8 +down 1 +down 3 +down 2 +forward 9 +down 7 +down 4 +forward 2 +up 9 +down 7 +down 1 +down 9 +forward 2 +down 2 +forward 9 +down 5 +up 1 +down 3 +up 6 +down 4 +forward 8 +down 2 +down 2 +down 9 +forward 9 +forward 2 +down 1 +forward 6 +down 2 +up 4 +down 8 +up 4 +down 6 +down 2 +forward 7 +down 3 +up 3 +forward 1 +up 4 +forward 5 +down 7 +down 8 +forward 7 +forward 3 +down 5 +up 6 +down 7 +down 1 +up 7 +down 1 +forward 6 +forward 3 +forward 3 +forward 7 diff --git a/d02/input_short.txt b/d02/input_short.txt new file mode 100644 index 0000000..b7172ac --- /dev/null +++ b/d02/input_short.txt @@ -0,0 +1,6 @@ +forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2 diff --git a/d02/src/main.rs b/d02/src/main.rs new file mode 100644 index 0000000..10dadbe --- /dev/null +++ b/d02/src/main.rs @@ -0,0 +1,131 @@ +use std::fmt; +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; + +fn read_lines

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +struct Coord(i32, i32); + +impl fmt::Display for Coord { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "(x={}, y={})", self.0, self.1) + } +} + +fn part1() { + let result = read_lines("input.txt") + .expect("Expected a file to read") + .map(|line| line.expect("Could not read line")) + .map(|line| { + let mut parts = line.split_whitespace(); + let direction = parts + .next() + .expect(format!("Missing direction on line: {}", line).as_str()); + let distance = parts + .next() + .map(|value| { + value.parse::().expect( + format!("Expect distance to be a value instead got: {}", value).as_str(), + ) + }) + .expect(format!("Missing distance on line: {}", line).as_str()); + if parts.next().is_some() { + panic!("More than 2 parts in line: {}", line); + } + match direction { + "forward" => Coord(distance, 0), + "down" => Coord(0, -1 * distance), + "up" => Coord(0, distance), + &_ => panic!("Unknown direction {}", direction), + } + }) + .reduce(|location, delta| Coord(location.0 + delta.0, location.1 + delta.1)) + .expect("No value reduced"); + + println!( + "Part 1 location: {}, result: {}", + result, + result.0 * result.1 * -1 + ) +} + +struct Position { + location: Coord, + aim: i32, +} + +fn part2() { + let result = read_lines("input.txt") + .expect("Expected a file to read") + .map(|line| line.expect("Could not read line")) + .map(|line| { + let mut parts = line.split_whitespace(); + let direction = parts + .next() + .expect(format!("Missing direction on line: {}", line).as_str()); + let value = parts + .next() + .map(|value| { + value.parse::().expect( + format!("Expect distance to be a value instead got: {}", value).as_str(), + ) + }) + .expect(format!("Missing distance on line: {}", line).as_str()); + if parts.next().is_some() { + panic!("More than 2 parts in line: {}", line); + } + match direction { + // Increase aim + "down" => Position { + location: Coord(0, 0), + aim: value, + }, + // Decrease aim + "up" => Position { + location: Coord(0, 0), + aim: -1 * value, + }, + // Increase horiziontal and depty by aim + "forward" => Position { + location: Coord(value, value), + aim: 0, + }, + &_ => panic!("Unknown direction {}", direction), + } + }) + .fold( + Position { + location: Coord(0, 0), + aim: 0, + }, + |position, delta| { + let aim = position.aim + delta.aim; + let x = position.location.0 + delta.location.0; + let y = position.location.1 - (aim * delta.location.1); + let coord = Coord(x, y); + println!("Coord: {}, aim: {}", coord, aim); + Position { + location: coord, + aim: aim, + } + }, + ); + + println!( + "Part 2 location: {}, result: {}", + result.location, + result.location.0 * result.location.1 * -1 + ) +} + +fn main() { + part1(); + part2(); +}