Refactor day 1 & remove compiler warnings
parent
5ea66062ee
commit
ca7f0d2d28
|
|
@ -1,5 +1,4 @@
|
|||
use crate::utils;
|
||||
use regex::Regex;
|
||||
|
||||
struct Dial {
|
||||
points_at : i16,
|
||||
|
|
@ -42,34 +41,27 @@ impl Dial {
|
|||
}
|
||||
|
||||
pub fn answer(text : String) ->( u32, u32 ) {
|
||||
let items : Vec<i16> = parse(text);
|
||||
|
||||
let mut dial : Dial = Dial::new();
|
||||
for d in items.iter() {
|
||||
dial.rotate_by(*d);
|
||||
}
|
||||
|
||||
println!("Length: {}", &items.len());
|
||||
for d in text.split("\n").filter_map(str_to_dir) {
|
||||
dial.rotate_by(d);
|
||||
}
|
||||
|
||||
( dial.clicks
|
||||
, dial.clicks + dial.passing_clicks
|
||||
// 5978 too low
|
||||
// 6298 too low
|
||||
// 6536 too high
|
||||
)
|
||||
}
|
||||
|
||||
fn parse(text : String) -> Vec<i16> {
|
||||
Regex::new(r"(L|R)(\d+)").unwrap()
|
||||
.captures_iter(&text)
|
||||
.map(|c| c.extract())
|
||||
.filter_map(
|
||||
|(_, [ d, n ])|
|
||||
match d {
|
||||
"L" => Some(-1 * utils::str_to_i16(n)?),
|
||||
"R" => utils::str_to_i16(n),
|
||||
_ => None,
|
||||
}
|
||||
)
|
||||
.collect()
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
@ -20,22 +20,22 @@ pub fn str_to_i16(s : &str) -> Option<i16> {
|
|||
s.trim().to_string().parse::<i16>().ok()
|
||||
}
|
||||
|
||||
pub fn str_to_i32(s : &str) -> Option<i32> {
|
||||
s.trim().to_string().parse::<i32>().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_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_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()
|
||||
// }
|
||||
|
|
|
|||
Loading…
Reference in New Issue