Skip to content

Commit 018ef41

Browse files
committed
Day 14, 2017 - Part I
1 parent 84868f9 commit 018ef41

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

2017/day10.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
class KnotHash:
44

5-
def __init__(self, nums, input):
5+
def __init__(self, input, nums = list(range(256)), dh = True):
66
self.nums = nums
77
self.input = input
88
self.pos = 0
99
self.skip_size = 0
10+
self.sparse_hash = self.sparse_hash()
11+
if dh:
12+
self.hash = self.dense_hash(self.sparse_hash)
1013

11-
def dense_hash(self):
12-
sh = self.sparse_hash()
14+
def __repr__(self):
15+
return self.hash
16+
17+
def dense_hash(self, sh):
1318
dh = [None] * 16
1419
for i in list(range(16)):
1520
section = [sh[j] for j in list(range(i*16, (i+1)*16))]
@@ -61,12 +66,14 @@ def string_ascii_converter(self, string):
6166
if __name__ == '__main__':
6267
test_data = '3,4,1,5'
6368
test_nums = [0, 1, 2, 3, 4]
64-
print(KnotHash(test_nums, test_data).knot_hash_simple())
69+
kh = KnotHash(test_data, test_nums, False)
70+
print(kh.sparse_hash)
6571

6672
input = '189,1,111,246,254,2,0,120,215,93,255,50,84,15,94,62'
67-
hashed = KnotHash(list(range(256)), input).knot_hash_simple()
73+
kh = KnotHash(input)
74+
hashed = kh.sparse_hash
6875
print(hashed[0] * hashed[1])
6976

70-
kh = KnotHash(list(range(256)), input)
71-
print(kh.dense_hash())
77+
print(KnotHash(input))
78+
7279

2017/day14.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# key is vbqugkhl
3+
#
4+
# python -m day14 vbqugkhl
5+
6+
import sys
7+
import os
8+
9+
from day10 import KnotHash
10+
11+
def solve(input):
12+
total = 0
13+
for i in list(range(128)):
14+
key = input + '-' + str(i)
15+
row = knot_hash_row(key)
16+
total += row_sum(row)
17+
return total
18+
19+
def row_sum(row):
20+
total = 0
21+
return sum([int(c) for c in list(row)])
22+
23+
def knot_hash_row(row):
24+
hashed = list(KnotHash(row).hash)
25+
return ''.join([binchar(h) for h in hashed])
26+
27+
def binchar(char):
28+
return format(int(char, 16), '0>4b')
29+
30+
if __name__ == '__main__':
31+
print(solve(sys.argv[1]))

0 commit comments

Comments
 (0)