41 lines
985 B
Python
41 lines
985 B
Python
![]() |
#!/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 += rep*line_length(match.group(4)[:size])
|
||
|
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)
|