Skip to content

delventhalz/AdventOfCode2019

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advent of Code 2019 Solutions and Runner

Been having a lot of fun with Advent of Code this year. In addition to solutions, I have written some Node.js CLI tools to help run and debug my work. Both the solution code and CLI tools are included in this repo. Mostly for myself, but also for anyone who wishes to reference or utilize them.

Contents

Setup

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

git clone https://github.com/delventhalz/AdventOfCode2019.git
cd AdventOfCode2019/
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. Complete Your Solution

Each solution file should export a single function which takes parsed puzzle inputs and returns a solution to log to the console.

Puzzle inputs should be copied and pasted into input.txt. The runner will read these inputs, parse them, and pass them to the exported solution functions.

Lodash, an Intcode runner, and a few other utilities are available as CommonJS modules. 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 test command. In addition to the path to the solution file, provide one or more example input strings as further arguments:

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

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

All of my solutions are 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 like any other CommonJS module:

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

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

Intcode

A repeated feature of this year's Advent of Code is an assembly-style instruction interpreter called "Intcode". After the first few nights, I pulled mine into lib/intcode.js. If you wish to use it, first import the getRunner function into your solution file:

const { getRunner } = require('../lib/intcode.js');

API

  • getRunner(program, [options])
    • program: An array of numbers, the Intcode program
    • options: Options object to toggle certain modes, all default to false
      • pauseOnOutput - each run call outputs a single value and pauses
      • pauseOnInput - pauses whenever it runs out of inputs
      • quietIO - turns off logging for input and output events
      • debugMode - turns on logging for every single opcode
    • returns a run function
  • run(...inputs) (default mode)
    • inputs: one or more numbers can be provided as inputs
    • returns an array of output values upon reaching a terminate opcode
  • run(...inputs) (pauseOnOutput mode)
    • inputs: one or more numbers can be provided as inputs
    • returns a single output value upon reaching an output opcode
    • returns null upon reaching a terminate opcode
    • Can be run repeatedly with new inputs
  • run(...inputs) (pauseOnInput mode)
    • inputs: one or more numbers can be provided as inputs
    • returns an array of output values, with null in the final position if a terminate opcode has been reached
    • Can be run repeatedly with new inputs

Examples

const run = getRunner(program);

const outputs = run(0, 1);
const run = getRunner(program, { pauseOnOutput: true, quietIO: true });

let output = run(0, 1);

while (output !== null) {
  output = run(output);
}

Leaderboard

Join me on caderek's leaderboard with this code:

107172-b51ab08f

License

Advent of Code and all Advent of Code 2019 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

Solutions and tools for Advent of Code 2019

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published