aboutsummaryrefslogtreecommitdiffstats
path: root/utils.hs
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2021-12-20 14:53:14 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2021-12-20 14:53:14 +0200
commit8cf112cad7c0a52676a834ec3ac77f3006031c87 (patch)
tree2c8aba70caa310f7c00ebe1a5eef739bbc3fe980 /utils.hs
parent47a2c42249aab79dd2aec4554d3387d2b47ced21 (diff)
Day 20
Diffstat (limited to 'utils.hs')
-rw-r--r--utils.hs10
1 files changed, 9 insertions, 1 deletions
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