Advent of Code: Day 6

For Day 6, the challenge is to redistribute values in an array until we find a loop in the process. My Python 2 solution is below:

def apply_redistribution(data):
    data = list(data)

    max_index = 0
    max_value = data[0]
    for i in range(len(data)):
        if data[i] > max_value:
            max_index = i
            max_value = data[i]

    to_distribute = max_value
    data[max_index] = 0
    i = max_index + 1
    while to_distribute > 0:
        data[i % len(data)] += 1
        i += 1
        to_distribute -= 1

    return tuple(data)

with open("input.txt", "r") as o:
    data = tuple([int(l.strip()) for l in o.read().split(" ") if l])

seen_states = {}
seen_states[data] = 0

data = apply_redistribution(data)
ticks = 1
while data not in seen_states:
    seen_states[data] = ticks
    data = apply_redistribution(data)
    ticks += 1
print "Ticks taken:", ticks, "loop size:", ticks - seen_states[data]

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

Show Comments

Get the latest posts delivered right to your inbox.