Add convenient project base
This commit is contained in:
commit
fbfffff512
6 changed files with 114 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
output/
|
||||
.env
|
25
Cargo.lock
generated
Normal file
25
Cargo.lock
generated
Normal file
|
@ -0,0 +1,25 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc2022"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "aoc2022"
|
||||
version = "0.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10.5"
|
31
README.md
Normal file
31
README.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
# Advent Of Code 2022
|
||||
|
||||
My solutions for [Advent Of Code](https://adventofcode.com) 2022.
|
||||
|
||||
## Usage
|
||||
|
||||
This project uses [`just`](https://github.com/casey/just) to speed up some commands. For some commands to work (like downloading puzzle input or submitting a solution) a session token needs to be provided in the `AOC_SESSION` environment variable. The easiest way to set it is to create the file `.env` with `AOC_SESSION=your token` inside in the root of this repository. The token can be received by reading the session cookie from the AOC website.
|
||||
|
||||
Download the puzzle input, create a file for the current day and open it in vscode:
|
||||
|
||||
```shell
|
||||
just begin # Prepare the current day
|
||||
just day=09 begin # Prepare day 9
|
||||
```
|
||||
|
||||
Run:
|
||||
|
||||
```shell
|
||||
just # Runs the current day
|
||||
just day=09 # Runs day 9
|
||||
```
|
||||
|
||||
After running a puzzle the output is saved to `output/XX.log`. This log will be read when submitting puzzles:
|
||||
|
||||
```shell
|
||||
just submit 1 # Submit the first half of a puzzle
|
||||
just submit 2 # Submit the second half of a puzzle
|
||||
just day=09 submit 2 # Submit the second half of the puzzle for day 9
|
||||
```
|
||||
|
||||
Submit looks for a line containing `level-1-solution=123` to submit 123 as the solution to level 1. There is a convenience function `solved_level_1` in `lib.rs` you can use to avoid printing this line manually.
|
38
justfile
Normal file
38
justfile
Normal file
|
@ -0,0 +1,38 @@
|
|||
set dotenv-load
|
||||
|
||||
day := `date +%d`
|
||||
dayWithout0 := trim_start_match(day, "0")
|
||||
year := "2022"
|
||||
file := "src/bin/" + day + ".rs"
|
||||
|
||||
# Format, lint, and run the program for today.
|
||||
run: _output-folder
|
||||
rustfmt src/bin/{{day}}.rs
|
||||
cargo clippy
|
||||
# Hide warning here because we just ran clippy
|
||||
RUSTFLAGS=-Awarnings cargo run --release --bin {{day}} | tee "output/{{day}}.log"
|
||||
|
||||
# Begin working on todays problem. Downloads input, creates template and opens the problem and code.
|
||||
begin: _input-folder
|
||||
echo "use aoc2022::*;\n\nconst INPUT: &str = include_str!(\"../../input/{{day}}\");\n\nfn main() {\n\n}" >> {{file}}
|
||||
curl --silent "https://adventofcode.com/{{year}}/day/{{dayWithout0}}/input" -H "Cookie: session=$AOC_SESSION" > "input/{{day}}"
|
||||
code {{file}}
|
||||
open "https://adventofcode.com/{{year}}/day/{{dayWithout0}}"
|
||||
|
||||
# Submit a solution from the previously run program for the given level (1 or 2).
|
||||
submit level:
|
||||
# Find the solution and pass it to the _submit command.
|
||||
just _submit {{level}} $(pcregrep -o1 "^level-{{level}}-solution=(.*)$" output/{{day}}.log)
|
||||
|
||||
_submit level solution:
|
||||
curl --silent "https://adventofcode.com/{{year}}/day/{{dayWithout0}}/answer" \
|
||||
-X "POST" \
|
||||
-H "Cookie: session=$AOC_SESSION" \
|
||||
--data "level={{level}}&answer={{solution}}" \
|
||||
| xmllint --html --xpath "//main" - 2> /dev/null
|
||||
|
||||
_output-folder:
|
||||
mkdir -p output
|
||||
|
||||
_input-folder:
|
||||
mkdir -p input
|
10
src/lib.rs
Normal file
10
src/lib.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
/// Remember the passed value as the solution for the current puzzle.
|
||||
/// Prints a specially formatted line so that the solution can be found from the justfile.
|
||||
pub fn solved_level_1<S: ToString>(s: S) {
|
||||
println!("level-1-solution={}", s.to_string());
|
||||
}
|
||||
|
||||
/// See [solved_level_1].
|
||||
pub fn solved_level_2<S: ToString>(s: S) {
|
||||
println!("level-2-solution={}", s.to_string());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue