-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_1.hs
31 lines (21 loc) · 855 Bytes
/
3_1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Data.Char(digitToInt)
import Data.Bits(shiftL)
main = do
contents <- getContents
let contentList = lines contents
let bitStrings = map getBits contentList
accumulated = foldl zipBits (head bitStrings) (init bitStrings)
gamma = getGammaRate accumulated (length contentList)
epsilon = map (\x -> if x == 0 then 1 else 0) gamma
gammaInteger = bitsToInt gamma
epsilonInteger = bitsToInt epsilon
print gamma
print (gammaInteger, epsilonInteger)
print ("Result is " ++ show (gammaInteger * epsilonInteger))
return ()
bitsToInt = foldl (\acc v -> acc `shiftL` 1 + v) 0
getBits :: [Char] -> [Int]
getBits = map digitToInt
zipBits = zipWith (+)
getGammaRate :: [Int] -> Int -> [Int]
getGammaRate items length = map (\x -> if x >= length `div` 2 then 1 else 0) items