From fbfffff512f5901cacd94a5d0af3792602bf2976 Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Sat, 19 Nov 2022 21:11:26 +0100 Subject: [PATCH] Add convenient project base --- .gitignore | 3 +++ Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 7 +++++++ README.md | 31 +++++++++++++++++++++++++++++++ justfile | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 10 ++++++++++ 6 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 justfile create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..623dc87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +output/ +.env diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b66cb4e --- /dev/null +++ b/Cargo.lock @@ -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", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0187458 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc2022" +version = "0.0.0" +edition = "2021" + +[dependencies] +itertools = "0.10.5" diff --git a/README.md b/README.md new file mode 100644 index 0000000..01903dc --- /dev/null +++ b/README.md @@ -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. diff --git a/justfile b/justfile new file mode 100644 index 0000000..f3134eb --- /dev/null +++ b/justfile @@ -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 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1585a14 --- /dev/null +++ b/src/lib.rs @@ -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: S) { + println!("level-1-solution={}", s.to_string()); +} + +/// See [solved_level_1]. +pub fn solved_level_2(s: S) { + println!("level-2-solution={}", s.to_string()); +}