{-# Language ImportQualifiedPost #-}
module Main where
import Advent (counts, getInputLines)
import Data.List (transpose, maximumBy)
import Data.Map qualified as Map
import Data.Ord (Down(Down), comparing)
main :: IO ()
IO ()
main =
do [String]
input <- Int -> Int -> IO [String]
getInputLines Int
2016 Int
6
String -> IO ()
putStrLn ((Int -> Int) -> [String] -> String
forall a. Ord a => (Int -> a) -> [String] -> String
decode Int -> Int
forall a. a -> a
id [String]
input)
String -> IO ()
putStrLn ((Int -> Down Int) -> [String] -> String
forall a. Ord a => (Int -> a) -> [String] -> String
decode Int -> Down Int
forall a. a -> Down a
Down [String]
input)
decode :: Ord a => (Int -> a) -> [String] -> String
decode :: forall a. Ord a => (Int -> a) -> [String] -> String
decode Int -> a
f [String]
xs = (Int -> a) -> String -> Char
forall a b. (Ord a, Ord b) => (Int -> b) -> [a] -> a
mostCommon Int -> a
f (String -> Char) -> [String] -> String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [String] -> [String]
forall a. [[a]] -> [[a]]
transpose [String]
xs
mostCommon :: (Ord a, Ord b) => (Int -> b) -> [a] -> a
mostCommon :: forall a b. (Ord a, Ord b) => (Int -> b) -> [a] -> a
mostCommon Int -> b
f = (a, Int) -> a
forall a b. (a, b) -> a
fst ((a, Int) -> a) -> ([a] -> (a, Int)) -> [a] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, Int) -> (a, Int) -> Ordering) -> [(a, Int)] -> (a, Int)
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
maximumBy (((a, Int) -> b) -> (a, Int) -> (a, Int) -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (Int -> b
f (Int -> b) -> ((a, Int) -> Int) -> (a, Int) -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Int) -> Int
forall a b. (a, b) -> b
snd)) ([(a, Int)] -> (a, Int)) -> ([a] -> [(a, Int)]) -> [a] -> (a, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a Int -> [(a, Int)]
forall k a. Map k a -> [(k, a)]
Map.assocs (Map a Int -> [(a, Int)])
-> ([a] -> Map a Int) -> [a] -> [(a, Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Map a Int
forall (f :: * -> *) a. (Foldable f, Ord a) => f a -> Map a Int
counts