Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[alias]
today = "run --quiet --release --features today -- today"
scaffold = "run --quiet --release -- scaffold"
download = "run --quiet --release -- download"
read = "run --quiet --release -- read"
Expand Down
156 changes: 154 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ publish = false
[lib]
doctest = false

[profile.dhat]
inherits = "release"
debug = 1

[features]
today = ["chrono"]
test_lib = []
dhat-heap = ["dhat"]

[dependencies]
chrono = { version = "0.4.31", optional = true }
pico-args = "0.5.0"
dhat = { version = "0.3.2", optional = true }

[profile.dhat]
inherits = "release"
debug = 1
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,41 @@ cargo read <day>
# ...the input...
```

### Scaffold, download and read in one go

> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).

During december, the `today` shorthand command can be used to:

- scaffold a solution for the current day
- download its input
- and read the puzzle

in one go.

```sh
# example: `cargo today` on December 1st
cargo today

# output:
# Created module file "src/bin/01.rs"
# Created empty input file "data/inputs/01.txt"
# Created empty example file "data/examples/01.txt"
# ---
# 🎄 Type `cargo solve 01` to run your solution.
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
# ---
# 🎄 Successfully wrote input to "data/inputs/01.txt".
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
#
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
# ...the input...
```

## Optional template features

### Configure aoc-cli integration
Expand Down
26 changes: 26 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
use args::{parse, AppArguments};

#[cfg(feature = "today")]
use advent_of_code::template::Day;
#[cfg(feature = "today")]
use std::process;

mod args {
use advent_of_code::template::Day;
use std::process;
Expand All @@ -27,6 +32,8 @@ mod args {
release: bool,
time: bool,
},
#[cfg(feature = "today")]
Today,
}

pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -54,6 +61,8 @@ mod args {
time: args.contains("--time"),
dhat: args.contains("--dhat"),
},
#[cfg(feature = "today")]
Some("today") => AppArguments::Today,
Some(x) => {
eprintln!("Unknown command: {x}");
process::exit(1);
Expand Down Expand Up @@ -96,6 +105,23 @@ fn main() {
dhat,
submit,
} => solve::handle(day, release, time, dhat, submit),
#[cfg(feature = "today")]
AppArguments::Today => {
match Day::today() {
Some(day) => {
scaffold::handle(day);
download::handle(day);
read::handle(day)
}
None => {
eprintln!(
"`today` command can only be run between the 1st and \
the 25th of december. Please use `scaffold` with a specific day."
);
process::exit(1)
}
};
}
},
};
}
16 changes: 16 additions & 0 deletions src/template/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::error::Error;
use std::fmt::Display;
use std::str::FromStr;

#[cfg(feature = "today")]
use chrono::{Datelike, Local};

/// A valid day number of advent (i.e. an integer in range 1 to 25).
///
/// # Display
Expand Down Expand Up @@ -37,6 +40,19 @@ impl Day {
}
}

#[cfg(feature = "today")]
impl Day {
/// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
pub fn today() -> Option<Self> {
let today = Local::now();
if today.month() == 12 && today.day() <= 25 {
Self::new(u8::try_from(today.day()).ok()?)
} else {
None
}
}
}

impl Display for Day {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:02}", self.0)
Expand Down