Compare commits

...

2 Commits

Author SHA1 Message Date
Bram ca7f0d2d28 Refactor day 1 & remove compiler warnings 2025-12-01 09:55:51 +01:00
Bram 5ea66062ee Add day 1 (rough version) 2025-12-01 09:27:00 +01:00
6 changed files with 4633 additions and 19 deletions

View File

@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
colored = "3.0.0"
regex = "1.12.2"

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,67 @@
use crate::utils;
pub fn answer(text : String) ->( u32, u32 ) {
( 0, 0 )
struct Dial {
points_at : i16,
clicks : u32,
passing_clicks : u32,
}
impl Dial {
fn new() -> Dial {
Dial { points_at : 50, clicks : 0, passing_clicks : 0 }
}
fn rotate_by(&mut self, r : i16) -> () {
let start : i16 = self.points_at;
self.points_at += r;
self.points_at = self.points_at.rem_euclid(100);
self.passing_clicks += (r.abs() / 100) as u32;
if self.points_at == 0 {
self.clicks += 1;
// Avoid double counting when starting and landing on zero
if r / 100 != 0 && r % 100 == 0 {
self.passing_clicks -= 1;
}
}
if r < 0 && start < self.points_at && start != 0 {
self.passing_clicks += 1;
}
if r > 0 && start > self.points_at && self.points_at != 0 {
self.passing_clicks += 1;
}
// println!(
// "Dial {} now points at {} ({} clicks, {} passing)",
// r, self.points_at, self.clicks, self.passing_clicks
// );
}
}
pub fn answer(text : String) ->( u32, u32 ) {
let mut dial : Dial = Dial::new();
for d in text.split("\n").filter_map(str_to_dir) {
dial.rotate_by(d);
}
( dial.clicks
, dial.clicks + dial.passing_clicks
)
}
fn str_to_dir(s : &str) -> Option<i16> {
if s.len() < 2 {
return None;
}
let (dir, l) : (&str, &str) = s.split_at(1);
match dir {
"L" => Some(-1 * utils::str_to_i16(l)?),
"R" => utils::str_to_i16(l),
_ => None,
}
}

View File

@ -17,6 +17,8 @@ use crate::utils::diagnostics;
type DailyOutput = ( u128, u128, Duration );
pub use crate::utils::diagnostics::AdventOfCode;
pub fn day_01() -> DailyOutput {
let s : String = read_from_file("inputs/01.txt");
let ( p1, p2, d ) = diagnostics::benchmark(s, day_01::answer);

View File

@ -1,7 +1,5 @@
mod utils;
fn main() {
let mut aoc = crate::utils::diagnostics::AdventOfCode::new();
let mut aoc = aoc2025::AdventOfCode::new();
aoc.insert(1, aoc2025::day_01());
// aoc.insert(2, aoc2025::day_02());

View File

@ -2,9 +2,9 @@ use std::fs;
pub mod diagnostics;
pub fn char_to_u64(s : char) -> Option<u64> {
s.to_string().parse::<u64>().ok()
}
// pub fn char_to_u64(s : char) -> Option<u64> {
// s.to_string().parse::<u64>().ok()
// }
pub fn read_from_file(name : &str) -> String {
match fs::read_to_string(name) {
@ -16,18 +16,26 @@ pub fn read_from_file(name : &str) -> String {
}
}
pub fn str_to_i32(s : &str) -> Option<i32> {
s.trim().to_string().parse::<i32>().ok()
pub fn str_to_i16(s : &str) -> Option<i16> {
s.trim().to_string().parse::<i16>().ok()
}
pub fn str_to_u8(s : &str) -> Option<u8> {
s.trim().to_string().parse::<u8>().ok()
}
// pub fn str_to_i32(s : &str) -> Option<i32> {
// s.trim().to_string().parse::<i32>().ok()
// }
// pub fn str_to_u8(s : &str) -> Option<u8> {
// s.trim().to_string().parse::<u8>().ok()
// }
// pub fn str_to_u16(s : &str) -> Option<u16> {
// s.trim().to_string().parse::<u16>().ok()
// }
pub fn str_to_u32(s : &str) -> Option<u32> {
s.trim().to_string().parse::<u32>().ok()
}
// pub fn str_to_u32(s : &str) -> Option<u32> {
// s.trim().to_string().parse::<u32>().ok()
// }
pub fn str_to_u64(s : &str) -> Option<u64> {
s.trim().to_string().parse::<u64>().ok()
}
// pub fn str_to_u64(s : &str) -> Option<u64> {
// s.trim().to_string().parse::<u64>().ok()
// }