day 04
This commit is contained in:
53
04/part01.py
Executable file
53
04/part01.py
Executable file
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
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 main(args):
|
||||
sector_sum = 0
|
||||
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)
|
||||
}
|
||||
sector_sum += check_room_code(room_data)*room_data['sector']
|
||||
|
||||
print(sector_sum)
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user