summaryrefslogtreecommitdiffstats
path: root/src/bin/2022/day6.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/2022/day6.rs')
-rw-r--r--src/bin/2022/day6.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/bin/2022/day6.rs b/src/bin/2022/day6.rs
new file mode 100644
index 0000000..6fe154f
--- /dev/null
+++ b/src/bin/2022/day6.rs
@@ -0,0 +1,38 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+use advent_of_code::prelude::*;
+
+pub fn parse(fh: File) -> Result<String> {
+ Ok(parse::string(fh))
+}
+
+pub fn part1(s: String) -> Result<usize> {
+ for (i, slice) in s.as_bytes().windows(4).enumerate() {
+ if slice.iter().copied().collect::<HashSet<u8>>().len() == 4 {
+ return Ok(i + 4);
+ }
+ }
+ Err(anyhow!("couldn't find marker"))
+}
+
+pub fn part2(s: String) -> Result<usize> {
+ for (i, slice) in s.as_bytes().windows(14).enumerate() {
+ if slice.iter().copied().collect::<HashSet<u8>>().len() == 14 {
+ return Ok(i + 14);
+ }
+ }
+ Err(anyhow!("couldn't find marker"))
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2022, 6).unwrap()).unwrap()).unwrap(),
+ 1155
+ );
+ assert_eq!(
+ part2(parse(parse::data(2022, 6).unwrap()).unwrap()).unwrap(),
+ 2789
+ );
+}