I got told about this fantastic coding challenge site Advent of Code, which publishes a new pair of coding challenges every day from December 1st until Christmas Day.

Unfortunately I didn’t get the chance to start until January, but the challenges are still there at time of writing, so hopefully I will be able to make my way through them as I get time through 2021.

I will add my solutions below so you can compare; as always, it’s unlikely to be the most elegant answer but it works! I’ve only reproduced answers to part 2 of each challenge, which has always been a harder variant of part 1 so far.

Day 1 – Report Repair

#open text file
with open("C:\\aoc1.txt") as file:
    #create a list from lines in the file
    data = file.readlines()
    #strip whitespace and convert string to integer for maths
    data = [int(x.strip()) for x in data]
    #loop through every combo until match condition found
    for i in data:
        for j in data:
            for k in data:
                if i + j + k == 2020:
                    print(i * j * k)

Day 2 – Password Philosophy

#read in data from the file and create a list of lines
with open("C:\\aoc2.txt") as file:
    data = file.readlines()
    #create lists of three members, each containing specific parameter
    data = [x.split() for x in data]
    #initiate counter
    count = 0
    #loop through each parameter list
    for password in data:
        #from first parameter, split the numbers, convert to integers and assign vars
        params = password[0].split("-")
        low, high = int(params[0]), int(params[1])
        #set variable to search parameter
        target = password[1].strip(":")
        #set variable to parameter to search
        source = password[2]
        #set the match condition and perform the count
        if ((source[low-1] == target) or (source[high-1] == target)) and (source[low-1] != source[high-1]):
            count += 1  
    print(count)

Day 3 – Toboggan Trajectory

#open file and create a list of lines
with open("C:\\aoc3.txt") as file:
    data = file.readlines()
    data = [line.strip() for line in data]
    #create function taking in slope, a list of two integers
    def tree_counter(slope):
        #initialise function variables
        x,y,tree_count,column_count = 0,0,0,0
        #create a game field from input that will accomodate the slope
        while column_count < (slope[0] * len(data)):
            column_count += len(data[0])
        input_count = int(column_count / len(data[0]))
        i = 1
        new_data = data.copy()
        while i < input_count:
            for j in range(len(data)):
                new_data[j] += data[j]
            i += 1
        #move through the game field counting trees
        while y < len(new_data) - 1:
            x, y = x + slope[0], y + slope[1]
            if new_data[y][x] == '#':
                tree_count += 1
        return tree_count
    #run the function for provided inputs
    a1 = tree_counter([1,1])
    a2 = tree_counter([3,1])
    a3 = tree_counter([5,1])
    a4 = tree_counter([7,1])
    a5 = tree_counter([1,2])
    #calculate condition
    answer = a1 * a2 * a3 * a4 * a5
    print(answer)