Last active
December 6, 2022 16:19
-
-
Save skatenerd/c8d8de75cc52633ea10cbd95a64a09af to your computer and use it in GitHub Desktop.
advent of code day 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module DaySix | |
( signalStart | |
) where | |
import qualified Data.Maybe as M | |
import qualified Data.List as L | |
import Data.Set as S | |
countDistinct :: (Ord a) => [a] -> Int | |
countDistinct = S.size . S.fromList | |
hasNDistinct :: (Ord a) => Int -> [a] -> Bool | |
hasNDistinct required candidate = (countDistinct candidate) == required | |
-- stolen from https://github.com/MarkDBlackwell/ruby-methods-haskell/blob/master/RubyMethods/EachCons.hs | |
batches digits items = L.transpose shortenedLists | |
where shortenedLists = L.map dropFromItems [0..digits-1] | |
dropFromItems n = L.drop n items | |
findIndexDangerous predicate items = M.fromJust $ L.findIndex predicate items | |
signalStart :: (Ord a) => [a] -> Int -> Int | |
signalStart candidate requiredDigits = requiredDigits + (findIndexDangerous (hasNDistinct requiredDigits) $ batches requiredDigits candidate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment