Day 2: Fulminating Farmland

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@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 days ago

    Haskell

    import Data.List (findIndex, sort)
    import Data.Maybe (mapMaybe)
    import Control.Arrow ((&&&))
    
    data Color = R | O | Y | G | B | V
      deriving (Show, Read, Enum, Eq, Ord)
    fromChar c = toEnum <$> findIndex (==c) "roygbv"
    
    part1 :: [Color] -> Int
    part1 = foldl' go 0
     where
      go pos c
       | current < next = pos + next - current
       | otherwise = pos + (6 - current) + next
       where
        current = pos `mod` 6
        next = fromEnum c
    
    part2 = part1 . reverse . sort
    
    main = print . (part1 &&& part2) . mapMaybe fromChar =<< getContents
    

    Somehow, I’m off by one. I forgot why that was and simply added one before entering my solution.