-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day6.hs
76 lines (67 loc) · 2.1 KB
/
Day6.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
module Day6
( part1
, part2
) where
import Control.Lens.Getter ((^.))
import Control.Monad (void)
import Data.Char (isDigit)
import Data.Either (fromRight)
import Data.List (group, sort, sortBy)
import Data.Maybe (Maybe (Just, Nothing), mapMaybe)
import Helpers.Graph (Pos)
import Helpers.Parsers (Parser)
import Linear.V2 (V2 (..), _x, _y)
import Text.Megaparsec (eof, many, optional, parse, takeWhile1P)
import Text.Megaparsec.Char (char, eol, string)
parser :: Parser [Pos]
parser = many parseLine <* eof
parseLine :: Parser Pos
parseLine = do
x <- read <$> takeWhile1P Nothing isDigit
void . string $ ", "
y <- read <$> takeWhile1P Nothing isDigit
void . optional $ eol
return (V2 x y)
manDist :: Pos -> Pos -> Int
manDist (V2 a b) (V2 c d) = abs (a - c) + abs (b - d)
largestArea :: [Pos] -> Int
largestArea coords =
maximum .
map length .
group .
sort .
filter (\(V2 x y) -> x `notElem` [xm, xM] && y `notElem` [ym, yM]) .
mapMaybe (closest coords) $
[V2 x y | x <- [xm .. xM], y <- [ym .. yM]]
where
xs = map (^. _x) coords
ys = map (^. _y) coords
xm = minimum xs
xM = maximum xs
ym = minimum ys
yM = maximum ys
safest :: Int -> [Pos] -> Int
safest dist coords =
length . filter (\p -> (< dist) . sum . map (manDist p) $ coords) $
[V2 x y | x <- [xm .. xM], y <- [ym .. yM]]
where
xs = map (^. _x) coords
ys = map (^. _y) coords
xm = minimum xs
xM = maximum xs
ym = minimum ys
yM = maximum ys
closest :: [Pos] -> Pos -> Maybe Pos
closest coords pos
| manDist pos s == manDist pos o = Nothing
| otherwise = Just s
where
(s:o:_) = sortBy (\a b -> compare (manDist pos a) (manDist pos b)) coords
part1 :: Bool -> String -> String
part1 _ = show . largestArea . fromRight [] . parse parser ""
part2 :: Bool -> String -> String
part2 test = show . safest dist . fromRight [] . parse parser ""
where
dist
| test = 32
| otherwise = 10000