Day 6 – Fixing the Communicator

I’m not real sure what’s up here, but today’s project was easy. Like… stupidly easy. Maybe it’s just something better suited to how Python Handles data? The Part 2 was even easier. It literally just involved duplicating the same function, changing one number, and running the code.

The Problem Part 1:

As you move through the dense undergrowth, one of the Elves gives you a handheld device. He says that it has many fancy features, but the most important one to set up right now is the communication system.

However, because he’s heard you have significant experience dealing with signal-based systems, he convinced the other Elves that it would be okay to give you their one malfunctioning device – surely you’ll have no problem fixing it.

As if inspired by comedic timing, the device emits a few colorful sparks.

To be able to communicate with the Elves, the device needs to lock on to their signal. The signal is a series of seemingly-random characters that the device receives one at a time.

To fix the communication system, you need to add a subroutine to the device that detects a start-of-packet marker in the datastream. In the protocol being used by the Elves, the start of a packet is indicated by a sequence of four characters that are all different.

The Problem Part 2:

Your device’s communication system is correctly detecting packets, but still isn’t working. It looks like it also needs to look for messages.

A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4.

So, yeah, the input is a long ass string of garbled characters. For part 1, it’s a simple matter of running along the string in 4 character chunks, and using count() to see if all 4 characters are unique. For Part 2, it’s literally the same thing, except it’s checking 14 character strings instead of 4.

The hardest part was figuring out how much to add to the output to make sure it was int he correct position, because I was off by one, because my money brain frequently gets confused on if I am counting from 0 or counting from 1.

from Day06Input import *

def check_string(string):
    for n in string:
        # print(n)
        if string.count(n) > 1:
            return False
    return True

def check_signal(signal):
    for i in range(0,len(signal)-3):
        if check_string(signal[i:i+4]):
            return i+4

def check_message(signal):
    for i in range(0,len(signal)-3):
        if check_string(signal[i:i+14]):
            return i+14

print(check_signal(signal))
# Low (Probably by 1
#1080 - Yep!
print(check_message(signal))
# 3645