From d46cca4ccf9c131116abe36b0080648375432ad0 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 6 Dec 2022 14:38:55 +0100 Subject: [PATCH] day6 part 1 --- day6/README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ day6/__init__.py | 0 day6/input.txt | 1 + day6/main.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 day6/README.md create mode 100644 day6/__init__.py create mode 100644 day6/input.txt create mode 100644 day6/main.py diff --git a/day6/README.md b/day6/README.md new file mode 100644 index 0000000..8863c72 --- /dev/null +++ b/day6/README.md @@ -0,0 +1,44 @@ +# Day 6: Tuning Trouble + +The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star +fruit grove. + +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 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. + +For example, suppose you receive the following datastream buffer: + +`mjqjpqmgbljsphdztnvjfqwrcgsmlb` + +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. + +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. + +Here are a few more examples: + +- `bvwbjplbgvbhsrlpgdmjqwftvncz`: first marker after character **5** +- `nppdvjthqldpwncqszvftbrmjlhg`: first marker after character **6** +- `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`: first marker after character **10** +- `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`: first marker after character **11** + +How many characters need to be processed before the first start-of-packet marker is detected? \ No newline at end of file diff --git a/day6/__init__.py b/day6/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/day6/input.txt b/day6/input.txt new file mode 100644 index 0000000..ed57bcd --- /dev/null +++ b/day6/input.txt @@ -0,0 +1 @@ +tnmmpfmfzmmnsmsjmjjbvvhnhzzfmmgpmgpgbgnnwffjhffzqqmzzbnbssrqqrnnhsnngsszsqzszhzfhzfzwzfzrrmhmghgwhhjjqwqttwhttjllrtrtzzcfzfgzznfznfzfnnbddvmvzmmfsmfsmfffhlfldlqqrnrznnhmmgqqzhhmjhmhppqbpbbngnlldvvdqvvrtrdrtrnttnppfllrbbrprpnpdplpmllhwwddqpdprddzzfccqpcqpcpcbbdhdjdjwjcwcctdcttzgzmmscmsmdmttwhwzhhnjhnhlhvhlvlglpgpmmjmgmrgrddmwddjfftfwflfslffqtfqttpftppflfmmhvhvcvbvhbhggpbgbppvdpvpvfppbwwsnnhphllbdbnbvbmvvzffvsffdldmlmtmccnlnbnjbnjnhhbfhhgzzlwlfflzffdccggdcgcjjhffjfgfgcczjccvwcvvqgvvqvllqzqmqllhjjqnqggttsdddjgdjgjzzrgrfrbrssrgrgdgrgbbssmdsdfddsndnsdnsdnnmqqsspqqmrqqpmmsjmmszzqvqrvrzznnjdndtntfnttgtctqtwwnwswrrthrttsdttlhlvvdzzgqgttnppjpljplpgpvgvqqvppzmmqggtjgtgstslltjltjjgcjcmjmsshvvtppgmmlslqqshqshsllbggfpgffdsdgssncchctcwwtllgqlqblqlqvvmsvmmwnnzppqllsttgmttftvfvjjrzzswzzjvzjzljjchcshcscbbrdbrbcrrnvvtctntvtvbvjvqjqggsrspsprrbgghdghhmwwldldzdttrvrnrfftqtftrrdsszlzvvbtbffftzzrzqrrhjhghhwbhhsjsfsttdjdjnjhjmjpmplplrrdjdcdjdbjblllbqlqdlqlpqptppdhhqmqfqhqhchwwqjqfjqfqhqshsmswsbbvssdspdpsdssstntltrrgnnmttgmmsjjrlrnlrnrnwrwfwlfltlzllcjcmjcjpjhphcpcwppmvmjjzbzvbbfnfcflfddntddbmmmhnnsrnrrvdvnvcvwvcwvwrrqwqccqmmswmmjrjmmwjmjfjhwrtbjzdvlgrjmvzfmhcqsncvlhzzncjlbvcwrdwjmqjcnptqslvfzpsvltgzsvjdsjrppdrmqrbqwhddfhnftfblspsrhtdtjwdnhbcbtlwlvccsfscvczzrrqmwbwbdmwgzqntvflppqvppwrhnvtlsbzqglhsfdgssqzdtjdpwrrhbnbtwhhnmnlwfwlqffjjrndbpwwsvdrhddbjnnqzmtpvvtwbcpndjzlhcfrrdvmljswjzvmfqcdsgqwclqshwrmblszdvsnrpdgnllmlchzdjlrrpndmmgddjqgjqrhwfbwddqdfbvptrmzhtsqfsfswpnvmtswqprjhbzvntgrlzthhnqbtpplqpvcfnpgdtbhqbhflltbbtmmhcwztslmpznttmssclhmnbsbrwlblrbsdfmnpqbwwmsncvzmpqwhzjgcgdrzvglgdtswmstdhrprdjfmqtjlmplbjtzcgnrwpdvpfjjfwjfnnpmdtwtqsgfndngsbmcwjtglqwtfrclbczfcmjtgcwszhzrbcphrhwmhcwghjznzthnwpljjltdlvqtffsrbmwcsvrdmqqggbznnlzbbqtgspqvnjpbdhtzmgttrcwwszwpgdrcnfqtgrgqdrctlzwtdwqppbhnwgldnqltznnfpbfqtgmmwpcqnndbgmrrtgtvnmlfcwsldchjnnqfrhpzwtclrzftsqllgvpqbgmfjdhqjttwcvbpvfqsvhbhhtwnqnbgndbtzhcvgglbhghbzrbrmdllmgfgttqmhtdnwrpwllhnghrjctrbzrcpnjnctvmrlpjhftnfbczrjrnnbqplplcrbngbhvmmvcffmgvbhjzbhcmtwmwgmjmwjvvlqfldswpntjnsjvmdlbzqqlgbwspwvmnwtwjbczmwplrhmjgsppnmtwmvsfwnsgddgwqcvpftcpzrhpldnwmcjgtjmljjbcmjcqdbwczndnjnjgrmtjrqnnjndzqdqpcgdqptdbrqftnwrgqmrzrvsfmmmbpltlncvtgrjfjmvtgwqphczwjhdrdwtfvgztbhrndvpcbgfjfvmrrljwrvcrtdmtjndfnwgcnfrzgsnjpztbwwsbvqfnpjctgrhsflhnzbbsfqbnmtnvrmjzsbjfndvttpvpfjhqntflgbfnzcclcwmhbsgqfjdcgsvrhtstspfzgvgglgddqmclsmzgzgtncdsfmwdvtcsgwvbzjvclwppqdjgfcrcbzcwbdhrnssjbmnmfmwthdrnmlfhqlddwqrdhsdvdcsmcgjsgcmpnhlbnqftpdjswtmpbznlcrhtswgnmwjcdfmljdngzfsmlzjjnzmfzshmztdbdmcqwmlvcrzgpmbjqcghclwvdbrhgvwqchnndftnrtptmctdlhmfjvpzrpccddfpcdwmzqfhnsqzrvwblzfhcjdcjfctczwqrcbjnrpdcbbnsgnlvqqmnsfgsqschjlbzhhsrbvdbfrhvsgrlzwncgwpdbvmblgzbwbcbgqfwmdmgcrbbjfcvmqgztqpptdhwmvmsdqwplpgcjzgqzdrftzhqbltvhrmlrfffcgfpqzwrrbbtlsjgmtbjvtnmhwdpjptjwfwgjgvbfqwmflrrqzlzdcmtlnptdrpcpdnswcfscnndnrfbgwvvncdjgsdpbwptdtvrqlmrhmvvcwblhhzbjdpsbszhrftfbcgwhwrgglnjzqdhcqnvlhgqjhnddvrslhntssptsbhmqwwqqnbvfmcbgpvgjbrttnvlljdbtfplgmbwtcbcdtqdpqqdvhbmpmtszwpzblcfrtznhhtcljtdlhjdbnlhvwgjsmgvrslrfwnmzwlstpgltvrgnpdqztvfnvdhdtwwqdfsmtpbpdclsbnwcgjzchjcsjmvhbjshmjjlpgdzcgbmmchwmcsddsvhsnpqtcpnhqnbvwgwqhtjbqncgwwftnrzsbsjtvqmjzqvvncmncwflcfpcjqgdtbsmjzzsdjfvhnqbgjhmfgjghwscthbfmbndltbqzwpqtmrswvprpmgwqnqpfnmffrpdlpfqmhrthppzvzwbrtjvwvjndsqdlqtbpqwfcttggnjmcqqnmjwfhfjgcvlnmtlgbdvmctzlwbfgnflwtsflgnfbnfbhhdgjctzvvmrhdsmvmmtnqwtszmqcpsbrqrgjfrzctcbzmtdlhwjtfdqbtthdnqcrpwrhcrvjstbhpltvgmvpmvfjstgzjsgzprzcqzqztvvdcnrrqwrhddcrhhncdrlwzwqlnbbzcfmqtnwgfdscmrbwnbldlfrqchzdnlnmwncgrzdclnvcvplgwjsbzmbnnsdrsfhrlssvncnwmcrjdjbjpdtrrvlnbjvspfqbwdpcnnpjzfnmbhcdhlmdgbpvbzmfltzstnznfctcdzhbfsvnfbsjqzmwfllhtrsfghlrpjgrgzgchlwrdmqzbrncsvnwhfqmwjbnvjctzphcsftqsbmwntgvjqhhvwndvmfmjhhhmfdvrlhpvzmmhrbhbddqbdmgqqsvddsswmzqcjmvhztfqpchzpwhdshzjlmbmnsgzqhbnmrshwvtmgmgndtddpfwsjrrjdhncdhtlczdvlbvqplttnzrblthlcffdtfsdtpwzdgbldvnsttvpzmbgnqddrszftcpwrgmfzhjjvghpntmzcttcsnrjnfpqzqqqljhzlrpgwngllqjwnwfcsphqplgbzmfqfgbfsqpsrntszqbcqnhctsnbfshmlbwfflrwwsjwqwfqlgnftdwmctmclwjhjhbsspqldlshbmpbgrftpnbpsqldhrrbdqwfwvfhclrlfdjfmzgmptdjdcsplcspznfjrfhtsjndwpslrdgnllllwqjgznrhswfssdlvdpmwwgmstqbhfmdhtzvzzvhwzbrrvvsl \ No newline at end of file diff --git a/day6/main.py b/day6/main.py new file mode 100644 index 0000000..a2ce10b --- /dev/null +++ b/day6/main.py @@ -0,0 +1,36 @@ +from common.file import read_data + +test_cases = [ + {"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "expect": 7}, + {"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "expect": 5}, + {"input": "nppdvjthqldpwncqszvftbrmjlhg", "expect": 6}, + {"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "expect": 10}, + {"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "expect": 11}, +] + + +def end_of_start_marker_1(data: str) -> int: + # use 2 counters + i = 0 + j = 4 + while j < len(data): + # read data by window of 4 characters + packet = data[i:j] + # check if all are different + if len(set(packet)) == len(packet): + # if yes return end counter + return j + + i += 1 + j += 1 + + return 0 + + +if __name__ == "__main__": + for test in test_cases: + res = end_of_start_marker_1(test["input"]) + assert res == test["expect"], f"{res} is not the right value, want {test['expect']}" + + dataset = read_data() + print(end_of_start_marker_1(dataset[0]))