Skip to content

delventhalz/advent-of-code-2022

Repository files navigation

Advent of Code 2022 Solutions and Runner

Merry Advent of Code to those who celebrate!

This is a copy of my 2021 AoC Runner, without last year's solutions. It is mostly aimed at streamlining developing JavaScript solutions, but I am starting to experiment with supporting other languages. I expect the project to evolve and change over the month depending on my needs.

Contents

Setup

Ensure you have a recent version of Node.js installed, then run in your terminal:

git clone https://github.com/delventhalz/advent-of-code-2022.git
cd advent-of-code-2022/
npm install

Usage

1. Generate Stub Files

Begin your solution by first generating stub files:

npm run generate <directory name>

This will create a new directory with three files:

  • 1.js
  • 2.js
  • input.txt

2. Write Your Solution

Copy your puzzle inputs and paste them into input.txt. The runner will read these inputs, parse them, and pass them to your solution file.

The solution file should export a single function.

module.exports = (inputs, rawInput) => {

    // Calculate solution...

    return solution
};
  • Parameters
    • inputs - The parsed input.txt file. Typically an array, possibly a nested array. The runner progressively splits the input string on any sensible delimiters (blank lines, new lines, commas), and then parses any numbers. If the input contains no sensible delimiters, will just be the raw string.
    • rawInput (optional) - Always the raw unparsed string version of input.txt. Useful if the input is formatted in such a way that the default parser creates something erroneous or unhelpful. Usually if I have to use this parameter, I end up refactoring the parser the next day ;).
  • Returns
    • The solution you calculated. Will be logged to the console by the runner.

Lodash and a few custom utilities are available as CommonJS modules as well. Import with require:

const { chunk } = require('lodash');

Of course new modules can be installed with NPM:

npm install <module name>

3. Run Your Solution

Once the solution file is complete, and the input string is in input.txt, running your solution can be done with the npm start command, with the path to the solution file provided as the single argument:

npm start <path to solution file>

4. (Optional) Test Your Solution

Advent of Code often provides shortened example inputs which can be useful to debug failing solutions. These can be substituted for the inputs at input.txt directly from the command line using the npm run as-test command. In addition to the path to the solution file, provide one or more example input strings as further arguments:

npm run as-test <path to solution file> "<example input 1>" "<example input 2>"

For particularly troublesome problems, Mocha/Chai is available. Simply create a unit test file in your solution folder and name it with the .test.js suffix.

To run all .test.js files:

npm test

To test only a single file, use the -- option to pass the path to the test file as a command line argument to the npm script:

npm test -- <path to test file>

Linting

A basic linter is included to catch stupid errors. It will automatically check all files before running your solution, though errors will not stop the run. To run the linter manually:

npm run lint

Solutions

Any solutions of mine will be in numbered directories, and should run correctly with the tools above. Reference or run them as you will.

Helper Utils

For the most part I just copied and pasted the code I reused. But if a particular function was repeatedly useful across multiple nights, I pulled it out into a module, and put that module in the lib/ directory. These can be imported by using require on the lib directory:

const { sum } = require('../lib');

const nums = [1, 2, 3];
console.log(sum(nums));  // 6

Using Other Languages

For this year I am playing around more with languages other than JavaScript. To that end I have created a simple run script which can compile and run solution files:

./run <path to solution file>

Currently the script supports:

  • JavaScript - requires node
  • Python - requires python
  • C - requires gcc
  • Rust - requires rustc (install here)
  • Clojure - requires clj (install here)

For non-JS languages, the support is very basic. There is no parsing of input.txt and no logging of the solution. The source file is simply compiled and then run.

Leaderboard

In past years I have competed on caderek's leaderboard. Assuming it is still active, I will stay on it this year as well, and you can join yourself with this code:

107172-b51ab08f

I am also using my own leaderboard with a few friends, which you can join with this code:

472347-2b04474b

License

Advent of Code and all Advent of Code 2022 puzzle text and content were created by and belong to Eric Wastl, and are reproduced here for reference purposes only.

All solution code and associated tools were written by me, Zac Delventhal, and are made available under the MIT open source license.

About

Advent of Code 2022 solutions and test runner

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published