|
1 | 1 | module Day8 (partOne, partTwo) where
|
2 | 2 |
|
3 |
| -import Data.List (nub, subsequences) |
| 3 | +import Data.List (nub) |
| 4 | +import Data.List.Extra (pairs) |
4 | 5 | import qualified Data.Map.Lazy as Map
|
5 |
| -import Data.Matrix (Position) |
| 6 | +import Data.Matrix (Matrix, Position) |
6 | 7 | import qualified Data.Matrix as M
|
7 |
| -import Data.Maybe (mapMaybe) |
8 | 8 |
|
9 |
| -type AntiNode = (Int, Int) |
| 9 | +type Node = (Int, Int) |
10 | 10 |
|
11 | 11 | partOne :: String -> Int
|
12 |
| -partOne input = length $ filter (`M.isInBounds` matrix) $ nub $ concatMap (uncurry getAntiNodes) combinations |
| 12 | +partOne input = length $ filter (`M.isInBounds` grid) $ nub antiNodes |
13 | 13 | where
|
14 |
| - matrix = M.buildMatrix (lines input) |
15 |
| - noDotsMatrix = M.filter (/= '.') matrix |
16 |
| - pointGroups = Map.elems $ M.groupByWith (\(position, value) -> (value, position)) noDotsMatrix |
17 |
| - combinations = concatMap pairs pointGroups |
| 14 | + grid = M.buildMatrix (lines input) |
| 15 | + antiNodes = concatMap (uncurry getAntiNodes) $ nodePairs grid |
18 | 16 |
|
19 |
| -partTwo :: String -> Int |
20 |
| -partTwo input = undefined |
| 17 | + getAntiNodes x y = case distance x y of |
| 18 | + (0, 0) -> [] |
| 19 | + d -> [add x d, substract y d] |
21 | 20 |
|
22 |
| -pairs :: [a] -> [(a, a)] |
23 |
| -pairs xs = mapMaybe parse $ subsequences xs |
24 |
| - where |
25 |
| - parse :: [a] -> Maybe (a, a) |
26 |
| - parse [x, y] = Just (x, y) |
27 |
| - parse _ = Nothing |
28 |
| - |
29 |
| -getAntiNodes :: Position -> Position -> [AntiNode] |
30 |
| -getAntiNodes (xrow, xcol) (yrow, ycol) = case distance of |
31 |
| - (0, 0) -> [] |
32 |
| - (drow, dcol) -> [(xrow + drow, xcol + dcol), (yrow - drow, ycol - dcol)] |
| 21 | +partTwo :: String -> Int |
| 22 | +partTwo input = length antiNodes |
33 | 23 | where
|
34 |
| - distance = (xrow - yrow, xcol - ycol) |
| 24 | + matrix = M.buildMatrix (lines input) |
| 25 | + combinations = nodePairs matrix |
| 26 | + antiNodes = nub $ concatMap (uncurry getAntiNodes) combinations |
| 27 | + |
| 28 | + getAntiNodes x y = case distance x y of |
| 29 | + (0, 0) -> [] |
| 30 | + d -> computePointsAtDistance x d add ++ computePointsAtDistance y d substract |
| 31 | + |
| 32 | + computePointsAtDistance point dist f = |
| 33 | + let point' = f point dist |
| 34 | + in if M.isInBounds point matrix |
| 35 | + then point : computePointsAtDistance point' dist f |
| 36 | + else [] |
| 37 | + |
| 38 | +nodePairs :: Matrix Char -> [(Node, Node)] |
| 39 | +nodePairs matrix = |
| 40 | + let noDotsMatrix = M.filter (/= '.') matrix |
| 41 | + pointGroups = Map.elems $ M.groupByWith (\(position, value) -> (value, position)) noDotsMatrix |
| 42 | + in concatMap pairs pointGroups |
| 43 | + |
| 44 | +substract :: Position -> (Int, Int) -> (Int, Int) |
| 45 | +substract (x, y) (dx, dy) = (x - dx, y - dy) |
| 46 | + |
| 47 | +add :: Position -> (Int, Int) -> (Int, Int) |
| 48 | +add (x, y) (dx, dy) = (x + dx, y + dy) |
| 49 | + |
| 50 | +distance :: Position -> Position -> (Int, Int) |
| 51 | +distance (xrow, xcol) (yrow, ycol) = (xrow - yrow, xcol - ycol) |
0 commit comments