r/adventofcode • • Dec 07 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 7 Solutions -๐ŸŽ„-

--- Day 7: Recursive Circus ---


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.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


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!

10 Upvotes

222 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Dec 07 '17

[deleted] โ€” view deleted comment

2

u/Flurpm Dec 07 '17

Another Haskell solution! I first wrote part2 as an unholy mess that used Debug.Trace to print out through the deep call stack, but a fresh look gave me this more structural approach.

{-# LANGUAGE OverloadedStrings #-}
module Day07 where

import           Data.Text             (Text)
import qualified Data.Text             as T
import qualified Data.Text.IO          as TIO

import           Text.Megaparsec
import qualified Text.Megaparsec.Lexer as L
import           Text.Megaparsec.Text  (Parser)

import           Data.List

import qualified Data.Map.Strict       as M
import qualified Data.Set              as S

tprint :: Show a => a -> IO ()
tprint = TIO.putStrLn . T.pack . show

main = do
  input <- TIO.readFile "src/y2017/input07"
  case parse p "input07" input of
    Left  err   -> TIO.putStr $ T.pack $ parseErrorPretty err
    Right betterInput -> do
      tprint $ part1 betterInput
      tprint $ part2 betterInput
  return ()

p :: Parser [(Text, Int, [Text])]
p = program `sepBy` char '\n'

program :: Parser (Text, Int, [Text])
program = do
  name <- some letterChar
  string " ("
  size <- int
  string ")"
  deps <- option [] supports
  pure (T.pack name, size, deps)

int :: Parser Int
int = do
      change <- option id (negate <$ char '-')
      fromInteger . change <$> L.integer

supports :: Parser [Text]
supports = string " -> "   *>  (T.pack <$> some letterChar) `sepBy` string ", "


name (a, _, _) = a
size (_, a, _) = a
sups (_, _, a) = a


part1 :: [(Text, Int, [Text])] -> Text
part1 xs = name $ head $ filter (appearsInNoSupports xs) xs

appearsInNoSupports :: [(Text, Int, [Text])] -> (Text, Int, [Text]) -> Bool
appearsInNoSupports xs x = not . any (elem (name x) . sups) $ xs


part2 :: [(Text, Int, [Text])] -> Int
part2 xs = findImbalance xs root 0
  where
    root = head $ filter (appearsInNoSupports xs) xs

findImbalance :: [(Text, Int, [Text])] -> (Text, Int, [Text]) -> Int -> Int
findImbalance others curr actual = case findUniqueWeight supports supWeights of
                                     Nothing  -> actual - sum supWeights
                                     Just u   -> findImbalance others u (ordinary supWeights)
  where
    supporters x = filter (\o -> elem (name o) (sups x)) others
    supports = supporters curr
    supWeights = map weight supports
    weight x = size x + sum (map weight (supporters x))

ordinary :: [Int] -> Int
ordinary (x:xs) = if elem x xs then x else ordinary xs

findUniqueWeight :: [(Text, Int, [Text])] -> [Int] -> Maybe (Text, Int, [Text])
findUniqueWeight programs weights = case filter (snd) $ zip programs $ map (/=expected) weights of
                                      (prog,_):[] -> Just prog
                                      _           -> Nothing
  where
    expected = ordinary weights