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 too embarrassed to post my entire answer, but I parsed each part of the puzzle input one character at a time, appending it to a string of characters, and validating each character in the string was unique or not.

I assumed that the first N characters (N being equal to the message token length, so 4 for part A and 14 for part B), were not going to have uniques (imagine if the very first four numbers were all unique and the answer was just 4 lmao) so I read in N number of characters all at once, then from there, validated that string to see if there were any dupes, and then would push a new character to the end and pop off the front, and then validate the new string, repeat until you get N unique characters.

My validation algorithm is bare-bones stupid, literally just a for-loop nested inside another for-loop, checking each character one-by-one. This would be monstrously inefficient if the input or message-length was huge but hey, the point is to do it fast right?

Jump in the discussion.

No email address required.

I would recommend learning some low level computer architecture/digital systems.

Boolean logic, shift registers, any of which would remind you very quickly how much of a monstrously bad Idea what you did was and will make you a better coder.

But you did it, you perservered, so its not completely hopeless

Jump in the discussion.

No email address required.

Can you go into more detail about

>Boolean logic, shift registers

?

I've processed packets of data over a stream before, but usually you have the luxury of either knowing the packet size ahead of time (which this puzzle omits), and/or having a checksum to verify its integrity. And you're usually not blindly checking each byte against the other, and you can just memcpy it into a struct to fill out all your members.

Jump in the discussion.

No email address required.

I'm assuming you enjoy the low level aspects since youre working in c++

c++ has the unordered_set template which is the equivalent of what you've been seeing in other solutions so you could accomplish the same much faster.

usually you have the luxury of either knowing the packet size ahead of time

For the puposes of this problem, this is actually not important. You're only doing it for one file, and you are guaranteed that it will happen at some point. You could select all in notepad++ and check char count or just iterate infinitely through the stream and not give a frick.

And you're usually not blindly checking each byte against the other

From an optimization standpoint, you're redoing 90+% of your comparisons every time you shift in a new byte. You already know if the first n-1 values are unique, so you can either disregard entirely until that's resolved or only have to iterate through the array once to check the newest one

Granted none of this is ideal for just banging out a solution as fast as you can. Its just ideas for what would be better in a proper environment

Jump in the discussion.

No email address required.

Hi @JollyMoon,

Your comment has been automatically removed because you forgot to include trans lives matter.

Don't worry, we're here to help! We won't let you post or comment anything that doesn't express your love and acceptance towards the trans community. Feel free to resubmit your comment with trans lives matter included.

This is an automated message; if you need help, you can message us here.

Jump in the discussion.

No email address required.

or u could do it in three lines of python :marseyzoomer:

index = 0
while len(set([*message[index:index+MESSAGE_LENGTH]])) != MESSAGE_LENGTH: index+=1
print(index+MESSAGE_LENGTH)

trans lives matter ![](/i/chud/supportjews.webp)

Jump in the discussion.

No email address required.

what does the asterisk do

Jump in the discussion.

No email address required.

yeah I'm there with you, and unless you have access to those things (not all languages have the luxury of direct memory access) I don't see what the better siolutino would be. Possibly just read it all iinto memory and check a substring?

Jump in the discussion.

No email address required.

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