-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.hs
86 lines (74 loc) · 2.02 KB
/
Day05.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module Main where
import Advent.Utils (run)
import Control.Arrow ((***))
import Data.List (find, sortBy)
import Data.List.Split (splitOn)
type Input = (SortRules, Updates)
data Order = Order Int Int deriving (Eq, Show)
type SortRules = [Order]
type Updates = [[Int]]
testInput :: Input
testInput =
prepare
[ "47|53"
, "97|13"
, "97|61"
, "97|47"
, "75|29"
, "61|13"
, "75|53"
, "29|13"
, "97|29"
, "53|29"
, "61|53"
, "97|53"
, "61|29"
, "47|13"
, "75|47"
, "97|75"
, "47|61"
, "75|61"
, "47|29"
, "75|13"
, "53|13"
, ""
, "75,47,61,53,29"
, "97,61,53,29,13"
, "75,29,13"
, "75,97,47,61,53"
, "61,13,29"
, "97,13,75,29,47"
]
getMiddle :: [a] -> a
getMiddle ls = ls !! (length ls `div` 2)
isSorted :: (Int -> Int -> Ordering) -> [Int] -> Bool
isSorted f ls = ls == sortBy f ls
{- | After messing with this for a while, I noticed
all numbers which appear next to each other has an
associated rule. There is no need to check for
transitive rules, so this niave approach works.
-}
sortFunc :: [Order] -> (Int -> Int -> Ordering)
sortFunc orders a b =
case find (\(Order x y) -> x == a && y == b) orders of
(Just _) -> LT
Nothing -> GT
part1 :: Input -> Int
part1 (orders, updates) =
sum $ getMiddle <$> filter (isSorted $ sortFunc orders) updates
part2 :: Input -> Int
part2 (orders, updates) =
sum $ getMiddle . sortBy f <$> filter (not . isSorted f) updates
where
f = sortFunc orders
prepare :: [String] -> Input
prepare = (map parseOrder *** (map parseUpdate . tail)) . break (== "")
where
parseOrder :: String -> Order
parseOrder s = case read <$> splitOn "|" s of
[a, b] -> Order a b
_ -> error ("Failed to parse order rule: " <> s)
parseUpdate :: String -> [Int]
parseUpdate = map read . splitOn ","
main :: IO ()
main = run part1 part2 prepare