r/adventofcode • • Dec 05 '16

SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---

--- Day 5: How About a Nice Game of Chess? ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


STAYING ON TARGET IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

14 Upvotes

188 comments sorted by

2

u/daggerdragon Dec 05 '16

I was really hoping #5 would be a redditor :(

10

u/kaveman909 Dec 05 '16

I addressed the optional portion of the challenge: "Be extra proud of your solution if it uses a cinematic "decrypting" animation." My animation is here

import hashlib
import random
index = 0
password = '________'
while 1:
    m = hashlib.md5()
    m.update(('ugkcyxxp'+str(index)).encode('utf-8'))
    hex_m = m.hexdigest()
    if hex_m[0:5] == '00000':
        password_pos = int(hex_m[5], 16)
        if password_pos < 8:
            password_dig = int(hex_m[6], 16)
            if password[password_pos] == '_':
                password = password[:password_pos] + hex(password_dig)[-1] + password[password_pos + 1:]
    if index % 30000 == 0:
        for char in password:
            if char == '_':
                print(str(random.random())[-1], end='')
            else:
                print(char, end='')
        print('\r', end='')
    index += 1

5

u/Noxime Dec 05 '16

I edited yours to have some cool colors that show what has been solved

→ More replies (1)

3

u/[deleted] Dec 05 '16

Hehe, I kind of did the same as you:

import md5
import sys

door_id = "ffykfhsq"
#door_id = "abc"
running_num = 0
passbuild = {}
password = ['-', '-', '-', '-', '-', '-', '-', '-' ]

while len(passbuild) < 8:
    test = door_id + str(running_num)
    hashed = md5.new(test).hexdigest()
    if running_num % 1000 == 0:
        sys.stdout.write("password: {} cracking hash: {} \r".format("".join(password),hashed))
        sys.stdout.flush()
    if hashed.startswith("00000"):
        place = hashed[5]
        char = hashed[6]
        if place.isdigit() and int(place) > -1 and int(place) < 8:
            if int(place) not in passbuild:
                passbuild[int(place)] = char
                password[int(place)] = char
    running_num += 1

sys.stdout.write("password: {} \r".format("".join(password)))
sys.stdout.flush()
print("password: {} \r".format("".join(password)))

Looks really fancy in the console :D

7

u/Godspiral Dec 05 '16

didn't like this. Very easy, but speed of machine/implementation/language dependent.

in J, did tacit and explicit version to compare speed. J805 has nice speedups of explicit code. Both same speed.

pD =: 1!:2&2 :(] [ 1!:2&2@:(,&<))
f5 =: 3 : 0
'c i' =.y
while. c > 0 do.
 t =. 'md5' gethash 'uqwqemis' ,": i
 if. '00000' -: 5 {. t do.
  c =. <: c
  pD t
 end.
 i =. >: i
end.
)

