def __load(self, s): '''Creates a 128x128 grid of 0s and 1s using knot hashes''' # instantiate KnotHash object kh = KnotHash() # create empty list to hold grid grid = [] # perform row creation method 128 times for i in range(128): # reset KnotHash kh.reset() # create empty list to hold row binary_string = [] # create knot hash from key knot_hash = kh.create_hash('%s-%d' % (s, i)) # iterate through chars in knot hash and convert to binay for c in knot_hash: binary_string.append(hex_to_bin[c]) # append row to grid grid.append(list(map(int, ''.join(binary_string)))) return grid
def test_knot_hash(self, list_, inputs, output): knot_hash_list, _, _ = KnotHash(inputs)._single_round(list_) assert knot_hash_list == output
def test_str(self): assert str(KnotHash('AoC 2017')) == \ '33efeb34ea91902bb2f59c9920caa6cd' assert str(KnotHash('')) == 'a2582a3a0e66e6e86e3812dcb672a272'
from collections import deque from day10 import KnotHash from day12 import group_qty if __name__ == '__main__': print('Advent of Code 2017 - Day 14') INPUT = 'vbqugkhl' # part 1 disk_rows_hash = [] for row in range(128): lengths = KnotHash.get_lengths(f'{INPUT}-{row}') row = KnotHash(deque(range(256)), lengths, 0, 0) disk_rows_hash.append(row.final_knot_hash()) disk = [] def convert_to_bits(hex_digit): bits = bin(int(hex_digit, 16))[2:] bits = '0'*(4 - len(bits)) + bits return bits for row in disk_rows_hash: disk_row = '' for hex_digit in row: disk_row += convert_to_bits(hex_digit) disk.append(disk_row) used_squares = 0
def binary_knot_hash(input_): hexadecimal = [ str(KnotHash(row_input)) for row_input in row_strings(input_) ] binary = [''.join(map(binary_from_hex, row)) for row in hexadecimal] return binary
def test_knot_hash(): first_row_hexadecimal = str(KnotHash(row_strings(INPUT)[0])) first_row_binary = ''.join(map(binary_from_hex, first_row_hexadecimal)) assert first_row_binary.startswith('11010100')
def hash(prefix, row): key = "%s-%d" % (prefix, row) hash = int(KnotHash().update(key).digest(), 16) return [1 if hash & 1 << (127-b) else 0 for b in range(128)]