Unable to load image

Advent of Code day 6

was preparing my butthole after the last one but ended up being the fastest one i've done :marseyexcited:

with open("day6input.txt") as f:
    input = f.read()

# part 1

for i in range(len(input)):
    marker = set(input[i:i+4])
    if len(marker) == 4:
        answer = i + 4
        break

print(answer)

# part 2 

for i in range(len(input)):
    marker = set(input[i:i+14])
    if len(marker) == 14:
        answer = i + 14
        break

print(answer)
28
Jump in the discussion.

No email address required.

I'm not super familiar with what programming language you're using but "set" is a data structure with unique keys, right? and if you stick four characters in a set and any are dupes, the size of the container will be less than 4 (or 14)? Honestly I had that idea too at first but didn't know off the top of my head how to do that in C++ and I figured a dumbass manually-check-each-character algorithm would take my brain less time to understand. I like your answer a lot.


https://i.rdrama.net/images/1737673901UvHSoHNlCgDLyg.webp

Current HP: 8/75

Current Mana: 130/250

Inventory:

* 50 Gald

Jump in the discussion.

No email address required.

yeah, sets in python are basically just an unordered collection with no duplicates that have methods like intersection(), union() and issuperset() that are supposed to be analagous to operations on sets in mathematics. they have been pretty useful so far for AoC - used them for days 3 and 4 also.

Jump in the discussion.

No email address required.



Now playing: Bonus Room Blitz (DKC).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.