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!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay