Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Created December 12, 2022 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/cf4ffb90aa48c82cf8e4946e941c5940 to your computer and use it in GitHub Desktop.
Save dminuoso/cf4ffb90aa48c82cf8e4946e941c5940 to your computer and use it in GitHub Desktop.
findMarker :: Int -> Int -> Int -> BS.ByteString -> Int
findMarker l o n buf = do
case BS.elemIndexEnd c window of
Nothing | o <= 0 -> n
| otherwise -> findMarker l (o - 1) (n + 1) buf
Just i -> let o' = max i (o - 1)
in findMarker l o' (n + 1) buf
where
c = BS.index buf (n - 1)
window = BS.take windowW (BS.drop windowL buf)
windowW = min l n - 1
windowL = max (n - l) 0
main = do
buf <- BS.readFile "day6/input.txt"
let l = 14
print (findMarker l 14 1 buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment