Merge Bram's branch into benchmark branch
commit
85972ec4bf
|
|
@ -0,0 +1,2 @@
|
|||
8123221734-8123333968,2665-4538,189952-274622,4975-9031,24163352-24202932,1233-1772,9898889349-9899037441,2-15,2147801-2281579,296141-327417,8989846734-8989940664,31172-42921,593312-632035,862987-983007,613600462-613621897,81807088-81833878,13258610-13489867,643517-782886,986483-1022745,113493-167913,10677-16867,372-518,3489007333-3489264175,1858-2534,18547-26982,16-29,247-366,55547-103861,57-74,30-56,1670594-1765773,76-129,134085905-134182567,441436-566415,7539123416-7539252430,668-1146,581563513-581619699
|
||||
|
||||
|
|
@ -1,55 +1,41 @@
|
|||
use crate::utils;
|
||||
|
||||
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();
|
||||
pub fn answer(text : String) ->( u16, u16 ) {
|
||||
let mut old : i16 = 50;
|
||||
let mut clicks : u16 = 0;
|
||||
let mut passing_clicks : u16 = 0;
|
||||
|
||||
for d in text.split("\n").filter_map(str_to_dir) {
|
||||
dial.rotate_by(d);
|
||||
let new : i16 = old + d;
|
||||
|
||||
// Part 1 clicks
|
||||
if new % 100 == 0 {
|
||||
clicks += 1;
|
||||
}
|
||||
|
||||
// Part 2 clicks
|
||||
if d > 0 {
|
||||
passing_clicks += (
|
||||
new.div_euclid(100) - old.div_euclid(100)
|
||||
) as u16
|
||||
} else {
|
||||
passing_clicks += (
|
||||
(old - 1).div_euclid(100) - (new - 1).div_euclid(100)
|
||||
) as u16
|
||||
}
|
||||
|
||||
old = new;
|
||||
}
|
||||
|
||||
( dial.clicks
|
||||
, dial.clicks + dial.passing_clicks
|
||||
)
|
||||
( clicks, passing_clicks )
|
||||
}
|
||||
|
||||
fn count_passing_clicks(old : i16, new : i16) -> u16 {
|
||||
if old < new {
|
||||
(new.div_euclid(100) - old.div_euclid(100)).abs() as u16
|
||||
} else {
|
||||
((new - 1).div_euclid(100) - (old - 1).div_euclid(100)).abs() as u16
|
||||
}
|
||||
}
|
||||
|
||||
fn str_to_dir(s : &str) -> Option<i16> {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
use crate::utils;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
pub fn answer(text : String) -> ( u64, u64 ) {
|
||||
( text.split(",").flat_map(to_range).filter(|x| is_invalid_by(x, 2)).sum()
|
||||
, text.split(",").flat_map(to_range).filter(is_invalid).sum()
|
||||
)
|
||||
}
|
||||
|
||||
fn is_invalid(n : &u64) -> bool {
|
||||
for b in 2..=10 {
|
||||
if is_invalid_by(n, b) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn is_invalid_by(n : &u64, by : usize) -> bool {
|
||||
let s : String = n.to_string();
|
||||
let size : usize = s.len() / by;
|
||||
|
||||
if s.len() % by == 0 {
|
||||
for i in 1..by {
|
||||
if s[0..size] != s[i * size..(i + 1) * size] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn to_range(s : &str) -> RangeInclusive<u64> {
|
||||
let empty = 1..=0;
|
||||
if let Some((low, high)) = s.split_once("-") {
|
||||
match ( utils::str_to_u64(low), utils::str_to_u64(high) ) {
|
||||
( Some(l), Some(h) ) =>
|
||||
l..=h,
|
||||
_ =>
|
||||
// Empty iterator
|
||||
empty,
|
||||
}
|
||||
} else {
|
||||
empty
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
mod day_01;
|
||||
// mod day_02;
|
||||
mod day_02;
|
||||
// mod day_03;
|
||||
// mod day_04;
|
||||
// mod day_05;
|
||||
|
|
@ -26,12 +26,12 @@ pub fn day_01() -> DailyOutput {
|
|||
( p1 as u128, p2 as u128, d )
|
||||
}
|
||||
|
||||
// pub fn day_02() -> DailyOutput {
|
||||
// let s : String = read_from_file("inputs/02.txt");
|
||||
// let (p1, p2, d) = diagnostics::benchmark(s, day_02::answer);
|
||||
pub fn day_02() -> DailyOutput {
|
||||
let s : String = read_from_file("inputs/02.txt");
|
||||
let (p1, p2, d) = diagnostics::benchmark(s, day_02::answer);
|
||||
|
||||
// ( p1 as u128, p2 as u128, d )
|
||||
// }
|
||||
( p1 as u128, p2 as u128, d )
|
||||
}
|
||||
|
||||
// pub fn day_03() -> DailyOutput {
|
||||
// let s : String = read_from_file("inputs/03.txt");
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ fn run_all_days() {
|
|||
let mut aoc = aoc2025::AdventOfCode::new();
|
||||
|
||||
aoc.insert(1, aoc2025::day_01());
|
||||
// aoc.insert(2, aoc2025::day_02());
|
||||
aoc.insert(2, aoc2025::day_02());
|
||||
// aoc.insert(3, aoc2025::day_03());
|
||||
// aoc.insert(4, aoc2025::day_04());
|
||||
// aoc.insert(5, aoc2025::day_05());
|
||||
|
|
|
|||
|
|
@ -36,6 +36,6 @@ pub fn str_to_i16(s : &str) -> Option<i16> {
|
|||
// 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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue