advent-0.1.0.0: Advent of Code common library
Copyright(c) Eric Mertens 2018-2021
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Advent.Prelude

Description

Various helper functions for common operations needed in Advent of Code problems.

Synopsis

Documentation

count :: (Foldable f, Eq a) => a -> f a -> Int Source #

Count the number of elements in a foldable value that satisfy a predicate.

countBy :: Foldable f => (a -> Bool) -> f a -> Int Source #

Count the number of elements in a foldable value that satisfy a predicate.

same :: (Foldable t, Eq a) => t a -> Bool Source #

Return true when the whole list is comprised of equal elements.

>>> same [1,1,1]
True
>>> same []
True
>>> same [1]
True
>>> same [1,1,2]
False

pickOne :: [a] -> [(a, [a])] Source #

Returns a list of ways to select an element from a list without replacement.

>>> pickOne []
[]
>>> pickOne [1]
[(1,[])]
>>> pickOne [1,2,3]
[(1,[2,3]),(2,[1,3]),(3,[1,2])]

ordNub :: Ord a => [a] -> [a] Source #

Implementation of nub that uses Ord for efficiency.

minimumMaybe :: Ord a => [a] -> Maybe a Source #

Compute the minimum element of a list or return Nothing if it is empty.

>>> minimumMaybe []
Nothing
>>> minimumMaybe [2,1,3]
Just 1

maximumMaybe :: (Foldable f, Ord a) => f a -> Maybe a Source #

Compute the maximum element of a list or return Nothing if it is empty.

>>> maximumMaybe []
Nothing
>>> maximumMaybe [2,1,3]
Just 3

counts :: (Foldable f, Ord a) => f a -> Map a Int Source #

Compute the number of occurrences of the elements in a given list.

>>> counts "bababc"
fromList [('a',2),('b',3),('c',1)]

compose :: [a -> a] -> a -> a Source #

Compose a list of functions together

>>> compose [ (1:), (2:), (3:) ] []
[1,2,3]

chunks :: Int -> [a] -> [[a]] Source #

Split list into chunks. The last chunk might be incomplete.

>>> chunks 3 [1..9]
[[1,2,3],[4,5,6],[7,8,9]]
>>> chunks 3 [1..7]
[[1,2,3],[4,5,6],[7]]
>>> chunks 3 []
[]

möb :: (((a -> b) -> b) -> c -> a) -> c -> a Source #

löb generalized over fmap

arrIx :: (IArray a e, Ix i, Alternative f) => a i e -> i -> f e Source #

Index an array returning Nothing if the index is out of bounds.

times :: Int -> (a -> a) -> a -> a Source #

Apply a function n times strictly.

timesM :: Monad m => Int -> (a -> m a) -> a -> m a Source #

Apply a function n times strictly.

uniqueAssignment Source #

Arguments

:: (Traversable t, Ord a) 
=> t (Set a)

element must map to one of the corresponding set members

-> [t a]

possible assignments

Given a list of constraints such that each constraint identifies a unique variable and the set of assignments it can have, this computes assignments of those variables such that no two input variables are assigned the same value.

>>> uniqueAssignment [Set.fromList "ab", Set.fromList "bc"]
["ab","ac","bc"]

fromDigits :: (HasCallStack, Integral a) => a -> [a] -> a Source #

Convert a big-endian list of digits to a single number.

>>> fromDigits 10 [1,2,3,4]
1234
>>> fromDigits 2 [12]
12
>>> fromDigits 10 []
0

toDigits :: (HasCallStack, Integral a) => a -> a -> [a] Source #

Convert a number to a list of digits in a given radix.

>>> toDigits 2 12
[1,1,0,0]
>>> toDigits 10 1234
[1,2,3,4]
>>> toDigits 10 0
[]

power :: HasCallStack => (a -> a -> a) -> a -> Integer -> a Source #

Efficient exponentiation using an associative operator

>>> power (+) 1 10
10
>>> power (*) 2 10
1024 

scanlM :: (Traversable t, Monad m) => (b -> a -> m (c, a)) -> a -> t b -> m (t c, a) Source #

stageTH :: DecsQ Source #

Helper for putting declarations into scope for future Template Haskell expressions. In particular this gets used so that the format quasiquoter can see data types that it might want to parse.

partialSums :: Num a => [a] -> [a] Source #

Partial sums of a list.

partialSums = map sum . inits
>>> partialSums [1..3]
[0,1,3,6]

binSearchLargest Source #

Arguments

:: (Int -> Bool)

predicate

-> Int

small enough

-> Int

too big

-> Int 

Binary search for the largest value satisfying a predicate. Finds the largest value when the predicate switches from True to False. There should only be one such point in the range.