never executed always true always false
1 {-|
2 Module: Y2015.D13
3 Description: Advent of Code Day 13 Solutions.
4 License: MIT
5 Maintainer: @tylerjl
6
7 Solutions to the day 13 set of problems for <adventofcode.com>.
8 -}
9
10 module Y2015.D13 (solveSeating) where
11
12 import Data.List (nub, permutations)
13 import Data.Map (Map, findWithDefault, fromList, keys)
14
15 type Guest = String
16 type Happiness = Int
17 type Edge = (Guest, Guest)
18 type Preferences = Map Edge Happiness
19
20 -- |Find optimal seating happiness!
21 solveSeating :: String -- ^ Input of seating happiness stats
22 -> Int -- ^ Optimal possible happiness
23 solveSeating i = maximum $ map sum guestMoods
24 where prefs = toSeating i
25 guests = nub . uncurry (++) . unzip $ keys prefs
26 pairs = map (zip <*> tail . cycle) $ permutations guests
27 arrangements = map rePair pairs
28 guestMoods = map (map (flip (findWithDefault 0) prefs)) arrangements
29
30 rePair :: [(a, a)] -> [(a, a)]
31 rePair [] = []
32 rePair ((x,y):xs) = [(x,y),(y,x)] ++ rePair xs
33
34 toSeating :: String -> Preferences
35 toSeating = fromList . map (parseSeating . words . init) . lines
36
37 parseSeating :: [String] -> (Edge, Happiness)
38 parseSeating [a,_,s,h,_,_,_,_,_,_,b] = ((a, b), hap s)
39 where change = read h
40 hap "lose" = negate change
41 hap _ = change
42 parseSeating _ = (("", ""), 0)