Browse Source

Day 12

master
Chris Smith 8 years ago
parent
commit
20ec9c8332
2 changed files with 29 additions and 0 deletions
  1. 28
    0
      12.py
  2. 1
    0
      12.txt

+ 28
- 0
12.py View File

@@ -0,0 +1,28 @@
1
+#!/usr/bin/python
2
+
3
+import json
4
+import re
5
+
6
+with open('12.txt', 'r') as file:
7
+    input = file.read()
8
+
9
+print("Part 1: %d" % sum(int(x) for x in re.findall('-?[0-9]+', input)))
10
+
11
+
12
+def process(obj):
13
+    if isinstance(obj, dict):
14
+        if 'red' in obj.values():
15
+            return 0
16
+        else:
17
+            return process(obj.values())
18
+
19
+    if isinstance(obj, list):
20
+        return sum(process(x) for x in obj)
21
+
22
+    if isinstance(obj, int):
23
+        return obj
24
+
25
+    return 0
26
+
27
+
28
+print("Part 2: %d" % process(json.loads(input)))

+ 1
- 0
12.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save