Rust Day 6
This commit is contained in:
parent
870d70001b
commit
c785df0a3a
11 changed files with 99 additions and 2 deletions
2
rust/Cargo.lock
generated
2
rust/Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "advent-of-code"
|
name = "advent-of-code"
|
||||||
version = "22.5.2"
|
version = "22.6.2"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "advent-of-code"
|
name = "advent-of-code"
|
||||||
version = "22.5.2"
|
version = "22.6.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
|
@ -42,3 +42,7 @@ path = "src/day04/main.rs"
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "day05"
|
name = "day05"
|
||||||
path = "src/day05/main.rs"
|
path = "src/day05/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "day06"
|
||||||
|
path = "src/day06/main.rs"
|
||||||
|
|
1
rust/src/day06/input.txt
Normal file
1
rust/src/day06/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
zcncrcnrrlccmhchssgqsqrsstfffnqfnfsswgwjjcmcnnvjvwjvwvfvnvwwhvvwmwhmwwwhbhhldhdmhhdfdbdfbdblllnfllgslllqhlhfhlhqllncnfnsffwmwzzlglzlwlhwwmvmddpvvbrvrdvrdvdhhzdhdpdzzbgbsssrpsrshrshhbqbgbmmtwwcbcrbbvnvhnncscwcwlcwlwnwswbbnwntthzzdlzdzffvqvdqvvtptdptphpddzzvhzvzwwqgqjjjvsspvspsbsffvpvcvjcvjvrvjjgmjjhrrdhhdvhvttjttzptpssnlnjjlnnhmnndjnjwwtjjtbbfflwfwgwvvpjpwpcwcgglmgmdmnmbbqtqctcwcmwwvmvlmvmdvdvhvlhlzhzttghttnvnjntnqqtmtwwhvhrhfhchqchhdrdllnwnfwfjjmsmmnhhshnhpptstjjpnpzzhmmpssgrsrzsrrffzjjhvjhjcjcpcvpccfnfjnfjfllssrdsdnnmffldldppcctcmtctffprpsrrrfhfmflldccnpppvsswppdgpgjpgpqgpqpjppclcjczcnzzzqvzzwlzzqfzffsqqphqhvqqbcccstsdttwcwcvvmjjhqqmllpglgtgwgnnbnrbnnjdndhhbrbvrvwwwblwwwppmdmttztqzqrqjjpggpmgpmpqqmssvnsvnndnvddtztddrrsnsbbshhqmhqmmdnnrsrllqvvfjfpfqpqfqflqlmqmrrqgrqrrhvvfddfhhfnhnlncnzzwwjwswpsscpchcssrzssjqsqrsswbbzggnqnwqqsrqqzbqbddtffcjjdggwlwnwtnnpddcqddsppcwpcwpphvphhhrprqrqttwgwqwppvdpvvrgggmpmddzvvwwthwtttbqtbbftfrffbdfbbspshphpwhppnhnmhhbbshbsbmbdmmtrmrbrprbprphhnmhmvhmhhqqbvqvqcvcwcbwcchbbgwwqffrcffsvvcczrztrzzdhdmhdmmzbbvlvqqtptpvvfsfppdzppmddfvdfdwdfdmdwwlvvqdvqdqmdqqmnmdndsswsfwfvwfwzzhrzhhwfhhpvhppmssnhsnsnwswvwlwdwzwnwswwmqmjmbbdjdbbtllcpllltqlttnvvppznpnttlrrwlwvllphpbblccgjcggbtbwtwdwrrbnndpddbqqnpnrnnqqshspsggtptztpzzclcggtqtgqttcmmbgbfbdfdfsddlrlvrlvvmqqgbqgbqbcbbnpplqqblqbqmbbbdqbbhjjcgcdgdwdjwjqqlsqqnnwffglfglgnlggjgjtgjtgjtgjjclcwcqqvpvwwvgvhgvhvjjzpzbznnvrrrpbpdbdhhmshsnnnwppcbcvvlldccdggqnqgnqqzbzzcfflnnmppcgcmgmrgmrgmgngwwptpzzpfzpfzpzdppgcgtcgtgccvtctzzdmzmlmzlzwllbplblltgtctrrghgvhgvvfvssngsschshrhvrvddghgwwwwzgwgvgccjdjzjwwvdvvqsqshsvvddmddbllvppmlpmlmwlwppjmppwrrdzrrpmrmrdmrmbmzzwttvwwsfswwlflnlrlvvdllzbztbtpbtppnjpnpdnnjrjsshtssvvsjvjfjwffsszvsvjssqzsslpslppjpspqptbncvzrlwtjvsrwtnzzhwdfsmlthvgqgjrpshpbsrrvnsdbqslbcplnpcjqwmwqsnwdcjsdmccbdglwbrcdcqsfhjqhstvhqqdwltqwhhqcrnpvnzjhhbjvqbqhclwggjqfvnfsvcnjjhbmrvbpjqrbljbtltvnsgdfhddlmsdhcrfwvlvbsdrjwjvtnqzhrlqgjmzsmjlpdjsrjmdhmvgwjmfwtqffnzfrtswrlgvvhhqgpzcjwscfqgjmdhtvbgzdlvzfhgqlqbfwsjrmmhrlcwhrcnwwvngcmsrfgczsfqvvmdtmtprfvjrwrwcqwvgmzcjncrzvcswlzsdszvdtwmptnrhgzqwrhjjtbchhpwsdjnqmnsgzwqzvlzlsznpqgvtqnldjqpvndtsjlzhpzsgthbwvwnlbwjlmndqpcdvjdgdzhctpghlfwrtqtvfwdpgrjbmwzqgthjpmlrsqmzsznddhrbjnggqrdntpbngvldnnltfnmdwfhftjvpqbrzqvdzbzzctshzldtcdgfnczglrrjtwswzdvjrfgwztwznbpplmbgwpcmstcsjtqhmzmzsjwsfbjlbnbdtdsmlpdmrrbhdhpzrjdpzhcwsrgfrhmqzqtjfhpvltnthwjrrrpnsbmmwrhsfqbmnvwhpntltsgwgnqhcvlndfrtrfrnlmbhltmtgzhlzqgtsbbnggdjvbslfbczhpghqqcqlrbtpnbqbflfjrmpmwwvjqgvcqtmfggmptqlqstcmdtqlslnnzwbgnstftfsvsjdrmgbzfnzltwbjmqhsvshnmwhftjdndltdpngzwbrjpgpwmqgfsflnhmtzcmdjmzwrsrrrmpvwgggwrhrwtfwdbgbpmwpcdspdtbqvdwwsnwdtrtdtgnfzzsmlbcqdzbsqnrgtvvnlfcdlcgcnfpqbddqcjfqtmpndmnwvfqgjzrltqvlprmbbhmtwbzjjgfhhhfjswpffmjnsdmrcjrwlwpmfrmhljpphlwwwwmgsjcsrcvmfrdjdhshddshpplzsnsphcmdhvllgmdgrvbvjmtpltdthffsvwwhvgqrhmfjfpdswcqldrhpmznffjsntwrnmnpmsshljszbchctptsdlnbcvpfvtlfnzcrljpdwrsjnlpqcpnwvnqhzhqmjbvlbtgslzthlbbjzsgdglbrltzjdshpfbndjtssvsjqlstnrjdzzjvlpqhmwvrsvndcqrqjjcsqvmvrbhngtcfdprlbnqmhqllddgjpdzbjlphntrtgjrdgtbslrtzczbnnlddzzsvqvqvvzjpjqfhztgtsfggdppfdhzsbjzqjmpnmgqzlsdhjjbfpbsbnzpmhwrzjqhczrgcsflfwtrgwbnbrshjpwltntsnsdhmhqlmzdprcrcpcpjnphsmjwhzdqtncdbwgspmnfzsgmpbdhmslqchhhbbwfrghhnfjplsvrtbvplgrwdnbnfsgpwrqczvzlnfsngnsnbwvpmfdcmjztdnrllslnwcfwwnwsvztqmgqtfvmdqrrrmwfmphbcvwwttpmwjjbvqrmlwtwfsjdpcbmdlnlzcqntfzzmslshwprjfhwwpbbdfcdjwllfwcznwpjpwrlsfnnbgzjllrgtzcdcvhdhbtlrcvfdvsdjlzsmwwqvpzfhzjlqpfbqstvfrpcchmtwgbrhqqbglrvzmctdlpnvmglgdtzpbdngtfdnmsmwbgjstzbqwqcdlhfrtqqnhqvpfhdrjqvsvstftdgwwnwpfbfbdcfqnqlwpdnfhhfctwrgdqpbpbmgnfsnbpjfctvdtjnsfqlrtctrnjgltndngcmrdphhsqpjhprbngjzqqhnhhrdwlwwpmhzwshvrtzfgzlrhwghvpvfprbbvflltplpptvrmwcrdqndfqbfqtlqqwvphsmcvnbzghvsptrphhfcgdsslhfbcwhtjcmnpbvqrfgpsjgqpnnwwhjjwqrhhqgznwdzjqbtmmjljjwctqtfgwqbdrjwqwbbcftvjwfdfrgvsrlcrccpvfzdrcjvqfbhddpvrrhjrmhdgchrghbzsqpmgnmslfctblwlvphdfpvtdtwpdfsjwssmgnsvsqpdbqngccsplhmjbwjwtzwsbjhwpwcslqjdchmbvzrbgnwvjrrrdtvhtlzlrbwthzlhhqzzpvpwbzrrbrbtpwnhldhqqltqrqdddfwdmjzgctnlrjrjwvddfmjpnptdmrvnqjvsjfrmlvlqsthhsbvnjlsdzrjngfnqdjfssmvgrchbwmwbbvfqfhvrtwghmrpddnwbrbvbmqvfzbjdsnbzgrtmsfhmsmjtrqsgmpnwwbfwtp
|
11
rust/src/day06/main.rs
Normal file
11
rust/src/day06/main.rs
Normal file
|
@ -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));
|
||||||
|
}
|
38
rust/src/day06/part_1.rs
Normal file
38
rust/src/day06/part_1.rs
Normal file
|
@ -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::<Vec<_>>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
rust/src/day06/part_2.rs
Normal file
38
rust/src/day06/part_2.rs
Normal file
|
@ -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::<Vec<_>>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
rust/src/day06/sample_inputs/1.txt
Normal file
1
rust/src/day06/sample_inputs/1.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
mjqjpqmgbljsphdztnvjfqwrcgsmlb
|
1
rust/src/day06/sample_inputs/2.txt
Normal file
1
rust/src/day06/sample_inputs/2.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
bvwbjplbgvbhsrlpgdmjqwftvncz
|
1
rust/src/day06/sample_inputs/3.txt
Normal file
1
rust/src/day06/sample_inputs/3.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
nppdvjthqldpwncqszvftbrmjlhg
|
1
rust/src/day06/sample_inputs/4.txt
Normal file
1
rust/src/day06/sample_inputs/4.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
|
1
rust/src/day06/sample_inputs/5.txt
Normal file
1
rust/src/day06/sample_inputs/5.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
|
Loading…
Reference in a new issue