Browse Source

Day 10.

master
Chris Smith 6 years ago
parent
commit
7204e3fc9a
2 changed files with 41 additions and 0 deletions
  1. 40
    0
      10.py
  2. 1
    0
      data/10.txt

+ 40
- 0
10.py View File

@@ -0,0 +1,40 @@
1
+import functools
2
+import itertools
3
+import operator
4
+
5
+
6
+def reverse_slice(input, start, length):
7
+    output = list(input)
8
+    for i, pos in enumerate(range(start, start + length)):
9
+        output[pos % len(output)] = input[(start + length - i - 1) % len(output)]
10
+    return output
11
+
12
+
13
+def do_rounds(lengths, count=1):
14
+    position, skip, data = 0, 0, list(range(256))
15
+    for _ in range(count):
16
+        for length in lengths:
17
+            data = reverse_slice(data, position, length)
18
+            position += length + skip
19
+            skip += 1
20
+    return data
21
+
22
+
23
+def group(n, iterable):
24
+    # https://docs.python.org/3.1/library/itertools.html#recipes
25
+    args = [iter(iterable)] * n
26
+    return itertools.zip_longest(*args)
27
+
28
+
29
+with open('data/10.txt', 'r') as file:
30
+    lengths = list(map(int, file.readline().strip().split(',')))
31
+    data = do_rounds(lengths)
32
+    print(f'Part one: {data[0]*data[1]}')
33
+
34
+
35
+with open('data/10.txt', 'r') as file:
36
+    lengths = list(map(ord, file.readline().strip())) + [17, 31, 73, 47, 23]
37
+    sparse_hash = do_rounds(lengths, 64)
38
+    dense_hash = [functools.reduce(operator.xor, g) for g in group(16, sparse_hash)]
39
+    # Yo dawg, I heard you like f-strings, so we put an f-string in your f-string.
40
+    print(f'Part two: {"".join(f"{c:02x}" for c in dense_hash)}')

+ 1
- 0
data/10.txt View File

@@ -0,0 +1 @@
1
+206,63,255,131,65,80,238,157,254,24,133,2,16,0,1,3

Loading…
Cancel
Save