AdventOfCode/Haskell/2021/03/day3.hs

58 lines
16 KiB
Haskell
Raw Normal View History

getBit :: String -> Int -> Char
getBit x n = head (drop (n - 1) x)
data1 = ["00100", "11110", "10110", "10111", "10101", "01111", "00111", "11100", "10000", "11001", "00010", "01010"]
mostCommon :: String -> Int -> Int -> Char
mostCommon [] x y
| x > y = '1'
| y > x = '0'
| otherwise = '1'
mostCommon st x y
| bit == '1' = mostCommon (tail st) (x + 1) y
| bit == '0' = mostCommon (tail st) x (y + 1)
| otherwise = error "Not 1 or 0"
where bit = head st
leastCommon :: String -> Int -> Int -> Char
leastCommon [] x y
| x < y = '1'
| y < x = '0'
| otherwise = '0'
leastCommon st x y
| bit == '1' = leastCommon (tail st) (x + 1) y
| bit == '0' = leastCommon (tail st) x (y + 1)
| otherwise = error "Not 1 or 0"
where bit = head st
oxygenRating :: [String] -> Int -> String
oxygenRating [st] _ = st
oxygenRating sts x
| bit == '1' = oxygenRating [ st | st <- sts, (getBit st x) == '1'] (x + 1)
| bit == '0' = oxygenRating [ st | st <- sts, (getBit st x) == '0'] (x + 1)
| otherwise = error "Not 1 or 0"
where bit = mostCommon [getBit y x | y <- sts] 0 0
c02Rating :: [String] -> Int -> String
c02Rating [st] _ = st
c02Rating sts x
| bit == '1' = c02Rating [ st | st <- sts, (getBit st x) == '1'] (x + 1)
| bit == '0' = c02Rating [ st | st <- sts, (getBit st x) == '0'] (x + 1)
| otherwise = error "Not 1 or 0"
where bit = leastCommon [getBit y x | y <- sts] 0 0
makeList :: [String] -> Int -> String
makeList _ 0 = []
makeList l x = mostCommon [getBit y x | y <- l] 0 0 : makeList l (x - 1)
inverse :: String -> String
inverse [] = []
inverse l
| bit == '1' = '0' : inverse (tail l)
| bit == '0' = '1' : inverse (tail l)
where bit = head l
data2 = ["101000111100","000011111101","011100000100","100100010000","011110010100","101001100000","110001010000","111110011011","000110100110","110100000001","110100001001","010000100011","000010011100","111001110001","011001000010","100011100110","101010100111","001011001101","001101100100","111010110100","010101011100","001010010000","101001111001","101110010001","110100011100","001010111110","011110110100","110110101110","000000101101","001100000110","110010110001","010110101110","100111000111","000010101111","010101111011","011101000100","010000011011","110011111111","000001100100","100100110110","100001101001","110001000010","010111110110","101101011001","010101101101","010001101100","100010000000","111111001001","111101001011","010110011001","011000000100","011100101000","111101001000","111000110010","110000000110","001101011000","101110010011","100010011111","111110010000","000011010011","111110100010","111111001111","101001011110","111001101001","111010011100","001011001111","001010011110","110111101010","101111101101","011101110101","001111110101","010111110011","000010011000","111111111000","000101001100","110011001000","100010100110","100110101110","001010101101","110000111000","101100010110","000000100101","111001101101","111010001101","111110000111","010101000010","100000101101","000011100011","011011000111","011000011111","100101011100","101011000110","111110001110","001010011101","101001101001","001101111110","000100000010","011110000001","010111101101","010101100010","000110000011","100001111010","011001011000","001011110100","001101111011","001110110010","001010101000","100001001100","101010100110","100010101011","111000010000","100101100111","010001011001","010010101101","001001000111","100010111100","001011100011","100100001101","110000110010","100010011101","111001011100","101101011011","010011111111","010100111111","000101100011","111001110111","000010100100","101100000000","111110100011","110101111111","001101001101","010110000110","110100010100","011100011100","110011001100","000001010111","110100001000","110110100111","010000001111","100011111110","001000010000","011101001001","100110101111","000010101101","100011010101","010101001000","110101011101","111111101010","101011101101","010101001001","101101000110","110110101000","110100100101","000011110101","001110101001","011101011110","111111110100","010111000011","100101010000","010011111010","111001101100","100100010101","100101100001","001111000111","110100101001","010010011100","011001000101","111101010001","001100010000","111100000011","000101001000","010011010111","101110011000","110000111010","110010001011","111110110010","000000001011","011001111001","001001001110","100111001111","110110001010","110110010101","110011001101","010001101001","000010100111","100111111110","111011100001","011000100000","111111000100","111100100010","110110110110","001100111110","101000100001","000010000101","111101011101","001001001101","101011001101","000010010001","011010110001","110000000010","000111001000","111110101100","011111000111","010101111000","010100010110","111111100100","110011010001","010110001110","000100010011","100101100011","101010001011","001010111101","111110111000","101001100011","011100110010","101001101010","111011110000","011101110000","000001101111","111101101100","110101000101","011000110101","000111100101","111110011101","000111101010","010011100101","101001001001","100010101000","111111100011","011011101111","001011111001","001001101011","100101100101","100011111000","010110100001","000100111111","010100111101","010100100000","110000101000","100001011000","100010011000","100011101111","001100010001","010001010001","011011001001","001001110110","111011011100","000110010000","111110011111","001111101000","010010010001","000011001110","101011101100","001000110110","111000010001","010111000101","110000001110","000011111010","010000010100","011101001110","000111011011","000111110010","111110110101","101110100001","111101011100","010001111001","010011011000","111110111011","111000100110","000110001001","100000111110","001101101010","10110