tacit version, (hard coded input, params are number of chars and starting index to allow resuming). console prints, and returns last index checked.

 (] ({.@[ , >:@{:@[)`(<:@{.@[ , >:@{:@[ [ pD@])@.('00000' -: 5 {. ]) 'md5' gethash 'uqwqemis' , ":@{:)^:(0 < {.)(^:_) 8 0

5

u/topaz2078 (AoC creator) Dec 05 '16

I have pretty old test hardware that had to finish each input for this puzzle in similar runtimes and at most 30sec on a single core, so I hope the speed didn't have a large impact for any reasonable implementation.

→ More replies (10)

7

u/askalski Dec 05 '16

Could this be... a "rehash" of last year's AdventCoins puzzle?

This solution in C increments the hash input using the same method as my Synacor Challenge implementation of AdventCoins last year. The never-before-seen source code is now available for the low, low price of only two AdventCoins (which you can generate yourself by running the code. Runtime environment not included.)

#include <stdio.h>
#include <stdint.h>
#include <openssl/md5.h>

#define INPUT "abc"
#define ALOT 16

int main(void)
{
    uint8_t digest[MD5_DIGEST_LENGTH], data[sizeof(INPUT) + ALOT] = INPUT;
    uint8_t *end = data + sizeof(INPUT), *count = end - 1;
    uint32_t part1 = 0, part2 = 0, part1_shift = 32, part2_mask = 0xff;
    *count = '0';
    for (;;) {
        MD5(data, end - data, digest);
        uint8_t skip = digest[0] | digest[1] | (digest[2] & 0xf0);
        if (!skip) {
            uint32_t six   = digest[2] & 0xf;
            uint32_t seven = digest[3] >> 4;
            if (part1_shift) {
                part1 |= six << (part1_shift -= 4);
                if (!part1_shift) {
                    printf("Part 1: %08x\n", part1);
                }
            }
            if (part2_mask & (1 << six)) {
                part2_mask ^= 1 << six;
                part2 |= seven << ((7 - six) << 2);
                if (!part2_mask) {
                    printf("Part 2: %08x\n", part2);
                    break;
                }
            }
        }
        for (uint8_t *p = end - 1; ; *p-- = '0') {
            if (*p != '9') {
                (*p)++;
                break;
            }
            if (p == count) {
                *p = '1';
                *end++ = '0';
                break;
            }
        }
    }

    return 0;
}

2

u/VideoPrincess Dec 05 '16

Skalski YES! As a fellow Synacor hacker it's great to see your assembly for the MD5 implementation. But the thing that blows me away is that you wrote a right-shifter only using registers!

Can you explain how the right-shifter routine works? It seems so small and a bit magic - it NOTs the number of bits to shift by and then adds 16, so I'm confused as to what it's actually doing.

Also I love how you've used Perl to implement the assembler. I don't know Perl so I did my assembler in C++ by using chunks of code from my VM implementation. My assembler instantiates a VM and then uses its program counter to step through it writing memory as it goes. I then dump the finished VM to a file descriptor. However it seems like a sledgehammer to crack a nut next to your elegant Perl implementation!

4

u/askalski Dec 05 '16 edited Dec 05 '16

It's subtracting 15 - shift_bits using addition and 2's complement (-n == ~n + 1), so 15 - shift_bits == 15 + (~shift_bits + 1) == 16 + ~shift_bits. This result is the number of bits to copy from input to output.

Here's a Perl version (and example output) that prints the intermediate values each iteration of the loop.

→ More replies (1)

6

u/BafTac Dec 05 '16 edited Dec 05 '16

Cinematic decryption animation of part 2, in C++

Edit: Video

Luckily, I could copy part 4 from last year. Took me a while though, so I ended up just at rank 118. Could have been a lot faster for part 2 but I messed up the indexes (used index 6 and 7 instead of 5 and 6), and didn't catch it for like 20 minutes. :(

2

u/VideoPrincess Dec 05 '16

Yay C++! Here are my solutions in C++ for Part 1 and Part 2 using no external libraries. I copied an MD5 implementation from some public-domain code several years back so I've just linked to that: MD5.h and MD5.cpp if you're interested.

→ More replies (2)

6

u/jwstone Dec 05 '16

10

u/topaz2078 (AoC creator) Dec 05 '16

Have you at least asked your doctor whether this behavior is safe to continue?

2

u/jwstone Dec 05 '16

Idk, what can be done for a pre-existing condition?

4

u/topaz2078 (AoC creator) Dec 05 '16

Condition? I think you use an index to optimize it.

→ More replies (2)

3

u/sowpods Dec 05 '16 edited Dec 05 '16

Late to the party, but more postgres:

I really like the with recursive implementation you used, ay more efficient than just making an absurd number of md5s.

select string_agg(val, '')
from(
select pos
	,first(val) as val
from(
select substring(val from 6 for 1) as pos
	, substring(val from 7 for 1) val
	, generate_series
from(
select md5('ojvtpuvg'||generate_series) as val
	, generate_series
from generate_series(1,90000000)
)a
where val ilike '00000%'
and (case when substring(val from 6 for 1) ~'\d+' then substring(val from 6 for 1)::int else 9 end) <8
order by 1, 3
)b
group by 1
order by 1
)c
→ More replies (1)

5

u/Sir_Mr_Bman Dec 05 '16 edited Dec 05 '16

WOW you guys are stupid fast.

My code is still running in Java for star 1

EDIT: It's done! Run time for part one was just over 25 minutes (I messed up on the first run through, oops.) Part 2 is running right now. ninja: 557 for part one

EDIT 2: I'm dumb I had my program printing stuff... slowing it down. Oh well. Part two is nearly done.

EDIT 3 (final): rank 732 for part 2! This is the first time I've stayed up for it all... so pretty happy to have a rank above 1000!

3

u/jwoLondon Dec 05 '16

That seems like quite a long runtime. Are you using Java's MessageDigest to compute the hash? When I run it, it takes about 7 seconds to compute both Part A and B:

int n=0;
byte[] result;
boolean hasZeros = false;

try {
    MessageDigest mDigest = MessageDigest.getInstance("MD5");
    
    int digitsFound = 0;
    int partAPos = 0;
    char[] pwdPartA = new char[8];
    char[] pwdPartB = "xxxxxxxx".toCharArray();
        
    while (digitsFound < 8) {
        // Find next five zero hash
        do {
            mDigest.update((input+n).getBytes());
            result = mDigest.digest(); 
            hasZeros = startsWith5Zeros(result);
            n++;
        }
        while (!hasZeros);

        // (Part a): Password digits are in sequence.
        if (partAPos < 8){ 
            pwdPartA[partAPos++] = toHex(result).charAt(5);
        }
        
        // (Part b) Find the first instance of positions between 0-7
        int partBPos = Character.getNumericValue(toHex(result).charAt(5));
        if ((partBPos <8) && (pwdPartB[partBPos] == 'x')) {
            pwdPartB[partBPos] = toHex(result).charAt(6);
            digitsFound++;
        }
    }
    println("Part A password: "+new String(pwdPartA).toLowerCase());
    println("Part B password: "+new String(pwdPartB).toLowerCase());
}
catch (NoSuchAlgorithmException e) {
    System.err.println("Problem creating MD5: "+e);
}

static boolean startsWith5Zeros(byte[] bytes) {
    // To be padded with 5 zeros in hex, the first two bytes must both be zero
    if ((bytes[0] != 0) || (bytes[1] != 0) ) {
        return false;
    }
    String hex = toHex(bytes);
    if (hex.charAt(4) == '0') return true;
    return false;
}

static String toHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length*2];
	for (int i=0; i<bytes.length; i++) {
        int v = bytes[i] & 0xFF;
        hexChars[i*2]     = HEX_ARRAY[v >>> 4];
        hexChars[i*2 + 1] = HEX_ARRAY[v & 0x0F];
    }
    return new String(hexChars);
}
→ More replies (1)

5

u/aceshades Dec 05 '16

Holy crap, I wasted so much time just trying to understand what an MD5 hash was. Were we expected to write the code to hash it ourselves? I just ended up looking for a Python md5 library, and there was one.

#!/usr/bin/python3

import hashlib

def find_code(door_id):
    s = [None] * 8
    i = 0
    while None in s:
        m = hashlib.md5(door_id + str(i).encode('utf-8')).hexdigest()
        if m.startswith('00000'):
            print("{}: {}".format(door_id + str(i).encode('utf-8'), m))
            location = int(m[5], 16)
            if location < 8 and s[location] is None:
                s[location] = m[6]
        i += 1
    return ''.join(s)

sample = 'abc'.encode('utf-8')
print('Sample: {}'.format(find_code(sample)))

print('\n')

door_id = 'wtnhxymk'.encode('utf-8')
print('Challenge: {}'.format(find_code(door_id)))    

13

u/topaz2078 (AoC creator) Dec 05 '16

You're expected to produce the correct answer. How you get there is up to you!

5

u/aceshades Dec 05 '16

Ha, awesome. Sounds like I did the right thing then. As a plus learned a new thing today and also felt like a total l33t haxx0r while doing it :)

3

u/Kwpolska Dec 05 '16

Nobody is expected to write their own MD5 implementation. (The first rule of crypto: never write your own crypto.)

4

u/sblom Dec 05 '16

I realized while debugging my build-the-combination feature on part 2 that I should have just sent a process ahead to compute hashes beginning with 00000 that I could use to instantly re-run the rest of the code. I wasted a ton of time tonight re-calculating the same hashes over and over and over.

6

u/PangolinRex Dec 05 '16

If it makes you feel better, I didn't catch the fact that only the first valid code for each spot should be used (assumed you kept swapping until it was complete), panicked, and stopped running the script with only one spot missing ;_;

Could've easily waited another couple seconds and just figured out the result directly from the hashes...

→ More replies (1)

2

u/bildzeitung Dec 05 '16

That's a really good point.

4

u/Hwestaa Dec 05 '16

Python 3 solution, cleaned up and merged both to one function. Github

I ran part 2 on CPython2, CPython 3, Pypy2 and Pypy3 to see which was fastest. Pypy(2) is the winner!

  • CPython2: 93 seconds
  • CPython3: 72 seconds
  • Pypy2: 57 seconds
  • Pypy3: 68 seconds
→ More replies (6)

3

u/gerikson Dec 05 '16

I just want to say that the example given was well written and clear (they usually are, just this one stood out a bit more!) I had zero issues working with the example to check my implementation, then just plug in the real input to get the answer.

2

u/balidani Dec 05 '16

Got #7 and #11 today, not bad. My Python solution, which I cleaned up a little:

from hashlib import md5

salt = "ffykfhsq"
password = {}

i = 0
while len(password) < 8:
    digest = md5(salt + str(i)).hexdigest()
    if digest.startswith("00000"):
        pos = int(digest[5], 16)
        val = digest[6]

        if pos in range(8) and pos not in password:
            password[pos] = val

            pass_str = ['_'] * 8
            for key, val in password.items():
                pass_str[key] = val
            print ''.join(pass_str)

    i += 1

2

u/Unknownloner Dec 05 '16

Wow, I guess python just insta-wins on this one. Thanks libraries.

→ More replies (1)

1

u/Joe_Shlabotnik Dec 05 '16

Why the salt?

3

u/balidani Dec 05 '16

In password hashing they call randomly generated tokens that you can prepend or append to a password a salt. Although this is not password hashing the name feels fitting.

→ More replies (1)

2

u/andars_ Dec 05 '16

That is just his input string.

→ More replies (3)

2

u/skynet_creator Dec 05 '16

ughhhhhh i was checking for valid positions for part 2 and was checking that pos > 0 so it got 7 chars and was running forever before i killed it and investigated....

2

u/bluewave41 Dec 05 '16 edited Dec 05 '16

Thanks for making me run though like 25 million hashes just to find index 3. I was sure I had messed up somewhere but didn't.

public static void main(String[] args) throws FileNotFoundException {
    char[] b = new char[8];
    String data = "abbhdwsy";
    
    for(int a=0;a<=30000000;a++) {
        String n = data + a;
        String hash = md5(n);
        boolean good = true;
        for(int i=0;i<5;i++) { //replace with substring lul I was rushing
            if(hash.charAt(i) != '0') {
                good = false;
                break;
            }
        }
        if(good) {
            System.out.println(hash);
        }
    }
    for(char c : b)
        System.out.print(c);
}

public static String md5( String input ) {
try {
    java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
    byte[] array = md.digest(input.getBytes( "UTF-8" ));
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
        sb.append( String.format( "%02x", array[i]));
    }
    return sb.toString();
} catch (Exception e) {
    return null;            
}

Read the hashes in console and did indexing manually. Easier than writing code to do.

→ More replies (1)

2

u/MarkRebuck Dec 05 '16

This one was almost identical to day 4 of last year.

Still took me a while to do part one (67th place), and silly mistake on part two slipped me to 112th. I'm getting slow in my old age!

→ More replies (1)

2

u/1wheel Dec 05 '16

104/52 with JS:

var crypto = require('crypto');

var input = 'uqwqemis'
var i = 0

var out = ['','','','','','','','']

while (out.join('').length < 8){
  var next = crypto.createHash('md5').update(input + i).digest('hex')
  if (next.slice(0, 5) == '00000'){
    var pos = next[5]
    if (out[pos] == '') out[pos] = next[6]
  }
  i++

  if (i % 100000 == 0) console.log(i, out, next)
}

console.log(out.join(''))

process.exit()

hacking gif

→ More replies (2)

2

u/Deckard666 Dec 05 '16 edited Dec 08 '16

In Rust, parts 1 and 2: Link

Not very familiar with hashing in Rust, I ended up doing it with the first library I could get my hands on. The problem is the output was a slice of u8's, so I ended up doing some awkward bit operations trying to get the answer >.<

→ More replies (2)

2

u/tdecker91 Dec 05 '16

Kind of sloppy, but a working solution to part 2 in golang

    package main

    import (
        "crypto/md5"
        "fmt"
        "strconv"
    )

    func main() {

        input := []byte("ugkcyxxp")
        counter := 0

        part2 := "        "

        found := 0
        for found < 8 {
            hashInput := append(input, []byte(strconv.Itoa(counter))...)
            counter = counter + 1
            output := fmt.Sprintf("%x", md5.Sum(hashInput))

            if output[:5] == "00000" {
                index, err := strconv.Atoi(string(output[5]))
                if err != nil || index > 7 || string(part2[index]) != " " {
                    continue
                }

                newPassword := []rune(part2)
                newPassword[index] = rune(output[6])
                part2 = string(newPassword)
                found = found + 1

            }

        }

        fmt.Println(part2)

    }

2

u/FuriousProgrammer Dec 05 '16 edited Dec 05 '16

I really need to keep a fast Lua md5 library on hand!

235th for the first star, still calculating the second.... (Ignore the first value there, it's wrong. :P)

EDIT: 517!

EDIT2: So it turns out that luajit comes with a very fast md5 library, which quite possibly could have given me a spot on the leaderboard had I known about it. Gonna go cry a bit, then read a book before I go to bed.

EDIT3: Anyway here's wonderwall my code:

local input = "ugkcyxxp"
local md5 = require("md5")
local glue = require("glue")
--ran using LuaJIT

out1 = ""
out2 = {'_', '_', '_', '_', '_', '_', '_', '_'}

local i = 0
while true do
	local s = glue.tohex(md5.sum(input .. i))
	if s:sub(1,5) == "00000" then
		print("VALID STRING: " .. input .. i)
		print("Hash: " .. s)
		if #out1 < 8 then
			out1 = out1 .. s:sub(6,6)
		end
		local pos = tonumber(s:sub(6,6))
		local let = s:sub(7,7)
		if pos and pos >= 0 and pos <= 7 and out2[pos + 1] == '_' then
			out2[pos + 1] = let
		end
		print("current outputs: " .. out1 .. ("_"):rep(8 - #out1) .. " " .. table.concat(out2))
	end
	i = i + 1
	if not table.concat(out2):find("_") then break end
end

print("Part 1: " .. out1)
print("Part 2: " .. table.concat(out2))

2

u/bildzeitung Dec 05 '16

It's almost a thing where you'd want to write a quick FFI thing with LuaJIT to bring it across, given the calc time.

2

u/bildzeitung Dec 05 '16

I'm tempted. I've already got Python and C versions posted...

2

u/FuriousProgrammer Dec 05 '16

Don't even need to go that far: LuaJIT comes with an md5 library that is wicked fast compared to the one I found, I just didn't know about it!

→ More replies (3)

2

u/oantolin Dec 05 '16 edited Dec 05 '16

Are you sure LuaJIT comes with an MD5 library? You're sure you didn't install it a while ago and just forgot? My LuaJIT says "module 'md5' not found" if I try require "md5".

EDIT: You're probably using luapower.com's md5 and glue libraries (the name "glue" is what gave it away).

→ More replies (1)

2

u/Pyrobolser Dec 05 '16

C# solution with the mandatory animated password for part two (still too slow for an Hollywood movie)

2

u/advanced_caveman Dec 05 '16

Rust Code for Part 2:

extern crate md5;
extern crate rustc_serialize;

use rustc_serialize::hex::ToHex;

fn getPassword(input: String) -> String {
    let mut password = String::new();
    let mut passwordPos: Vec<(char, u8)> = Vec::new();

    let mut currentChar = 1;
    let mut currentIndex = 0;

    while currentChar < 9 {
        let passwordChar = isInteresting(&input, currentIndex, 1, &passwordPos);
        if let Some(passChar) = passwordChar {
            passwordPos.push(passChar);
            currentChar += 1;
            println!("{:?}", currentIndex);
        }
        currentIndex += 1;
    }

    for i in 0..8 {
        let pChar = passwordPos
            .clone()
            .iter()
            .filter(|x| x.1 == i)
            .map(|x| x.0)
            .nth(0)
            .unwrap();
        password.push(pChar);
    }

    password
}

fn isInteresting(input: &String, num: u64, charIndex: usize, currentPos: &Vec<(char, u8)>) -> Option<(char, u8)> {
    let mut indexInput = input.clone();
    let numString = num.to_string();
    indexInput.push_str(numString.as_str());

    let hash: md5::Digest = md5::compute(indexInput.into_bytes().as_slice());

    let hex = hash.to_hex();
    let hexString = hex.as_bytes();

    let found = currentPos
        .clone()
        .iter()
        .filter(|x| x.1 == hexString[5] - 48)
        .count();

    if (hexString[0] == 48 && hexString[1] == 48 && hexString[2] == 48 && hexString[3] == 48 && hexString[4] == 48 && hexString[5] < 56 && found == 0) {
        Some((hexString[6] as char, hexString[5] - 48))
    } else {
        None
    }
}

fn main() {
    let testInput = "abc";
    let input = "ojvtpuvg";

    println!("{:?}", getPassword(String::from(input)));
}

Runs in 18.5 seconds

2

u/studiosi Dec 05 '16

My Python solutions. IMHO, this day was easier than some of the days before, but still pretty cool. :D

https://github.com/studiosi/AoC2016/tree/master/5

2

u/grayrest Dec 05 '16

Rust Part 2 using rust-crypto:

use crypto::md5::Md5;
use crypto::digest::Digest;
use std::str;

fn fmt_hex(x: u8) -> u8 {
    if x > 9 {
        b'a' + x - 10
    } else {
        b'0' + x
    }
}

pub fn ans() {
    let mut hasher = Md5::new();

    let mut passwd = *b"________";
    for i in 0.. {
        hasher.input_str("cxdnnyjw");
        hasher.input_str(&i.to_string());
        let mut digest = [0; 16];
        hasher.result(&mut digest);

        if digest[0] == 0 && digest[1] == 0 && digest[2] >> 4 == 0 {
            let idx = digest[2] as usize;
            if idx < passwd.len() && passwd[idx] == b'_' {
                passwd[idx] = fmt_hex(digest[3] >> 4);
                if passwd.iter().all(|&x| x != b'_') {
                    println!("{} iters: {}", i, str::from_utf8(&passwd).unwrap());
                    break
                }
            }
        }
        hasher.reset();
    }
}

2012 Macbook Air (1.8GHz i5):

 25370046 iters: 999828ec
 cargo run --release  7.67s user 0.06s system 99% cpu 7.742 total

2

u/gyorokpeter Dec 05 '16

Q: (some code reuse from last year, but a bit optimized. "Animation" included for part 2)

d5p1:{    
	gh:{[str;nf]
		if[8<=count last nf;:nf];
		step:100000;
		n:first nf;
		hs:md5 each str,/:string n+til step;
		n2:where{(x[;0]=0)&(x[;1]=0)&(x[;2]<16)}hs;
		(n+step;last[nf],hs n2)
		}[x]/[(0;())];
	(raze each string[8#gh 1])[;5]
	}
d5p2:{    
	res:{[str;nf]
		if[not"_"in last nf;:nf];
		step:100000;
		n:first nf;
		hs:md5 each str,/:string n+til step;
		n2:where{(x[;0]=0)&(x[;1]=0)&(x[;2]<16)}hs;
		pw:{p:y[2]mod 16;if[p within 0 7;if["_"=x p;x[p]:raze[string y][6]]];x}/[last nf;hs n2];
		(n+step;0N!pw)
		}[x]/[(0;"________")];
	last res}

2

u/Ulyssesp Dec 05 '16

Haskell, using cryptohash and base16-bytestring because I don't know haskell libraries. This one was a great reason to learn about scans and folds too late in the night.

findCode :: BC.ByteString -> String
findCode input = concat . take 1 . dropWhile (elem '-') $ scanl (calcHash input) "--------" num0

calcHash :: BC.ByteString -> String -> Int -> String
calcHash input out i = if (BC.take 5 hashed) == "00000" && (checkFilled . digitToInt) (BC.index hashed 5) then out & ix (digitToInt $ BC.index hashed 5) .~ (BC.index hashed 6) else out
  where
    hashed = encode . hash . BC.append input . BC.pack .show $ i
    checkFilled pos = (pos < length out) && (out !! pos == '-')

num0 = 0 : map (+1) num0

run = print $ findCode input

1

u/pyow_pyow Dec 05 '16

^ that's quite a bit more concise than what I had - http://lpaste.net/3784856620119359488 (Haskell as well)

2

u/KoxAlen Dec 05 '16 edited Dec 22 '16

Kotlin solution, using a sequence for the hashes

https://github.com/KoxAlen/AdventOfCode2016/blob/master/src/main/kotlin/aoc/day5/Day5.kt

package aoc.day5

import aoc.utils.toHex
import java.security.MessageDigest

fun main(args: Array<String>) {
    val input = "cxdnnyjw" //Input

    val md5 = MessageDigest.getInstance("MD5")
    val hashSequence = generateSequence(0, Int::inc).map { md5.digest("$input$it".toByteArray()).toHex() }

    val p1 = hashSequence.filter { it.startsWith("00000") }.take(8).map { it[5] }.joinToString(separator = "")
    println("[Part 1] The code is $p1")

    val p2 = hashSequence.filter { it.startsWith("00000") }.filter { it[5] >= '0' && it[5] < '8' }.distinctBy { it[5] }.take(8)
            .fold(kotlin.arrayOfNulls<Char>(8)) {
                acc, it ->
                acc[it[5]-'0'] = it[6]
                acc
            }.joinToString(separator = "")
    println("[Part 2] The code is $p2")
}

edit: Removed sequence class as per u/mnjmn suggestion

2

u/mnjmn Dec 05 '16 edited Dec 05 '16

You could replace that class with generateSequence(0, Int::inc).map { "$salt$it".toByteArray() }.map { md5.digest(it).toHex() }

→ More replies (2)

2

u/drakehutner Dec 05 '16 edited Dec 05 '16

(To print) || !(to print), that is the question:

Here's my anwser:

Time     Hashes / sec   Index   Current Hash                     Part 2   Part 1
[  4151s|   6039H/s]   25067104 000006274ce1d3978e2396f6456c4adc 863dde27 f97c354d
[    76s| 331041H/s]   25067104 000006274ce1d3978e2396f6456c4adc 863dde27 f97c354d

Cobbled together in Python. Maybe I try to transform it into a single-line-of-code solution

import hashlib
import time

idx = 0
key_1, key_2 = "", "________"
start = time.time()
while "_" in key_2:
    m = hashlib.md5()
    m.update("{}{}".format("reyedfim", idx).encode("utf-8"))
    h = m.hexdigest()
    if h.startswith('00000'):
        key_1 += h[5]
        p = int(h[5], 16)
        if p < 8 and key_2[p] == "_":
            key_2 = key_2[:p] + h[6] + key_2[p+1:]
        now = time.time() - start
        print("[{:>7.0f}s|{:7.0f}H/s] {:10} {:32} {:8} {:8}".format(now, idx/now, idx, h, key_2, key_1[:8]))
    idx += 1

2

u/drakehutner Dec 05 '16

Well, here is the single line of code doing the same. Possible because of lambda-based class methods implementing a generator (in other words: pure madness)

It even includes the mandatory hollywood style decryption

The code is split over multiple lines for better readability.

import functools, hashlib, sys
print(*((lambda salt, state={"ready": False}: (
    (lambda hasher: (
        functools.reduce(
            (lambda keys, hash: (
                # Closure to provide a converted position
                (lambda p: (
                    # Closure for the two keys, will modify the global state
                    # to interrupt the generator
                    (lambda key1, key2: ((
                        (key1[:8], key2),
                        sys.stdout.write("{:32} {:8} {:8}\n".format(hash, key2, key1[:8])),
                        sys.stdout.flush(),
                        state.update(ready="_" not in key2)
                    )[0]))(
                        # Part 1: Just append the value
                        keys[0] + hash[5],
                        # Part 2: Insert the value in the right position,
                        #         if said position is empty
                        keys[1][:p] + hash[6] + keys[1][p+1:]
                        if p < 8 and keys[1][p] == "_" else keys[1]
                    )
                ))(int(hash[5], 16))
            )),
            (hash for idx, hash in hasher(salt, state) if hash.startswith("00000")),
            ("", "________"),
        )
    ))(
        # Constructing a custom generator class, that produces md5-hashes based
        # on a salt and an incrementing counter
        # The generation will stop if the externally supplied `state['ready']`
        # value is set to true.
        type("hash_generator", (object, ),
            {
                # Since the elements of a tuple are evaluated in order,
                # this can be used to execute multiple statements.
                # By indexing the tuple directly, the return value is controlled
                "__init__": lambda self, salt, state: (
                    (setattr(self, "state", state),
                    setattr(self, "salt", salt),
                    setattr(self, "hash", hashlib.md5()),
                    setattr(self, "n", 0),
                    None # __init__ has to return None
                    )[-1]
                ),
                "__iter__": lambda self: self,
                "__next__": (lambda self: (
                    setattr(self, "hash", hashlib.md5()),
                    self.hash.update("{}{}".format(self.salt, self.n).encode("utf8")),
                    # Uncomment the following lines for mandatory hollywood style
                    # This will slow the computation by at least factor 100
                    ## sys.stdout.write("{}\r".format(self.hash.hexdigest())),
                    ## sys.stdout.flush(),
                    setattr(self, "n", self.n + 1),
                    # Using an empty generator to raise an exception, since
                    # the 'raise' keyword is not allowed inside a lambda expression
                    (self.n, self.hash.hexdigest()) if not self.state['ready'] else (_ for _ in ()).throw(StopIteration())
                    )[-1])
            }
            )
    )
))("reyedfim")))

2

u/Kristler Dec 05 '16

I feel like I'm in an intense hacking movie now :D

I also learned that juggling characters, character arrays, byte arrays, and strings all simultaneously in Java is a massive pain.

2

u/[deleted] Dec 05 '16 edited Dec 05 '16

Perl, with a simple cinematic decryption animation.

https://www.dropbox.com/s/hgunceaaen7s2zy/day05.pl?dl=0

2

u/fkaaaa Dec 05 '16

Using C with OpenSSL. Struggled a bit to get the correct MD5 hash at first, but after that it was all smooth sailing (bonus h4ck1ng video!)

Part 2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(__APPLE__)
#  define COMMON_DIGEST_FOR_OPENSSL
#  include <CommonCrypto/CommonDigest.h>
#else
#  include <openssl/md5.h>
#endif

void md5(const char *str, int length, char out[static 8]) {
    unsigned char digest[MD5_DIGEST_LENGTH];

    MD5_CTX c;

    MD5_Init(&c);
    MD5_Update(&c, str, length);
    MD5_Final(digest, &c);

    snprintf(out, 8, "%02x%02x%02x%02x", digest[0], digest[1], digest[2], digest[3]);
}


int main(int argc, char *argv[]) {
    char hash[9] = {0};
    char pwd[9] = {0};
    char input[64] = "wtnhxymk";

    char filled = 0;
    int n = 0;

    while (filled != '\xff') {
        int sz = snprintf(&input[8], 56, "%d", n++);
        md5(input, 8 + sz, hash);

        if (!memcmp("00000", hash, 5)) {
            char pos = hash[5] - '0';
            if (pos >= 0 && pos <= 7 && !(filled & (1 << pos))) {
                filled |= (1 << pos);
                pwd[pos] = hash[6];
            }

        }

        for (int i = 0; i < 8 && (n % 50000 == 0); ++i) {
            if (filled & (1 << i)) {
                printf("\x1b[32m%c", pwd[i]);
            } else {
                printf("\x1b[31m%c", hash[i]);
            }
        }
        printf("\r");
    }

    printf("%s\n", pwd);

    return 0;
}

2

u/[deleted] Dec 05 '16

I love the colours :) The videos and gifs of this day really has made it one of the best ones for me, after I managed to find out how to do it myself, and then seeing all the more clever ones that others made.

1

u/bildzeitung Dec 05 '16

Gotta love stackexchange :) Nicely done on the colours.

2

u/GrassGiant Dec 05 '16

Here's my Clojure solution for the second problem :

(ns adventofcode2016.problems.day5b)
  (require 'digest)
  (use 'clojure.java.io)

(defn get-lines [fname]
    (with-open [r (reader fname)]
          (doall (line-seq r))))

(defn isValidPosition? [chr]
  (let [value (first (re-seq #"\d" chr))]
    (if (not (= value nil))
      (< (Integer. value) 8)
      false)))

(defn checkMD5 [answer startstr endint]
  (let [encrypted (re-seq #"\w" (digest/md5 (str startstr endint)))]
    (if (= (take 5 encrypted) (take 5 (repeat "0")))
      (if (isValidPosition? (nth encrypted 5))
        (let [answerList (into [] (re-seq #"[\w-]" answer))]
          (if (= (nth answerList (Integer. (nth encrypted 5))) "-")
            (let [newAnswerList (assoc answerList (Integer. (nth encrypted 5)) (nth encrypted 6))]
              (println (apply str newAnswerList))
              (apply str newAnswerList))
            answer))
        answer)
      answer)))

(defn decrypt [startstr]
  (println "--------")
  (loop [i 0 answer "--------"]
    (if (not (.contains answer "-"))
      answer
      (recur (+ i 1) (checkMD5 answer startstr i)))))

(defn showMeYaMoves [inputList]
  (decrypt (first inputList)))

(defn handleInput [filename]
  (let [lines (get-lines filename)]
    (showMeYaMoves lines)))

(defn solve
  "Get the door's password"
  [filename]
  (let [solution (handleInput filename)]
    (println (str "Your solution is " solution))
    solution))

It prints a pretty output when you run solve (I actually run a small app that calls solve, but it looks like this

2

u/tg-9000 Dec 05 '16 edited Dec 05 '16

My solution in Kotlin. I had fun with this, it's essentially just a generator with a filter on the end of it, which differs for each problem. This solution is simple if you spend a few minutes understanding what Kotlin provides in terms of stream operations.

Solutions to other days problems and unit tests can be found in my github repo. I'm just learning Kotlin so I welcome all feedback if you have any.

class Day05(private val doorId: String) {

    private val md5Digester = MessageDigest.getInstance("MD5")

    fun solvePart1(): String =
        generateHashes()
            .map { it[5] }
            .take(8)
            .joinToString(separator = "")

    fun solvePart2(): String =
        generateHashes()
            .map { Pair(it[5], it[6]) }
            .filter { it.first.isDigit() && it.first.asDigit() < 8 }
            .distinctBy { it.first }
            .take(8)
            .sortedBy { it.first }
            .map { it.second }
            .joinToString(separator = "")

    private fun md5(text: String): String =
        md5Digester.digest(text.toByteArray()).toHex()

    // Common sequence logic (increment, hash, filter)
    private fun generateHashes(): Sequence<String> =
        generateSequence(0, Int::inc)
            .map { md5("$doorId$it") }
            .filter { it.startsWith("00000") }
}

1

u/Quick_Question404 Dec 05 '16

For anyone familiar with OpenSSL or C, can you give me a hand with figuring out how it works? The documentation provided online is mediocre at best, and I can't seem to get my compiling right.

2

u/jcfs Dec 05 '16

Here is my solution for part 2 (got #59 on part 1, but messed up on the second - C is not good for leaderboards on this kind of problem :p) :

#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>

int main(int argc, char ** argv) {
  unsigned char c[MD5_DIGEST_LENGTH];
  MD5_CTX mdContext;
  char out[64], f[8] = {0};
  char * format = "ugkcyxxp%d\0";
  int i = 0, j = 0;

  while(1) {
    MD5_Init (&mdContext);
    snprintf(out, 64, format, j++);
    MD5_Update (&mdContext, out, strlen(out));
    MD5_Final(c, &mdContext);

    if (!c[0] && !c[1] && !(c[2] & 0xF0)) {
      if ((c[2]&0x0f) <= 7){
        if (f[c[2]&0x0f] == 0) {
          f[c[2]&0x0f] = ((c[3]&0xf0)>>4)+1;
          if (++i == 8)
            break;
        }
      }
    }
  }

  for(i = 0; i < 8; i++) {
    printf("%x", f[i]-1);
  }

  return 0;
}

You can check out my other solutions here: https://github.com/jcfs/aoc/tree/master/2016/

→ More replies (4)
→ More replies (1)

1

u/TheNiXXeD Dec 05 '16

My solution in JavaScript / Node. I threw in memoization instead of golfing this time to improve the repeat runtime. Wish I'd have done that on my first go-around, might have placed better on 2nd star.

https://github.com/NiXXeD/adventofcode/tree/master/2016/day5

1

u/futureman_pm Dec 05 '16

Is there anywhere to check your rank for a level after submission? I think I was just over 100 for both, but not sure.

Python:

import hashlib
import string

def gen_passwd(door_id):
    passwd = ''
    pass_d = {}
    indx = -1

    while len(pass_d) != 8:
        indx += 1
        hsh = hashlib.md5("%s%d" % (door_id, indx)).hexdigest()
        if hsh[0:5] == "00000":
            if hsh[5] not in string.digits:
                continue
            pos = int(hsh[5])
            if pos > 7 or pos < 0:
                continue
            if pos in pass_d:
                continue
            pass_d[pos] = hsh[6]

    for i in range(0, 8):
        passwd += pass_d[i]
    return passwd

1

u/Unknownloner Dec 05 '16 edited Dec 05 '16

Haskell, using the cryptonite library for MD5 hashes. It recomputes unnecessarily for part 2, but avoiding recomputation wouldn't have saved me that much time here (already had no chance of part 2 leaderboards by the time I finished part 1).

module Days.Day5
  ( day5
  ) where

import Days.Prelude
import Crypto.Hash
import qualified Data.ByteString as B
import qualified Data.Map.Strict as Map
import Data.Char
import Data.List
import Data.Ord

packASCII :: String -> B.ByteString
packASCII = B.pack . map (fromIntegral . fromEnum)

numbers :: B.ByteString -> [B.ByteString]
numbers prefix =
  [ prefix `B.append` packASCII (show i)
  | i <- [0 ..] ]

hashes :: B.ByteString -> [String]
hashes prefix = map show hs
  where
    hs :: [Digest MD5]
    hs = map hash (numbers prefix)

isCodePart :: String -> Bool
isCodePart xs = all (== '0') $ take 5 xs



part1 :: B.ByteString -> String
part1 = map (!! 5) . take 8 . filter isCodePart . hashes

part2 :: B.ByteString -> String
part2 =
  map snd .
  sortBy (comparing fst) .
  Map.toList .
  head .
  filter (\m -> Map.size m == 8) . scanl' f Map.empty . filter isCodePart . hashes
  where
    f m x
      | d && ne = Map.insert loc char m
      | otherwise = m
      where
        (loc:char:_) = drop 5 x
        d = isOctDigit loc
        ne = not (Map.member loc m)

day5 =
  Day
  { _parser = parser
  , _dayPart1 = part1
  , _dayPart2 = part2
  }
  where
    parser :: Parser B.ByteString
    parser = packASCII <$> some alphaNum

1

u/broadwall Dec 05 '16 edited Dec 05 '16

Solution: GitHub

A bunch of mistakes postponed my 2nd part. They include accidentally calling part 1's code (I should split the files next time) and overwriting certain positions of the password.

This program should display a nice hacking effect on ANSI terminals. For part 2, call this using ruby day5.rb part2

1

u/Tokebluff Dec 05 '16

My C# solution (#91 silver, #193 gold) Too bad I didn't realise that I should ignore hashes with invalid position for part 2 and just move on :( oh well

https://github.com/Tokebluff/AdventOfCode2016/blob/master/Advent%20of%20code%202016/Advent%20of%20code%202016/Day5.cs

1

u/WildCardJoker Dec 05 '16

Nice! I see we both used the same MD5 code.

My C# solution, part 1 and 2.

I switched out the function for a Nuget package, thinking there was something wrong with my calculations - it took nearly 3 minutes to calculate part 1. Nope, just a slow PC :) Currently sitting at over 8 minutes, waiting to complete part 2.

→ More replies (1)

1

u/XiiencE Dec 05 '16

Python3, cleaned up a bit. Missed leaderboard.

from hashlib import md5

def part1(input):
    index, password = 0, ''
    
    while(len(password) < 8):   
        hash = md5(str.encode(str(input) + str(index))).hexdigest()
        if (hash.startswith('00000')):
            password += str(hash[5])
        index += 1
        
    return password

def part2(input):
    index, password = 0, [None] * 8
    
    while(not all(password)):
        hash = md5(str.encode(str(input) + str(index))).hexdigest()
        if (hash.startswith('00000') and hash[5].isdigit() and int(hash[5]) < 8 and password[int(hash[5])] is None):
            password[int(hash[5])] = hash[6]
        index += 1
        
    return ''.join(password)

def day5(input):
    return part1(input), part2(input)
    
input = open('input.txt', 'r').read()

a = day5(input)
print(str.format('Part 1: {0}\nPart 2: {1}', a[0], a[1]))

1

u/_Le1_ Dec 05 '16 edited Dec 05 '16

My C# solution:

   static void Main(string[] args)
    {
        part1();
        part2();
        Console.ReadLine();
    }

    static void part1()
    {
        string door_id = "ojvtpuvg";
        //string door_id = "abc";
        string pass = ""; int cnt = 0;

        for (int i = 0; i < int.MaxValue; i++)
        {
            string hash = getMd5Hash(door_id + i.ToString());
            if (hash.Substring(0, 5) == "00000")
            {
                pass += hash[5];
                cnt++;
            }
            if (cnt == 8) break;
        }

        Console.WriteLine("Part1 password is: {0}", pass);            
    }

    static void part2()
    {
        string door_id = "ojvtpuvg";
                    
        char[] pass = new char[8] { '#', '#', '#', '#', '#', '#', '#', '#' };

        for (int i = 0; i < int.MaxValue; i++)
        {
            string hash = getMd5Hash(door_id + i.ToString());
            if (hash.Substring(0, 5) == "00000")
            {
                int pos = (int)char.GetNumericValue(hash[5]);

                if (pos < 8 && pos >= 0 && pass[pos].Equals('#'))
                {
                    pass[pos] = hash[6];
                    Console.WriteLine(new string(pass));
                }

                if (!pass.Contains('#'))
                    break;
            }       
        }
        
        Console.WriteLine("Part2 password is: {0}", new string(pass).ToLower());
    }

    static string getMd5Hash(string input)
    {
        MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hash = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("X2"));
        }

        return sb.ToString();
    }

1

u/Iain_M_Norman Dec 05 '16

Do you have to create the MD5 inside getMd5Hash ? Might be faster if you can get away with creating it once IIRC.

1

u/[deleted] Dec 05 '16 edited Dec 05 '16

md5.js

var md5cycle=function(x,k){var a=x[0],b=x[1],c=x[2],d=x[3];a=ff(a,b,c,d,k[0],7,-680876936);d=ff(d,a,b,c,k[1],12,-389564586);c=ff(c,d,a,b,k[2],17,606105819);b=ff(b,c,d,a,k[3],22,-1044525330);a=ff(a,b,c,d,k[4],7,-176418897);d=ff(d,a,b,c,k[5],12,1200080426);c=ff(c,d,a,b,k[6],17,-1473231341);b=ff(b,c,d,a,k[7],22,-45705983);a=ff(a,b,c,d,k[8],7,1770035416);d=ff(d,a,b,c,k[9],12,-1958414417);c=ff(c,d,a,b,k[10],17,-42063);b=ff(b,c,d,a,k[11],22,-1990404162);a=ff(a,b,c,d,k[12],7,1804603682);d=ff(d,a,b,c,k[13],12,-40341101);c=ff(c,d,a,b,k[14],17,-1502002290);b=ff(b,c,d,a,k[15],22,1236535329);a=gg(a,b,c,d,k[1],5,-165796510);d=gg(d,a,b,c,k[6],9,-1069501632);c=gg(c,d,a,b,k[11],14,643717713);b=gg(b,c,d,a,k[0],20,-373897302);a=gg(a,b,c,d,k[5],5,-701558691);d=gg(d,a,b,c,k[10],9,38016083);c=gg(c,d,a,b,k[15],14,-660478335);b=gg(b,c,d,a,k[4],20,-405537848);a=gg(a,b,c,d,k[9],5,568446438);d=gg(d,a,b,c,k[14],9,-1019803690);c=gg(c,d,a,b,k[3],14,-187363961);b=gg(b,c,d,a,k[8],20,1163531501);a=gg(a,b,c,d,k[13],5,-1444681467);d=gg(d,a,b,c,k[2],9,-51403784);c=gg(c,d,a,b,k[7],14,1735328473);b=gg(b,c,d,a,k[12],20,-1926607734);a=hh(a,b,c,d,k[5],4,-378558);d=hh(d,a,b,c,k[8],11,-2022574463);c=hh(c,d,a,b,k[11],16,1839030562);b=hh(b,c,d,a,k[14],23,-35309556);a=hh(a,b,c,d,k[1],4,-1530992060);d=hh(d,a,b,c,k[4],11,1272893353);c=hh(c,d,a,b,k[7],16,-155497632);b=hh(b,c,d,a,k[10],23,-1094730640);a=hh(a,b,c,d,k[13],4,681279174);d=hh(d,a,b,c,k[0],11,-358537222);c=hh(c,d,a,b,k[3],16,-722521979);b=hh(b,c,d,a,k[6],23,76029189);a=hh(a,b,c,d,k[9],4,-640364487);d=hh(d,a,b,c,k[12],11,-421815835);c=hh(c,d,a,b,k[15],16,530742520);b=hh(b,c,d,a,k[2],23,-995338651);a=ii(a,b,c,d,k[0],6,-198630844);d=ii(d,a,b,c,k[7],10,1126891415);c=ii(c,d,a,b,k[14],15,-1416354905);b=ii(b,c,d,a,k[5],21,-57434055);a=ii(a,b,c,d,k[12],6,1700485571);d=ii(d,a,b,c,k[3],10,-1894986606);c=ii(c,d,a,b,k[10],15,-1051523);b=ii(b,c,d,a,k[1],21,-2054922799);a=ii(a,b,c,d,k[8],6,1873313359);d=ii(d,a,b,c,k[15],10,-30611744);c=ii(c,d,a,b,k[6],15,-1560198380);b=ii(b,c,d,a,k[13],21,1309151649);a=ii(a,b,c,d,k[4],6,-145523070);d=ii(d,a,b,c,k[11],10,-1120210379);c=ii(c,d,a,b,k[2],15,718787259);b=ii(b,c,d,a,k[9],21,-343485551);x[0]=add32(a,x[0]);x[1]=add32(b,x[1]);x[2]=add32(c,x[2]);x[3]=add32(d,x[3])};
var cmn=function(n,c,r,t,u,d){return c=add32(add32(c,n),add32(t,d)),add32(c<<u|c>>>32-u,r)};
var ff=function(n,c,r,t,u,d,f){return cmn(c&r|~c&t,n,c,u,d,f)};
var gg=function(n,c,r,t,u,d,f){return cmn(c&t|r&~t,n,c,u,d,f)};
var hh=function(n,c,r,t,u,d,f){return cmn(c^r^t,n,c,u,d,f)};
var ii=function(n,c,r,t,u,d,f){return cmn(r^(c|~t),n,c,u,d,f)};
var md51=function(r){var txt="";var t,c=r.length,e=[1732584193,-271733879,-1732584194,271733878];for(t=64;t<=r.length;t+=64)md5cycle(e,md5blk(r.substring(t-64,t)));r=r.substring(t-64);var n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(t=0;t<r.length;t++)n[t>>2]|=r.charCodeAt(t)<<(t%4<<3);if(n[t>>2]|=128<<(t%4<<3),t>55)for(md5cycle(e,n),t=0;16>t;t++)n[t]=0;return n[14]=8*c,md5cycle(e,n),e};
var md5blk=function(r){var o,t=[];for(o=0;64>o;o+=4)t[o>>2]=r.charCodeAt(o)+(r.charCodeAt(o+1)<<8)+(r.charCodeAt(o+2)<<16)+(r.charCodeAt(o+3)<<24);return t};
var hex_chr = "0123456789abcdef".split("");
var rhex=function(r){for(var h="",e=0;4>e;e++)h+=hex_chr[r>>8*e+4&15]+hex_chr[r>>8*e&15];return h};
var hex=function (n){for(var r=0;r<n.length;r++)n[r]=rhex(n[r]);return n.join("")};
var md5=function(s){return hex(md51(s));};
var add32=function(n,d){return n+d&4294967295};
if (md5("hello")!=="5d41402abc4b2a76b9719d911017c592") {add32=function(n,r){var a=(65535&n)+(65535&r),d=(n>>16)+(r>>16)+(a>>16);return d<<16|65535&a};}

puzzle input

var prefix="cxdnnyjw";

part1

var i=0,passwd="";while(true){var hash=md5(prefix+i);if(hash.slice(0,5)==="00000"){passwd+=hash.slice(5,6);console.log(prefix+i,md5(prefix+i),passwd);if(passwd.length>=8){break}}i++}

part2

var i=0,passwd=Array(8).fill(null),pi=0;while(true){var hash=md5(prefix+i);if(hash.slice(0,5)==="00000"){var pos=hash.slice(5,6);var c=hash.slice(6,7);if(0<=parseInt(pos)&&parseInt(pos)<=7){pos=parseInt(pos);if(passwd[pos]===null){passwd[pos]=c;pi++;console.log(pi,passwd.join(""));if(pi===8){break}}}}i++}

1

u/[deleted] Dec 05 '16

Haskell:

Decided to play around w/ a State Monad to keep track of which numbers had been checked. Wish the crypto library computed md5 faster, part 2 takes a full 15s.

import Control.Monad (replicateM)
import Control.Monad.Trans.State (State, evalState, state)
import Crypto.Hash.MD5 (hash)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.ByteString.Base16 (encode)
import Data.Monoid ((<>))
import qualified Data.HashMap.Strict as M


findHashPrefixedByFiveZeros :: ByteString -> State Int ByteString
findHashPrefixedByFiveZeros seed =
    state $ \start -> head [ (checksum, num + 1) | num <- [start..]
                           , let checksum = encode . hash . (seed <>) . B.pack $ show num
                           , fiveZeros `B.isPrefixOf` checksum]
        where fiveZeros = B.pack "00000"

part1 :: String -> String
part1 s = B.unpack $ evalState (B.pack . map (B.head . B.drop 5) <$> replicateM 8 nextHash) 0
    where nextHash = findHashPrefixedByFiveZeros $ B.pack s

part2 :: String -> String
part2 s = B.unpack $ evalState (findPassword M.empty) 0
    where nextHash = findHashPrefixedByFiveZeros $ B.pack s
          indices = "01234567"
          addChar m pos char
              | pos `elem` indices && not (M.member pos m) = M.insert pos char m
              | otherwise                                  = m
          findPassword m
              | M.size m == 8 = return . B.pack $ map (m M.!) indices
              | otherwise = do
                  (p : c : []) <- B.unpack . B.take 2 . B.drop 5 <$> nextHash
                  findPassword $ addChar m p c

main = do
  putStrLn $ part1 "ugkcyxxp"
  putStrLn $ part2 "ugkcyxxp"

1

u/stuque Dec 05 '16

A Python 2 solution:

import hashlib

def part1():
    door_id = 'ojvtpuvg'
    password = 8 * ['-']
    count = 0
    n = 0
    while count < 8:
        md5 = hashlib.md5()
        md5.update(door_id + str(n))
        h = md5.hexdigest()
        if h.startswith('00000'):
            password[count] = h[5]
            count += 1
        n += 1
    print ''.join(password)

def part2():
    door_id = 'ojvtpuvg'
    password = 8 * ['-']
    count = 0
    n = 0
    while count < 8:
        md5 = hashlib.md5()
        md5.update(door_id + str(n))
        h = md5.hexdigest()
        if h.startswith('00000'):
            pos = h[5]
            if '0' <= pos <= '7': 
                c = h[6]
                i = int(pos)
                if password[i] == '-':
                    password[i] = c
                    count += 1
        n += 1
    print password

if __name__ == '__main__':
    part1()
    part2()

1

u/DeathWalrus Dec 05 '16

quick python implementation (code for part 2), got 5 and 7 for today's challenge!

import hashlib
i = 1
found = False
validpos = "01234567"
password = [' ' for i in xrange(0,8)]
count = 0
while not found:
    string = "cxdnnyjw" + str(i)
    i += 1
    m = hashlib.md5()
    m.update(string)
    encrypted = m.hexdigest()
    if encrypted[:5] == "00000":
        if encrypted[5] in validpos and password[int(str(encrypted[5]))] == ' ':
            password[int(encrypted[5])] = encrypted[6]
            count += 1
            if count == 8:
                found = True
print password

1

u/xkufix Dec 05 '16

Scala makes this actually quite easy, just create a stream of hashes and use the magic of filter/scan/take and map to hack your way to the result: https://gist.github.com/kufi/750a13ca9d37ab12e378e2bc1d8c5e2d

Found the first solution in about 1 minute and the second one in about 3 minutes. For the second one it crunched through about 25'600'000 hashes.

4

u/orukusaki Dec 05 '16 edited Dec 05 '16

One thing lot of people miss, is that converting each hash to a string before testing it is a big waste of time. The string version represents each byte in 2 chars, so really you want to know if the first 2-and-a-half bytes are 0.

This is my part 1 solution:

import java.security.MessageDigest
val md5 = MessageDigest.getInstance("MD5")

val input = "wtnhxymk"

def getPwChar(s: String): Option[Char] = {

  val hash = md5.digest(s.getBytes)
  if (hash(0) == 0
    && hash(1) == 0
    && (hash(2) & 0xF0) == 0
  ) Some(hash.map("%02x".format(_)).mkString.charAt(5)) else None
}

val ans = Iterator.from(1)
  .flatMap {n => getPwChar(input + n.toString)}
  .take(8)

println(ans.mkString)
→ More replies (2)

1

u/bahuljain Dec 05 '16 edited Dec 05 '16

Using a Map and simple tailrec function in part 2 made things real easy for me.. Here's my solution -- https://github.com/bahuljain/scala-fun/blob/master/src/adventcode2016/Day5.scala

1

u/artesea Dec 05 '16

PHP for both answers, fortunately remembered from last year that PHP would evaluate "0e100" == "00000" as true, so used the triple equals. Unfortunately forgot how slow the WAMP on my laptop is compared to the LAMP on my dedicated server and wasted a good bit of time waiting for it to timeout.

<?php
$s='abbhdwsy';
$i=0;
$p="";
$a=[];
while(sizeof($a)<8){
  $m=md5("$s$i");
  if(substr($m,0,5)==="00000") {
    $x=substr($m,5,1);
    $p.=$x;
    if(hexdec($x)<8 && !isset($a[$x])) $a[$x]=substr($m,6,1);
  }
  $i++;
}
ksort($a);
echo substr($p,0,8)."/".implode($a);

1

u/schlocke Dec 05 '16

What PHP version are you using and what were your execution times. I resorted to one character at a time via ajax and it took about 5.7min doing both in parallel.

→ More replies (1)

1

u/[deleted] Dec 05 '16

Clojure. Seems pretty inefficient, since it took 3.6 minutes to compute the second part.

(ns aoc2016.day05
  (:require [digest :refer :all]
            [clojure.string :as s]))

(def input "cxdnnyjw")

(def candidates
  (->> (range)
       (map (comp digest/md5 #(str input %)))
       (filter #(= "00000" (subs % 0 5)))))

(defn part-1 []
  (->> candidates
       (take 8)
       (map #(subs % 5 6))
       (s/join)))

(defn get-index [string]
  "Returns the index value, if valid, and 10 otherwise"
  (let [index (try (Integer. (subs string 5 6))
             (catch NumberFormatException e))]
    (if (number? index) index 10)))

(defn parse-result [resmap]
  (s/join (vals (into (sorted-map) resmap))))

(defn part-2 []
  (loop [cands candidates
         indices (zipmap (range 8) (range 8))
         results {}]
    (let [cand (first cands)
          i (get-index cand)
          value (subs cand 6 7)]
      (if (empty? indices)
        (parse-result results)
        (if (contains? indices i)
          (do
            (prn (str "Now is the time to be jolly - and patient: " (inc (count results)) " character(s) found..."))
            (recur (rest cands)
                   (dissoc indices i)
                   (assoc results i value)))
          (recur (rest cands)
                 indices
                 results))))))

1

u/Borkdude Dec 05 '16

Clojure!

(ns day5
  (:require [clojure.string :as str])
  (:import [java.security MessageDigest]
           [java.math BigInteger]))

(def input "ojvtpuvg")

(defn md5 [^String s]
  (let [algorithm (MessageDigest/getInstance "MD5")
        size (* 2 (.getDigestLength algorithm))
        raw (.digest algorithm (.getBytes s))
        sig (.toString (BigInteger. 1 raw) 16)
        padding (apply str (repeat (- size (count sig)) "0"))]
    (str padding sig)))

;; first password
(take 8 (map #(nth % 5)
             (filter #(str/starts-with? % "00000")
                     (map #(md5 (str input %))
                          (range))))) ;;=> (\4 \5 \4 \3 \c \1 \5 \4)

;; second password
(reduce (fn [m [pos value]]
          (if-not (m pos)
            (assoc m pos value)
            (if (= 8 (count m))
              (reduced (map val (sort m)))
              m)))
        {} 
        (keep
         #(let [[position value] (.substring ^String % 5 7)
                position (Integer/parseInt (str position) 16)]
            (when (<= position 7)
              [position value]))
         (filter #(str/starts-with? % "00000")
                 (map #(md5 (str input %))
                      (range))))) ;;=> (\1 \0 \5 \0 \c \b \b \d)

2

u/sitri Dec 05 '16

Wow, that is neat how you just call take 8 there. I used the same md5 method but the rest of my solution was totally different. I've been learning so much about clojure just by looking the other solutions on here.

1

u/eregontp Dec 05 '16

A clean and simple Ruby solution:

require 'digest/md5'
door_id = "reyedfim"
start = "00000"
code = " "*8
(0..Float::INFINITY).each { |i|
  md5 = Digest::MD5.hexdigest "#{door_id}#{i}"
  if md5.start_with?(start)
    p md5
    pos = Integer(md5[5], 16)
    digit = md5[6]
    if 0 <= pos and pos < 8
      if code[pos] == " "
        code[pos] = digit
      end
      break if code.count(" ") == 0
    end 
  end
}
p code

1

u/beefamaka Dec 05 '16

spotted the similarity with last year's problem, so thought I might get this one solved nice and quickly. But it ran really slowly on my laptop, and I started after leaderboard was full anyway (puzzles go live at 5am here in UK). Here's my F# solution after attempting to speed it up a bit: https://github.com/markheath/advent-of-code-2016/blob/master/day%2005/day5.fsx

1

u/AlexPalla Dec 05 '16

On my PC the solution of part 2 has taken 67 secs, using only 12,5% Of CPU. Too slow! So I wrote a multithread solution that takes only 21 secs with 6 threads. Multithread c# solution!

1

u/johanw123 Dec 05 '16

This is my C# Solution for part 2.

`
    static void Main(string[] args)
    {
      var hash = MD5.Create();
     const string doorId = "wtnhxymk";

      var password = "xxxxxxxx".ToCharArray();

      int count = 0;

      while (password.Contains('x'))
      {
        string hashedId = Join("", from b in hash.ComputeHash(Encoding.ASCII.GetBytes(doorId + count++)) select b.ToString("x2"));

        if (!hashedId.StartsWith("00000")) continue;

        int position;
        if(!int.TryParse(hashedId.ElementAt(5).ToString(), out position) || position > 7) continue;

        char character = hashedId.ElementAt(6);

        password[position] = password[position] == 'x' ? character : password[position];
      }

      hash.Dispose();
      Console.WriteLine(password);
      Console.ReadKey();      
    }

`

1

u/miran1 Dec 05 '16 edited Dec 05 '16

python 3

all solutions here

day5 solution:

import hashlib

DOOR_ID = 'wtnhxymk'

first_password = ''
second_password = [''] * 8
available_positions = set('01234567')

i = 0
while available_positions:
    char_input = (DOOR_ID+str(i)).encode()
    md5_hex = hashlib.md5(char_input).hexdigest()

    if md5_hex[:5] == 5*'0' and len(first_password) < 8:
        first_password += md5_hex[5]
    if md5_hex[:5] == 5*'0' and md5_hex[5] in available_positions:
        second_password[int(md5_hex[5])] = md5_hex[6]
        available_positions.remove(md5_hex[5])
    i += 1

print("The password for a first door is:", first_password)
print('....')
print("The password for a second door is:", ''.join(second_password))

1

u/d1sxeyes Dec 05 '16 edited Jul 21 '26

Redacted.

1

u/Gummoz Dec 05 '16

Powershell!

Part one is pretty straight forward:

$someString = "reyedfim"
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$i = 0
$passwordCounter = 0
[string]$password = $null
while ($true) {
    $someString2 = $someString + [string]$i
    $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString2)))

    if ($hash.Replace("-",'') -match '^00000') {

        if ($passwordCounter -lt 8){
            $password += [string]($hash.Split("-")[2]).toCharArray()[1]
            $passwordCounter++
        }
        else {$password;break}
    }
    $i++

}

Part two -wanted that cinematic feeling - it looks okay, using clear to update the console so you might get some epilepsy:

$someString = "reyedfim"
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
$i = 0
$position1 = $null
$passwordCounter = 0
[string]$output = $null
[string]$output2 = $null
[array]$password = "_","_","_","_","_","_","_","_"
while ($true) {
    $someString2 = $someString + [string]$i
    $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString2)))

    if ($i % 9999 -eq 0) {
    cls
    [string]$output2 = $null
            for ($a = 0; $a -lt 8; $a++){

                if ($password[$a] -eq "_") {

                    [string]$output2 += (Get-Random -Minimum 1 -Maximum 9)

                }
                else {[string]$output2 += $password[$a]}

            }
            
            "Current hash: " + $hash
            "Last character: " + $character
            "Last position: " + $position1
            Write-host -ForegroundColor Green "Password is: $output2"
    }

    if ($hash.Replace("-",'') -match '^00000[0-7]') {

        [string]$output = $null
        [string]$output2 = $null
        $position1 = $hash.split("-")[2].ToCharArray()[1]
        [int]$intNum = [convert]::ToInt32($position1, 10)

        if ($password[$intNum] -eq "_") {

            $character = ($hash.Split("-")[3]).toCharArray()[0]
            $password[$intNum] = $character
            $password | % {[string]$output += $_}

        }
    }
    $i++

}

1

u/NeilNjae Dec 05 '16

Haskell: Laziness FTW! Generate an infinite stream of hashes, just take the first few from them. https://git.njae.me.uk/?p=advent-of-code-16.git;a=blob;f=advent05.hs

module Main(main) where

import Data.Hash.MD5 (md5s, Str(..))
import Data.List (isPrefixOf)
import qualified Data.Map.Lazy as Map 

type Password = Map.Map Integer Char

input = "cxdnnyjw"

main :: IO ()
main = do 
        part1 
        part2

part1 :: IO ()
part1 = do 
    print $ take 8 [h!!5 | h <- filter (interesting) $ md5sequence input 0]

part2 :: IO ()
part2 = do 
    print $ Map.foldr (:) [] password
    where interestingHashes = 
            [(read [h!!5], h!!6) | 
              h <- filter (interesting) (md5sequence input 0), 
              h!!5 `elem` "01234567"]
          password = findPassword Map.empty interestingHashes

md5sequence :: String -> Integer -> [String]
md5sequence key i = (md5s (Str (key ++ show i))) : (md5sequence key (i+1))

interesting :: String -> Bool
interesting hash = "00000" `isPrefixOf` hash

dontReplace :: (Integer, Char) -> Password -> Password
dontReplace (k, v) = Map.insertWith (_ v -> v) k v

findPassword :: Password -> [(Integer, Char)] -> Password
findPassword p (c:cs)
  | Map.size p == 8 = p
  | otherwise = findPassword p' cs
      where p' = dontReplace c p

1

u/_AceLewis Dec 05 '16

Python 3 solution to both parts:

Day 5 part 1: https://repl.it/EgKP/1

import hashlib

hash_string = "ffykfhsq"
hash_num = 0
password = ''

while len(password) != 8:
  MD5 = hashlib.md5((hash_string+str(hash_num)).encode('utf-8')).hexdigest()
  if MD5.startswith('00000'):
    password += MD5[5]
  hash_num += 1
  
print("The password is", password)

Day 5 part 2: https://repl.it/EgKP/2

import hashlib
import re

hash_string = "ffykfhsq"
hash_num = 0
password = '*'*8

while '*' in password:
  MD5 = hashlib.md5((hash_string+str(hash_num)).encode('utf-8')).hexdigest()
  if re.search('^00000[0-7]', MD5):
    if password[int(MD5[5])] == '*':
      password = password[:int(MD5[5])] + MD5[6] + password[int(MD5[5])+1:]
      print(password)
  hash_num += 1
  
print("The password is", password)

1

u/Reibello Dec 05 '16

Here's a pastebin to my solution in Python.

http://pastebin.com/hfDkyXXy

1

u/JakDrako Dec 05 '16

VB.Net, LinqPad

I really liked this puzzle, a lot of fun. I think I'll try a parallel version for the second part.

Part 1

Sub Main
	Dim md5 = System.Security.Cryptography.MD5.Create
	Dim key = "abc"
	Dim pass = ""
	Dim i = 0
	Do
		Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & i))
		If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then
			pass &= hash(2).ToString("X2")(1)
			If pass.Length = 8 Then Exit Do
		End If
		i += 1
	Loop
	pass.ToLowerInvariant.Dump("password")
End Sub

Part 2

Sub Main
	Dim md5 = System.Security.Cryptography.MD5.Create
	Dim key = "abc"
	Dim pass = "........".ToCharArray, found = 0
	Dim i = 0
	Do
		Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & i))
		If hash(0) = 0 AndAlso hash(1) = 0 AndAlso hash(2) < 16 Then
			Dim pos = hash(2) And 15
			If pos < 8 Then
				If pass(pos) = "." Then
					pass(pos) = hash(3).ToString("X2")(0)
					found += 1
					If found = 8 Then Exit Do
				End If
			End If
		End If
		i += 1
	Loop
	String.Join("", pass).ToLowerInvariant.Dump("Password")
End Sub

2

u/JakDrako Dec 05 '16

VB.Net, LinqPad

Parallel versions

Part 1 (Runs "abc" in ~5 seconds)

Sub Main

	Dim key = "abc"

	Dim chunck = 100000
	Dim dic = New ConcurrentDictionary(Of Integer, Byte())
	Dim maxThreads = New SemaphoreSlim(Environment.ProcessorCount)

	For i = 0 To Integer.MaxValue Step chunck

		maxThreads.Wait

		Dim start = i
		Dim count = chunck - 1

		Dim tsk = Task.Factory.StartNew(
			Sub()
				Dim md5 = Security.Cryptography.MD5.Create
				'Console.WriteLine($"Starting thread for {start} to {start + count}")
				For n = start To start + count
					Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & n))
					If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then dic.TryAdd(n, hash)
				Next
			End Sub).ContinueWith(Sub(t) maxthreads.Release)
		If dic.Count >= 8 Then Exit For
	Next

	Dim pass = String.Join("", dic.OrderBy(Function(kvp) kvp.Key) _
									.Select(Function(kvp) kvp.Value(2).ToString("X2")(1)) _
									.Take(8))
	pass.ToLowerInvariant.dump("Password")

End Sub

Part 2 (Runs "abc" in ~7 seconds)

Sub Main

	Dim key = "abc"

	Dim chunck = 100000
	Dim dicPos = New ConcurrentDictionary(Of Integer, Integer)
	For pos = 0 To 7
		dicpos.TryAdd(pos, Integer.MaxValue)
	Next
	Dim dicHash = New ConcurrentDictionary(Of Integer, Byte())
	Dim maxThreads = New SemaphoreSlim(Environment.ProcessorCount)

	For i = 0 To Integer.MaxValue Step chunck

		maxThreads.Wait

		Dim start = i
		Dim count = chunck - 1

		Dim tsk = Task.Factory.StartNew(
			Sub()
				Dim md5 = Security.Cryptography.MD5.Create
				'Console.WriteLine($"Starting thread for {start} to {start + count}")
				For n = start To start + count
					Dim hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key & n))
					If hash(0) = 0 Andalso hash(1) = 0 Andalso hash(2) < 16 Then
						Dim pos = hash(2) And 15
						If pos < 8 Then
							If n < dicPos(pos) Then
								dicpos(pos) = n
								dicHash(pos) = hash
							End If
						End If
					End If
				Next
			End Sub).ContinueWith(Sub(t) maxthreads.Release)
		Dim done = True
		For Each kvp In dicpos
			If kvp.Value = Integer.MaxValue Then done = False : Exit For
		Next
		If done Then Exit For
	Next

	Dim pass = String.Join("", dicHash.OrderBy(Function(kvp) kvp.Key) _
									  .Select(Function(kvp) kvp.Value(3).ToString("X2")(0)) _
									  .Take(8))
	pass.ToLowerInvariant.dump("Password")

End Sub

1

u/ShroudedEUW Dec 05 '16

C#

This was really fun! Couldn't find a quick way to limit the possible hashes, so looped through everything one at a time and then checked if they had the 0s (and the correct input for p2):

https://github.com/KVooys/AdventOfCode/blob/master/AdventOfCode/Day5.cs

1

u/llimllib Dec 05 '16 edited Dec 05 '16

Python with a hacker animation:

import hashlib
i = 0
pw = []
pw2 = [-1] * 8
while -1 in pw2:
    hash_ = hashlib.md5(b"wtnhxymk"+bytes(str(i), 'ascii')).hexdigest()
    if hash_.startswith('00000'):
        pw.append(hash_[5])
        key = int(hash_[5], 16)
        if key < 8 and pw2[key] == -1:
            pw2[key] = hash_[6]
    i += 1
    if i % 1000 == 0:
        fake1 = (''.join(pw[:8]) + hash_[5:13])[:8]
        fake2 = (''.join(map(str,pw2)).replace('-1','') + hash_[13:21])[:8]
        print("{} {}".format(fake1, fake2), end='\r')
print("{} {}".format(''.join(pw[:8]), ''.join(pw2)))

1

u/asperellis Dec 05 '16

my js solution - https://github.com/asperellis/adventofcode2016/blob/master/day5.js

any suggestions to make this run faster? improving md5, something else or is it just a waiting game? just curious. never ran them together obviously.

2

u/[deleted] Dec 05 '16

I think most people just use a library for md5 hashes, the one for python is coded in c I think so it's quite fast.

1

u/schlocke Dec 05 '16

PHP, JS, HTML... Initially I just wanted PHP but my local machine couldn't handle it and I'm not about to set up a dedicated server for my AoC solutions. I realized if I just loaded one character at a time via AJAX i wouldn't hit my execution limits in PHP. NOTE: this is on PHP 5.5.12, 8 GB ram, i5 4310M 2.7 GHz, 64 bit windows 7 while running chrome, WAMP, security scan, outlook, slack, etc. Takes a while (took me 5.7 min) to get the answers but both are done in parallel. NOTE2: I also know i could've probably done this all in JS but I had the PHP figured out already.

PHP (day5.php):

<?php
	$input = "ojvtpuvg";
	$p = '';
	$i= $_POST['i'];
	$index = 0;

	while(strlen($p) < 1) {
		$c = md5($input.$i);
		if( substr($c,0,5) === "00000") {
			if($_POST['part'] === '1') { 
				$p = substr($c, 5, 1);
			} else {
				$index = substr($c, 5, 1);
				$p = substr($c, 6, 1);
			}
		}
		$i++;
	}

	echo json_encode(array("p" => $p, "index" => $index, "i" => $i));

HTML+JS(day5.html):

<html>
	<head>
		<title>test</title>
	</head>
	<body>
		Password1<span id="load1">(loading...)</span> = <span id="password1"></span>
		<br>
		Password2<span id="load2">(loading...)</span> = <span id="password2"></span>
	
		<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
		<script>
			$(document).ready(function(){

				function getp1(i, j) {
					$.ajax({
						method: "POST",
						url: "day5.php",
						dataType: "json",
						data: { i:i , part:1}
						})
						.done(function( data ) {
							$("#password1").append(data.p);
							j++;
							if(j < 8) getp1(data.i, j);
							else $("#load1").text("DONE");
							return;
						}
					);
				}

				function getp2(i, j, arr) {
					$.ajax({
						method: "POST",
						url: "day5.php",
						dataType: "json",
						data: { i: i, part:2 }
						})
						.done(function( data ) {
							
							if(arr[data.index] === '') {
								arr[data.index] = data.p;
								$("#password2").text(arr.join("_"));
								j++;
							}
							if(j < 8) {
								getp2(data.i, j, arr);
							} else {
								$("#password2").text(arr.join(""));
								$("#load2").text("DONE");
							}
							return;
						}
					);
				}

				getp1(0, 0);
				getp2(0, 0, ['','','','','','','','']);
			});			
		</script>
	</body>
</html>

EDIT: added execution time 5.7min. EDIT2: if some web designer wants to steal this and do the cool hacking movie animation go a head im too lazy for that right now.

1

u/qwertyuiop924 Dec 05 '16

Here's the day 5 part 2 solution: no animation (yet!), but it solves pretty well: http://pastebin.com/HpvGZ52G

1

u/Timmeh7 Dec 05 '16

C#, both parts:

    static void Main(string[] args)
    {
        String doorID = "abbhdwsy";
        int preID = 0;
        String result1 = "";
        Char[] result2 = { '|', '|', '|', '|', '|', '|', '|', '|'};
        MD5 md5 = MD5.Create();


        while(result2.Contains('|'))
        {
            String source = doorID + preID.ToString();
            String hash = GetMd5Hash(md5, source);
             
            if(hash.StartsWith("00000"))
            {
                if (result1.Length < 8)
                {
                    result1 += hash[5];
                }

                int index = 0;
                if (int.TryParse(hash[5].ToString(), out index))
                {
                    if (index < 8 && result2[index] == '|')
                    {
                        result2[index] = hash[6];
                    }
                }
            }

            preID++;
        }

        Console.WriteLine("1: " + result1);
        Console.WriteLine("2: " + new string(result2));
        Console.ReadLine();
        
    }

    static string GetMd5Hash(MD5 md5Hash, string input)
    {
        //Shamelessly stolen from msdn.
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();

        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }

1

u/Zeroeh Dec 05 '16

Java Solution:

part 1:

	String input = "cxdnnyjw";

	String decryptedPassword = "";

	int currentIndex = 0;
	System.out.println("searching...");
	while (decryptedPassword.length() < 8) {
		String hash = Util.getMD5Hash(input + Integer.toString(currentIndex));

		if (hash.startsWith("00000")) {
			String chr = hash.substring(5, 6);
			decryptedPassword += chr;
			System.out.println("Found a letter in the hash: " + hash + "\n Character: " + chr);
		}
		currentIndex++;
	}
	
	System.out.println("Password: " + decryptedPassword);
	System.out.println("Last Index: " + currentIndex);

part 2:

	String input = "cxdnnyjw";

	String[] decryptedPassword = new String[8];

	int currentIndex = 0;
	int foundIndex = 0;
	System.out.println("searching...");
	while (foundIndex < 8) {

		String hash = Util.getMD5Hash(input + Integer.toString(currentIndex));

		if (hash.startsWith("00000")) {
			
			/** Is this a valid pos? IE a digit? **/
			int pos = ((hash.substring(5, 6).matches("-?\\d+(\\.\\d+)?"))) ? Integer.parseInt(hash.substring(5, 6)) : -1;
			
			/** Character **/
			String chr = hash.substring(6, 7).toString();

			/** If valid Position and the position was not taken yet **/
			if (pos != -1 && pos > -1 && pos < decryptedPassword.length && decryptedPassword[pos] == null) {
				decryptedPassword[pos] = chr;
				foundIndex++;
				System.out.println("Found a letter in the hash: " + hash + "\n Pos: " + pos + " \n Character: " + chr);
			}
		}

		currentIndex++;
	}

	StringBuffer buff = new StringBuffer();
	
	for (String s : decryptedPassword)
		buff.append(s);

	System.out.println("Password: " + buff.toString());
	System.out.println("Last Index: " + currentIndex);

Md5 code:

		MessageDigest md = MessageDigest.getInstance("MD5");
	md.update(part.getBytes());

	byte byteData[] = md.digest();

	StringBuffer sb = new StringBuffer();
	for (int i = 0; i < byteData.length; i++) {
		sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
	}

	return sb.toString();

1

u/JakDrako Dec 05 '16

C#, both parts at the same time, in parallel. Completes "abc" in about 7 seconds.

I'm having a lot of fun with this problem.

void Main()
{
	var key = "abc";
	var suffix = 0;

	var pass = "";
	var pswd = "________".ToCharArray();
	var found = 0;

	var lock1 = new Object();
	var done1 = false;

	var lock2 = new Object();
	var done2 = false;

	Parallel.For(0, Environment.ProcessorCount, x =>
	{
		var md5 = MD5.Create();
		do
		{
			var hash = md5.ComputeHash(Encoding.ASCII.GetBytes(key + Interlocked.Increment(ref suffix)));
			
			if (hash[0] == 0 && hash[1] == 0 && hash[2] < 16)
			{
				if (!done1)
				{
					lock (lock1)
					{
						pass += hash[2].ToString("X2")[1];
						if (pass.Length == 8) done1 = true;
					}
				}
				
				if (hash[2] < 8 && !done2)
				{
					if (pswd[hash[2]] == '_')
					{
						lock (lock2)
						{
							pswd[hash[2]] = hash[3].ToString("X2")[0];
							found += 1;
							if (found == 8) done2 = true;
						}
					}
				}
			}
		} while (!done1 || !done2);
	});
	Console.WriteLine("Part 1: " + pass.ToLowerInvariant());
	Console.WriteLine("Part 2: " + new string(pswd).ToLowerInvariant());
}

1

u/Pyrobolser Dec 05 '16

That's neat, I never really use Parallel.For(...) stuff, is this still the "modern" way to do it with the era of async/await?
Sorry if this is a dumb question, I'll read your code more carefully afterwork, I really need to learn how to use paralellism, etc.

1

u/JakDrako Dec 05 '16

This case is pretty simple to parallelize, the various advantages provided by Tasks (cancellation tokens, etc) are not needed. I did post a VB version before that does the multithreading with Tasks, but the performance was similar. I prefer the simpler implementation in the C# code above.

What seems to slow down my current version is .Net's MD5 provider... I'm playing around with custom implementations and it seems I might be able to get into the low 2 seconds...

→ More replies (4)

1

u/SikhGamer Dec 07 '16

Interesting. Using the "abc" input, my single-thread code takes 7 seconds too. I remembered from last time, the Microsoft implementation of MD5 is slow. If you use something like BouncyCastle, you'll shave off seconds.

1

u/JakDrako Dec 07 '16

Yes, using a custom MD5 implementation drops my time to ~2.3 seconds.

1

u/Kwpolska Dec 05 '16

Boo, a repeat from last year.

#!/usr/bin/env python3
# Repeat of 2015 day 4. I stole my own code from there.
import hashlib


def solve(data):
    answer = [''] * 8
    filled = 0
    i = 0
    while filled != 8:
        d = hashlib.md5(data + str(i).encode('ascii')).hexdigest()
        i += 1
        if d.startswith("00000"):
            print("Match", d[5])
            try:
                pos = int(d[5])
            except ValueError:
                continue  # ignore invalid position
            if pos == 8 or pos == 9:
                continue  # ignore invalid position
            elif answer[pos] != '':
                continue  # ignore repeat characters
            char = d[6]
            answer[pos] = char
            print(pos, char)
            filled += 1
    return ''.join(answer)


test_data = b"abc"
test_output = solve(test_data)
test_expected = "05ace8e3"
print(test_output, test_expected)
assert test_output == test_expected
print(solve(b'abbhdwsy'))

1

u/Fishy07x Dec 05 '16

My python solution:

import sys
import md5

code = sys.stdin.readline().rstrip()

i = 0
counter = 0
password = ""
while (counter < 8):
    x = md5.new(code + str(i)).hexdigest()
    if x.startswith("00000"):
        password += x[5]
        counter += 1
    i += 1

print(password)

1

u/SaltyPeaches Dec 05 '16

Here's my solutions in PowerShell:

Part 1
Part 2

1

u/efexen Dec 05 '16

Attempted some concurrency stuff in Elixir, parts 1 and 2: https://github.com/efexen/advent_of_code/blob/master/lib/day5.ex

1

u/LainIwakura Dec 05 '16

Erlang solutions: https://github.com/LainIwakura/AdventOfCode2016/tree/master/Day5

This one was a bit tricky because the erlang functions for md5 return the values as a bit string which needs to be converted into hex representation. Most naive ways of doing this were horribly slow so I had to grab someones fast bin-to-hex function from stackoverflow.

In part 2, since erlang lists are immutable, I stored the index and character in a tuple in the decoded pw list - at the end keysort on the index and collect your decoded characters and bam you have a password =)

1

u/muchomouse Dec 05 '16

C# Part 2 complete, with pointless cinematic presentation ftw.

Had to run the drawing in a separate thread, because it slowed down the program so much that just finding the first char took several minutes :P

https://github.com/kjellskogsrud/AoC_2016/blob/master/AoC_2016/Classes/Day5.cs

1

u/fixed_carbon Dec 05 '16

Ruby solution (part two) using an Enumerator to generate [digit, position] pairs. This one is interesting because it generalizes to passwords of any length between 1 and 16 simply by changing the number in the last line of the take_while block.

require 'digest'

# Yields *all* digit, position pairs
coderator = Enumerator.new do |y|
  md5 = Digest::MD5.new
  (0..Float::INFINITY).each do |n|
    digest = md5.hexdigest("#{INPUT}#{n}")
    if digest.start_with?("00000")
      y.yield [digest[6], digest[5].hex]
    end
  end
end

# Applies positional and stateful constraints to digit, position pairs
code = Array.new(16, nil)
coderator.take_while do |digit, pos|
  code[pos] = digit if code[pos].nil?
  puts code.map{|c| c.nil? ? '_' : c}.join
  code.take(8).include?(nil)
end

puts code.take(8).join

1

u/ashleyhindle Dec 05 '16

Live streamed mine using PHP but didn't have time for an awesome animation!

1

u/Quick_Question404 Dec 05 '16

Woohoo! Finally was able to finish with this today's problem. Was delayed due to never having used MD5 before, and giving up trying to stay awake all night to figure it out. Placed around 3500~, which is a long way from the <1000 before. I did make my code have a little animation though. What do you guys think?

https://github.com/HighTide1/adventofcode2016/tree/master/05

1

u/yasgur99 Dec 06 '16

I did it in java: Wish i did it in one pass through... next time.

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;

public class AdventDay5 {

    public static void main(String[] args) throws Exception {
        partOne("abbhdwsy");
        partTwo("abbhdwsy");
    }

    public static String hasher(String input) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] digest = md.digest();
        return DatatypeConverter
                .printHexBinary(digest).toLowerCase();
    }

    public static void partOne(String base) throws Exception {
        String password = "";
        int numbers = 0;
        while (password.length() < 8) {
            if (hasher(base + numbers).startsWith("00000")) {
                password += hasher(base + numbers).charAt(5) +"";
            }
            numbers++;
        }
        System.out.println("Password to part one: " + password);

    }

    public static void partTwo(String base) throws Exception {
        char[] pass = new char[8];
        int numbers = 0;
        int counter = 0;
        for (int i = 0; i < 8; numbers++) {
            if (hasher(base + numbers).startsWith("00000")) {
                try {
                    int num = Integer.parseInt(hasher(base + numbers).charAt(5) + "");
                    if (num < 8) {
                        if (pass[num] == '\0') {
                            i++;
                            pass[num] = hasher(base + numbers).charAt(6);
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
        System.out.print("Password to part two: ");
        for (char c : pass) {
            System.out.print(c);
        }
        System.out.println("");
     }
}

1

u/bhauman Dec 06 '16 edited Dec 06 '16

Clojure Solution:

(ns advent-of-clojure-2016.day5
  (:require
   [digest :refer [md5]]
   [medley.core :refer [distinct-by]]))

(def input "ffykfhsq")

(defn find-password [code]
  (transduce
   (comp
    (map #(md5 (str code %)))
    (filter #(= "00000" (subs % 0 5)))
    (map #(nth % 5))
    (take 8))
   conj
   []
   (range)))

#_ (def res (time (find-password input)))
;; => [\c \6 \6 \9 \7 \b \5 \5]
;; 23.4 seconds baby!

(defn find-codes-2 [code]
  (transduce
   (comp
    (map #(md5 (str code %)))
    (filter #(and (= "00000" (subs % 0 5))
                  (> 8 (Integer/parseInt (subs % 5 6) 16))))
    (map (juxt #(Integer/parseInt (str (nth % 5)))
               #(nth % 6)))
    (distinct-by first)
    (take 8))
   conj
   []
   (range)))

(defn result-password [codes]
  (reduce #(assoc %1 (first %2) (second %2)) (vec (replicate 8 '_)) codes))

#_ (def res2 (time (result-password (find-codes-2 input))))
;; => [\8 \c \3 \5 \d \1 \a \b]
;; 110 seconds

Other Solutions in my AOC 2016 repo

1

u/grayrest Dec 06 '16

Clojure. Not super fast, ~12.5s and ~23.5s on my macbook air.

(ns advent.day5
  (:require [clojure.string :as str])
  (:import (java.security MessageDigest)
           (javax.xml.bind DatatypeConverter)))

(set! *warn-on-reflection* true)
(def ^MessageDigest hasher (MessageDigest/getInstance "MD5"))

(defn md5 [^String what]
  (->> (.getBytes what "utf-8")
       (.digest hasher)
       (DatatypeConverter/printHexBinary)))

(defn hash-sequence
  ([salt] (hash-sequence salt 0))
  ([salt n]
   (lazy-seq
     (cons
       (md5 (str salt n))
       (lazy-seq (hash-sequence salt (inc n)))))))

(defn ans1 [salt]
  (->> (hash-sequence salt)
       (filter #(= "00000" (subs % 0 5)))
       (map #(nth % 5))
       (take 8)
       str/join))

(defn fancy-passwd-builder [passwd ^String hash]
  (let [pos (Character/getNumericValue (.charAt hash 5))]
    (if (and (< pos 8)
             (= _ (nth passwd pos)))
      (assoc passwd pos (.charAt hash 6))
      passwd)))

(defn ans2 [salt]
  (reduce (fn [passwd hash]
            (println (pr-str passwd))
            (if (some #{_} passwd)
              (fancy-passwd-builder passwd hash)
              (reduced (apply str passwd))))
          (into [] "________")
          (->> (hash-sequence salt)
               (filter #(= "00000" (subs % 0 5))))))

1

u/cashews22 Dec 06 '16

Part 1 and 2 in Golang using GoRoutine, make me save 1 minute of processing

https://github.com/cashew22/adventofcode2016/blob/master/5/solve.go

1

u/PendragonDaGreat Dec 06 '16

Little late to the party, but here's my massively multi-threaded version in C# (batching 32 new threads at a time was the sweet spot for my 8-core laptop): https://github.com/Bpendragon/AOC-Day5/blob/master/Day5/Program.cs

No animation bonus points, but it does switch between modes just by swapping the static bool at the top.

1

u/[deleted] Dec 06 '16

Mathematica.

The built-in md5 function is too slow, so I use a helper function to call the implementation in OpenSSL.

Needs["CCompilerDriver`"]
helperSource = NotebookDirectory[] <> "helper.c";
CreateLibrary[{helperSource}, "adventhelp",
  "IncludeDirectories" -> "/opt/local/include",
  "LibraryDirectories" -> "/opt/local/lib",
  "Libraries" -> {"ssl", "crypto"}];

md5hash = 
  LibraryFunctionLoad["adventhelp", "md5hash", {"UTF8String"}, 
   "UTF8String"];

input = "abbhdwsy";

StringJoin@Monitor[
  Block[{cs = {}, i = 0, s},
   While[Length@cs != 8,
    s = md5hash[input <> IntegerString[i++]];
    If[StringStartsQ[s, "00000"],
     AppendTo[cs, StringTake[s, {6}]]]];
   cs], cs]

StringJoin@Monitor[
  Block[{cs = ConstantArray["*", 8], i = 0, rem = 8, s, p = 0},
   While[rem > 0,
    s = md5hash[input <> IntegerString[i++]];
    If[StringStartsQ[s, "00000"],
     p = FromDigits[StringTake[s, {6}], 16];
     If[p <= 7 && cs[[p + 1]] == "*",
      cs[[p + 1]] = StringTake[s, {7}];
      rem--]]];
   cs], cs]

helper.c:

#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>
#include "WolframLibrary.h"

DLLEXPORT mint WolframLibrary_getVersion( ) {
    return WolframLibraryVersion;
}

DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) {
    return 0;
}

static char hexstr[33];

DLLEXPORT int md5hash(WolframLibraryData libData,
		  mint Argc, MArgument *Args, MArgument Res)
{
  unsigned char digest[16];
  const char *msg = MArgument_getUTF8String(Args[0]);
  MD5(msg, strlen(msg), digest);
  for (int i=0; i<16; i++) {
sprintf(&hexstr[i*2], "%02x", (unsigned int)digest[i]);
  } 
  MArgument_setUTF8String(Res, hexstr);
  libData->UTF8String_disown(msg);
  return LIBRARY_NO_ERROR;
}

1

u/tehjimmeh Dec 07 '16 edited Dec 08 '16

PowerShell, part2:

$puzzin = "cxdnnyjw"

$md5 = new-object security.cryptography.md5cryptoserviceprovider
$utf8 = new-object text.utf8encoding
[char[]]$part2 = @(0,0,0,0,0,0,0,0)
for($i = 0; ; $i++) {
    $hash = $md5.computehash($utf8.getbytes("$puzzin$i"))
    if($hash[0] -eq 0 -and $hash[1] -eq 0 -and $hash[2] -le 0xf) {
        $str = (-join ($hash | %{ "{0:x2}" -f $_ }))
        if([int]"0x$($str[5])" -lt 8) {
            $index = ([int]"0x$($str[5])")
            if($part2[$index] -eq 0) {
                $part2[$index] = $str[6]
                if($part2 -notcontains [char]0) {
                    -join $part2
                    break
                }
            }
        }
    }
}

1

u/tartrate Dec 11 '16

Haskell

import Data.Hash.MD5
import Data.List.Utils
import Data.Char
import Control.Concurrent.MVar
import Control.Concurrent
import Control.Monad
import System.IO

hashes :: [String]
hashes = [md5s (Str ("reyedfim" ++ show i)) | i <- [(1 :: Int)..]]

candidates :: [String]
candidates = filter ((&&) <$> isOctDigit . (!! 5) <*> startswith "00000") hashes

search :: [MVar String] -> IO ()
search ms = forM_ candidates $
    \h -> tryPutMVar (ms !! digitToInt (h !! 5)) [h !! 6]

main :: IO ()
main = do
    digits <- replicateM 8 newEmptyMVar
    _ <- forkIO (search digits)
    hSetBuffering stdout NoBuffering
    putStr "The passcode is: "
    forM_  digits (putStr <=< takeMVar)
    putStrLn ""

1

u/superfunawesomedude Dec 31 '16 edited Dec 31 '16

C# Took just under 19 seconds for part 1.

using System;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication25
{
	class Program
	{
		static void Main(string[] args)
		{
			DateTime starttime = DateTime.Now;

			int add = 0;

			MD5 md5Hash = MD5.Create();
			byte[] data = new byte[16];

			for (int i = 0; i < 8; i++)
			{
				while (true)
				{
					string source = "reyedfim" + add;
					data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source));

					if (data[0] == 0 && data[1] == 0 && data[2] < 16)
					{
						Console.Write(data[2]+ " ");
						add++;
						break;
					}
					add++;
				}
			}
			Console.WriteLine((DateTime.Now - starttime).TotalSeconds);
			Console.ReadKey();
		}
	}
}

1

u/inerte May 18 '17

My solution for Part Two using Golang

package five

import (
	"crypto/md5"
	"encoding/hex"
	"io"
	"strconv"
	"strings"
)

func passwordHasEmptyCharacters(password [8]string) bool {
	for _, character := range password {
		if character == "" {
			return true
		}
	}
	return false
}

func DayFivePartTwo(doorID string) string {
	var password [8]string
	i := 0
	// for passwordHasEmptyCharacters(password) {
	for passwordHasEmptyCharacters(password) != false {
		toHash := doorID + strconv.Itoa(i)
		h := md5.New()
		io.WriteString(h, toHash)
		encode := hex.EncodeToString(h.Sum(nil))
		if encode[:5] == "00000" {
			position, err := strconv.Atoi(string(encode[5]))
			if err == nil && position < 8 && password[position] == "" {
				character := string(encode[6])
				password[position] = character
			}
		}
		i++
	}
	return strings.Join(password[:], "")
}

0

u/[deleted] Dec 05 '16

[deleted] — view deleted comment

1

u/JiffierBot Dec 05 '16

To aid mobile users, I'll fix gfycat links to spare bandwidth from choppy gifs.


~1.1x smaller: http://gfycat.com/YellowRegularAtlanticsharpnosepuffer


I am a bot | Mail ^BotOwner | v1.1 | Code | ^Ban - ^Help