Browse Source

Day 5

master
Chris Smith 5 years ago
parent
commit
f081393751
2 changed files with 21 additions and 0 deletions
  1. 20
    0
      05.py
  2. 1
    0
      data/05.txt

+ 20
- 0
05.py View File

@@ -0,0 +1,20 @@
1
+from functools import partial, reduce
2
+
3
+with open('data/05.txt', 'r') as file:
4
+    polymer = map(ord, file.readline().strip())
5
+
6
+
7
+def react(ignored, head, n):
8
+    if n == ignored or n ^ ignored == 32:
9
+        return head
10
+    elif not head:
11
+        return [n]
12
+    elif head[-1] ^ n == 32:
13
+        return head[0:-1]
14
+    else:
15
+        return head + [n]
16
+
17
+
18
+reacted = reduce(partial(react, 0), polymer, [])
19
+print(len(reacted))
20
+print(min(len(reduce(partial(react, i), reacted, [])) for i in range(ord('A'), ord('Z'))))

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