From 1a41096917c7554aa504624801f4aeed7a080185 Mon Sep 17 00:00:00 2001 From: Agatha Lovelace Date: Fri, 3 Dec 2021 19:44:53 +0200 Subject: [PATCH] Day 3 --- Day3.hs | 34 ++++++++++++++++++++++++++++++++++ Day3_1.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Day3.hs create mode 100644 Day3_1.hs diff --git a/Day3.hs b/Day3.hs new file mode 100644 index 0000000..0813fbb --- /dev/null +++ b/Day3.hs @@ -0,0 +1,34 @@ +module Day3 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.List ( maximumBy, minimumBy, group, sort ) +import Data.Function (on) +import Data.Char (digitToInt) + +column :: String -> Int -> String +column str i = map (!! i) $ lines str + +columns :: String -> Int +columns str = maximum $ map length $ lines str + +mostCommon :: String -> Char +mostCommon str = head . maximumBy (compare `on` length) . group $ sort str + +leastCommon :: String -> Char +leastCommon str = head . minimumBy (compare `on` length) . group $ sort str + +binaryToDecimal :: Int -> Int +binaryToDecimal = foldl1 ((+) . (2 *)) . map digitToInt . show + +main :: IO () +main = do + input <- openFile "inputs/3.txt" ReadMode + contents <- hGetContents input + + let gamma = mostCommon . column contents <$> [0..(columns contents - 1)] + let epsilon = leastCommon . column contents <$> [0..(columns contents - 1)] + + let energy = product (binaryToDecimal . read <$> [gamma, epsilon] :: [Int]) + + print energy + + hClose input diff --git a/Day3_1.hs b/Day3_1.hs new file mode 100644 index 0000000..15a333a --- /dev/null +++ b/Day3_1.hs @@ -0,0 +1,45 @@ +module Day3_1 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.List ( maximumBy, minimumBy, group, sort ) +import Data.Function (on) +import Data.Char (digitToInt) + +column :: [String] -> Int -> String +column str i = map (!! i) str + +mostCommon :: String -> Char +mostCommon str = head . maximumBy (compare `on` length) . group $ sort str + +leastCommon :: String -> Char +leastCommon str = head . minimumBy (compare `on` length) . group $ sort str + +-- in: input strings, sorting method, current digit +-- out: filtered strings +filterColumns :: [String] -> (String -> Char) -> Int -> [String] +filterColumns str sort i = filter (\a -> (a !! i) == commonChar) str + where + commonChar = sort $ column str i + +-- in: input strings, sorting method, current digit +-- out: final rating +rating :: [String] -> (String -> Char) -> Int -> String +rating [str] _ _ = str +rating str sort i = rating (filterColumns str sort i) sort (i+1) + + +binaryToDecimal :: Int -> Int +binaryToDecimal = foldl1 ((+) . (2 *)) . map digitToInt . show + +main :: IO () +main = do + input <- openFile "inputs/3.txt" ReadMode + contents <- hGetContents input + + let oxygen = rating (lines contents) mostCommon 0 + let co2 = rating (lines contents) leastCommon 0 + + let rating = product (binaryToDecimal . read <$> [oxygen, co2] :: [Int]) + + print rating + + hClose input \ No newline at end of file