part1 grid = length $ filter (== targetWord)
$ foundWords grid
$ validWords grid
- $ potentialWords grid
+ $ potentialWords grid
+ $ extensions targetLength
part2 grid = length $ filter isXmas
$ foundWords grid
$ validWords grid
- $ potentialXs grid
+ $ potentialWords grid xExtension
targetWord :: String
targetWord = "XMAS"
, dr /= 0 || dc /= 0
]
-xExtension :: [Position]
-xExtension = [V2 0 0, V2 -1 -1, V2 1 -1, V2 -1 1, V2 1 1]
+xExtension :: [[Position]]
+xExtension = [[V2 0 0, V2 -1 -1, V2 1 -1, V2 -1 1, V2 1 1]]
-potentialWords, potentialXs :: Grid -> [[Position]]
-potentialWords grid = concatMap go $ indices grid
- where go pos = fmap (^+^ pos) <$> extensions targetLength
-
-potentialXs grid = go <$> indices grid
- where go pos = fmap (^+^ pos) xExtension
+potentialWords :: Grid -> [[Position]] -> [[Position]]
+potentialWords grid exts = concatMap go $ indices grid
+ where go pos = fmap (^+^ pos) <$> exts
validWords :: Grid -> [[Position]] -> [[Position]]
validWords grid = filter allInBounds