Add day 1 (rough version)
parent
590cfd770b
commit
5ea66062ee
|
|
@ -5,3 +5,4 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
colored = "3.0.0"
|
colored = "3.0.0"
|
||||||
|
regex = "1.12.2"
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,5 +1,75 @@
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
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 ) {
|
pub fn answer(text : String) ->( u32, u32 ) {
|
||||||
( 0, 0 )
|
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());
|
||||||
|
|
||||||
|
( 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()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ pub fn read_from_file(name : &str) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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> {
|
pub fn str_to_i32(s : &str) -> Option<i32> {
|
||||||
s.trim().to_string().parse::<i32>().ok()
|
s.trim().to_string().parse::<i32>().ok()
|
||||||
}
|
}
|
||||||
|
|
@ -23,6 +27,10 @@ pub fn str_to_i32(s : &str) -> Option<i32> {
|
||||||
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> {
|
||||||
|
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()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue