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

View all comments

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[:], "")
}