Refactor day 1 & remove compiler warnings
parent
5ea66062ee
commit
ca7f0d2d28
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
struct Dial {
|
struct Dial {
|
||||||
points_at : i16,
|
points_at : i16,
|
||||||
|
|
@ -42,34 +41,27 @@ impl Dial {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn answer(text : String) ->( u32, u32 ) {
|
pub fn answer(text : String) ->( u32, u32 ) {
|
||||||
let items : Vec<i16> = parse(text);
|
|
||||||
|
|
||||||
let mut dial : Dial = Dial::new();
|
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.clicks + dial.passing_clicks
|
, dial.clicks + dial.passing_clicks
|
||||||
// 5978 too low
|
|
||||||
// 6298 too low
|
|
||||||
// 6536 too high
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(text : String) -> Vec<i16> {
|
fn str_to_dir(s : &str) -> Option<i16> {
|
||||||
Regex::new(r"(L|R)(\d+)").unwrap()
|
if s.len() < 2 {
|
||||||
.captures_iter(&text)
|
return None;
|
||||||
.map(|c| c.extract())
|
}
|
||||||
.filter_map(
|
|
||||||
|(_, [ d, n ])|
|
let (dir, l) : (&str, &str) = s.split_at(1);
|
||||||
match d {
|
|
||||||
"L" => Some(-1 * utils::str_to_i16(n)?),
|
match dir {
|
||||||
"R" => utils::str_to_i16(n),
|
"L" => Some(-1 * utils::str_to_i16(l)?),
|
||||||
|
"R" => utils::str_to_i16(l),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
)
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ use crate::utils::diagnostics;
|
||||||
|
|
||||||
type DailyOutput = ( u128, u128, Duration );
|
type DailyOutput = ( u128, u128, Duration );
|
||||||
|
|
||||||
|
pub use crate::utils::diagnostics::AdventOfCode;
|
||||||
|
|
||||||
pub fn day_01() -> DailyOutput {
|
pub fn day_01() -> DailyOutput {
|
||||||
let s : String = read_from_file("inputs/01.txt");
|
let s : String = read_from_file("inputs/01.txt");
|
||||||
let ( p1, p2, d ) = diagnostics::benchmark(s, day_01::answer);
|
let ( p1, p2, d ) = diagnostics::benchmark(s, day_01::answer);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
mod utils;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut aoc = crate::utils::diagnostics::AdventOfCode::new();
|
let mut aoc = aoc2025::AdventOfCode::new();
|
||||||
|
|
||||||
aoc.insert(1, aoc2025::day_01());
|
aoc.insert(1, aoc2025::day_01());
|
||||||
// aoc.insert(2, aoc2025::day_02());
|
// aoc.insert(2, aoc2025::day_02());
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ use std::fs;
|
||||||
|
|
||||||
pub mod diagnostics;
|
pub mod diagnostics;
|
||||||
|
|
||||||
pub fn char_to_u64(s : char) -> Option<u64> {
|
// pub fn char_to_u64(s : char) -> Option<u64> {
|
||||||
s.to_string().parse::<u64>().ok()
|
// s.to_string().parse::<u64>().ok()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn read_from_file(name : &str) -> String {
|
pub fn read_from_file(name : &str) -> String {
|
||||||
match fs::read_to_string(name) {
|
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()
|
s.trim().to_string().parse::<i16>().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn str_to_i32(s : &str) -> Option<i32> {
|
// pub fn str_to_i32(s : &str) -> Option<i32> {
|
||||||
s.trim().to_string().parse::<i32>().ok()
|
// s.trim().to_string().parse::<i32>().ok()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn str_to_u8(s : &str) -> Option<u8> {
|
// pub fn str_to_u8(s : &str) -> Option<u8> {
|
||||||
s.trim().to_string().parse::<u8>().ok()
|
// s.trim().to_string().parse::<u8>().ok()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn str_to_u16(s : &str) -> Option<u16> {
|
// pub fn str_to_u16(s : &str) -> Option<u16> {
|
||||||
s.trim().to_string().parse::<u16>().ok()
|
// s.trim().to_string().parse::<u16>().ok()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn str_to_u32(s : &str) -> Option<u32> {
|
// pub fn str_to_u32(s : &str) -> Option<u32> {
|
||||||
s.trim().to_string().parse::<u32>().ok()
|
// s.trim().to_string().parse::<u32>().ok()
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub fn str_to_u64(s : &str) -> Option<u64> {
|
// pub fn str_to_u64(s : &str) -> Option<u64> {
|
||||||
s.trim().to_string().parse::<u64>().ok()
|
// s.trim().to_string().parse::<u64>().ok()
|
||||||
}
|
// }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue