This repository was archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay06.hs
58 lines (52 loc) · 1.52 KB
/
Day06.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- |
-- Module : AOC.Challenge.Day06
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 6. See "AOC.Solver" for the types used in this module!
module AOC.Challenge.Day06 (
day06a
, day06b
) where
import AOC.Solver ((:~>)(..))
import Data.Functor ((<&>))
import Data.List.Split (splitOn)
import Data.Map (Map)
import qualified Data.Map as M
import qualified Data.Set as S
parseParents :: String -> Map String String
parseParents str = M.fromList [
(y, x)
| ln <- lines str
, [x,y] <- [splitOn ")" ln]
]
day06a :: Map String String :~> Int
day06a = MkSol
{ sParse = Just . parseParents
, sShow = show
, sSolve = \parents -> Just $
let orbits :: Map String Int
orbits = parents <&> \v ->
case M.lookup v orbits of
Nothing -> 1
Just s -> s + 1
in sum orbits
}
day06b :: Map String String :~> Int
day06b = MkSol
{ sParse = Just . parseParents
, sShow = show
, sSolve = \parents -> do
let orbits :: Map String [String]
orbits = parents <&> \v ->
case M.lookup v orbits of
Nothing -> []
Just ss -> v:ss
you <- S.fromList <$> M.lookup "YOU" orbits
san <- S.fromList <$> M.lookup "SAN" orbits
let onlyYou = you S.\\ san
onlySan = san S.\\ you
pure $ S.size onlyYou + S.size onlySan
}