-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
97 lines (73 loc) · 1.94 KB
/
solution.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import binascii
input = open("data.txt").read()
inp = []
lv = []
def GetRange(a,b,l):
return [a[(x+b) % (a.__len__())] for x in range(l)]
def ReverseRange(a,b,l):
r = list(reversed(GetRange(a,b,l)))
for i in range(l):
a[(i+b) % (a.__len__())] = r[i]
return a
position = 0
skip = 0
def KnotHashRound():
global position
global lv
global inp
global skip
for i,x in enumerate(inp):
lv = ReverseRange(lv, position, x)
position += x + skip
skip += 1
def DenseHash(l):
h = list(range(16))
for x in range(16):
h[x] = l[(16*x)]^l[(16*x)+1]^l[(16*x)+2]^l[(16*x)+3]^l[(16*x)+4]^l[(16*x)+5]^l[(16*x)+6]^l[(16*x)+7]^l[(16*x)+8]^l[(16*x)+9]^l[(16*x)+10]^l[(16*x)+11]^l[(16*x)+12]^l[(16*x)+13]^l[(16*x)+14]^l[(16*x)+15]
q = list(map(hex,h))
q = [("0" if x.__len__()==3 else "")+x.replace("0x","") for x in q]
return "".join(q)
def KnotHash(s):
global inp
global lv
global position
global skip
lv = list(range(256))
inp = [ord(x) for x in s]
inp.extend([17, 31, 73, 47, 23])
lv = list(range(256))
position = 0
skip = 0
for i in range(64):
KnotHashRound()
return DenseHash(lv)
assert KnotHash("TEST") == "273d1ca62e42c54300605f37b66958c5"
rows = []
for i in range(128):
r = KnotHash(input+"-"+str(i))
b = (bin(int(r, 16))[2:])
rows.append( list( (128-b.__len__()) * "0" + b) )
# First part
cnt = [x.count("1") for x in rows]
print("First part: " + str(sum(cnt)))
# Second part
regions = 0
def ClearRegion(y,x):
global rows
if rows[y][x]=="0":
return False
rows[y][x] = "0"
if y>0:
ClearRegion(y-1, x)
if y<127:
ClearRegion(y+1, x)
if x>0:
ClearRegion(y, x-1)
if x<127:
ClearRegion(y, x+1)
return True
for y in range(128):
for x in range(128):
if ClearRegion(y,x):
regions += 1
print("Second part: " + str(regions))