day 09
This commit is contained in:
40
09/part01.py
Executable file
40
09/part01.py
Executable file
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
import argparse
|
||||
|
||||
# 11038 - too low
|
||||
|
||||
RE_COMPRESS= re.compile(r'^([^\(]*)\((\d+)x(\d+)\)(.*)$')
|
||||
|
||||
def load_file(filename):
|
||||
with open(filename) as f:
|
||||
for line in f:
|
||||
yield line.strip()
|
||||
|
||||
def line_length(data):
|
||||
length = 0
|
||||
match = RE_COMPRESS.search(data)
|
||||
while match:
|
||||
size = int(match.group(2))
|
||||
rep = int(match.group(3))
|
||||
length += len(match.group(1))
|
||||
length += size * rep
|
||||
data = match.group(4)[size:]
|
||||
match = RE_COMPRESS.search(data)
|
||||
length += len(data)
|
||||
|
||||
return length
|
||||
|
||||
def main(args):
|
||||
length = 0
|
||||
for LINE in load_file(args.input):
|
||||
length += line_length(LINE)
|
||||
print(length)
|
||||
|
||||
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