From 8cd7240103246fbb11c29651485d12de2268307c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 4 Dec 2024 14:00:43 +0000 Subject: [PATCH] Bit of refactoring --- advent04/Main.hs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/advent04/Main.hs b/advent04/Main.hs index fc8b0b7..2ee0453 100644 --- a/advent04/Main.hs +++ b/advent04/Main.hs @@ -21,12 +21,13 @@ part1, part2 :: Grid -> Int 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" @@ -44,15 +45,12 @@ extensions n = fmap go directions , 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 -- 2.34.1