Compare commits

..

No commits in common. "000004605293ea4e110e0109a5a151a7c764cb7c" and "000003607b0229cbbd5bc95b6a4f2b249bdb989f" have entirely different histories.

12 changed files with 110 additions and 92 deletions

View File

@ -1,4 +1,4 @@
[alias]
scaffold = "run -p scaffold --release --quiet --"
download = "run -p download --release --quiet --"
solve = "run --bin"
solve = "run --release --bin"

View File

@ -1,2 +1,2 @@
TOKEN=secret
YEAR=year
YEAR=2022

4
.gitignore vendored
View File

@ -11,6 +11,6 @@ target/
.env
# downloaded inputs
/data/inputs/*
!/data/inputs/.keep
/src/inputs/*
!/src/inputs/.keep

24
Cargo.lock generated
View File

@ -19,7 +19,10 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aoc"
version = "46.0.0"
version = "36.0.0"
dependencies = [
"itertools",
]
[[package]]
name = "autocfg"
@ -102,13 +105,19 @@ checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0"
[[package]]
name = "download"
version = "46.0.0"
version = "36.0.0"
dependencies = [
"dotenvy",
"pico-args",
"reqwest",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "encoding_rs"
version = "0.8.31"
@ -351,6 +360,15 @@ version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e"
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.5"
@ -625,7 +643,7 @@ checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "scaffold"
version = "46.0.0"
version = "36.0.0"
dependencies = [
"dotenvy",
"pico-args",

View File

@ -1,15 +1,3 @@
[workspace.package]
description = "template for advent of code"
readme = "README.md"
version = "46.0.0"
edition = "2021"
license = "MIT"
authors = ["Matej Janežič <janezic.mj@gmail.com>"]
repository = "https://github.com/janezicmatej/aoc-template.git"
[workspace]
members = ["utils/download", "utils/scaffold"]
[package]
name = "aoc"
description.workspace = true
@ -21,4 +9,19 @@ authors.workspace = true
repository.workspace = true
[dependencies]
# many cool stuff for iterators
itertools = "0.12.0"
[workspace]
members = ["utils/download", "utils/scaffold"]
[workspace.package]
description = "template for advent of code"
readme = "README.md"
version = "36.0.0"
edition = "2021"
license = "MIT"
authors = ["Matej Janežič <janezic.mj@gmail.com>"]
repository = "https://github.com/janezicmatej/aoc-template.git"

View File

@ -5,16 +5,15 @@
## Project overview
### Project structure
- `data/` :
- `examples/`: example files go here; you can push this as test are run in ci
- `inputs/`: this directory is gitignored, input files go here
- `src/` :
- `bin/`:
- `<day>.rs`: solution files
- `lib.rs`: library entrypoint, reusable code goes here
- `template.rs`: contains template code
- `utils/`: binary packages with convenience scripts structured using cargo workspaces
- `inputs/`: this directory is gitignored, input files go here
- `examples/`: example files go here; you can push this as test are run in ci
- `utils/`: utils files go here
- `lib.rs`: contains framework code
- `.env.example`: example dotenv file
- `utils/`: binary packages with convenience scripts structured using cargo workspaces
### Cli
- `cargo scaffold <day>`: prepare solution files for `day`

View File

@ -1 +1,49 @@
pub mod template;
/*
* This file contains template code.
* There is no need to edit this file unless you want to change template functionality.
* Prefer `./helpers.rs` if you want to extract code from your solutions.
*/
use std::env;
use std::fs;
pub const ANSI_ITALIC: &str = "\x1b[3m";
pub const ANSI_BOLD: &str = "\x1b[1m";
pub const ANSI_RESET: &str = "\x1b[0m";
#[macro_export]
macro_rules! solve {
($part:expr, $solver:ident, $input:expr) => {{
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
use std::fmt::Display;
use std::time::Instant;
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
let timer = Instant::now();
let result = func(input);
let elapsed = timer.elapsed();
match result {
Some(result) => {
println!(
"{} {}(elapsed: {:.2?}){}",
result, ANSI_ITALIC, elapsed, ANSI_RESET
);
}
None => {
println!("not solved.")
}
}
}
println!("🎄 {}Part {}{} 🎄", ANSI_BOLD, $part, ANSI_RESET);
print_result($solver, $input);
}};
}
pub fn read_file(folder: &str, day: u8) -> String {
let cwd = env::current_dir().unwrap();
let filepath = cwd.join("src").join(folder).join(format!("{day:02}.txt"));
let f = fs::read_to_string(filepath);
f.expect("could not open input file").trim().to_string()
}

