adventofcode-2016/04/part02.py

65 lines
1.7 KiB
Python
Raw Normal View History

2018-01-16 10:44:38 +01:00
#!/usr/bin/env python
# look for: northpole object storage
import re
import argparse
RE_LINE = re.compile(r'^([a-z\-]+)\-(\d+)\[([a-z]+)\]$')
def load_file(filename):
with open(filename) as f:
for line in f:
yield line.strip()
def check_room_code(room):
chars = dict()
# count chars
for char in room['name']:
if char not in chars:
chars[char] = 0
chars[char] += 1
del chars['-']
# sort by
chars = [(k,v) for k,v in chars.items()]
chars = sorted(chars, key=lambda x: (-x[1], x[0]))
# get checksum
checksum = "".join([x[0] for x in chars])[0:5]
if checksum == room['checksum']:
return 1
return 0
def decrypt_room_name(room):
room_name = ""
shift = room['sector'] % 26
for char in room['name']:
if char == '-':
room_name = "{}{}".format(room_name, ' ')
else:
new = chr(((ord(char) - ord('a') + shift) % 26) + ord('a'))
room_name = "{}{}".format(room_name, new)
room['name_decrypted'] = room_name
def main(args):
for ROOM in load_file(args.input):
match = re.search(RE_LINE, ROOM)
if not match:
print("{} syntax error".format(ROOM))
continue
room_data = {
'name': match.group(1),
'sector': int(match.group(2)),
'checksum': match.group(3)
}
decrypt_room_name(room_data)
print(room_data)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01')
parser.add_argument('--input', '-i', action='store', required=True, help='input file')
args = parser.parse_args()
main(args)