From 84de761c1af77ca804aaaeba13caa617ae1e4376 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 11 Dec 2022 22:27:32 -0500 Subject: [PATCH] Solution for day 6 --- 2022/Cargo.lock | 16 +++++++++++ 2022/Cargo.toml | 3 +- 2022/data/day6.txt | 1 + 2022/src/day6.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2022/src/main.rs | 3 ++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 2022/data/day6.txt create mode 100644 2022/src/day6.rs diff --git a/2022/Cargo.lock b/2022/Cargo.lock index 4eabd40..000c647 100644 --- a/2022/Cargo.lock +++ b/2022/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "memchr" version = "2.5.0" @@ -29,4 +35,14 @@ name = "problems" version = "0.1.0" dependencies = [ "nom", + "substring", +] + +[[package]] +name = "substring" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ee6433ecef213b2e72f587ef64a2f5943e7cd16fbd82dbe8bc07486c534c86" +dependencies = [ + "autocfg", ] diff --git a/2022/Cargo.toml b/2022/Cargo.toml index fb3d37e..d8c9737 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -nom = { version = "7" } +nom = { version = "7" } +substring = { version = "1" } diff --git a/2022/data/day6.txt b/2022/data/day6.txt new file mode 100644 index 0000000..86395e5 --- /dev/null +++ b/2022/data/day6.txt @@ -0,0 +1 @@ +tnmmpfmfzmmnsmsjmjjbvvhnhzzfmmgpmgpgbgnnwffjhffzqqmzzbnbssrqqrnnhsnngsszsqzszhzfhzfzwzfzrrmhmghgwhhjjqwqttwhttjllrtrtzzcfzfgzznfznfzfnnbddvmvzmmfsmfsmfffhlfldlqqrnrznnhmmgqqzhhmjhmhppqbpbbngnlldvvdqvvrtrdrtrnttnppfllrbbrprpnpdplpmllhwwddqpdprddzzfccqpcqpcpcbbdhdjdjwjcwcctdcttzgzmmscmsmdmttwhwzhhnjhnhlhvhlvlglpgpmmjmgmrgrddmwddjfftfwflfslffqtfqttpftppflfmmhvhvcvbvhbhggpbgbppvdpvpvfppbwwsnnhphllbdbnbvbmvvzffvsffdldmlmtmccnlnbnjbnjnhhbfhhgzzlwlfflzffdccggdcgcjjhffjfgfgcczjccvwcvvqgvvqvllqzqmqllhjjqnqggttsdddjgdjgjzzrgrfrbrssrgrgdgrgbbssmdsdfddsndnsdnsdnnmqqsspqqmrqqpmmsjmmszzqvqrvrzznnjdndtntfnttgtctqtwwnwswrrthrttsdttlhlvvdzzgqgttnppjpljplpgpvgvqqvppzmmqggtjgtgstslltjltjjgcjcmjmsshvvtppgmmlslqqshqshsllbggfpgffdsdgssncchctcwwtllgqlqblqlqvvmsvmmwnnzppqllsttgmttftvfvjjrzzswzzjvzjzljjchcshcscbbrdbrbcrrnvvtctntvtvbvjvqjqggsrspsprrbgghdghhmwwldldzdttrvrnrfftqtftrrdsszlzvvbtbffftzzrzqrrhjhghhwbhhsjsfsttdjdjnjhjmjpmplplrrdjdcdjdbjblllbqlqdlqlpqptppdhhqmqfqhqhchwwqjqfjqfqhqshsmswsbbvssdspdpsdssstntltrrgnnmttgmmsjjrlrnlrnrnwrwfwlfltlzllcjcmjcjpjhphcpcwppmvmjjzbzvbbfnfcflfddntddbmmmhnnsrnrrvdvnvcvwvcwvwrrqwqccqmmswmmjrjmmwjmjfjhwrtbjzdvlgrjmvzfmhcqsncvlhzzncjlbvcwrdwjmqjcnptqslvfzpsvltgzsvjdsjrppdrmqrbqwhddfhnftfblspsrhtdtjwdnhbcbtlwlvccsfscvczzrrqmwbwbdmwgzqntvflppqvppwrhnvtlsbzqglhsfdgssqzdtjdpwrrhbnbtwhhnmnlwfwlqffjjrndbpwwsvdrhddbjnnqzmtpvvtwbcpndjzlhcfrrdvmljswjzvmfqcdsgqwclqshwrmblszdvsnrpdgnllmlchzdjlrrpndmmgddjqgjqrhwfbwddqdfbvptrmzhtsqfsfswpnvmtswqprjhbzvntgrlzthhnqbtpplqpvcfnpgdtbhqbhflltbbtmmhcwztslmpznttmssclhmnbsbrwlblrbsdfmnpqbwwmsncvzmpqwhzjgcgdrzvglgdtswmstdhrprdjfmqtjlmplbjtzcgnrwpdvpfjjfwjfnnpmdtwtqsgfndngsbmcwjtglqwtfrclbczfcmjtgcwszhzrbcphrhwmhcwghjznzthnwpljjltdlvqtffsrbmwcsvrdmqqggbznnlzbbqtgspqvnjpbdhtzmgttrcwwszwpgdrcnfqtgrgqdrctlzwtdwqppbhnwgldnqltznnfpbfqtgmmwpcqnndbgmrrtgtvnmlfcwsldchjnnqfrhpzwtclrzftsqllgvpqbgmfjdhqjttwcvbpvfqsvhbhhtwnqnbgndbtzhcvgglbhghbzrbrmdllmgfgttqmhtdnwrpwllhnghrjctrbzrcpnjnctvmrlpjhftnfbczrjrnnbqplplcrbngbhvmmvcffmgvbhjzbhcmtwmwgmjmwjvvlqfldswpntjnsjvmdlbzqqlgbwspwvmnwtwjbczmwplrhmjgsppnmtwmvsfwnsgddgwqcvpftcpzrhpldnwmcjgtjmljjbcmjcqdbwczndnjnjgrmtjrqnnjndzqdqpcgdqptdbrqftnwrgqmrzrvsfmmmbpltlncvtgrjfjmvtgwqphczwjhdrdwtfvgztbhrndvpcbgfjfvmrrljwrvcrtdmtjndfnwgcnfrzgsnjpztbwwsbvqfnpjctgrhsflhnzbbsfqbnmtnvrmjzsbjfndvttpvpfjhqntflgbfnzcclcwmhbsgqfjdcgsvrhtstspfzgvgglgddqmclsmzgzgtncdsfmwdvtcsgwvbzjvclwppqdjgfcrcbzcwbdhrnssjbmnmfmwthdrnmlfhqlddwqrdhsdvdcsmcgjsgcmpnhlbnqftpdjswtmpbznlcrhtswgnmwjcdfmljdngzfsmlzjjnzmfzshmztdbdmcqwmlvcrzgpmbjqcghclwvdbrhgvwqchnndftnrtptmctdlhmfjvpzrpccddfpcdwmzqfhnsqzrvwblzfhcjdcjfctczwqrcbjnrpdcbbnsgnlvqqmnsfgsqschjlbzhhsrbvdbfrhvsgrlzwncgwpdbvmblgzbwbcbgqfwmdmgcrbbjfcvmqgztqpptdhwmvmsdqwplpgcjzgqzdrftzhqbltvhrmlrfffcgfpqzwrrbbtlsjgmtbjvtnmhwdpjptjwfwgjgvbfqwmflrrqzlzdcmtlnptdrpcpdnswcfscnndnrfbgwvvncdjgsdpbwptdtvrqlmrhmvvcwblhhzbjdpsbszhrftfbcgwhwrgglnjzqdhcqnvlhgqjhnddvrslhntssptsbhmqwwqqnbvfmcbgpvgjbrttnvlljdbtfplgmbwtcbcdtqdpqqdvhbmpmtszwpzblcfrtznhhtcljtdlhjdbnlhvwgjsmgvrslrfwnmzwlstpgltvrgnpdqztvfnvdhdtwwqdfsmtpbpdclsbnwcgjzchjcsjmvhbjshmjjlpgdzcgbmmchwmcsddsvhsnpqtcpnhqnbvwgwqhtjbqncgwwftnrzsbsjtvqmjzqvvncmncwflcfpcjqgdtbsmjzzsdjfvhnqbgjhmfgjghwscthbfmbndltbqzwpqtmrswvprpmgwqnqpfnmffrpdlpfqmhrthppzvzwbrtjvwvjndsqdlqtbpqwfcttggnjmcqqnmjwfhfjgcvlnmtlgbdvmctzlwbfgnflwtsflgnfbnfbhhdgjctzvvmrhdsmvmmtnqwtszmqcpsbrqrgjfrzctcbzmtdlhwjtfdqbtthdnqcrpwrhcrvjstbhpltvgmvpmvfjstgzjsgzprzcqzqztvvdcnrrqwrhddcrhhncdrlwzwqlnbbzcfmqtnwgfdscmrbwnbldlfrqchzdnlnmwncgrzdclnvcvplgwjsbzmbnnsdrsfhrlssvncnwmcrjdjbjpdtrrvlnbjvspfqbwdpcnnpjzfnmbhcdhlmdgbpvbzmfltzstnznfctcdzhbfsvnfbsjqzmwfllhtrsfghlrpjgrgzgchlwrdmqzbrncsvnwhfqmwjbnvjctzphcsftqsbmwntgvjqhhvwndvmfmjhhhmfdvrlhpvzmmhrbhbddqbdmgqqsvddsswmzqcjmvhztfqpchzpwhdshzjlmbmnsgzqhbnmrshwvtmgmgndtddpfwsjrrjdhncdhtlczdvlbvqplttnzrblthlcffdtfsdtpwzdgbldvnsttvpzmbgnqddrszftcpwrgmfzhjjvghpntmzcttcsnrjnfpqzqqqljhzlrpgwngllqjwnwfcsphqplgbzmfqfgbfsqpsrntszqbcqnhctsnbfshmlbwfflrwwsjwqwfqlgnftdwmctmclwjhjhbsspqldlshbmpbgrftpnbpsqldhrrbdqwfwvfhclrlfdjfmzgmptdjdcsplcspznfjrfhtsjndwpslrdgnllllwqjgznrhswfssdlvdpmwwgmstqbhfmdhtzvzzvhwzbrrvvsl diff --git a/2022/src/day6.rs b/2022/src/day6.rs new file mode 100644 index 0000000..84a4161 --- /dev/null +++ b/2022/src/day6.rs @@ -0,0 +1,68 @@ +use std::collections::HashSet; +use substring::Substring; + +const INPUT: &str = include_str!("../data/day6.txt"); + +pub fn part1() -> String { + format!("{:?}", start_of_packet(INPUT)) +} + +pub fn part2() -> String { + format!("{:?}", start_of_message(INPUT)) +} + +fn start_of_packet(input: &str) -> Option { + first_unique_window(input, 4) +} + +fn start_of_message(input: &str) -> Option { + first_unique_window(input, 14) +} + +fn first_unique_window(input: &str, window_size: usize) -> Option { + for window_idx in 0..(input.len() - window_size) { + let window = input + .substring(window_idx, window_idx + window_size) + .chars() + .collect::>(); + if window.len() == window_size { + return Some(window_idx + window_size); + } + } + None +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn it_finds_start_of_packet() { + assert_eq!(start_of_packet("mjqjpqmgbljsphdztnvjfqwrcgsmlb"), Some(7)); + assert_eq!(start_of_packet("bvwbjplbgvbhsrlpgdmjqwftvncz"), Some(5)); + assert_eq!(start_of_packet("nppdvjthqldpwncqszvftbrmjlhg"), Some(6)); + assert_eq!( + start_of_packet("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), + Some(10) + ); + assert_eq!( + start_of_packet("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), + Some(11) + ); + } + + #[test] + fn it_finds_start_of_message() { + assert_eq!(start_of_message("mjqjpqmgbljsphdztnvjfqwrcgsmlb"), Some(19)); + assert_eq!(start_of_message("bvwbjplbgvbhsrlpgdmjqwftvncz"), Some(23)); + assert_eq!(start_of_message("nppdvjthqldpwncqszvftbrmjlhg"), Some(23)); + assert_eq!( + start_of_message("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"), + Some(29) + ); + assert_eq!( + start_of_message("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"), + Some(26) + ); + } +} diff --git a/2022/src/main.rs b/2022/src/main.rs index 3d36ba3..077dac4 100644 --- a/2022/src/main.rs +++ b/2022/src/main.rs @@ -3,6 +3,7 @@ mod day2; mod day3; mod day4; mod day5; +mod day6; fn main() { let day = std::env::args().skip(1).next(); @@ -18,6 +19,8 @@ fn main() { Some("4b") => day4::part2(), Some("5a") => day5::part1(), Some("5b") => day5::part2(), + Some("6a") => day6::part1(), + Some("6b") => day6::part2(), _ => panic!("unrecognized day"), };