DEV Community

Cover image for JSAbacusFramework.io
Robert Mion
Robert Mion

Posted on

JSAbacusFramework.io

Advent of Code 2015 Day 12

Part 1

regex, map() and reduce() for an easy win!

I need to calculate:

the sum of all the numbers in the document

One part regex:

/-*\d+/g
Enter fullscreen mode Exit fullscreen mode

Two parts map() and reduce():

[...input.matchAll(/-*\d+/g)]
  .map(el => +el[0])
  .reduce((sum, num) => sum + num)
Enter fullscreen mode Exit fullscreen mode

Voila! The correct answer!

Part 2

  1. Collapse, collapse, collapse
  2. Copy-paste-rerun

Collapse, collapse, collapse

  • I viewed the parsed JSON in my web browser
  • This shows the document as a collapsible tree of key-value pair
  • I use Find... to locate all references to red
  • Then collapsed each object containing red as the value for one of its keys
  • Being sure not to collapse any arrays with red as an element

It took about 20 minutes to collapse everything, then to double-check I didn't miss anything.

Copy-paste-rerun

  • I copied the collapsed, parsed JSON as raw text into a new file
  • And I ran my algorithm on that file

Thankfully, I caught everything, because it generated the correct answer!

I did it!!

  • I solved both parts!
  • Using regex, my trusted array methods, and the toggle buttons in the parsed JSON view in my web browser!
  • In under a half hour!

I'm a bit sad I didn't use an algorithm to fully solve Part 2.

Although, I'm glad I didn't have to...thanks to my web browser's handy view!

This puzzle may be the first Post-Day-10 puzzle I solved in under a half hour.

Or maybe not.

Either way, on to the next one!

Top comments (0)