-
Notifications
You must be signed in to change notification settings - Fork 1
/
14a_aoc.py
75 lines (68 loc) · 1.38 KB
/
14a_aoc.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
grid = range(128)
M = 256
def main():
try:
while True:
process(raw_input())
except EOFError:
analyse_results()
def process(inp):
for i in range(128):
s = inp+'-'+str(i)
grid[i] = hash(s)
print "done, ",s
class List():
def __init__(self):
self.l = range(M)
self.currentPos = 0
self.skipSize = 0
def reverse(self, length):
elms = [self.l[(self.currentPos+x)%M] for x in range(length)]
elms.reverse()
for x in range(length):
self.l[(self.currentPos+x)%M] = elms[x]
self.currentPos += (self.skipSize + length)
self.currentPos %= M
self.skipSize += 1
def __repr__(self):
s = "["
for x in range(M):
if x == self.currentPos:
s+=" ("+str(self.l[x])+") "
else:
s+=" "+str(self.l[x])+" "
s += "] (%d)"%self.skipSize
return s
def hash(st):
L = List()
lengths = map(ord,st)
lengths += [17, 31, 73, 47, 23] # HARDCODED
for round in range(64):
for l in lengths:
L.reverse(l)
sparsehash = L.l
densehash = range(16)
i = 0
while i<256:
densehash[i/16] = reduce(lambda x,y:x^y,sparsehash[i:i+16])
i += 16
b = bin(int("".join(["%02x"%i for i in densehash]),16))
b = b[2:]
b = "0"*(128-len(b))+b
return b
def analyse_results():
m = 0
for l in grid:
m += l.count("1")
print l
print m
# out = ''
# for l in grid:
# for x in l:
# if x == '0':
# out += '.'
# elif x == '1':
# out += '#'
# out += '\n'
# print out
main()