2016 - Day 5 (How About a Nice Game of Chess?)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

#Advent of Code
#2016 Day 5, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from hashlib import md5

def solve(puzzle_input):
    characters_found = 0
    password = ""
    current_index = 0

    while characters_found < 8:
        current_hash = md5(str(puzzle_input + str(current_index)).encode("utf-8")).hexdigest()
        if len(current_hash) > 5 and current_hash[:5] == "0"*5:
            password += current_hash[5]
            characters_found += 1
        current_index += 1

    return password

def main():
    puzzle_input = util.read.as_string()

    password = solve(puzzle_input)

    print("The password is " + password + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("abc"), "18f47a30")

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3

#Advent of Code
#2016 Day 5, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from hashlib import md5
from random import choice
from string import ascii_letters, punctuation

def solve(puzzle_input):
    print()

    def printpass(replace=True):
        if replace: print("\x1b[1A\x1b[2K", end="")
        print("The password is ", end="")
        for i in password:
            if i != "_":
                print(i, end="")
            else:
                print(choice(ascii_letters + punctuation), end="")
        print(".")

    password = ["_" for i in range(8)]
    current_index = 0
    printpass(False)

    while "_" in password:
        current_hash = md5(str(puzzle_input + str(current_index)).encode("utf-8")).hexdigest()
        if len(current_hash) > 5 and current_hash[:5] == "0"*5 and 0 <= int(current_hash[5], 16) <= 7 and password[int(current_hash[5], 16)] == "_":
            password[int(current_hash[5], 16)] = current_hash[6]
            printpass()
            current_index += 1
            continue
        if current_index % 15000 == 0:
            printpass()
        current_index += 1

    return "".join(password)

def main():
    puzzle_input = util.read.as_string()

    solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("abc"), "05ace8e3")

if __name__ == "__main__":
    run(main)
Return to solution index