Skip to content

Commit

Permalink
Default to the next puzzle in cargo x wait
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Sep 30, 2024
1 parent 77edc50 commit e72ddcb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
37 changes: 37 additions & 0 deletions crates/utils/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,43 @@ impl Date {
pub fn release_time(&self) -> SystemTime {
SystemTime::UNIX_EPOCH + Duration::from_secs(self.release_timestamp())
}

/// The [`Date`] of the next puzzle.
#[must_use]
#[expect(clippy::cast_possible_truncation)]
pub fn next_puzzle() -> Option<Date> {
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();

let mut date = Date {
year: Year(2015),
day: Day(1),
};

// Skip ahead whole years
if now > Self::FIRST_RELEASE_TIMESTAMP {
let year = 2015 + ((now - Self::FIRST_RELEASE_TIMESTAMP) / 60 / 60 / 24 / 366);
if year > 9999 {
return None;
}
date.year = Year(year as u16);
}

while date.release_timestamp() < now {
if date.day.0 < 25 {
date.day.0 += 1;
} else if date.year.0 < 9999 {
date.year.0 += 1;
date.day.0 = 1;
} else {
return None;
}
}

Some(date)
}
}

impl Display for Date {
Expand Down
19 changes: 12 additions & 7 deletions crates/xtask/src/cmd/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ use utils::date::Date;
const ANSI_LINE_START: &str = "\x1B[0G";
const ANSI_CLEAR_LINE: &str = "\x1B[2K";

pub fn main(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
let year = crate::year_arg(&mut args)?;
let day = crate::day_arg(&mut args)?;
crate::ensure_no_args(args)?;

let date = Date { year, day };
pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
let mut args = args.peekable();

let date = if args.peek().is_none() {
Date::next_puzzle().unwrap()
} else {
let year = crate::year_arg(&mut args)?;
let day = crate::day_arg(&mut args)?;
crate::ensure_no_args(args)?;
Date { year, day }
};
let release = date.release_time();

let mut previous = (0, 0, 0, 61);
Expand Down Expand Up @@ -42,5 +47,5 @@ pub fn main(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>

println!("{ANSI_LINE_START}{ANSI_CLEAR_LINE}{date} is released!");

super::input::download(year, day)
super::input::download(date.year, date.day)
}

0 comments on commit e72ddcb

Please sign in to comment.