Advent of Code: Day 14

For Day 14, the challenge is to generate a sort of map by generating hashes, and then count the number of contiguous regions in that map. My Python solution is:

import Queue
from day10 import hash

with open("input.txt", "r") as o:
    key = o.read().strip()

ones = []

for i in range(128):
    h = hash(256, "%s-%s" % (key, i))
    l = int(h, 16)
    ones += [(i, x) for x in range(128) if (l >> x) & 1]

part1 = len(ones)
regions = 0
queue = Queue.Queue()
while len(ones) > 0:
    queue.put(ones[0])
    regions += 1
    while not queue.empty():
        n = queue.get()
        if n in ones:
            ones.remove(n)
            queue.put((n[0] - 1, n[1]))
            queue.put((n[0] + 1, n[1]))
            queue.put((n[0], n[1] - 1))
            queue.put((n[0], n[1] + 1))


print "Part 1", part1
print "Part 2", regions

It uses the hash method from my solution to day 10. My first solution stored everything in a two dimensional array but I simplified it to the queue version so I only need to store the (relatively few) ones.

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.