aocpy/2016/d4.py
2024-03-27 08:06:27 -04:00

45 lines
1.1 KiB
Python

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)