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/CyroS Dec 04 '15 edited Dec 04 '15

Perl:

Normal solution:

use strict;
use warnings;
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $string = 'bgvyzdsv';
my $i = 0;
while($i<10000000){
	$i++;
	my $data = "$string$i";
	my $digest = md5_hex($data);
	if($digest =~ /^0{6,}/){
		print "$digest - $data - $i\n";
		last;
	}
}
print $i;

Golfed:

use Digest::MD5 qw(md5_hex);my $i=0;while(){$i++;$_=md5_hex("bgvyzdsv$i");s/^0{5,}/last/e;}print $i;

2

u/xdg Dec 04 '15

Ooh. nice use of s/.../last/e. I'll have to remember that one.