From: Neil Smith Date: Wed, 4 Dec 2024 14:00:43 +0000 (+0000) Subject: Bit of refactoring X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=8cd7240103246fbb11c29651485d12de2268307c;p=advent-of-code-24.git Bit of refactoring --- 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