DEV Community

Cover image for The Ideal Stocking Stuffer
Robert Mion
Robert Mion

Posted on

The Ideal Stocking Stuffer

Advent of Code 2015 Day 4

Part 1

  1. How about another game of Chess?
  2. Re-writing...
  3. Waiting...

How about another game of Chess?

  • This puzzle is déjà vu!
  • I solved it eight times as part of 2016 Day 5!
  • So...solving it once should just be another waiting game!

Re-writing...

My algorithm in pseudocode:

Import my MD5 library
Set index to 0
Do as long as the first five characters in the generated hash are not zeros
  Increment index by 1
Enter fullscreen mode Exit fullscreen mode

My algorithm in JavaScript:

const MD5 = require('crypto-js/md5')
let index = 0, input = 'abcdef'
while (MD5(input + index).toString().slice(0,5) !== '00000') index++
return index
Enter fullscreen mode Exit fullscreen mode

Waiting...

  • It took a couple of seconds to run
  • Then generated the correct answer!

Part 2

Six zeroes? No problem!

My algorithm in JavaScript:

const MD5 = require('crypto-js/md5')
let index = 0, input = 'abcdef'
while (MD5(input + index).toString().slice(0,6) !== '000000') index++
return index
Enter fullscreen mode Exit fullscreen mode
  • It took several seconds to run
  • Then generated the correct answer!

I did it!!

  • I solved both parts!
  • Using a snippet of an algorithm I wrote in a nearly-identical puzzle from the prior year!
  • I wrote what may be my shortest Advent of Code program yet, at just four lines when condensed!

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)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

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

Okay