npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aoc-toolbox

v1.5.6

Published

Various useful tools for solving Advent of Code problems

Downloads

8

Readme

aoc-toolbox

This is a small utility to read an input data file from an Advent of Code problem, and offer a set of alternatives on how to extract and handle the data.

Features

The AoC problems start with a file of input data (input.txt), which need to be parsed and handled. After that, of course, there is solving the actual problem, but you won't even get that far until you've decided how to get data in.

The AoC Toolbox is currently only one class, InputData which reads the datafile and allows you to access the data in different ways, depending on how it is structured.

So, which typical input data structures will InputData be useful with? Here is a summary:

  • string per line - this is of course just a mirror of the actual file. Use the InputData.lines() method to access.
  • integers per line - if all lines contains a single integer (not uncommon in AoC problems), use InputData.linesInts() to access.
  • csv per line - each line consists of a set of fields, separated by a specific character (such as comma or colon). Use InputData.linefieldsSeparator().
  • complex field structure - sometimes, each line consists of fields, but has a more complex structure. If the fields can be identified with a regular expression, use InputData.linefieldsRegexp().
  • sections of the file - when the input data is separated into sections, by having eg empty lines in between, use InputData.sections() to be able to handle each section separately.
  • input data is a character matrix - when you need to access characters in the file by coordinates or extracting a sub matrix to work with, use InputData.matrixChar() and InputData.subMatrix() respectively.

Many of the above methods actually return a new InputData instance instead of raw data, allowing the use cases above to be chained in a string of steps.

There are also other ways to extract a subset of the data to operate on further:

  • filter out lines - the generic way to filter out a set of lines that matches any predicate is to use the InputData.filterLines() method. There is also a special case of this, to filter out lines matching the value of a character in a given column, InputData.filterOnColValue().

Example use

Assuming the below input.txt, which describes a number of teams, separated in sections. First line in each section is team name, second line is team members and their ages:

The Wonderboys
carl:12 jonas:47 doug:38

The Fast Ladies
karen:51 taylor:21 alicia:28 billie:18

This code extracts these elements in a structured way:

const {InputData} = require('aoc-toolbox')
const aoc = new InputData() // input.txt is default filename

let teamSections = aoc.sections() // assumes sections separated by empty lines
let teams = teamSections.map((team) => {
    let teamName = team.lines()[0] // name is in first line
    let memberData = team.lines()[1] // get second line as string
    let teamMembers = memberData.split(' ').map((m) => {
        let mData = m.split(':')
        return {name: mData[0], age: parseInt(mData[1])}
    })
    return {teamName, teamMembers}
})
console.log(JSON.stringify(teams, null, 2))

The code above will print out this:

[
  {
    "teamName": "The Wonderboys",
    "teamMembers": [
      {
        "name": "carl",
        "age": 12
      },
      {
        "name": "jonas",
        "age": 47
      },
      {
        "name": "doug",
        "age": 38
      }
    ]
  },
  {
    "teamName": "The Fast Ladies",
    "teamMembers": [
      {
        "name": "karen",
        "age": 51
      },
      {
        "name": "taylor",
        "age": 21
      },
      {
        "name": "alicia",
        "age": 28
      },
      {
        "name": "billie",
        "age": 18
      }
    ]
  }
]