From 8cf112cad7c0a52676a834ec3ac77f3006031c87 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 20 Dec 2021 14:53:14 +0200 Subject: Day 20 --- utils.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'utils.hs') diff --git a/utils.hs b/utils.hs index e7e0d4b..c67fd07 100644 --- a/utils.hs +++ b/utils.hs @@ -1,6 +1,7 @@ module Utils where import Control.Applicative (ZipList (ZipList, getZipList)) +import Data.Char (digitToInt) (.>) = flip (.) @@ -46,4 +47,11 @@ takeUntil _ [] = [] takeUntil p (x : xs) = x : if p x then takeUntil p xs else [] fromSymEither (Left x) = x -fromSymEither (Right x) = x \ No newline at end of file +fromSymEither (Right x) = x + +-- | Convert binary (list of zeros and ones) to decimal +binaryToInt :: String -> Int +binaryToInt = reverse .> binaryToIntRev + +binaryToIntRev [] = 0 +binaryToIntRev (x : xs) = digitToInt x + 2 * binaryToIntRev xs -- cgit v1.3