-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14-part1.py
60 lines (49 loc) · 1.35 KB
/
day14-part1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import re
def read(xs, pos, length):
ys = xs + xs
pos = pos % len(xs)
return ys[pos:pos+length]
def write(xs, pos, ys):
for i, y in enumerate(ys):
xs[(i + pos) % len(xs)] = y
def hash1(lengths, xs, pos, skip):
xs = xs + [] # Make a copy.
for length in lengths:
assert length < 256
write(xs, pos, reversed(read(xs, pos, length)))
pos += length + skip
skip += 1
return xs, pos, skip
def groups(xs, n):
"Ignores leftovers."
xss = []
for k in range(len(xs) // n):
xss.append(xs[k * n : (k + 1) * n])
return xss
def xor_all(xs):
r = 0
for x in xs:
r ^= x
return r
def knot_hash(key_string):
lengths = map(ord, key_string) + [17, 31, 73, 47, 23]
xs = range(256)
pos = 0
skip = 0
for _ in range(64):
(xs, pos, skip) = hash1(lengths, xs, pos, skip)
dense = map(xor_all, groups(xs, 16))
hash = ""
for value in dense:
hash += "%02x" % value
return hash
def hex_digit_to_binary(hex_digit):
return ('000' + bin(int('0x' + hex_digit, 0))[2:])[-4:]
input = sys.stdin.readline().strip()
key_strings = list(input + "-" + str(d) for d in range(128))
binary = ""
for key_string in key_strings:
for digit in knot_hash(key_string):
binary += hex_digit_to_binary(digit)
print len(re.findall('1', binary))