Day 5 Complete

This commit is contained in:
Brian Buller 2016-12-05 08:10:15 -06:00
parent 0741a1a924
commit 31ea3e4f54
4 changed files with 190 additions and 0 deletions

BIN
day05/day05 Executable file

Binary file not shown.

1
day05/input Normal file
View File

@ -0,0 +1 @@
ojvtpuvg

118
day05/main.go Normal file
View File

@ -0,0 +1,118 @@
package main
import (
"bufio"
"crypto/md5"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
func main() {
input := stdinToString()
//partOne(input)
partTwo(input)
}
func partOne(input string) {
complete := false
idx := 1
password := ""
for !complete {
if idx%1000000 == 0 {
fmt.Print("O")
} else if idx%100000 == 0 {
fmt.Print(".")
}
sum := fmt.Sprintf("%x", md5.Sum([]byte(input+itoa(idx))))
if strings.HasPrefix(sum, "00000") {
fmt.Println("d")
fmt.Println(itoa(idx))
password += string(sum[5])
}
idx++
if len(password) == 8 {
complete = true
}
}
fmt.Println("")
fmt.Println(password)
}
func partTwo(input string) {
fmt.Println("Cracking the Password, hold on to your butts: ")
rand.Seed(time.Now().UTC().UnixNano())
complete := false
idx := 1469580
password := []byte{0, 0, 0, 0, 0, 0, 0, 0}
printPassword(password)
fnd := 0
for !complete {
sum := fmt.Sprintf("%x", md5.Sum([]byte(input+itoa(idx))))
if strings.HasPrefix(sum, "00000") {
pos, err := strconv.Atoi(string(sum[5]))
if err == nil && pos < 8 && password[pos] == 0 {
password[pos] = sum[6]
fnd++
if fnd == 8 {
complete = true
}
}
}
if idx%100000 == 0 {
clearPassword()
printPassword(password)
}
idx++
}
clearPassword()
printPassword(password)
fmt.Println("")
fmt.Println("!~CRACK'D~!")
}
func clearPassword() {
fmt.Print("")
}
func printPassword(pw []byte) {
for i := 0; i < len(pw); i++ {
if pw[i] == 0 {
// Print random char
rndBt := byte(97 + rand.Intn(122-97))
if rndBt%3 == 0 {
fmt.Print(itoa(int(rndBt % 10)))
} else {
fmt.Print(string(byte(97 + rand.Intn(122-97))))
}
} else {
fmt.Print(string(pw[i]))
}
}
}
func stdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = input + scanner.Text()
}
return input
}
func itoa(i int) string {
return strconv.Itoa(i)
}
func atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

71
day05/problem Normal file
View File

@ -0,0 +1,71 @@
Advent of Code
--- Day 5: How About a Nice Game of Chess? ---
You are faced with a security door designed by Easter Bunny engineers that seem to have acquired most of their security knowledge by watching hacking movies.
The eight-character password for the door is generated one character at a time by finding the MD5 hash of some Door ID (your puzzle input) and an increasing integer
index (starting with 0).
A hash indicates the next character in the password if its hexadecimal representation starts with five zeroes. If it does, the sixth character in the hash is the next
character of the password.
For example, if the Door ID is abc:
 The first index which produces a hash that starts with five zeroes is 3231929, which we find by hashing abc3231929; the sixth character of the hash, and thus the
first character of the password, is 1.
 5017308 produces the next interesting hash, which starts with 000008f82..., so the second character of the password is 8.
 The third time a hash starts with five zeroes is for abc5278568, discovering the character f.
In this example, after continuing this search a total of eight times, the password is 18f47a30.
Given the actual Door ID, what is the password?
Your puzzle answer was ________.
--- Part Two ---
As the door slides open, you are presented with a second door that uses a slightly more inspired security mechanism. Clearly unimpressed by the last version (in what
movie is the password decrypted in order?!), the Easter Bunny engineers have worked out a better solution.
Instead of simply filling in the password from left to right, the hash now also indicates the position within the password to fill. You still look for hashes that begin
with five zeroes; however, now, the sixth character represents the position (0-7), and the seventh character is the character to put in that position.
A hash result of 000001f means that f is the second character in the password. Use only the first result for each position, and ignore invalid positions.
For example, if the Door ID is abc:
 The first interesting hash is from abc3231929, which produces 0000015...; so, 5 goes in position 1: _5______.
 In the previous method, 5017308 produced an interesting hash; however, it is ignored, because it specifies an invalid position (8).
 The second interesting hash is at index 5357525, which produces 000004e...; so, e goes in position 4: _5__e___.
You almost choke on your popcorn as the final character falls into place, producing the password 05ace8e3.
Given the actual Door ID and this new method, what is the password? Be extra proud of your solution if it uses a cinematic "decrypting" animation.
Your puzzle answer was ________.
Your puzzle input was ojvtpuvg.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2016/about
. http://adventofcode.com/2016/support
. http://adventofcode.com/2016/events
. http://adventofcode.com/2016/settings
. http://adventofcode.com/2016/auth/logout
. http://adventofcode.com/2016
. http://adventofcode.com/2016
. http://adventofcode.com/2016/leaderboard
. http://adventofcode.com/2016/stats
. http://adventofcode.com/2016/sponsors
. http://adventofcode.com/2016/sponsors
. https://www.detroitlabs.com/careers
. https://en.wikipedia.org/wiki/Hackers_(film)
. https://en.wikipedia.org/wiki/WarGames
. https://en.wikipedia.org/wiki/MD5
. https://en.wikipedia.org/wiki/Hexadecimal
. https://www.youtube.com/watch?v=NHWjlCaIrQo&t=25
. http://adventofcode.com/2016