r/adventofcode • • Dec 04 '15

SOLUTION MEGATHREAD --- Day 4 Solutions ---

--- Day 4: The Ideal Stocking Stuffer ---

Post your solution as a comment. Structure your post like the Day Three thread.

14 Upvotes

272 comments sorted by

View all comments

2

u/[deleted] Dec 04 '15

Javascript with md5 library:

var secretKey = 'bgvyzdsv';

function run() {
    var num = 0;
    var hash = md5(secretKey + num);

    while (hash.slice(0, 6) !== '000000') {		
	    num++;
	    hash = md5(secretKey + num);
    }

    document.getElementById('output').innerHTML = num;
}

2

u/cloudwad Dec 04 '15

Mine using the same library:

var puzzleInput = 'yzbqklnj';
var lowestFiveNum = 0;
var lowestSixNum = 0;
var hexCode = 0;

for (var i=0; i<99999999; i++) {

	//Converts puzzle input plus i integer tp hexcode
	hexCode = MD5(puzzleInput + i.toString());

	//Test for five leading 0s in hexCode
	if (lowestFiveNum == 0 && /^00000/.test(hexCode)) {
		//Set the lowestFiveNum to current i integer
		lowestFiveNum = i;
	}
	//Test for six leading 0s in hexCode
	if (lowestSixNum == 0 && /^000000/.test(hexCode)) {
		//Set the lowestSixNum to current i integer
		lowestSixNum = i;
	}

	//If both numbers have been found break the loop
	if (lowestFiveNum > 0 && lowestSixNum > 0) {
		break;
	}
}
console.log('Lowest Number containing five 0s: ' + lowestFiveNum);
console.log('Lowest Number containing six 0s: ' + lowestSixNum);

0

u/[deleted] Dec 04 '15

[deleted] — view deleted comment

1

u/[deleted] Dec 04 '15

I can believe it. I just used the first md5 lib that showed up in my google search (ha!). It was fast enough for these problems, but I will have to keep this in mind if I need md5 for any projects in the future though! Thanks :)

1

u/joeyrobert Dec 04 '15

Also JavaScript using a different md5 library:

'use strict';

const md5 = require('md5');
let secret = 'yzbqklnj';
let beginnings = ['00000', '000000'];

beginnings.forEach(beginning => {
    for (let counter = 1;; counter++) {
        let hashed = md5(secret + counter);

        if (hashed.substr(0, beginning.length) === beginning) {
            console.log(hashed, counter);
            break;
        }
    }
});