Since 2010

Advent of Code 2020 day 10

Advent of Code 2020 day 10 part one. I haven’t got a clue what knowledge I’m missing for part two. I have code that works fine with the example inputs, but it will take days to run for my actual input.

Folk on the subreddit are talking about concepts I’ve never heard of. Heck, even looking at some of the solutions for part one have me scratching my head. Can’t wrapt my head around the maths.

I’ll try again tomorrow. Here’s my Python solution for part one though:

from pathlib import Path

def get_raw_input():
    return (Path(__file__).parent/'day_10_input.txt').read_text()

def parse_raw_input(raw_input):
    return [int(line.strip()) for line in raw_input.strip().splitlines()]

def candidate_adapters(adapters, current_rating):
    return [adapter for adapter in adapters if (adapter > current_rating and adapter - current_rating <= 3)]

def part_one(parsed_input):
    current_rating = 0 # Initialised to charging outlet
    differences = {1: 0, 2: 0, 3: 0}
    while len(parsed_input) > 0:
        candidates = candidate_adapters(parsed_input, current_rating)
        next_adapter = sorted(candidates)[0]

        difference = next_adapter - current_rating
        differences[difference] += 1

        current_rating = next_adapter


    differences[3] += 1 # For device's built-in adapter

    return differences

def distribution(differences):
    return differences[1] * differences[3]

if __name__ == '__main__':
    print(f'Part one: {distribution(part_one(parse_raw_input(get_raw_input())))}')