Browse Source

Day 8

master
Chris Smith 6 years ago
parent
commit
374f7568d6
2 changed files with 1022 additions and 0 deletions
  1. 22
    0
      08.py
  2. 1000
    0
      data/08.txt

+ 22
- 0
08.py View File

@@ -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))}')

+ 1000
- 0
data/08.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save