diff --git a/2015/04/4.md b/2015/04/4.md new file mode 100644 index 0000000..f4e7b9d --- /dev/null +++ b/2015/04/4.md @@ -0,0 +1,40 @@ +## \-\-- Day 4: The Ideal Stocking Stuffer \-\-- + +Santa needs help [mining](https://en.wikipedia.org/wiki/Bitcoin#Mining) +some AdventCoins (very similar +to [bitcoins](https://en.wikipedia.org/wiki/Bitcoin)) to use as gifts +for all the economically forward-thinking little girls and boys. + +To do this, he needs to find [MD5](https://en.wikipedia.org/wiki/MD5) +hashes which, in +[hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal), start with at +least *five zeroes*. The input to the MD5 hash is some secret key (your +puzzle input, given below) followed by a number in decimal. To mine +AdventCoins, you must find Santa the lowest positive number (no leading +zeroes: `1`, `2`, `3`, \...) that produces such a hash. + +For example: + +- If your secret key is `abcdef`, the answer is `609043`, because the + MD5 hash of `abcdef609043` starts with five zeroes + (`000001dbbfa...`), and it is the lowest such number to do so. +- If your secret key is `pqrstuv`, the lowest number it combines with + to make an MD5 hash starting with five zeroes is `1048970`; that is, + the MD5 hash of `pqrstuv1048970` looks like `000006136ef...`. + +Your puzzle answer was `254575`. + +## \-\-- Part Two \-\-- {#part2} + +Now find one that starts with *six zeroes*. + +Your puzzle answer was `1038736`. + +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2015) and +try another puzzle. + +Your puzzle input was `bgvyzdsv`{.puzzle-input}. + diff --git a/2015/04/solution.py b/2015/04/solution.py new file mode 100644 index 0000000..e59eb31 --- /dev/null +++ b/2015/04/solution.py @@ -0,0 +1,73 @@ +#!/bin/python3 +import sys,re,hashlib +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int + +input_f = 'input' + +part = 2 +######################################### +# # +# Part 1 # +# # +######################################### + +if part == 1: + result = 0 + count = 0 + found = False + with open(input_f) as file: + for line in file: + tmp = line.rstrip() + while found == False: + m = hashlib.md5() + text = tmp + str(count) + #print(text) + m.update(text.encode('UTF-8')) + nr = m.hexdigest() + #print(nr) + + if nr[0:5] == '00000': + print(count) + #print(text) + #print(nr) + found = True + else: + count += 1 + #print(nr[ 0 : 5 ]) + found = False + + + + +######################################### +# # +# Part 2 # +# # +######################################### +if part == 2: + result = 0 + count = 0 + found = False + with open(input_f) as file: + for line in file: + tmp = line.rstrip() + while found == False: + m = hashlib.md5() + text = tmp + str(count) + #print(text) + m.update(text.encode('UTF-8')) + nr = m.hexdigest() + #print(nr) + + if nr[0:6] == '000000': + print(count) + #print(text) + #print(nr) + found = True + else: + count += 1 + #print(nr[ 0 : 5 ]) + found = False +