diff --git a/advent_of_code_2016/Cargo.lock b/advent_of_code_2016/Cargo.lock new file mode 100644 index 0000000..6d7513a --- /dev/null +++ b/advent_of_code_2016/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "advent_of_code_2025" +version = "0.1.0" +dependencies = [ + "paste", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" diff --git a/advent_of_code_2016/Cargo.toml b/advent_of_code_2016/Cargo.toml new file mode 100644 index 0000000..74028a7 --- /dev/null +++ b/advent_of_code_2016/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "advent_of_code_2025" +version = "0.1.0" +edition = "2024" + +[dependencies] +paste = "1.0" \ No newline at end of file diff --git a/advent_of_code_2016/rustfmt.toml b/advent_of_code_2016/rustfmt.toml new file mode 100644 index 0000000..b04e7a7 --- /dev/null +++ b/advent_of_code_2016/rustfmt.toml @@ -0,0 +1,14 @@ +binop_separator = "Back" +brace_style = "PreferSameLine" +edition = "2024" +enum_discrim_align_threshold = 120 +fn_single_line = false +group_imports = "StdExternalCrate" +imports_granularity = "Module" +indent_style = "Block" +max_width = 160 +reorder_impl_items = false +struct_field_align_threshold = 120 +unstable_features = true +use_small_heuristics = "Max" +where_single_line = true \ No newline at end of file diff --git a/advent_of_code_2016/src/day1/input.txt b/advent_of_code_2016/src/day1/input.txt new file mode 100644 index 0000000..b83fc6f --- /dev/null +++ b/advent_of_code_2016/src/day1/input.txt @@ -0,0 +1 @@ +R5, L2, L1, R1, R3, R3, L3, R3, R4, L2, R4, L4, R4, R3, L2, L1, L1, R2, R4, R4, L4, R3, L2, R1, L4, R1, R3, L5, L4, L5, R3, L3, L1, L1, R4, R2, R2, L1, L4, R191, R5, L2, R46, R3, L1, R74, L2, R2, R187, R3, R4, R1, L4, L4, L2, R4, L5, R4, R3, L2, L1, R3, R3, R3, R1, R1, L4, R4, R1, R5, R2, R1, R3, L4, L2, L2, R1, L3, R1, R3, L5, L3, R5, R3, R4, L1, R3, R2, R1, R2, L4, L1, L1, R3, L3, R4, L2, L4, L5, L5, L4, R2, R5, L4, R4, L2, R3, L4, L3, L5, R5, L4, L2, R3, R5, R5, L1, L4, R3, L1, R2, L5, L1, R4, L1, R5, R1, L4, L4, L4, R4, R3, L5, R1, L3, R4, R3, L2, L1, R1, R2, R2, R2, L1, L1, L2, L5, L3, L1 \ No newline at end of file diff --git a/advent_of_code_2016/src/day1/mod.rs b/advent_of_code_2016/src/day1/mod.rs new file mode 100644 index 0000000..8e2a92e --- /dev/null +++ b/advent_of_code_2016/src/day1/mod.rs @@ -0,0 +1,2 @@ +pub mod part1; +pub mod part2; \ No newline at end of file diff --git a/advent_of_code_2016/src/day1/part1.rs b/advent_of_code_2016/src/day1/part1.rs new file mode 100644 index 0000000..0423e7a --- /dev/null +++ b/advent_of_code_2016/src/day1/part1.rs @@ -0,0 +1,20 @@ +pub fn solve(input: &str) { + let instructions = input.split(", ").map(|instr| (&instr[..1], instr[1..].parse::().unwrap())).collect::>(); + + let mut direction = 0; + let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]; // N E S W + let mut position = (0, 0); + + for (rotation, distance) in instructions { + if rotation == "R" { + direction = (direction + 1) % 4; + } else { + direction = (direction + 3) % 4; + } + + position = (position.0 + distance * directions[direction as usize].0, position.1 + distance * directions[direction as usize].1) + } + + println!("position: {position:?}"); + println!("distance: {}", position.0.abs() + position.1.abs()); +} diff --git a/advent_of_code_2016/src/day1/part2.rs b/advent_of_code_2016/src/day1/part2.rs new file mode 100644 index 0000000..854876e --- /dev/null +++ b/advent_of_code_2016/src/day1/part2.rs @@ -0,0 +1,32 @@ +use std::collections::HashSet; + +pub fn solve(input: &str) { + let instructions = input.split(", ").map(|instr| (&instr[..1], instr[1..].parse::().unwrap())).collect::>(); + + let mut direction = 0; + let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]; // N E S W + let mut position: (i32, i32) = (0, 0); + + let mut visited = HashSet::new(); + visited.insert((0, 0)); + + 'outer: for (rotation, distance) in instructions { + if rotation == "R" { + direction = (direction + 1) % 4; + } else { + direction = (direction + 3) % 4; + } + + for _ in 0..distance { + position = (position.0 + directions[direction].0, position.1 + directions[direction].1); + + if visited.contains(&position) { + break 'outer; + } + visited.insert(position); + } + } + + println!("position: {position:?}"); + println!("distance: {}", position.0.abs() + position.1.abs()); +} diff --git a/advent_of_code_2016/src/day1/test.txt b/advent_of_code_2016/src/day1/test.txt new file mode 100644 index 0000000..60f9cff --- /dev/null +++ b/advent_of_code_2016/src/day1/test.txt @@ -0,0 +1 @@ +R8, R4, R4, R8 \ No newline at end of file diff --git a/advent_of_code_2016/src/main.rs b/advent_of_code_2016/src/main.rs new file mode 100644 index 0000000..3c394c4 --- /dev/null +++ b/advent_of_code_2016/src/main.rs @@ -0,0 +1,5 @@ +use paste::paste; +mod utility; + +solve_day!{1} +// test_day!{1} diff --git a/advent_of_code_2016/src/utility/inputstring.rs b/advent_of_code_2016/src/utility/inputstring.rs new file mode 100644 index 0000000..e9653ef --- /dev/null +++ b/advent_of_code_2016/src/utility/inputstring.rs @@ -0,0 +1,10 @@ +use std::fs::read_to_string; + +pub fn get_input_string(day: u8) -> String { + read_to_string(format!("src/day{}/input.txt", day)).expect("Failed to read input file.") +} + +pub fn get_test_string(day: u8, file: Option<&str>) -> String { + let filename = file.unwrap_or("test.txt"); + read_to_string(format!("src/day{}/{}", day, filename)).expect("Failed to read input file.") +} \ No newline at end of file diff --git a/advent_of_code_2016/src/utility/mod.rs b/advent_of_code_2016/src/utility/mod.rs new file mode 100644 index 0000000..cd9db7a --- /dev/null +++ b/advent_of_code_2016/src/utility/mod.rs @@ -0,0 +1,6 @@ +#![allow(unused)] + +mod inputstring; +pub use inputstring::{get_input_string, get_test_string}; + +mod solveday; \ No newline at end of file diff --git a/advent_of_code_2016/src/utility/solveday.rs b/advent_of_code_2016/src/utility/solveday.rs new file mode 100644 index 0000000..d68ac7f --- /dev/null +++ b/advent_of_code_2016/src/utility/solveday.rs @@ -0,0 +1,31 @@ +#[macro_export] +macro_rules! solve_day { + ($day:literal) => { + paste! {mod [];} + + pub fn main() { + println!("-- Day {} --", $day); + let input = utility::get_input_string($day); + paste! { [] ::part1::solve(&input) }; + paste! { [] ::part2::solve(&input) }; + } + }; + ($day:literal, $filename:literal) => { + paste! {mod [];} + + pub fn main() { + println!("-- Day {} --", $day); + let input = utility::get_test_string($day, Some($filename)); + paste! { [] ::part1::solve(&input) }; + println!("\n---\n"); + paste! { [] ::part2::solve(&input) }; + } + }; +} + +#[macro_export] +macro_rules! test_day { + ($day:literal) => { + solve_day!($day, "test.txt"); + }; +}