DEV Community


Posted on • Updated on


Advent of Code 2020: Python Solution Day 9

This challenge is the easiest one I encountered on this season. And I was able to find a working algorithm. Still I am not even on top 7k. I started with copying test example and pasting on day9_test.txt and my input on day9.txt both are at same directory as my notebook is.

Lets share your solution too.

Challenge 1

with open("day9.txt", "r") as fp:
    lines = [int(line.rstrip()) for line in fp.readlines()]
# lines

def challenge1(codes, premable):
    previous_stack = []
    i = 0
    start = 0
    end = start+premable
    curr_index = premable
    while curr_index < len(codes)-1:
        stack = codes[start:end]
        if curr_index > len(stack)-1:
            valid = False
            for i in stack[:-1]:
                for j in stack[1:]:
                    #print(codes[curr_index], i, j)
                    if i+j == codes[curr_index]:
                        valid = True
                        valid == False
                if valid:
            if valid == False:
                return codes[curr_index]

#         print(stack)
#         break

challenge1(lines, 25)
Enter fullscreen mode Exit fullscreen mode

Challenge 2

Used NumPy for taking sum faster.

import numpy as np
def challenge2(codes, invalid_num):
    contigous_list = []

    for i in range(0, len(codes)-1):
        for j in range(1, len(codes)-1):
            stack = np.array(codes[i:j])
            if np.sum(stack) == invalid_num:
                print(stack, stack.min()+stack.max())
#             print(stack)
challenge2(lines, challenge1(lines, 25))
Enter fullscreen mode Exit fullscreen mode

I write blogs about Computer Vision projects on my GitHub page and if you got some time please share yours too.

Top comments (3)

philippurwoko profile image
Philip Purwoko

You can use class (OOP style) to get better management of your code and make it more readable. Here is my solution

with open('input.txt') as file:
    preamble = list(map(lambda x: int(x[:-1]), file.readlines()))

class Xmas:
    def __init__(self, preamble):
        self.preamble = preamble

    def is_valid(self, int):
        for i in self.preamble[:-1]:
            for j in self.preamble[1:]:
                if i + j == int:
                    return True
        return False

    def shift(self, number):
        self.preamble = self.preamble[1:]

    def find_weakness(self, invalid):
        not_found = True
        for step in range(2, len(self.preamble)+1):
            start = 0
            array = True
            while array and not_found:
                array = self.preamble[start:step]
                if sum(array) == invalid:
                    print(f'Weakness : {min(array)+max(array)}')
                    not_found = False
                start += 1
                step += 1

# Part 1
preamble_scale = 25
first_invalid = 0
xmas = Xmas(preamble[:preamble_scale])
for i in preamble[preamble_scale:]:
    if not xmas.is_valid(i):
        first_invalid = i
        print(f'{i} is The First Invalid')

# Part 2
xmas = Xmas(preamble)

Enter fullscreen mode Exit fullscreen mode
qviper profile image

It is so neat an clear. Thank you for sharing.

philippurwoko profile image
Philip Purwoko

You are welcome