Skip to content

Commit

Permalink
feat: added scaffold script and scaffolded day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Dec 7, 2023
1 parent 6ee210c commit da41bf0
Show file tree
Hide file tree
Showing 8 changed files with 1,202 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ members = [
"y2023/ex03",
"y2023/ex04",
"y2023/ex05",
"y2023/ex07",
"y2023/ex07",
"y2023/ex07",
"y2023/ex07",
]

[profile.bench]
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ cargo test --package ex01 --lib --all-features -- tests::part_2

## Create a new exercise

**New**: You can now use the `scaffold.mjs` script to create a new exercise. Simply run:

```bash
./scaffold.mjs <year> <day>
```

(Note: it requires [`zx`](https://github.com/google/zx) to be installed).

---


Alternatively, you can create a new exercise manually by following these steps:


Cd into the specific **year folder** (e.g. `y2020`) and run:

```bash
Expand Down
120 changes: 120 additions & 0 deletions scaffold.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env zx
import fs from 'fs/promises'

const year = process.argv[3]
const day = process.argv[4]

if (!year || !day) {
console.log('Usage: scaffold.mjs <year> <day>')
process.exit(1)
}

const yearDir = `y${year}`
const dayDir = `ex${day.padStart(2, '0')}`

// create year folder
await $`mkdir -p ./${yearDir}`

// Create rust project
// await $`cd ./y${year} && cargo new --lib ${dayDir}`

// update Cargo.toml
const cargoToml = `[package]
name = "${yearDir}${dayDir}"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bench]]
name = "bench_${yearDir}${dayDir}"
harness = false
[dev-dependencies]
criterion = "0.5.1"
`
await fs.writeFile(`./${yearDir}/${dayDir}/Cargo.toml`, cargoToml)

// Scaffold lib.rs
const libRs = `
pub fn part1(input: &str) -> usize {
0
}
pub fn part2(input: &str) -> usize {
0
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = include_str!("../input.txt");
#[test]
fn test_part1() {
assert_eq!(part1(INPUT), 0);
}
#[test]
fn test_part2() {
assert_eq!(part2(INPUT), 0);
}
}
`
await fs.writeFile(`./${yearDir}/${dayDir}/src/lib.rs`, libRs)

// Create input.txt
await fs.writeFile(`./${yearDir}/${dayDir}/input.txt`, '')

// Create benchmark files
const benchRs = `
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ${yearDir}${dayDir}::{part1, part2};
fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../input.txt");
c.bench_function("${yearDir}${dayDir}::part1", |b| b.iter(|| part1(black_box(input))));
c.bench_function("${yearDir}${dayDir}::part2", |b| b.iter(|| part2(black_box(input))));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
`
await $`mkdir -p ./${yearDir}/${dayDir}/benches`
await fs.writeFile(`./${yearDir}/${dayDir}/benches/bench_${yearDir}${dayDir}.rs`, benchRs)

// updates Cargo.toml workspace
const mainCargoToml = await fs.readFile('./Cargo.toml', 'utf8')
const updatedCargoToml = mainCargoToml.replace(`]
[profile.bench]
debug = true`,
` "${yearDir}/${dayDir}",
]
[profile.bench]
debug = true`);
await fs.writeFile('./Cargo.toml', updatedCargoToml)

// format files
await $`cd ./${yearDir}/${dayDir} && cargo fmt`

// create README.md
const readme = `# Day ${day}: TODO: ADD TITLE HERE
[Check it out on adventofcode.com](https://adventofcode.com/${year}/day/${day})
## Part One
TODO: ADD DESCRIPTION HERE
Your puzzle answer was \`?\`. (TODO: )
## Part Two
TODO: ADD DESCRIPTION HERE
Your puzzle answer was \`?\`. (TODO: )`
fs.writeFile(`./${yearDir}/${dayDir}/README.md`, readme)
15 changes: 15 additions & 0 deletions y2023/ex07/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "y2023ex07"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[[bench]]
name = "bench_y2023ex07"
harness = false

[dev-dependencies]
criterion = "0.5.1"
15 changes: 15 additions & 0 deletions y2023/ex07/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Day 7: TODO: ADD TITLE HERE

[Check it out on adventofcode.com](https://adventofcode.com/2023/day/7)

## Part One

TODO: ADD DESCRIPTION HERE

Your puzzle answer was `?`. (TODO: )

## Part Two

TODO: ADD DESCRIPTION HERE

Your puzzle answer was `?`. (TODO: )
11 changes: 11 additions & 0 deletions y2023/ex07/benches/bench_y2023ex07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use y2023ex07::{part1, part2};

fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../input.txt");
c.bench_function("y2023ex07::part1", |b| b.iter(|| part1(black_box(input))));
c.bench_function("y2023ex07::part2", |b| b.iter(|| part2(black_box(input))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit da41bf0

Please sign in to comment.