From c785df0a3abfced7974ddb1238319efc467e5d2d Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Tue, 6 Dec 2022 08:59:06 +0100 Subject: [PATCH] Rust Day 6 --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 6 ++++- rust/src/day06/input.txt | 1 + rust/src/day06/main.rs | 11 +++++++++ rust/src/day06/part_1.rs | 38 ++++++++++++++++++++++++++++++ rust/src/day06/part_2.rs | 38 ++++++++++++++++++++++++++++++ rust/src/day06/sample_inputs/1.txt | 1 + rust/src/day06/sample_inputs/2.txt | 1 + rust/src/day06/sample_inputs/3.txt | 1 + rust/src/day06/sample_inputs/4.txt | 1 + rust/src/day06/sample_inputs/5.txt | 1 + 11 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 rust/src/day06/input.txt create mode 100644 rust/src/day06/main.rs create mode 100644 rust/src/day06/part_1.rs create mode 100644 rust/src/day06/part_2.rs create mode 100644 rust/src/day06/sample_inputs/1.txt create mode 100644 rust/src/day06/sample_inputs/2.txt create mode 100644 rust/src/day06/sample_inputs/3.txt create mode 100644 rust/src/day06/sample_inputs/4.txt create mode 100644 rust/src/day06/sample_inputs/5.txt diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 1870c7d..aa48d27 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "22.5.2" +version = "22.6.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index d87b622..832223b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "22.5.2" +version = "22.6.2" edition = "2021" resolver = "2" @@ -42,3 +42,7 @@ path = "src/day04/main.rs" [[bin]] name = "day05" path = "src/day05/main.rs" + +[[bin]] +name = "day06" +path = "src/day06/main.rs" diff --git a/rust/src/day06/input.txt b/rust/src/day06/input.txt new file mode 100644 index 0000000..254f124 --- /dev/null +++ b/rust/src/day06/input.txt @@ -0,0 +1 @@ +zcncrcnrrlccmhchssgqsqrsstfffnqfnfsswgwjjcmcnnvjvwjvwvfvnvwwhvvwmwhmwwwhbhhldhdmhhdfdbdfbdblllnfllgslllqhlhfhlhqllncnfnsffwmwzzlglzlwlhwwmvmddpvvbrvrdvrdvdhhzdhdpdzzbgbsssrpsrshrshhbqbgbmmtwwcbcrbbvnvhnncscwcwlcwlwnwswbbnwntthzzdlzdzffvqvdqvvtptdptphpddzzvhzvzwwqgqjjjvsspvspsbsffvpvcvjcvjvrvjjgmjjhrrdhhdvhvttjttzptpssnlnjjlnnhmnndjnjwwtjjtbbfflwfwgwvvpjpwpcwcgglmgmdmnmbbqtqctcwcmwwvmvlmvmdvdvhvlhlzhzttghttnvnjntnqqtmtwwhvhrhfhchqchhdrdllnwnfwfjjmsmmnhhshnhpptstjjpnpzzhmmpssgrsrzsrrffzjjhvjhjcjcpcvpccfnfjnfjfllssrdsdnnmffldldppcctcmtctffprpsrrrfhfmflldccnpppvsswppdgpgjpgpqgpqpjppclcjczcnzzzqvzzwlzzqfzffsqqphqhvqqbcccstsdttwcwcvvmjjhqqmllpglgtgwgnnbnrbnnjdndhhbrbvrvwwwblwwwppmdmttztqzqrqjjpggpmgpmpqqmssvnsvnndnvddtztddrrsnsbbshhqmhqmmdnnrsrllqvvfjfpfqpqfqflqlmqmrrqgrqrrhvvfddfhhfnhnlncnzzwwjwswpsscpchcssrzssjqsqrsswbbzggnqnwqqsrqqzbqbddtffcjjdggwlwnwtnnpddcqddsppcwpcwpphvphhhrprqrqttwgwqwppvdpvvrgggmpmddzvvwwthwtttbqtbbftfrffbdfbbspshphpwhppnhnmhhbbshbsbmbdmmtrmrbrprbprphhnmhmvhmhhqqbvqvqcvcwcbwcchbbgwwqffrcffsvvcczrztrzzdhdmhdmmzbbvlvqqtptpvvfsfppdzppmddfvdfdwdfdmdwwlvvqdvqdqmdqqmnmdndsswsfwfvwfwzzhrzhhwfhhpvhppmssnhsnsnwswvwlwdwzwnwswwmqmjmbbdjdbbtllcpllltqlttnvvppznpnttlrrwlwvllphpbblccgjcggbtbwtwdwrrbnndpddbqqnpnrnnqqshspsggtptztpzzclcggtqtgqttcmmbgbfbdfdfsddlrlvrlvvmqqgbqgbqbcbbnpplqqblqbqmbbbdqbbhjjcgcdgdwdjwjqqlsqqnnwffglfglgnlggjgjtgjtgjtgjjclcwcqqvpvwwvgvhgvhvjjzpzbznnvrrrpbpdbdhhmshsnnnwppcbcvvlldccdggqnqgnqqzbzzcfflnnmppcgcmgmrgmrgmgngwwptpzzpfzpfzpzdppgcgtcgtgccvtctzzdmzmlmzlzwllbplblltgtctrrghgvhgvvfvssngsschshrhvrvddghgwwwwzgwgvgccjdjzjwwvdvvqsqshsvvddmddbllvppmlpmlmwlwppjmppwrrdzrrpmrmrdmrmbmzzwttvwwsfswwlflnlrlvvdllzbztbtpbtppnjpnpdnnjrjsshtssvvsjvjfjwffsszvsvjssqzsslpslppjpspqptbncvzrlwtjvsrwtnzzhwdfsmlthvgqgjrpshpbsrrvnsdbqslbcplnpcjqwmwqsnwdcjsdmccbdglwbrcdcqsfhjqhstvhqqdwltqwhhqcrnpvnzjhhbjvqbqhclwggjqfvnfsvcnjjhbmrvbpjqrbljbtltvnsgdfhddlmsdhcrfwvlvbsdrjwjvtnqzhrlqgjmzsmjlpdjsrjmdhmvgwjmfwtqffnzfrtswrlgvvhhqgpzcjwscfqgjmdhtvbgzdlvzfhgqlqbfwsjrmmhrlcwhrcnwwvngcmsrfgczsfqvvmdtmtprfvjrwrwcqwvgmzcjncrzvcswlzsdszvdtwmptnrhgzqwrhjjtbchhpwsdjnqmnsgzwqzvlzlsznpqgvtqnldjqpvndtsjlzhpzsgthbwvwnlbwjlmndqpcdvjdgdzhctpghlfwrtqtvfwdpgrjbmwzqgthjpmlrsqmzsznddhrbjnggqrdntpbngvldnnltfnmdwfhftjvpqbrzqvdzbzzctshzldtcdgfnczglrrjtwswzdvjrfgwztwznbpplmbgwpcmstcsjtqhmzmzsjwsfbjlbnbdtdsmlpdmrrbhdhpzrjdpzhcwsrgfrhmqzqtjfhpvltnthwjrrrpnsbmmwrhsfqbmnvwhpntltsgwgnqhcvlndfrtrfrnlmbhltmtgzhlzqgtsbbnggdjvbslfbczhpghqqcqlrbtpnbqbflfjrmpmwwvjqgvcqtmfggmptqlqstcmdtqlslnnzwbgnstftfsvsjdrmgbzfnzltwbjmqhsvshnmwhftjdndltdpngzwbrjpgpwmqgfsflnhmtzcmdjmzwrsrrrmpvwgggwrhrwtfwdbgbpmwpcdspdtbqvdwwsnwdtrtdtgnfzzsmlbcqdzbsqnrgtvvnlfcdlcgcnfpqbddqcjfqtmpndmnwvfqgjzrltqvlprmbbhmtwbzjjgfhhhfjswpffmjnsdmrcjrwlwpmfrmhljpphlwwwwmgsjcsrcvmfrdjdhshddshpplzsnsphcmdhvllgmdgrvbvjmtpltdthffsvwwhvgqrhmfjfpdswcqldrhpmznffjsntwrnmnpmsshljszbchctptsdlnbcvpfvtlfnzcrljpdwrsjnlpqcpnwvnqhzhqmjbvlbtgslzthlbbjzsgdglbrltzjdshpfbndjtssvsjqlstnrjdzzjvlpqhmwvrsvndcqrqjjcsqvmvrbhngtcfdprlbnqmhqllddgjpdzbjlphntrtgjrdgtbslrtzczbnnlddzzsvqvqvvzjpjqfhztgtsfggdppfdhzsbjzqjmpnmgqzlsdhjjbfpbsbnzpmhwrzjqhczrgcsflfwtrgwbnbrshjpwltntsnsdhmhqlmzdprcrcpcpjnphsmjwhzdqtncdbwgspmnfzsgmpbdhmslqchhhbbwfrghhnfjplsvrtbvplgrwdnbnfsgpwrqczvzlnfsngnsnbwvpmfdcmjztdnrllslnwcfwwnwsvztqmgqtfvmdqrrrmwfmphbcvwwttpmwjjbvqrmlwtwfsjdpcbmdlnlzcqntfzzmslshwprjfhwwpbbdfcdjwllfwcznwpjpwrlsfnnbgzjllrgtzcdcvhdhbtlrcvfdvsdjlzsmwwqvpzfhzjlqpfbqstvfrpcchmtwgbrhqqbglrvzmctdlpnvmglgdtzpbdngtfdnmsmwbgjstzbqwqcdlhfrtqqnhqvpfhdrjqvsvstftdgwwnwpfbfbdcfqnqlwpdnfhhfctwrgdqpbpbmgnfsnbpjfctvdtjnsfqlrtctrnjgltndngcmrdphhsqpjhprbngjzqqhnhhrdwlwwpmhzwshvrtzfgzlrhwghvpvfprbbvflltplpptvrmwcrdqndfqbfqtlqqwvphsmcvnbzghvsptrphhfcgdsslhfbcwhtjcmnpbvqrfgpsjgqpnnwwhjjwqrhhqgznwdzjqbtmmjljjwctqtfgwqbdrjwqwbbcftvjwfdfrgvsrlcrccpvfzdrcjvqfbhddpvrrhjrmhdgchrghbzsqpmgnmslfctblwlvphdfpvtdtwpdfsjwssmgnsvsqpdbqngccsplhmjbwjwtzwsbjhwpwcslqjdchmbvzrbgnwvjrrrdtvhtlzlrbwthzlhhqzzpvpwbzrrbrbtpwnhldhqqltqrqdddfwdmjzgctnlrjrjwvddfmjpnptdmrvnqjvsjfrmlvlqsthhsbvnjlsdzrjngfnqdjfssmvgrchbwmwbbvfqfhvrtwghmrpddnwbrbvbmqvfzbjdsnbzgrtmsfhmsmjtrqsgmpnwwbfwtp \ No newline at end of file diff --git a/rust/src/day06/main.rs b/rust/src/day06/main.rs new file mode 100644 index 0000000..a146bb4 --- /dev/null +++ b/rust/src/day06/main.rs @@ -0,0 +1,11 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +pub fn main() { + println!("Part 1: {}", part_1(INPUT)); + println!("Part 2: {}", part_2(INPUT)); +} diff --git a/rust/src/day06/part_1.rs b/rust/src/day06/part_1.rs new file mode 100644 index 0000000..8c6f794 --- /dev/null +++ b/rust/src/day06/part_1.rs @@ -0,0 +1,38 @@ +const STRING_SLICE_LENGTH: usize = 4; + +pub(crate) fn part_1(input: &'static str) -> usize { + for idx in 0..(input.len() - STRING_SLICE_LENGTH) { + let mut unique_chars = input[idx..idx + STRING_SLICE_LENGTH] + .chars() + .collect::>(); + unique_chars.sort(); + unique_chars.dedup(); + if unique_chars.len() == STRING_SLICE_LENGTH { + return idx + STRING_SLICE_LENGTH; + } + } + unreachable!("We should always find an answer"); +} + +#[cfg(test)] +mod tests { + const SAMPLES: [(&str, usize); 5] = [ + (include_str!("sample_inputs/1.txt"), 7), + (include_str!("sample_inputs/2.txt"), 5), + (include_str!("sample_inputs/3.txt"), 6), + (include_str!("sample_inputs/4.txt"), 10), + (include_str!("sample_inputs/5.txt"), 11), + ]; + + #[test] + fn test_with_solution() { + assert_eq!(super::part_1(crate::INPUT), 1723); + } + + #[test] + fn test_with_sample_solution() { + for (sample_input, sample_answer) in SAMPLES { + assert_eq!(super::part_1(sample_input), sample_answer); + } + } +} diff --git a/rust/src/day06/part_2.rs b/rust/src/day06/part_2.rs new file mode 100644 index 0000000..9602598 --- /dev/null +++ b/rust/src/day06/part_2.rs @@ -0,0 +1,38 @@ +const STRING_SLICE_LENGTH: usize = 14; + +pub(crate) fn part_2(input: &'static str) -> usize { + for idx in 0..(input.len() - STRING_SLICE_LENGTH) { + let mut unique_chars = input[idx..idx + STRING_SLICE_LENGTH] + .chars() + .collect::>(); + unique_chars.sort(); + unique_chars.dedup(); + if unique_chars.len() == STRING_SLICE_LENGTH { + return idx + STRING_SLICE_LENGTH; + } + } + unreachable!("We should always find an answer"); +} + +#[cfg(test)] +mod tests { + const SAMPLES: [(&str, usize); 5] = [ + (include_str!("sample_inputs/1.txt"), 19), + (include_str!("sample_inputs/2.txt"), 23), + (include_str!("sample_inputs/3.txt"), 23), + (include_str!("sample_inputs/4.txt"), 29), + (include_str!("sample_inputs/5.txt"), 26), + ]; + + #[test] + fn test_with_solution() { + assert_eq!(super::part_2(crate::INPUT), 3708); + } + + #[test] + fn test_with_sample_solution() { + for (sample_input, sample_answer) in SAMPLES { + assert_eq!(super::part_2(sample_input), sample_answer); + } + } +} diff --git a/rust/src/day06/sample_inputs/1.txt b/rust/src/day06/sample_inputs/1.txt new file mode 100644 index 0000000..5a2b0a7 --- /dev/null +++ b/rust/src/day06/sample_inputs/1.txt @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb \ No newline at end of file diff --git a/rust/src/day06/sample_inputs/2.txt b/rust/src/day06/sample_inputs/2.txt new file mode 100644 index 0000000..9e3311c --- /dev/null +++ b/rust/src/day06/sample_inputs/2.txt @@ -0,0 +1 @@ +bvwbjplbgvbhsrlpgdmjqwftvncz \ No newline at end of file diff --git a/rust/src/day06/sample_inputs/3.txt b/rust/src/day06/sample_inputs/3.txt new file mode 100644 index 0000000..cca46f5 --- /dev/null +++ b/rust/src/day06/sample_inputs/3.txt @@ -0,0 +1 @@ +nppdvjthqldpwncqszvftbrmjlhg \ No newline at end of file diff --git a/rust/src/day06/sample_inputs/4.txt b/rust/src/day06/sample_inputs/4.txt new file mode 100644 index 0000000..848af1c --- /dev/null +++ b/rust/src/day06/sample_inputs/4.txt @@ -0,0 +1 @@ +nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg \ No newline at end of file diff --git a/rust/src/day06/sample_inputs/5.txt b/rust/src/day06/sample_inputs/5.txt new file mode 100644 index 0000000..a4fc5ba --- /dev/null +++ b/rust/src/day06/sample_inputs/5.txt @@ -0,0 +1 @@ +zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw \ No newline at end of file