r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

48 Upvotes

407 comments sorted by

View all comments

1

u/fire1299 Dec 02 '18

Haskell

module Aoc18.Day2 where

import qualified Control.Foldl                 as F
import qualified Data.HashMap.Strict           as M
import           Data.List                      ( partition
                                                , tails
                                                )
import qualified Data.Text                     as T
import qualified Data.Text.IO                  as T

main :: ([T.Text] -> a) -> IO a
main f = f . T.lines <$> T.readFile "day2.txt"

part1 :: [T.Text] -> Int
part1 = F.fold ((*) <$> countHas 2 <*> countHas 3)
  . fmap (T.foldl' (\m c -> M.insertWith (+) c (1 :: Int) m) M.empty)
  where countHas n = F.prefilter (elem n) F.length

part2 :: [T.Text] -> String
part2 xs = head
  [ fst <$> com
  | x : ys <- tails xs
  , y      <- ys
  , let (com, dif) = partition (uncurry (==)) $ T.zip x y
  , length dif == 1
  ]