Day 6

Part 1

  • Read in input file
with open("day6/input.txt") as f:
    input = f.read()

First 100 lines of input looks like this:

bgdbdsbsbsttldddzzwnzzmpzmmzmqqcgglrglgbbbtmtddrssjtjqqtrtqtqppcvcddswdwbwlblfljfljlhhpchcfcgfcfwfll
sample = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
  • Create a function that detects the start of the packet
def marker_count(input):
    window = []
    for i,o in enumerate(input):
        window.insert(0,o)
        if len(window) > 4:
            window.pop()
        if len(set(window)) == 4: 
            return i + 1
    return None

print(marker_count(sample))
7
print(f'the correct answer for part 1 is {marker_count(input)}')
the correct answer for part 1 is 1794

Part 2

sample2 = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
  • Modify the marker to count 14 instead of 4
def marker_count2(input):
    window = []
    for i,o in enumerate(input):
        window.insert(0,o)
        if len(window) > 14:
            window.pop()
        if len(set(window)) == 14: 
            return i + 1
    return None

print(marker_count2(sample2))
19
print(f'the correct answer for part 2 is {marker_count2(input)}')
the correct answer for part 2 is 2851
print(f'the correct answer for part 1 is')
the correct answer for part 1 is