From 609cf69d407c41a17fe920f8fed4686c71e82fe7 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 3 Dec 2021 22:06:32 +0100 Subject: [PATCH] Improvements --- day3.hs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/day3.hs b/day3.hs index 9fa9fbc..2dfa0fb 100644 --- a/day3.hs +++ b/day3.hs @@ -1034,20 +1034,18 @@ bitsAt input pos = bits $ foldl (count pos) (0, 0) input toNumber :: Foldable t => t Bool -> Int toNumber = foldl addBit 0 - where addBit a b = a `shiftL` 1 + (if b then 1 else 0) + where addBit a b = a `shiftL` 1 .|. (if b then 1 else 0) + +combineList [] = ([], []) +combineList ((a, b) : rest) = (a : arest, b: brest) + where (arest, brest) = combineList rest {- ->>> import Data.Bits ->>> 1 `shiftL` 1 -2 - ->>> (toNumber $ snd <$> (bitsAt input) <$> [0..11]) * (toNumber $ fst <$> (bitsAt input) <$> [0..11]) +>>> (a, b) = ( combineList $ (bitsAt input) <$> [0..(length $ head input) - 1]) +>>> toNumber a * toNumber b 2003336 ->>> toNumber [False, True, True, False] -6 - -} @@ -1057,23 +1055,23 @@ filterUntilLast pred list = filterUntilLastAt 0 pred list | otherwise = filterUntilLastAt (pos + 1) pred (filter (pred pos list) list) -oxyFilter pos list item = +oxyFilter :: Int -> [[Char]] -> [Char] -> Bool +oxyFilter = makeFilter (<=) + +co2Filter :: Int -> [[Char]] -> [Char] -> Bool +co2Filter = makeFilter (>) + +makeFilter predicate pos list item = item !! pos == keep - where + where (zero, one) = foldl (count pos) (0, 0) list - keep = if zero > one then '0' else '1' + keep = if predicate zero one then '1' else '0' -co2Filter pos list item = - item !! pos == keep - where - (zero, one) = foldl (count pos) (0, 0) list - keep = if zero > one then '1' else '0' - - -{- +{- | >>> toNumber [x == '1' | x <- filterUntilLast co2Filter input] 737 + >>> toNumber [x == '1' | x <- filterUntilLast oxyFilter input] 2547