tangled
alpha
login
or
join now
eldridge.cam
/
advent-of-code
2
fork
atom
Advent of Code solutions
2
fork
atom
overview
issues
pulls
pipelines
2025 day 9 in hs to be fast enough
eldridge.cam
3 months ago
f26fa6aa
6ae18e86
+70
1 changed file
expand all
collapse all
unified
split
2025
9
p2.hs
+70
2025/9/p2.hs
···
1
1
+
import Text.Parsec
2
2
+
import Data.List (sortBy, sort)
3
3
+
import Control.Monad (guard)
4
4
+
5
5
+
integer = read <$> many digit
6
6
+
point = do
7
7
+
x :: Int <- integer
8
8
+
char ','
9
9
+
y :: Int <- integer
10
10
+
return (x, y)
11
11
+
12
12
+
area ((x1, y1), (x2, y2)) = (abs (x1 - x2) + 1) * (abs (y1 - y2) + 1)
13
13
+
14
14
+
isHorizontal ((_, y1), (_, y2)) = y1 == y2
15
15
+
isVertical ((x1, _), (x2, _)) = x1 == x2
16
16
+
17
17
+
collide (p1, p2) ((bx1, by1), (bx2, by2)) =
18
18
+
let
19
19
+
(px1, px2) = if fst p1 < fst p2 then (fst p1, fst p2) else (fst p2, fst p1)
20
20
+
(py1, py2) = if snd p1 < snd p2 then (snd p1, snd p2) else (snd p2, snd p1)
21
21
+
in px1 < bx2 && px2 > bx1 && py1 < by2 && py2 > by1
22
22
+
23
23
+
containsBy key (a, b) (p1, p2) =
24
24
+
let (w, z) = if key p1 < key p2 then (key p1, key p2) else (key p2, key p1) in
25
25
+
w <= a && a <= z && w <= b && b <= z
26
26
+
27
27
+
answer :: String -> Int
28
28
+
answer input = best
29
29
+
where
30
30
+
Right points = parse (point `sepEndBy` char '\n') "" input
31
31
+
edges = zip ((last points) : points) points
32
32
+
33
33
+
verticals = sortBy (\a b -> compare (fst $ fst a) (fst $ fst b)) $ filter isVertical edges
34
34
+
horizontals = sortBy (\a b -> compare (snd $ fst a) (snd $ fst b)) $ filter isHorizontal edges
35
35
+
36
36
+
xs = (fst.fst) <$> verticals
37
37
+
ys = (snd.fst) <$> horizontals
38
38
+
39
39
+
min_x = head xs
40
40
+
max_x = last xs
41
41
+
min_y = head ys
42
42
+
max_y = last ys
43
43
+
44
44
+
y_ranges = zip ys (tail ys)
45
45
+
x_ranges = zip xs (tail xs)
46
46
+
47
47
+
illegal_x = do
48
48
+
range@(y1, y2) <- y_ranges
49
49
+
guard (y1 /= y2)
50
50
+
let
51
51
+
xs = (fst . fst) <$> filter (containsBy snd range) verticals
52
52
+
x1 = head xs
53
53
+
x2 = last xs
54
54
+
in [((min_x, y1), (x1, y2)), ((x2, y1), (max_x, y2))]
55
55
+
56
56
+
illegal_y = do
57
57
+
range@(x1, x2) <- x_ranges
58
58
+
guard (x1 /= x2)
59
59
+
let
60
60
+
ys = (snd . fst) <$> filter (containsBy fst range) horizontals
61
61
+
y1 = head ys
62
62
+
y2 = last ys
63
63
+
in [((x1, min_y),(x2, y1)), ((x1, y2),(x2, max_y))]
64
64
+
illegal = illegal_x ++ illegal_y
65
65
+
best :: Int = foldl max 0
66
66
+
$ fmap area
67
67
+
$ filter (\point -> not $ any (collide point) illegal)
68
68
+
$ [(points !! i, points !! j) | i <- [0..length points - 1], j <- [i + 1..length points - 1]]
69
69
+
70
70
+
main = getContents >>= print . answer