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

Show parent comment

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);