r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 4 Solutions -πŸŽ„-

--- Day 4: Repose Record ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

38 Upvotes

340 comments sorted by

View all comments

1

u/fire1299 Dec 06 '18

Haskell

{-# LANGUAGE OverloadedStrings, TupleSections #-}

module Aoc18.Day4 where

import           Control.Applicative            ( (<|>) )
import qualified Data.Attoparsec.Text          as P
import qualified Data.IntMap.Strict            as M
import           Data.List                      ( foldl'
                                                , maximumBy
                                                , sortOn
                                                )
import           Data.Ord                       ( comparing )
import qualified Data.Text.IO                  as T

data Time = Time
  { year   :: !Int
  , month  :: !Int
  , day    :: !Int
  , hour   :: !Int
  , minute :: !Int
  } deriving (Eq, Ord, Show)

data Event
  = Begin !Int
  | Sleep
  | Wake
  deriving (Show)

main :: (M.IntMap (M.IntMap Int) -> a) -> IO a
main f =
  either error (f . toMap . sortOn fst) . P.parseOnly parseFile <$> T.readFile
    "day4.txt"
 where
  parseFile = P.sepBy' parseLine P.endOfLine
  parseLine = (,) <$> parseTime <* " " <*> parseEvent
  parseTime =
    Time
      <$> ("[" *> P.decimal <* "-")
      <*> P.decimal
      <*  "-"
      <*> P.decimal
      <*  " "
      <*> P.decimal
      <*  ":"
      <*> P.decimal
      <*  "]"
  parseEvent =
    Begin
      <$> ("Guard #" *> P.decimal <* " begins shift")
      <|> Sleep
      <$  "falls asleep"
      <|> Wake
      <$  "wakes up"

toMap :: [(Time, Event)] -> M.IntMap (M.IntMap Int)
toMap = fst . foldl' go (M.empty, (undefined, undefined))
 where
  go (m, (g, s)) (t, e) = case e of
    Begin h -> (m, (h, undefined))
    Sleep   -> (m, (g, minute t))
    Wake    -> (M.insertWith (M.unionWith (+)) g m' m, (g, undefined))
    where m' = M.fromDistinctAscList $ (, 1) <$> [s .. minute t - 1]

part1 :: M.IntMap (M.IntMap Int) -> Int
part1 =
  uncurry (*)
    . fmap (fst . maximumBy (comparing snd) . M.toList . fst)
    . maximumBy (comparing $ snd . snd)
    . M.toList
    . fmap ((,) <*> sum)

part2 :: M.IntMap (M.IntMap Int) -> Int
part2 =
  uncurry (*) . fmap fst . maximumBy (comparing $ snd . snd) . M.toList . fmap
    (maximumBy (comparing snd) . M.toList)