@thefotios/advent_puzzle

1.0.2 • Public • Published

npm version

advent_puzzle

Simple helper class for parsing and running Advent of Code puzzles.

This will parse the data file in a few ways then call your processing function. Since each day has 2 puzzles (referred to here as A and B for simplicity), you can specify separate runners for each type.

Usage

  1. Create a new Puzzle
const Puzzle = require('@thefotios/advent_puzzle');
const p = new Puzzle({
  // Each line is comma separated (with spaces)
  delimiter: /,\s+/,
  // Parse each element as an Integer
  numeric: true,
});
  1. Define processors. These can also be passed into the constructor.

Valid Processors are run in the following order:

Key When it's run
before After parsing the data, but before each puzzle
A or B Run depending on which puzzle is passed in the args
after After the A or B processor is run
logger Last, by default will console.log data
// Add elements of each line
p.A = lines => lines.map(data => data.reduce((acc, x) => acc + x, 0));

// Multiply elements of each line
p.B = lines => lines.map(data => data.reduce((acc, x) => acc * x, 1));

// Sort the results of each line (descending)
p.after = data => data.sort((a, b) => b - a);
  1. Add run function

By default, this will run console.log on your final data

p.run()
  1. Run it!
node your_script.js [data_file] [A or B]

Readme

Keywords

Package Sidebar

Install

npm i @thefotios/advent_puzzle

Weekly Downloads

2

Version

1.0.2

License

ISC

Last publish

Collaborators

  • thefotios