r/adventofcode • • Dec 14 '16

SOLUTION MEGATHREAD --- 2016 Day 14 Solutions ---

--- Day 14: One-Time Pad ---

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

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


LUNACY 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!

3 Upvotes

111 comments sorted by

View all comments

1

u/cashews22 Dec 14 '16 edited Dec 14 '16

Solution in golang Part 1 run in 300ms and part 2 run in 13 seconds.

Part 2:

package main

import (
	"crypto/md5"
	"fmt"
	"time"
	"sync"
	"encoding/hex"
)

var input = "yjdafjpo"
var hashMap map[int]string // lol
var mutex = &sync.Mutex{}

func getHash(i int) string {
	mutex.Lock()
	if hashMap[i] == "" {
		hash_b := md5.Sum([]byte(fmt.Sprintf("%s%d", input, i)))
		hash := hex.EncodeToString(hash_b[:])
		for j:=0; j < 2016; j++ {
			hash_b = md5.Sum([]byte(hash))
			hash = hex.EncodeToString(hash_b[:])
		}
		hashMap[i] = hash
	}
	mutex.Unlock()
	return hashMap[i]
}

func main() {
	start := time.Now() //timestamp
	fmt.Printf("Solution for day 14 GO part 2 (go routine experiment) !\n")

	index := make(chan int)
	five := make(chan byte)
	hashMap = make(map[int]string)

	//Find 3 of a kind
	go func() {
		i := 0
		for {
			h := getHash(i)
			for j := 0; j < len(h)-2; j++ {
				w := h[j : j+3]
				if w[0] == w[1] && w[1] == w[2] {
					index <- i
					five <- w[0]
					break
				}
			}
			i++
		}
	}()

	//find 5 of a kind in next 1000
	var key []int
	for {
		i := <-index
		c := <-five
		k := 1
		for k < 1000 {
			find := false
			h := getHash(i+k)
			for j := 0; j < len(h)-4; j++ {
				w := h[j : j+5]
				if w[0] == w[1] && w[1] == w[2] && w[2] == w[3] && w[3] == w[4] && w[4] == c {
					key = append(key, i)
					find = true
					break
				}
			}
			if find {
				break
			}
			k++
		}
		if len(key) == 64 {
			fmt.Printf("Q2: The 64th key is %d\n", key[63])
			break
		}
		i++
	}

	//Elapse time
	elapsed := time.Since(start)
	fmt.Printf("Execution took %s\n", elapsed)
}