Advent of Code 2016 solutions https://adventofcode.com/2016/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

09.py 872B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/python3
  2. import re
  3. # Version of parse() that recursively parses repeated sections
  4. rparse = lambda x: parse(x, rparse)
  5. def parse(data, rfun=len):
  6. # Find the first bracketed part (if one exists)
  7. index, bracket = data.find('('), data.find(')')
  8. if index == -1:
  9. return len(data)
  10. # Grab the (NxR) values from the brackets
  11. num, reps = map(int, data[index + 1:bracket].split('x'))
  12. # Return the initial, non-repeated data, plus...
  13. return (len(data[:index])
  14. # the repeated data, potentially parsed recursively, plus...
  15. + reps * rfun(data[bracket + 1:bracket + 1 + num])
  16. # the remainder of the string, parsed
  17. + parse(data[bracket + 1 + num:], rfun))
  18. with open('data/09.txt', 'r') as file:
  19. input = re.sub('\s+', '', file.read())
  20. print(parse(input))
  21. print(rparse(input))