adventofcode

https://adventofcode.com/
Log | Files | Refs

puzzle14.py (2055B)


      1 binary_hash = {"0" : 0, "1" : 1, "2" : 1, "3" : 2, "4" : 1, "5" : 2, "6" : 2, "7" : 3, "8" : 1, "9" : 2, "a" : 2, "b" : 3, "c" : 2, "d" : 3, "e" : 3, "f" : 4}
      2 
      3 def round(this_input, this_list, skip, pos):
      4     for length in this_input:
      5         twist = reversed([this_list[i % len(this_list)] for i in range(pos[0], pos[0] + length)])
      6         for i, t in zip(range(pos[0], pos[0] + length), twist):
      7             this_list[i % len(this_list)] = t
      8         pos[0] += length + skip[0]
      9         skip[0] += 1
     10 
     11 def knot_hash(this_input):
     12     this_input.extend([17, 31, 73, 47, 23])
     13     my_list = [i for i in range(256)]
     14     skip, pos = [0], [0]
     15 
     16     for i in range(64):
     17         round(this_input, my_list, skip, pos)
     18 
     19     knot_hash = []
     20 
     21     for block in [my_list[i:i + 16] for i in range(0, len(my_list), 16)]:
     22         knot_hash.append(chr(reduce(int.__xor__, block)))
     23 
     24     return "".join(knot_hash).encode("hex")
     25 
     26 disk = [knot_hash([ord(c) for c in "amgozmfv-" + str(i)]) for i in range(128)]
     27 
     28 print sum([binary_hash[c] for c in "".join(disk)])
     29 
     30 def traverse(node, network):
     31     network.append(node)
     32     x_y = [int(n) for n in node.split(',')]
     33     neighbours = [str(x_y[0]) + ',' + str(x_y[1] - 1), str(x_y[0]) + ',' + str(x_y[1] + 1), str(x_y[0] - 1) + ',' + str(x_y[1]), str(x_y[0] + 1) + ',' + str(x_y[1])]
     34     for neighbour in neighbours:
     35         if neighbour not in network:
     36             if neighbour in disk_hash:
     37                 if disk_hash[neighbour]:
     38                     traverse(neighbour, network)
     39 
     40 network = []
     41 size_of_network = 0
     42 network_count = 0
     43 binary_disk = ["".join(["".join([str(int(int(c, 16) & 2**i > 0)) for i in reversed(range(4))]) for c in row]) for row in disk]
     44 disk_hash = {}
     45 
     46 for x in range(128):
     47     for y in range(128):
     48         disk_hash[str(x) + ',' + str(y)] = int(binary_disk[x][y])
     49 
     50 for node in disk_hash.keys():
     51     if disk_hash[node] and node not in network:
     52         traverse(node, network)
     53         if len(network) > size_of_network:
     54             size_of_network = len(network)
     55             network_count += 1
     56 
     57 print network_count