Solutions to Advent of Code 2017 https://adventofcode.com/2017/
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.

08.py 984B

12345678910111213141516171819202122
  1. import re
  2. from collections import defaultdict
  3. import operator
  4. REGEX = r'^(?P<target>.*?) (?P<op>inc|dec) (?P<amount>.*?) if (?P<operand>.*?) (?P<comparator>.*?) (?P<value>.*?)(\s|$)'
  5. comparators = {'>': operator.gt, '>=': operator.ge, '==': operator.eq, '!=': operator.ne, '<': operator.lt, '<=': operator.le}
  6. operators = {'inc': 1, 'dec': -1}
  7. def evaluate(instructions):
  8. registers = defaultdict(lambda: 0)
  9. for instruction in instructions:
  10. if comparators[instruction['comparator']](registers[instruction['operand']], int(instruction['value'])):
  11. registers[instruction['target']] += operators[instruction['op']] * int(instruction['amount'])
  12. yield dict(registers)
  13. with open('data/08.txt', 'r') as file:
  14. instructions = [re.search(REGEX, line).groupdict() for line in file.readlines()]
  15. print(f'Part one: {max(list(evaluate(instructions))[-1].values())}')
  16. print(f'Part two: {max(max(regs.values()) for regs in evaluate(instructions))}')