Solve 2016 days 4 and 5.
This commit is contained in:
44
2016/d4.py
Normal file
44
2016/d4.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from lib import *
|
||||||
|
|
||||||
|
second = True
|
||||||
|
data = open(0).read()
|
||||||
|
|
||||||
|
|
||||||
|
def decipher(cipher, shift):
|
||||||
|
clear = ""
|
||||||
|
for c in cipher:
|
||||||
|
if c == "-":
|
||||||
|
nc = "-"
|
||||||
|
else:
|
||||||
|
nc = LETTERS_LOWER[(LETTERS_LOWER.index(c) + shift) % len(LETTERS_LOWER)]
|
||||||
|
clear += nc
|
||||||
|
return clear
|
||||||
|
|
||||||
|
|
||||||
|
if not second:
|
||||||
|
s = 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
hash, key = line.split("[")
|
||||||
|
key = key.replace("]", "")
|
||||||
|
|
||||||
|
counts = {}
|
||||||
|
for c in LETTERS_LOWER:
|
||||||
|
counts[c] = hash.count(c)
|
||||||
|
|
||||||
|
counts = sorted(list(zip(counts.values(), counts.keys())), key=lambda t: (t[0], -ord(t[1])), reverse=True)
|
||||||
|
|
||||||
|
real = True
|
||||||
|
for i, (_, c) in enumerate(counts[:5]):
|
||||||
|
if c != key[i]:
|
||||||
|
real = False
|
||||||
|
if real:
|
||||||
|
s += int(hash.split("-")[-1])
|
||||||
|
print(s)
|
||||||
|
else:
|
||||||
|
for line in data.splitlines():
|
||||||
|
line = line[:-7]
|
||||||
|
hash = "-".join(line.split("-")[:-1])
|
||||||
|
shift = int(line.split("-")[-1])
|
||||||
|
cleartext = decipher(hash, shift)
|
||||||
|
if "north" in cleartext and "pole" in cleartext:
|
||||||
|
print(shift)
|
||||||
30
2016/d5.py
Normal file
30
2016/d5.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from lib import *
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
second = True
|
||||||
|
data = open(0).read().strip()
|
||||||
|
|
||||||
|
if second:
|
||||||
|
p = [" " for _ in range(8)]
|
||||||
|
for i in range(10**12):
|
||||||
|
s = data + str(i)
|
||||||
|
h = hashlib.md5(s.encode()).hexdigest()
|
||||||
|
if h.startswith("0" * 5) and h[5] in "01234567":
|
||||||
|
index = int(h[5])
|
||||||
|
if p[index] == " ":
|
||||||
|
p[index] = h[6]
|
||||||
|
# print("\r" + "".join(p))
|
||||||
|
if not " " in p:
|
||||||
|
p = "".join(p)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
p = ""
|
||||||
|
for i in range(10**12):
|
||||||
|
s = data + str(i)
|
||||||
|
h = hashlib.md5(s.encode()).hexdigest()
|
||||||
|
if h.startswith("0" * 5):
|
||||||
|
p += h[5]
|
||||||
|
if len(p) == 8:
|
||||||
|
break
|
||||||
|
|
||||||
|
print(p)
|
||||||
@@ -36,4 +36,5 @@ written in Python.
|
|||||||
- Day 1: 29:00 That was emberassingly slow. Out of my rhythm?
|
- Day 1: 29:00 That was emberassingly slow. Out of my rhythm?
|
||||||
- Day 2: 13:24 Getting back into it but still slow af, obviously.
|
- Day 2: 13:24 Getting back into it but still slow af, obviously.
|
||||||
- Day 3: 11:20 Ugly and slow.
|
- Day 3: 11:20 Ugly and slow.
|
||||||
- Day 4:
|
- Day 4: 21:05 -__-
|
||||||
|
- Day 5: 29:35 -___-
|
||||||
|
|||||||
Reference in New Issue
Block a user