diff --git a/2022/README.md b/2022/README.md index 783d366..0660d6b 100644 --- a/2022/README.md +++ b/2022/README.md @@ -3,7 +3,7 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :--: | | | | | | [1] | [2] | [3] | -| [4] | [5] | 6 | 7 | 8 | 9 | 10 | +| [4] | [5] | [6] | 7 | 8 | 9 | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 24 | | | | | | | @@ -13,3 +13,4 @@ [3]: ./src/Aoc/Day3.hs [4]: ./src/Aoc/Day4.hs [5]: ./src/Aoc/Day5.hs +[6]: ./src/Aoc/Day6.hs diff --git a/2022/aoc.cabal b/2022/aoc.cabal index d000c4e..586c81c 100644 --- a/2022/aoc.cabal +++ b/2022/aoc.cabal @@ -31,6 +31,7 @@ library Aoc.Day3 Aoc.Day4 Aoc.Day5 + Aoc.Day6 other-modules: Paths_aoc hs-source-dirs: diff --git a/2022/app/Main.hs b/2022/app/Main.hs index 1f62862..0489d06 100644 --- a/2022/app/Main.hs +++ b/2022/app/Main.hs @@ -7,6 +7,7 @@ import qualified Aoc.Day2 import qualified Aoc.Day3 import qualified Aoc.Day4 import qualified Aoc.Day5 +import qualified Aoc.Day6 main :: IO () main = do @@ -21,6 +22,7 @@ main = do 3 -> Aoc.Day3.solve 4 -> Aoc.Day4.solve 5 -> Aoc.Day5.solve + 6 -> Aoc.Day6.solve _ -> error "unknown day" putStrLn $ f part contents diff --git a/2022/src/Aoc/Day6.hs b/2022/src/Aoc/Day6.hs new file mode 100644 index 0000000..ef5d61a --- /dev/null +++ b/2022/src/Aoc/Day6.hs @@ -0,0 +1,18 @@ +module Aoc.Day6 (solve) where + +import Data.List (nub, tails) + +-- https://stackoverflow.com/questions/27726739/implementing-an-efficient-sliding-window-algorithm-in-haskell#comment43882101_27733778 +windows :: Int -> [a] -> [[a]] +windows n = foldr (zipWith (:)) (repeat []) . take n . tails + +sequenceOffset :: Eq a => Int -> [a] -> Int +sequenceOffset len xs = len + (length $ takeWhile dupes $ windows len xs) + where + dupes ys = length ys > (length $ nub ys) + +solve :: Integer -> String -> String +solve 1 input = show $ sequenceOffset 4 input +solve 2 input = show $ sequenceOffset 14 input + +solve _ _ = "?" diff --git a/README.md b/README.md index c2837ae..7a786c1 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 1. 2019 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 -1. [2022] **10/50** 🌟 +1. [2022] **12/50** 🌟 [2015]: ./2015 [2017]: ./2017