1.0.2 • Published 7 years ago

@thefotios/advent_puzzle v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
7 years ago

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,
    });
  2. Define processors. These can also be passed into the constructor.

    Valid Processors are run in the following order:

    KeyWhen it's run
    beforeAfter parsing the data, but before each puzzle
    A or BRun depending on which puzzle is passed in the args
    afterAfter the A or B processor is run
    loggerLast, 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);
  3. Add run function

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

    p.run()
  4. Run it!

    node your_script.js [data_file] [A or B]