-
Notifications
You must be signed in to change notification settings - Fork 0
/
3b.hs
35 lines (30 loc) · 1018 Bytes
/
3b.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
32
33
34
35
import Data.List
bintodec :: [Bool] -> Int
bintodec xs = foldl (\acc x -> 2*acc+(fromEnum x)) 0 xs
stringtobin :: String -> [Bool]
stringtobin xs = map (=='1') xs
calcCommon :: [String] -> [Bool]
calcCommon xs =
let revList = transpose xs
counts = map (\x -> length $ filter (=='1') x) revList
greaters = map (\x -> x >= ((length xs + 1) `div` 2)) counts
in greaters
recurse :: [String] -> Int -> Char -> String
recurse strs ind inv
| length strs == 1 = head strs
| (ind >= length (head strs)) = head strs
| otherwise =
let com = calcCommon strs
cbool = (com !! ind)
filtL = \c -> ((c !! ind) == inv) == cbool
filtStrs = filter filtL strs
in recurse filtStrs (ind+1) inv
main :: IO()
main = do
content <- getContents
let binList = lines content
oxy = recurse binList 0 '1'
co2 = recurse binList 0 '0'
conv = bintodec . stringtobin
ans = (conv oxy) * (conv co2)
putStrLn $ show ans