Advent of Code 2018

Below are some notes/solutions for Advent of Code 2018.

Day 1 - Part 1

The first part of the problem is basically summing up all the numbers in the input file. It can be solved in Python like this.

freq = sum([int(x) for x in day_input(1)])

print(freq)

Day 1 - Part 2

On the second part, we need to keep track of the changes to the frequency, and find out which one is repeated twice. The problem description also tells us that we might have to loop through the input data multiple times until it repeats.

def day1_part2():
    freq = 0
    seen_freq = set([0])
    while True:
        for change in day_input(1):
            freq += int(change)
            if freq in seen_freq:
                return freq
            seen_freq.add(freq)

print(day1_part2())

Day 2 - Part 1

On this puzzle we have a list of Box IDs, and we’re trying to calculate a checksum from those. The problem description says we should count how many boxes have two repeating letters, how many of them have three repeating letters and multiply these numbers together.

def check_repeat(box, count):
    for c in box:
        if box.count(c) == count:
            return True
    return False

total_two = sum(map(lambda box: check_repeat(box, 2), day_input(2)))
total_three = sum(map(lambda box: check_repeat(box, 3), day_input(2)))

print(total_two * total_three)

Comments BETA