1
0
Fork 0

solve 2022 day 6 pt 1 & 2

This commit is contained in:
Sloane Perrault 2022-12-08 23:06:29 -05:00
parent 217c8cde38
commit e328f92f8d
No known key found for this signature in database
5 changed files with 24 additions and 2 deletions

View file

@ -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

View file

@ -31,6 +31,7 @@ library
Aoc.Day3
Aoc.Day4
Aoc.Day5
Aoc.Day6
other-modules:
Paths_aoc
hs-source-dirs:

View file

@ -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

18
2022/src/Aoc/Day6.hs Normal file
View file

@ -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 _ _ = "?"

View file

@ -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