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