-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
51 lines (41 loc) · 1.41 KB
/
run.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
import Data.Aeson
import Data.List (permutations, group, minimum, maximum, minimumBy, maximumBy, groupBy, (\\), sort)
import qualified Data.List as L
import Text.Parsec
import Debug.Trace
goodSplits = filter isOk
. splits
where isOk (s, r, val, _, _) = 2 * val == sum r
&& any ((== val) . sum) (L.subsequences r)
goodSplits' = filter isOk
. splits
where isOk (s, r, val, _, _) = 3 * val == sum r
&& any subIsOk (L.subsequences r)
where subIsOk s' = sum s' == 2 * val
&& any ((== val) . sum) (L.subsequences $ r \\ s')
splits vals = map (\s -> (s, vals \\ s, sum s, product s, length s))
. subsequences $ vals
--- vvv From stackoverflow
addoneall x xs = zipWith ((++) . (map (x:))) ([]:xs) (xs ++ [[]])
subsequences :: [a] -> [[a]]
subsequences = concat . foldr addoneall [[[]]]
--- ^^^
parseAll = map (read :: String -> Integer) . lines
fourth (_, _, _, x, _) = x
part1 = head
. sort
. map fourth
. head
. groupBy (\(_,_,_,_,a) (_,_,_,_,b) -> a == b)
. goodSplits
part2 = head
. sort
. map fourth
. head
. groupBy (\(_,_,_,_,a) (_,_,_,_,b) -> a == b)
. goodSplits'
test = [1..5] ++ [7..11] :: [Integer]
main = do
input <- parseAll <$> readFile "input.txt"
print (part1 input)
print (part2 input)