Advent 2016 Day 1 Part 1 & 2

This commit is contained in:
Jos van Goor 2025-12-06 17:30:26 +01:00
parent 569e7de087
commit 93b6ec8cc9
12 changed files with 145 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,2 @@
pub mod part1;
pub mod part2;

View file

@ -0,0 +1,20 @@
pub fn solve(input: &str) {
let instructions = input.split(", ").map(|instr| (&instr[..1], instr[1..].parse::<i32>().unwrap())).collect::<Vec<_>>();
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());
}

View file

@ -0,0 +1,32 @@
use std::collections::HashSet;
pub fn solve(input: &str) {
let instructions = input.split(", ").map(|instr| (&instr[..1], instr[1..].parse::<i32>().unwrap())).collect::<Vec<_>>();
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());
}

View file

@ -0,0 +1 @@
R8, R4, R4, R8

View file

@ -0,0 +1,5 @@
use paste::paste;
mod utility;
solve_day!{1}
// test_day!{1}

View file

@ -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.")
}

View file

@ -0,0 +1,6 @@
#![allow(unused)]
mod inputstring;
pub use inputstring::{get_input_string, get_test_string};
mod solveday;

View file

@ -0,0 +1,31 @@
#[macro_export]
macro_rules! solve_day {
($day:literal) => {
paste! {mod [<day $day>];}
pub fn main() {
println!("-- Day {} --", $day);
let input = utility::get_input_string($day);
paste! { [<day $day>] ::part1::solve(&input) };
paste! { [<day $day>] ::part2::solve(&input) };
}
};
($day:literal, $filename:literal) => {
paste! {mod [<day $day>];}
pub fn main() {
println!("-- Day {} --", $day);
let input = utility::get_test_string($day, Some($filename));
paste! { [<day $day>] ::part1::solve(&input) };
println!("\n---\n");
paste! { [<day $day>] ::part2::solve(&input) };
}
};
}
#[macro_export]
macro_rules! test_day {
($day:literal) => {
solve_day!($day, "test.txt");
};
}