Skip to content

Commit 822796a

Browse files
committed
feat(2025/Day09): done
1 parent 872e178 commit 822796a

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

2025/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Watch me code in Haskell for 12 days straight
1313
| 06 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day06/Main.hs) | [WriteUp](./writeups/Day06) |||
1414
| 07 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day07/Main.hs) | [WriteUp](./writeups/Day07) |||
1515
| 08 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day08/Main.hs) | [WriteUp](./writeups/Day08) |||
16-
| 09 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day09/Main.hs) | [WriteUp](./writeups/Day09) | | |
16+
| 09 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day09/Main.hs) | [WriteUp](./writeups/Day09) |||
1717
| 10 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day10/Main.hs) | [WriteUp](./writeups/Day10) | | |
1818
| 11 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day11/Main.hs) | [WriteUp](./writeups/Day11) | | |
1919
| 12 | [Code](https://github.com/Sheinxy/Advent-Of-Code/blob/main/2025/src/Day12/Main.hs) | [WriteUp](./writeups/Day12) | | |

2025/src/Day09/Main.hs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,58 @@
11
module Day09.Main (day09) where
22

3-
import AOC (submit)
3+
import AOC (submit)
4+
import AOC.Utils (isInRange)
45

5-
type Input = String
6+
type Point = (Int, Int)
7+
type Segment = (Point, Point)
8+
type Polygon = [Segment]
9+
10+
type Input = [Point]
611
type Output = Int
712

813
parseInput :: String -> Input
9-
parseInput = undefined
14+
parseInput = map (\x -> read $ "(" ++ x ++ ")") . lines
15+
16+
area :: (Int, Int) -> (Int, Int) -> Int
17+
area (a, b) (c, d) = (abs (a - c) + 1) * (abs (b - d) + 1)
1018

1119
partOne :: Input -> Output
12-
partOne = undefined
20+
partOne tiles = maximum [area x y | x <- tiles, y <- tiles, x < y]
21+
22+
getLoop :: Input -> Polygon
23+
getLoop tiles = zip tiles $ tail (cycle tiles)
24+
25+
getRectangleEdges :: (Point, Point) -> Polygon
26+
getRectangleEdges ((x1, y1), (x2, y2)) = [(a, b)
27+
| a@(x , y ) <- corners
28+
, b@(x', y') <- corners
29+
, a < b, x == x' || y == y'
30+
]
31+
where corners = [(minX, minY), (maxX, minY), (minX, maxY), (maxX, maxY)]
32+
(minX, maxX) = (min x1 x2 + 1, max x1 x2 - 1)
33+
(minY, maxY) = (min y1 y2 + 1, max y1 y2 - 1)
34+
35+
edgeIntersects :: Segment -> Segment -> Bool
36+
edgeIntersects ((x1, y1), (x2, y2)) ((x3, y3), (x4, y4))
37+
| den == 0 = False
38+
| otherwise = isInRange px (min x1 x2, max x1 x2) && isInRange py (min y1 y2, max y1 y2)
39+
&& isInRange px (min x3 x4, max x3 x4) && isInRange py (min y3 y4, max y3 y4)
40+
where den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
41+
numPx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)
42+
numPy = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)
43+
px = numPx `div` den
44+
py = numPy `div` den
45+
46+
intersectsPolygon :: Polygon -> Segment -> Bool
47+
intersectsPolygon poly segment = any (edgeIntersects segment) poly
1348

1449
partTwo :: Input -> Output
15-
partTwo = undefined
50+
partTwo tiles = maximum
51+
. map (uncurry area)
52+
. filter (not . any (intersectsPolygon loop) . getRectangleEdges)
53+
$ rects
54+
where rects = [(x, y) | x <- tiles, y <- tiles, x < y]
55+
loop = getLoop tiles
1656

1757
day09 :: String -> String -> IO ()
1858
day09 "parse" = print . parseInput

0 commit comments

Comments
 (0)