View File

@ -1,56 +0,0 @@
/*
* This file contains template code.
* There is no need to edit this file unless you want to change template functionality.
*/
use std::env;
use std::fs;
pub const ANSI_ITALIC: &str = "\x1b[3m";
pub const ANSI_BOLD: &str = "\x1b[1m";
pub const ANSI_RESET: &str = "\x1b[0m";
use std::fmt::Display;
use std::time::Duration;
use std::time::Instant;
fn time_solution<T>(func: impl FnOnce(&str) -> Option<T>, input: &str) -> Option<(T, Duration)> {
let timer = Instant::now();
let result = func(input);
let elapsed = timer.elapsed();
result.map(|result| (result, elapsed))
}
pub fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str, part: u8) {
match time_solution(func, input) {
Some((result, elapsed)) => {
println!(
"{}Part {}{}: {} {}(elapsed: {:.2?}){}",
ANSI_BOLD, part, ANSI_RESET, result, ANSI_ITALIC, elapsed, ANSI_RESET
);
}
None => {
println!("{}Part {}{}: not solved.", ANSI_BOLD, part, ANSI_RESET)
}
}
}
#[macro_export]
macro_rules! solution {
($day:expr) => {
fn main() {
let input = aoc::template::read_file("inputs", $day);
aoc::template::print_result(part_one, &input, 1);
aoc::template::print_result(part_two, &input, 2);
}
};
}
#[must_use]
pub fn read_file(folder: &str, day: u8) -> String {
let cwd = env::current_dir().unwrap();
let filepath = cwd.join("data").join(folder).join(format!("{day:02}.txt"));
let f = fs::read_to_string(filepath);
f.expect("could not open input file").trim().to_string()
}

View File

@ -39,7 +39,7 @@ fn main() {
.text()
.unwrap();
let input_path = format!("data/inputs/{day_padded}.txt");
let input_path = format!("src/inputs/{day_padded}.txt");
let mut file = match OpenOptions::new()
.write(true)
.create(true)

View File

@ -11,23 +11,26 @@ use std::{
const MODULE_TEMPLATE: &str = r#"pub fn part_one(input: &str) -> Option<u32> {
None
}
pub fn part_two(input: &str) -> Option<u32> {
None
}
aoc::solution!(DAY);
fn main() {
let input = &aoc::read_file("inputs", DAY);
aoc::solve!(1, part_one, input);
aoc::solve!(2, part_two, input);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
assert_eq!(part_one(&aoc::template::read_file("examples", DAY)), None);
let input = aoc::read_file("examples", DAY);
assert_eq!(part_one(&input), None);
}
#[test]
fn test_part_two() {
assert_eq!(part_two(&aoc::template::read_file("examples", DAY)), None);
let input = aoc::read_file("examples", DAY);
assert_eq!(part_two(&input), None);
}
}
"#;
@ -56,8 +59,8 @@ fn main() {
let day_padded = format!("{day:02}");
let input_path = format!("data/inputs/{day_padded}.txt");
let example_path = format!("data/examples/{day_padded}.txt");
let input_path = format!("src/inputs/{day_padded}.txt");
let example_path = format!("src/examples/{day_padded}.txt");
let module_path = format!("src/bin/{day_padded}.rs");
let mut file = match safe_create_file(&module_path) {
@ -99,5 +102,8 @@ fn main() {
}
println!("---");
println!("Type `cargo solve {}` to run your solution.", &day_padded);
println!(
"🎄 Type `cargo solve {}` to run your solution.",
&day_padded
);
}