From 235801201faca5f1bd6bbda99e9e8ee2a972f745 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 6 Dec 2022 07:04:10 -0600 Subject: [PATCH] 2022 Day 6 Complete! --- 2022/day06/input | 1 + 2022/day06/main.go | 51 ++++++++++++++++++++ 2022/day06/problem | 112 +++++++++++++++++++++++++++++++++++++++++++ 2022/day06/testinput | 1 + 4 files changed, 165 insertions(+) create mode 100644 2022/day06/input create mode 100644 2022/day06/main.go create mode 100644 2022/day06/problem create mode 100644 2022/day06/testinput diff --git a/2022/day06/input b/2022/day06/input new file mode 100644 index 0000000..0ba323b --- /dev/null +++ b/2022/day06/input @@ -0,0 +1 @@ +mgwwjddqzqdqsstctjjsdjsdsrsfsmfsfwwltwlwhwnhhlffzddgffwlffbsfshfshhgvvdrrltlzlnzznrrnrsnnhgnnfjnnvpnnbjjnwwrcwrrhlhvlhhmzmqzqrqtqmqpmpwwmssgsrgrgtgmtgmtgtdtvdvmvsvsbvsbvbtthmmftmmdnmddcrcvcrrfjfhhfjhffjllcpllmcctjtrttwmtwmwffrlrqlqzzpddsqdqqgjqgjgngwnncjnnvsnswwbzbtzzflzzqsqbsbvbmbnnjpnpnnpfpmpmnpmmjljtltssqnsqslstswtwswwjddvmmzlzqlzqzqjjlttmtrtbtmtgmtmsttrctrrsqrqvvrzrcrhhlnhllbfbtthrhdhllmwlmlgglgsgmgsmszzprpwpfprfftffpssjzjgzjzddqfqmmwqwvwlvlqqtbtwwrwttmsmppbmmpcmctcnnhssnjncnlcnctcjjrzrwrfwfcwffczztrtsrtstlsssljssmvssjzssrqqrcqqwlqwlwffsflssrrzhzzhrzzdgdppspwplpqptttvddggzszccrrnzzwwdwjddrvvwggpvgpvvhdhqddffrnngcncjcjlcchrrftrrjccrcrqqgcglcgcscmmlzmmtcmcffwfcfrcrggdmggdvvnrvnnphnngzzpdpgpspqqgrrnffmfpmffmgfmmjmzztlljlggljjcnnrqnqpnqndnffnwwbpwpjwjjlslmsmtmtjttsvsggrmmdpmmcjjswsqqwfwwrwffczfzggqvggdlldhllsdsfdsdhhmmzmjjmpjpddsccqrrjhjlhjjcnnpwnnffjwwcsszrrnmnsmnnjbnndwnnnhnwwjtwtlwtwqqbnqnbnqqfjfdjdbbwbqwqpqggbcbhhtrtqrrddpdwdlwdddzvzwvvdfdpdcdvdtdpttwwdzdzmdmqmzmnzmnmhmwmjwwshhcqcpcvvzgzdggnjnnhwnhhswwvccqrqlqggcngnmggmffblbglltlstshhrjjlvlppsqslljtjtgglvltvlvmllhrhdrrmqrmqmjjdcjjppqwwllvsvsrszslsvvghvvhmmfbfvfpfmmvdvppwggtrrjvvsbbzffbmffpqqqhnhncclzczwcwpwssrprfrsrbsbnbvnnwzwqqpsqsspmssztssstrtcczsznzvvpvttnssdjdhjddngdgvvmsszbzsbsmbbgsbgsgmmhwhghrhjhphshchgglmlvlhlbhlldwdggdsscvcbcssfbbvggvwwtstltrrwttjdtdvttlsttfhfmhhcbclbcbffqqslshlldhdqhhjwwlffrbrdbrrgcrrfmffbhhlslrslrslrlsrsnsnvvqfqnnfdfmfmttmcmcrrcmmmjttjvtvvjbjqjnnbtbnblbtlblplgltlqltlztzvvtdvvtpvvwdwfflbflfrrhbrbbmjmcjmccztzwwjzwwzwdzdnnwcclbllqgghjhlhthwrdglrmcpbmtrnrdtvjrpmzqmljzzrtpzsrhnjrsdmpnsgdhvqchcfqjqdncjqfnscwjqvszpzzfhpjljmvsqnjzmrsgsbzlvrddtdmwbwwgprlvdfflrpztdzrhtmlzrrtdmpmcprqzzwlnmfjvsrltfjgcnnfllnzmbjcbthvbffczsspmczrpgpdjmvrvfmprfmnqdcnfwwvgdrwvrbtlqmhrrjvtrmmgrlprtnzdlszgbtbwztdrmpmlfblshzcnsczlblgwzrpnlccwhmcqhssmpznbdnnqgzzmjprjttdjhmjbmgqvzblsjwmplzsthrswhsdbvtqgrfzmbpqtpqgqdqcvzlgjrtvrhvzgmcmrwdmfpdvjddsmmsnvrdgnsbsdzcbprbqchqcgnwmfsrmqtrcdhdtzztbvmpblftwqlmlmmjcjhhjlgnnhljnncvbnjhgbjrltlwscswgvqmcnssbcdrtbgnhgmpmvjwtrbrbrdbdqfrncvhdstwztwcpbjrjwzmdlwvlvmsrhghjwjnjstbcqjqtjrgcvhzjdhdgbgdlhvjmztwvhgzzggwwhhhzvtrldchztmwfjvnqnvhnwpfvzzvnlvsccmvsngzgtnttssmdmhwzlhtpnfhczsdfnrstbwvwpqmslcvpvhfzttzhsgzpbhqdtswshljpncznjhzmgvvbcllmzprhrvwljwcjpcdqmwbzvsdcgtmwnrhswsgqhwpwhbjpnhnpjvgsqcjltzrqvqfflcdcvpwnznvtqbfbtlpmtdgbbwdwncqsqnbtgfdzzqzzvjnwmzdmlgstmnjwznjqghglvmwjzlqrnddcqhgndlhlbmqdhrqgrjqztnhpzssnwmrqclmwpgbvfrvgqqvtthznsqwgndjrprbgrhcvhpzbfhdmgnhsrqjvjstbtmnltsbjfzczvjqnhtldqclsflbhvvlzjwrqqgbgpwqwpfjctqpzdqwcfstmwbzgrgrtzngljjnvtggrqcbgjwtqsdgwmfjqppnzgfsfdmlctztbhnntnntdlvrsdvnllvmpggjzspqfhzwrttwzpqrnqjhmpjnmrzrpnqzshcqgctbtflqflcrzpmnphgbbghhwzplljwngbtffwmrwggdztvtfgwldlswqvjptvbfvnbpglhgrdgcfmvrslqldmwjqvjpvwgpjddvglllvpqwvbchqsmjrncgvgmqbsbcwfbsbpqcqzjfpcdzszgmvqgqjlflpfzbsrhsrzrdbpssrjbcfhvztftlzqpsglpwhbscgwdlbgghzsbwznnbgnnsgjghmmpmmrmqmdhnflgvgprqfcbpzbcpjscvnpfrmtvzsbflmffvcfsvdsggzdqtppcjzphcqwrqtrczqmwcdmdqndzmhdpnfqsbndnvjlzrsjzmpcrfgjwccsdtzvslccwhlvzjwjgvwpsnsggmqgsjfbwmjstsgnqmtjhljvfnflnngdrqvscwlqqdsglhghczhjdvgrjcqblmncdbjvsbwgptgpvvzhcjgjnvttrgzrjnqlvfbrmpzdcbbnnrqptpzpssznbsrstdphbgdrsnrhcjwwgsncdzvqfnmnvqcmcgdgjdbqjzdrvvbvhjdfcqndmqwscmsvppclzrhgbldqtwctbdhpbbwfvwpcpsvddmrhqbhlrrmrblnmqqqbwvcwwbwprlmhtdncmjhmjgphmrrhcdrqgmcrzwsznqzpngbtsvjgglrddhjflbrhvqwmmhmqzhphwnvqwzczdvqjsnlhfqbcgddtwgnlcgbfqmzfpqmnbpvfhdhjlnwtrlmggtbfnfvmqrzjvjjvffctsrwgfcpghhnzqmwtlsfhjrvqpwqhngrhpswslsvtgnbvbmwsfwmpntfsfpshrjzvghhpvnlbmnrhltfpmqdwzfhztvhlmbnmhnbvdzbbtczvwbvwtvjghhjjrtgbrqrhmbgvssstdwztdmdsqtctghjhsnpslqttdlvndmjfnmdzwrblfjqcwptfttvlcgsvwcbmfzbdlmrtchgqlfspwznbzfjthjtfwshqgfsfdsmzsmpptzschlzjshvfwtmpszvrvlggbrgpcnqwndhjjprztdfddblhfljbvttfvhchhdfsftrhccrbncmhwpcpwfqthngcqptmvsmpcswdrdlcbqvvhwmcqqwbzlblrgfcrrndwdvlvnpjvwchzjzmgrqhzzmgqqdsdflpclpdtlhvhcthzjfbvjvzsnbvwfsnglvbnwnbgrqwpbgclhjhztttbjwvmlmmgmzncbwswncqhmcfjfnwnpbrmchhpgwngrfwgdfdqmblwlghdjvdhjftdblrtcvvgbvpmbjhfwgpmghqbqrcpgfvhtvqtlbjdblggcpjzlrhpbsqwntfhbhwwszpdlsgbpfqhvrjrhsldcgvqhqmwdfcrcmhrvvwvbrfsrrcvwzhqqvgltlnhwhdrhrdqsvmdzjwgmqdsccwhcgwltfhdfqpsltjccwsttmrc diff --git a/2022/day06/main.go b/2022/day06/main.go new file mode 100644 index 0000000..b7907bd --- /dev/null +++ b/2022/day06/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToString() + fmt.Println("# Part 1") + fmt.Printf("First Start of Packet @ %d\n", findFirstStartOfPacket(inp)) + fmt.Println("") + fmt.Println("# Part 2") + fmt.Printf("First Start of Message @ %d\n", findFirstStartOfMessage(inp)) +} + +func findFirstStartOfPacket(inp string) int { + return findFirstWithXUnique(inp, 4) +} + +func findFirstStartOfMessage(inp string) int { + return findFirstWithXUnique(inp, 14) +} + +func findFirstWithXUnique(inp string, x int) int { + for i := range inp { + if i < x { + continue + } + if findUniqueCountBefore(inp, i) >= x { + return i + 1 + } + } + return -1 +} + +func findUniqueCountBefore(inp string, pos int) int { + if len(inp) < pos { + return -1 + } + counts := make(map[byte]int) + for i := pos; i >= 0; i-- { + if _, ok := counts[inp[i]]; !ok { + counts[inp[i]]++ + } else { + return len(counts) + } + } + return pos +} diff --git a/2022/day06/problem b/2022/day06/problem new file mode 100644 index 0000000..bb0b4a7 --- /dev/null +++ b/2022/day06/problem @@ -0,0 +1,112 @@ +Advent of Code +br0xen (AoC++) 12* +{ʼyearʼ:2022} + +--- 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? + + Your puzzle answer was 1544. + +--- Part Two --- + + 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. + + Here are the first positions of start-of-message markers for all of the + above examples: + + • mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19 + • bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23 + • nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23 + • nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29 + • zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26 + + How many characters need to be processed before the first start-of-message + marker is detected? + + Your puzzle answer was 2145. + + Both parts of this puzzle are complete! They provide two gold stars: ** + +References + + Visible links + . https://adventofcode.com/ + . https://adventofcode.com/2022/about + . https://adventofcode.com/2022/events + . https://adventofcode.com/2022/settings + . https://adventofcode.com/2022/auth/logout + . Advent of Code Supporter + https://adventofcode.com/2022/support + . https://adventofcode.com/2022 + . https://adventofcode.com/2022 + . https://adventofcode.com/2022/support + . https://adventofcode.com/2022/sponsors + . https://adventofcode.com/2022/leaderboard + . https://adventofcode.com/2022/stats + . https://adventofcode.com/2022/sponsors + . https://adventofcode.com/2016/day/6 + . https://adventofcode.com/2016/day/25 + . https://adventofcode.com/2019/day/7 + . https://adventofcode.com/2019/day/9 + . https://adventofcode.com/2019/day/16 + . https://adventofcode.com/2021/day/25 + . https://adventofcode.com/2022 + . https://adventofcode.com/2022/day/6/input diff --git a/2022/day06/testinput b/2022/day06/testinput new file mode 100644 index 0000000..7980a82 --- /dev/null +++ b/2022/day06/testinput @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb