-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day7.hs
53 lines (45 loc) · 1.36 KB
/
Day7.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
module Day7
( part1
, part2
) where
import Data.List ((\\))
import Helpers.Parsers (alphaNum)
data Tower = Program
{ name :: String
, weight :: Int
, totalWeight :: Int
, subTowers :: [Tower]
} deriving (Show, Eq)
findRoot :: [[String]] -> String
findRoot l = head $ names \\ contents
where
names = map head l
contents = concatMap (drop 2) l
buildTower :: [[String]] -> Tower
buildTower l = tower l . findRoot $ l
tower :: [[String]] -> String -> Tower
tower l n = Program n w tw st
where
st = map (tower l) subs
tw = (+) w . sum . map totalWeight $ st
(_:rawW:subs) = head . filter ((== n) . head) $ l
w = read rawW
balance :: Int -> Tower -> Int
balance curDiff tower
| null sub || minV == maxV = curDiff + weight tower
| otherwise = balance newDiff nextTower
where
sub = subTowers tower
minV = minimum . map totalWeight $ sub
maxV = maximum . map totalWeight $ sub
minima = filter ((== minV) . totalWeight) sub
newDiff
| length minima == 1 = maxV - minV
| otherwise = minV - maxV
nextTower
| length minima == 1 = head minima
| otherwise = head . filter ((== maxV) . totalWeight) $ sub
part1 :: Bool -> String -> String
part1 _ = show . findRoot . alphaNum
part2 :: Bool -> String -> String
part2 _ = show . balance 0 . buildTower . alphaNum