task.txt (3446B)
1 --- Day 6: Tuning Trouble --- 2 3 The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove. 4 5 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. 6 7 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. 8 9 As if inspired by comedic timing, the device emits a few colorful sparks. 10 11 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. 12 13 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. 14 15 The device will send your subroutine a datastream buffer (your puzzle input); your subroutine needs to identify the first position where the four most recently received characters were all different. Specifically, it needs to report the number of characters from the beginning of the buffer to the end of the first such four-character marker. 16 17 For example, suppose you receive the following datastream buffer: 18 19 mjqjpqmgbljsphdztnvjfqwrcgsmlb 20 21 After the first three characters (mjq) have been received, there haven't been enough characters received yet to find the marker. The first time a marker could occur is after the fourth character is received, making the most recent four characters mjqj. Because j is repeated, this isn't a marker. 22 23 The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed. 24 25 Here are a few more examples: 26 27 bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 5 28 nppdvjthqldpwncqszvftbrmjlhg: first marker after character 6 29 nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 10 30 zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 11 31 32 How many characters need to be processed before the first start-of-packet marker is detected? 33 34 Your puzzle answer was 1566. 35 --- Part Two --- 36 37 Your device's communication system is correctly detecting packets, but still isn't working. It looks like it also needs to look for messages. 38 39 A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4. 40 41 Here are the first positions of start-of-message markers for all of the above examples: 42 43 mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19 44 bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23 45 nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23 46 nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29 47 zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26 48 49 How many characters need to be processed before the first start-of-message marker is detected? 50 51 Your puzzle answer was 2265.