From 42523f2455fb30181efc29e3a47799283b05fa80 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 11 Dec 2020 15:49:03 +0530 Subject: add initial solution to day11 --- lib/Utils.hs | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/Utils.hs b/lib/Utils.hs index 61c4e49..6358230 100644 --- a/lib/Utils.hs +++ b/lib/Utils.hs @@ -1,19 +1,11 @@ -module Utils ( binaryToInt - , countElem - , xor - , right - , bet - , (&+) - , howMany - , sublists - , windows - ) where - -import Data.Char (digitToInt) -import Control.Monad -import Data.Either -import Data.List (inits, tails) +module Utils where +import Data.Char (digitToInt) +import Control.Monad +import Data.Either +import Data.List (inits, tails) +import Data.Map (Map) +import qualified Data.Map as Map binaryToInt :: String -> Int binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 @@ -28,13 +20,16 @@ right :: Either a b -> b right (Right b) = b right _ = undefined -bet :: Int -> (Int, Int) -> Bool bet k (l, u) = k >= l && k <= u +bet' k (l, u) = k > l && k < u -- combine filter predicates (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) (&+) = liftM2 (&&) +(|+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) +(|+) = liftM2 (||) + howMany :: (a -> Bool) -> [a] -> Int howMany predicate = length . filter predicate @@ -49,3 +44,20 @@ kadane = go 0 0 where go :: Int -> Int -> [Int] -> Int go best _ [] = best go best current (l:ls) = go (max best (current + l)) (max current (current + l)) ls + +-- tuple stuff + +add (x, y) (a, b) = (x+a, y+b) +shear c (x, y) = (c * x, c * y) +inside (p, q) (r, s) (a, b) = bet a (p, r) && bet b (q, s) +inside' (p, q) (r, s) (a, b) = bet' a (p, r) && bet' b (q, s) + +-- [f, f.f, f.f.f, ...] +repeatF f = f : map (f .) (repeatF f) + +makeGrid :: String -> (Map (Int, Int) Char, Int, Int) +makeGrid s = (grid, width, height) where + rows = lines s + grid = Map.fromList [((x, y), a) | (y, row) <- zip [0..] rows , (x, a) <- zip [0..] row] + width = length (head rows) + height = length rows -- cgit v1.2.3