-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
61 lines (48 loc) · 1.39 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
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
import AoC.Grid
import Data.List (findIndices, sort, tails, transpose)
import qualified Data.HashMap.Strict as HashMap
parseAll :: String -> [[Char]]
parseAll = lines
compressed :: [[Char]] -> [Int]
compressed = findIndices (all (== '.'))
hitComp :: [Int] -> Int -> Int -> Int
hitComp cs from to = length $ filter p cs
where p c = from <= c && c < to
dist :: Int -> [Int] -> Int -> Int -> Int
dist k cs from to =
to - from + (k - 1) * hitComp cs from to
shortest :: Int -> [Int] -> [Int] -> (Int, Int) -> (Int, Int) -> Int
shortest k cs rs (fc, fr) (tc, tr) =
let [fc', tc'] = sort [fc, tc]
[fr', tr'] = sort [fr, tr]
in
dist k cs fc' tc' + dist k rs fr' tr'
-- TODO: move to AoC
pairs :: [a] -> [(a, a)]
pairs xs = do
x:rest <- tails xs
y <- rest
pure (x, y)
solve :: Int -> [[Char]] -> Int
solve k g =
let rs = compressed g
cs = compressed (transpose g)
stars = HashMap.keys $ HashMap.filter (== '#') $ toMapGrid g
in sum do
(s1, s2) <- pairs stars
pure $ shortest k cs rs s1 s2
part1, part2 :: [[Char]] -> Int
part1 = solve 2
part2 = solve 1_000_000
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)