Day 1: Dusty Shed

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • VegOwOtenks
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    2 days ago

    Haskell

    module Main where
    import Data.List (sort, sortOn)
    import Data.Ord (Down(Down))
    import Control.Arrow ((&&&))
    import Data.List.NonEmpty (groupBy1)
    import qualified Data.List.NonEmpty as NE
    
    interactLn :: (String -> String) -> IO ()
    interactLn f = interact f *> putStrLn ""
    
    main :: IO ()
    main = interactLn $ show . (part1 &&& part2) . counts . sort . map read . init . lines
    
    mkPart f = product . map snd . take 3 . sortOn f
    
    part1, part2 :: [(Int, Int)] -> Int
    part1 = mkPart Down
    part2 = mkPart id
    
    counts :: Eq a => [a] -> [(Int, a)]
    counts = map (length &&& NE.head) . NE.group
    

    Apparently, my part 2 is wrong? I double-checked with a second solution from here but it’s the same result. I’d like to ask for help, but since I’m not allowed to share my input I’m a bit at a loss.