Advent 2016 Day 1 Part 1 & 2
This commit is contained in:
parent
569e7de087
commit
93b6ec8cc9
12 changed files with 145 additions and 0 deletions
16
advent_of_code_2016/Cargo.lock
generated
Normal file
16
advent_of_code_2016/Cargo.lock
generated
Normal file
|
|
@ -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"
|
||||||
7
advent_of_code_2016/Cargo.toml
Normal file
7
advent_of_code_2016/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "advent_of_code_2025"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
paste = "1.0"
|
||||||
14
advent_of_code_2016/rustfmt.toml
Normal file
14
advent_of_code_2016/rustfmt.toml
Normal file
|
|
@ -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
|
||||||
1
advent_of_code_2016/src/day1/input.txt
Normal file
1
advent_of_code_2016/src/day1/input.txt
Normal 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
|
||||||
2
advent_of_code_2016/src/day1/mod.rs
Normal file
2
advent_of_code_2016/src/day1/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod part1;
|
||||||
|
pub mod part2;
|
||||||
20
advent_of_code_2016/src/day1/part1.rs
Normal file
20
advent_of_code_2016/src/day1/part1.rs
Normal 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());
|
||||||
|
}
|
||||||
32
advent_of_code_2016/src/day1/part2.rs
Normal file
32
advent_of_code_2016/src/day1/part2.rs
Normal 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());
|
||||||
|
}
|
||||||
1
advent_of_code_2016/src/day1/test.txt
Normal file
1
advent_of_code_2016/src/day1/test.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
R8, R4, R4, R8
|
||||||
5
advent_of_code_2016/src/main.rs
Normal file
5
advent_of_code_2016/src/main.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
use paste::paste;
|
||||||
|
mod utility;
|
||||||
|
|
||||||
|
solve_day!{1}
|
||||||
|
// test_day!{1}
|
||||||
10
advent_of_code_2016/src/utility/inputstring.rs
Normal file
10
advent_of_code_2016/src/utility/inputstring.rs
Normal 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.")
|
||||||
|
}
|
||||||
6
advent_of_code_2016/src/utility/mod.rs
Normal file
6
advent_of_code_2016/src/utility/mod.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
mod inputstring;
|
||||||
|
pub use inputstring::{get_input_string, get_test_string};
|
||||||
|
|
||||||
|
mod solveday;
|
||||||
31
advent_of_code_2016/src/utility/solveday.rs
Normal file
31
advent_of_code_2016/src/utility/solveday.rs
Normal 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");
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue