From 11c4b68ebc2031209fac443424c59060074150a0 Mon Sep 17 00:00:00 2001 From: zv0n Date: Thu, 8 Dec 2022 22:21:45 +0100 Subject: [PATCH] 2016: 01 --- 2016/.gitignore | 14 ++++++++ 2016/aoc01/Cargo.toml | 8 +++++ 2016/aoc01/input | 1 + 2016/aoc01/src/main.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 2016/.gitignore create mode 100644 2016/aoc01/Cargo.toml create mode 100644 2016/aoc01/input create mode 100644 2016/aoc01/src/main.rs diff --git a/2016/.gitignore b/2016/.gitignore new file mode 100644 index 0000000..6985cf1 --- /dev/null +++ b/2016/.gitignore @@ -0,0 +1,14 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/2016/aoc01/Cargo.toml b/2016/aoc01/Cargo.toml new file mode 100644 index 0000000..48329c8 --- /dev/null +++ b/2016/aoc01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc01" +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/2016/aoc01/input b/2016/aoc01/input new file mode 100644 index 0000000..d2e5207 --- /dev/null +++ b/2016/aoc01/input @@ -0,0 +1 @@ +R4, R4, L1, R3, L5, R2, R5, R1, L4, R3, L5, R2, L3, L4, L3, R1, R5, R1, L3, L1, R3, L1, R2, R2, L2, R5, L3, L4, R4, R4, R2, L4, L1, R5, L1, L4, R4, L1, R1, L2, R5, L2, L3, R2, R1, L194, R2, L4, R49, R1, R3, L5, L4, L1, R4, R2, R1, L5, R3, L5, L4, R4, R4, L2, L3, R78, L5, R4, R191, R4, R3, R1, L2, R1, R3, L1, R3, R4, R2, L2, R1, R4, L5, R2, L2, L4, L2, R1, R2, L3, R5, R2, L3, L3, R3, L1, L1, R5, L4, L4, L2, R5, R1, R4, L3, L5, L4, R5, L4, R5, R4, L3, L2, L5, R4, R3, L3, R1, L5, R5, R1, L3, R2, L5, R5, L3, R1, R4, L5, R4, R2, R3, L4, L5, R3, R4, L5, L5, R4, L4, L4, R1, R5, R3, L1, L4, L3, L4, R1, L5, L1, R2, R2, R4, R4, L5, R4, R1, L1, L1, L3, L5, L2, R4, L3, L5, L4, L1, R3 diff --git a/2016/aoc01/src/main.rs b/2016/aoc01/src/main.rs new file mode 100644 index 0000000..1de8d44 --- /dev/null +++ b/2016/aoc01/src/main.rs @@ -0,0 +1,78 @@ +use std::collections::HashMap; +use std::{fs, io, process}; +use std::io::BufRead; + +enum Direction { + Right, + Left +} + +struct Instruction { + direction: Direction, + steps: i32, +} + +fn read_instructions(filename: &str) -> Vec { + let mut vec: Vec = Vec::new(); + + let file = fs::File::open(filename).expect("File not found"); + + let lines = io::BufReader::new(file).lines(); + for line_res in lines { + if let Ok(line) = line_res { + let instructions = line.split(", "); + for instruction in instructions { + let direction = match instruction.chars().nth(0) { + Some('L') => Direction::Left, + Some('R') => Direction::Right, + _ => process::exit(1), // not elegant + }; + vec.push(Instruction{direction: direction, steps: instruction.to_string()[1..].parse::().unwrap()}); + } + } + } + return vec; +} + +fn part1(instructions: &Vec) -> i32 { + let mut position = (0,0); + let mut direction = (1,0); + for instruction in instructions { + direction = match instruction.direction { + Direction::Right => (-direction.1, direction.0), + Direction::Left => (direction.1, -direction.0), + }; + position = (position.0 + instruction.steps * direction.0, position.1 + instruction.steps * direction.1); + } + + return position.0.abs() + position.1.abs(); +} + +fn part2(instructions: &Vec) -> i32 { + let mut position = (0,0); + let mut direction = (1,0); + let mut map: HashMap<(i32, i32), i32> = HashMap::new(); + map.insert((0,0), 1); + for instruction in instructions { + direction = match instruction.direction { + Direction::Right => (-direction.1, direction.0), + Direction::Left => (direction.1, -direction.0), + }; + for _ in 0..instruction.steps { + position = (position.0 + direction.0, position.1 + direction.1); + if map.contains_key(&position) { + return position.0.abs() + position.1.abs(); + } else { + map.insert(position, 1); + } + } + } + + return -1; +} + +fn main() { + let instructions = read_instructions("input"); + println!("The Easter Bunny HQ is {} blocks away", part1(&instructions)); + println!("The Easter Bunny HQ is actually {} blocks away", part2(&instructions)); +}