diff --git a/3/example.txt b/3/example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/3/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/3/input.txt b/3/input.txt new file mode 100644 index 0000000..ebaf337 --- /dev/null +++ b/3/input.txt @@ -0,0 +1,1000 @@ +111010101100 +100001001100 +000111101100 +100100000000 +001001001110 +100110101011 +001001100101 +010000010110 +011011001001 +001001000101 +001000101011 +000111101111 +011000000001 +111111010100 +101101010010 +100111100111 +101000110100 +011101101101 +010101110010 +001011101010 +010001001100 +011001001100 +000110011100 +011001011100 +000101110110 +010011110111 +011101001001 +000001000111 +111110010100 +010011000000 +101001000010 +011001010011 +101011101010 +111000100111 +011110010110 +111111111010 +100011100101 +011100110000 +010011010011 +110110110010 +001110111011 +110100011111 +101011001110 +000001100101 +101001010000 +110001111011 +011101011100 +001100100001 +000100111111 +000110110111 +011100100010 +110101100111 +100110011110 +110011110010 +010111110110 +011010100010 +100100100010 +001000111011 +100101101110 +001001111101 +111000001100 +100011000100 +101011010010 +011100011001 +111100011001 +110000000011 +101110011010 +101000010011 +111011100111 +101111111100 +101111111101 +010000101101 +011111011011 +100011001100 +010011100010 +101100101011 +010011111010 +111010010101 +000011101100 +110011110101 +101000000101 +101101001110 +010111110101 +010001011001 +100000000011 +111111010000 +001110010010 +011110111101 +111010110001 +010001011010 +100101011100 +101100000100 +000001000110 +010010101001 +110101100101 +010000001111 +001010000100 +111011111100 +011111101000 +111100111101 +011100001111 +001101111010 +110100010000 +011111001101 +111001110001 +110010000100 +000110100011 +010000100010 +110010111001 +111100111010 +110110101011 +111011001110 +111000110101 +101110100110 +011100100001 +111110100010 +001010000001 +111000111111 +110010110101 +010101010100 +100101010111 +111111101110 +100101100111 +001011101000 +110010110001 +101010000111 +000000000100 +100000100100 +110001001000 +001101010100 +010101101010 +111100100000 +111100110010 +110100111000 +111110011000 +110001100111 +100111101100 +000001100110 +110111000111 +010011000111 +110111101100 +100111100000 +010101001011 +010011100101 +110000100010 +000010001010 +010001101010 +101010011101 +100011001011 +011000111001 +011010100001 +011011011110 +010000011101 +001010101101 +110011101011 +001111011110 +111111110110 +000101100110 +001110100011 +001010111001 +011010111101 +011000001001 +010101000010 +101100011011 +001110110101 +010111010100 +010110011100 +010101000111 +110000010010 +111011110110 +100101100110 +000011110010 +100111110000 +101110100010 +100010001101 +011111010100 +111101011000 +010010111010 +011111001111 +011100000010 +111000100011 +001010000110 +000101110001 +101001000111 +111111101011 +001010000010 +110011101101 +111000100100 +010111111100 +010000100101 +000111000101 +101001000100 +000001100011 +011011111100 +100011110000 +011100011111 +000011010100 +001110111101 +101001011110 +111100011000 +001100111011 +101101010100 +110101001000 +100010000011 +001101100110 +111100101011 +001101100101 +100000000111 +011001111101 +000111110101 +100100001000 +100001000010 +011011001110 +110000110010 +010110101111 +111001111000 +100110010111 +101111100000 +110000110110 +100010000100 +001110001110 +111010110111 +010111010000 +001110111000 +000101110010 +111011001011 +000011011011 +110100011001 +101010100101 +010001111110 +011101000000 +011001111100 +101101100010 +010000101111 +010010100011 +010010010001 +100000110000 +010100111011 +011101001011 +111001011110 +010100011111 +011111111100 +110110000100 +111001010000 +111000110011 +101110010111 +011001110010 +111111000100 +000001010000 +001011010101 +000000011101 +111111110001 +100011100010 +001101011101 +011111110101 +100101111001 +100100111110 +011110010010 +010110110000 +011101000101 +001000001110 +100111011001 +100110101111 +010001001111 +110011011111 +010100000001 +111100110001 +011101000111 +010110101000 +000110110000 +111101101101 +010010111011 +010011011100 +111110101101 +101001101111 +101110001111 +010101110110 +110100100011 +000000111000 +011100011100 +111001010111 +111000000111 +001101110000 +000000110010 +000110110011 +100101110010 +000100101010 +100000110110 +111100110100 +101001101110 +011100101110 +000111011111 +100111101010 +010011000101 +011000100111 +011100011011 +011110000011 +110110111001 +011111111111 +001010101111 +110111011011 +111111000111 +101010000000 +001100001001 +101001000001 +101100111110 +011000001101 +011101001010 +011011101100 +110111000000 +100100010011 +000111110111 +000101010010 +010000001010 +010010100101 +110001100101 +010101011110 +001100110001 +001111111000 +110000001111 +001101101110 +100111000101 +001010010001 +100100110110 +010011100111 +111101010001 +010010101010 +000010101001 +100111100101 +001100011011 +011101111010 +110010000111 +011101110000 +001011001010 +110101110010 +110111100001 +111000000011 +010010111111 +010011110101 +011000101000 +000101000010 +001110000000 +101010001100 +010011101010 +101100111101 +011110101011 +011110110010 +110101100110 +010001110000 +000011000100 +011110100110 +110101101010 +100110001000 +111100000001 +001000111000 +110001000110 +000111100111 +111111011101 +000001101010 +111101011110 +110011010011 +010100101011 +110010110010 +011011111101 +101110101001 +100011001101 +010110110010 +111001100011 +101101101001 +000100110001 +001011001110 +010011000011 +101111100001 +110011010000 +000011010101 +011100111110 +110100110100 +010100001001 +100010100111 +100101000111 +011010011000 +000000101101 +001111100000 +110001101110 +110110111111 +110100111100 +001010110001 +010001100010 +100000110100 +011110011100 +111010001110 +101111110001 +110000011011 +000100001011 +110110001101 +010000100001 +011001010000 +010110100110 +001100101010 +100101101011 +111110111001 +101011110000 +000101001010 +111001001101 +110100001111 +000111110000 +000010001111 +111101011010 +000011100111 +101000100000 +100011001000 +000000100011 +011011001101 +111101111101 +010001010110 +010001100100 +110111101110 +101111101001 +011101010101 +011000001011 +000100011110 +100100100011 +000110011111 +010001010101 +001110000011 +011100100100 +101111100111 +110101010000 +000101110100 +001011100010 +101000010101 +100000100111 +100111100001 +111100001011 +100101001100 +100000000001 +101110110100 +000101011100 +010111000101 +111010001010 +100111001010 +101011110001 +100111010101 +001101111000 +110011101100 +011011011111 +111100010100 +011101011111 +001011010110 +000101100111 +001010110110 +101000000100 +110010111011 +001011000100 +110000111100 +101001001111 +011000101110 +101111110010 +110101001110 +010100111110 +110100101100 +100011010110 +011101001100 +110101101001 +110111110110 +101001011010 +001100111100 +001010100111 +100001001001 +110110100011 +010111101011 +100000010101 +011000010010 +111000100010 +100111101011 +010101011101 +111100011111 +001010111111 +010111111010 +010000111000 +000011000101 +110100110011 +101101111110 +101110000101 +011101111110 +010000011000 +011111110111 +100001000100 +010101100110 +001001000010 +010011111000 +101111111011 +001000110101 +101110101101 +100101001101 +010001111111 +000001000001 +100000010010 +001010101110 +000111110100 +011100110011 +000100001110 +011100010011 +110100111101 +000111001100 +001011011100 +000110001000 +011100011110 +100101111010 +111110011110 +101001111010 +011000010111 +011111010010 +001101101011 +100001010010 +110110001001 +101000100110 +100000000100 +111010000100 +000010101000 +111011101000 +010110100101 +100110010110 +110110000101 +011011110011 +111110101010 +011000101101 +001010011101 +001001101001 +000010010110 +100010001010 +100001101111 +110010111000 +100011100110 +011011110110 +001000010001 +011000011110 +011011010001 +100100000001 +011111001011 +001100001111 +110010111010 +000101000111 +100011011010 +011110110001 +101001000101 +111111010111 +110000010101 +101111001000 +000010111011 +100000011001 +111111111110 +101111011110 +010000101000 +010011011101 +000100000111 +110100101011 +101101100110 +011111011000 +101001001101 +000110111110 +101001010011 +011100011010 +010100110111 +101001000011 +001000101010 +011110111111 +000101100101 +111111111001 +111011100110 +000100000001 +001001001100 +110100001011 +000001001111 +111000011101 +000111001110 +010000110101 +011011110101 +000101011010 +110110111101 +010011000100 +001111011000 +001001110101 +100110000110 +010110100011 +110010101110 +001111011010 +000010110011 +110010001000 +110001000010 +100100000100 +000110000110 +001000011010 +111100001000 +011001101010 +110100110010 +111001101001 +101111001100 +101100111001 +100010100010 +101011001011 +001010111100 +111001101110 +111110011001 +001100110101 +110110010010 +001100001101 +001100110110 +101100010101 +110111110000 +100011011001 +100001111011 +010000000011 +011101011110 +010011110001 +101101011011 +000011101000 +010111011010 +111000101101 +000111000001 +111000001110 +010110011111 +100110111101 +101111001101 +101100100101 +111000110111 +110010011100 +111010000010 +011100111011 +101010001101 +000000111101 +100000100110 +001011110110 +111010111110 +100110111100 +001111001110 +010110100001 +110011110100 +000110101110 +011011000100 +010000010010 +000100110101 +100100011100 +001100001100 +101110000001 +000101000000 +110001011111 +111001101100 +111100101111 +100010110001 +111111000101 +111100111111 +011010011001 +010001000110 +011011111111 +100101001111 +010000001000 +101011101001 +011011000001 +000100000000 +101110101010 +100100110011 +010110000001 +100101000101 +000100110000 +111101010111 +000100100010 +100110100000 +011000010100 +111010100010 +010101100000 +101000001000 +011011010100 +110101001011 +101000101011 +111100000011 +001101000101 +001011110010 +111111011011 +111001001001 +001001001000 +000000000010 +111100111110 +010100101111 +110111110101 +111101000011 +100101010100 +000110001111 +110000001101 +011010011010 +000001011001 +011000111000 +011111111011 +101101101010 +110110011001 +100000111011 +110000111111 +101101010111 +001001001010 +100011011000 +101001110101 +100000101111 +100011010000 +000000001110 +101010011010 +000101111011 +110101110001 +000010011011 +000110011011 +101100101010 +100111111110 +110111111100 +110000011001 +001011001100 +111010010001 +111010101110 +111000100000 +001100110100 +110100010101 +011000011010 +011101000100 +000011100101 +101111100011 +011110000110 +000100010001 +111101000100 +111000000100 +000001111001 +100000111111 +001000001101 +100110001110 +010010111110 +000001011111 +110001011101 +101010010000 +111110110010 +111010010000 +101101000111 +111110110111 +001110100001 +111110000011 +110001001101 +001000101101 +000100110110 +110111010111 +100010100100 +110100001101 +111100110011 +001100000101 +101011001101 +000000111011 +101110011000 +100100100000 +001101001111 +001110000111 +011100011000 +001100000011 +101001011100 +110100010010 +011011110001 +000100110010 +001000010101 +101000011110 +001100100010 +010100100011 +100110001101 +111111001101 +011000111101 +101101011010 +001100000100 +101100011100 +000010110000 +100110110010 +011100110111 +111001111101 +001110010111 +101010111111 +100011101011 +110010000011 +111000010101 +101101101101 +001111100110 +000101110000 +110100000111 +010100010110 +111000010010 +101011111110 +111101010010 +011010000101 +000100010000 +001010100101 +000000110111 +010011010111 +110010101001 +110010001100 +100001111101 +011100000011 +010000000100 +111000000000 +010000100011 +101111001011 +101011110110 +110110101000 +111111011010 +001001000100 +101001011000 +001000001001 +101001111101 +110000110001 +101001010111 +110101100010 +101101111111 +101101011110 +111111011001 +111010110011 +101111000001 +010001010111 +110100000011 +100110101100 +100100101011 +010100001110 +101111101111 +111111001110 +111110111000 +010101110011 +101101001010 +100100111101 +110100100010 +100011101001 +110100111001 +101000000000 +110111101011 +101111001001 +000000101011 +111011101110 +110110001011 +001111110011 +111101100011 +100100011111 +000010000110 +000010011100 +000000101001 +000001001101 +000010101111 +100100001101 +011010101000 +001001110001 +001111101110 +010110111001 +000011011100 +010101011100 +110111111011 +011101101100 +010000001001 +001011000010 +011110001010 +101011101110 +011100111100 +000010011101 +011110010100 +010010110111 +010000000001 +100111101000 +111100001100 +101100110001 +111101000001 +100111111111 +000101101000 +111010011100 +111000001010 +011001110101 +111101111100 +110011111111 +000011000010 +101000110011 +100111011010 +110011011010 +000011101110 +111100101110 +101000001001 +011110010000 +010001110111 +101100001100 +011101101001 +001110101111 +101011011011 +001110010101 +110000100111 +111110001011 +000010110111 +100001010111 +101000010110 +101100110111 +000001010110 +100001110000 +010110010110 +000110010100 +001101011010 +001100010001 +110101011100 +111101110000 +001000111111 +001101010101 +000000011111 +000001110011 +001101010011 +100001011011 +011110101010 +010100111101 +111110100011 +110100001110 +111001001000 +101000011101 +000100011001 +111101001010 +011001100111 +101010111010 +001011110000 +110100111111 +101111011111 +000011000110 +100001110011 +110110110100 +000010010101 +001010101001 +010010110010 +111011000001 +011011011001 +000110111001 +101111010110 +100111111000 +010010111101 +101011101011 +110110011000 +101001011011 +110010001001 +001100110000 +111110110110 +001111011111 +000110010010 +111100010010 +100010101011 +110100000110 +011111011110 +001111011011 +111010011111 +010001110100 +011010101001 +011100111010 +110110011111 +101000000110 +100010101101 +011101111101 +100100001010 +110111000001 +101010111101 +010000111010 +110011111001 +001000100100 +011001111001 +000111010110 +010100001111 +111100000111 +111011111111 +001101011100 +111000100001 +010100000110 +010010110001 +000011011010 +111100011110 +101000101101 +110011110111 +000110011000 +011000011011 +110000110100 +010110011001 +101110010010 +110000111001 +001011110001 +100101100100 +110011010111 +100111110111 +111001100010 +110011000000 +011101111011 +101001111111 +111011000100 +100100011000 +010110100100 +111100010011 +101100010000 +111101111010 +111101100100 +000011101001 +101111010011 +001100011110 +010100000100 +000001110111 +011111000000 +101011010101 diff --git a/3/solution.hs b/3/solution.hs new file mode 100644 index 0000000..6baf06b --- /dev/null +++ b/3/solution.hs @@ -0,0 +1,55 @@ +import Data.Bits ( complement, Bits (shift, shiftR, rotate, complementBit), shiftL, (.|.) ) +import Data.List (transpose, isPrefixOf, inits, find, tails, sort) +import Debug.Trace +import Data.Maybe + +main :: IO () +main = readFile "3/input.txt" >>= print . solve . parse . lines + +parse :: [String] -> [String] +parse = id + +solve :: [String] -> (Int,Int) +solve input = (f input, g input) + where f = powerUse . transpose + g = lifeSupport + +powerUse :: [String] -> Int +powerUse i = gamma i * epsilon i + where + gamma = pack . rate (>=) + epsilon = pack . rate (<) + +rate :: (Int -> Int -> Bool) -> [String] -> [Int] +rate op = foldr (\c -> (:) ( + let ones = count '1' c; + zeroes = count '0' c; + in if ones `op` zeroes + then 1 + else 0 + )) [] + where + count x = length . filter (== x) + +lifeSupport :: [String] -> Int +lifeSupport i = oxygen x * scrubber x + where + x = reverse i + oxygen = rating 0 (>=) + scrubber = rating 0 (<) + +rating :: Int -> (Int -> Int -> Bool) -> [String] -> Int +rating _ _ [result] = convert result +rating n op list = rating (n + 1) op (filter (\a -> (a !! n) == (f (transpose list) !! n)) list) + where + f = map (head . show) . rate op + +pack :: [Int] -> Int +pack = foldl f 0 + where + f acc i = acc `shiftL` 1 .|. i + +unpack :: String -> [Int] +unpack = map (\x -> if x == '0' then 0 else 1) + +convert = pack . unpack