This commit is contained in:
Peter Hudec 2018-01-21 15:41:37 +01:00
parent f4073c9846
commit 0d591796a6
Signed by: peter.hudec
GPG Key ID: 427BD558E277E410
6 changed files with 92 additions and 0 deletions

1
09/input.puzzle Normal file

File diff suppressed because one or more lines are too long

6
09/input.sample01 Normal file
View File

@ -0,0 +1,6 @@
ADVENT
A(1x5)BC
(3x3)XYZ
A(2x2)BCD(2x2)EFG
(6x1)(1x3)A
X(8x2)(3x3)ABCY

4
09/input.sample02 Normal file
View File

@ -0,0 +1,4 @@
(3x3)XYZ
X(8x2)(3x3)ABCY
(27x12)(20x12)(13x14)(7x10)(1x12)A
(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN

40
09/part01.py Executable file
View 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)

40
09/part02.py Executable file
View 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 += 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)

View File

@ -12,3 +12,4 @@
- **Day 6**: Signals and Noise
- **Day 7**: Internet Protocol Version 7
- **Day 8**: Two-Factor Authentication
- **Day 9**: Explosives in Cyberspace