2024 day 4

This commit is contained in:
eriedaberrie 2024-12-03 22:10:00 -08:00
parent d2892f1a3b
commit 605e8076bd
3 changed files with 60 additions and 0 deletions

23
2024/4/Main1.hs Normal file
View file

@ -0,0 +1,23 @@
import Data.List (isPrefixOf, transpose)
import Data.Universe.Helpers (diagonals)
countTimes :: String -> String -> Int
countTimes _ [] = 0
countTimes s fullLine@(_:nextLine)
| s `isPrefixOf` fullLine = 1 + countTimes s (drop (length s) fullLine)
| otherwise = countTimes s nextLine
countHoriz :: String -> Int
countHoriz l = countTimes "XMAS" l + countTimes "SAMX" l
countLines :: [String] -> Int
countLines = sum . map countHoriz
countAll :: [String] -> Int
countAll ls = countLines ls
+ countLines (transpose ls)
+ countLines (diagonals ls)
+ countLines (diagonals (reverse ls))
main :: IO ()
main = print . countAll . lines =<< readFile "data.txt"

36
2024/4/Main2.hs Normal file
View file

@ -0,0 +1,36 @@
import qualified Data.Vector as V
isMas :: (Char, Char) -> Bool
isMas x = x == ('M', 'S') || x == ('S', 'M')
noEnds :: V.Vector a -> Int -> Int -> Int
noEnds v i
| i == 0 || i == V.length v - 1 = const 0
| otherwise = id
countAll :: V.Vector (V.Vector Char) -> Int
countAll v = V.ifoldl'
(\acc x l -> acc
+ noEnds
v
x
(V.ifoldl'
(\acc2 y c -> acc2
+ noEnds
l
y
(if getAt x y == 'A'
&& (isMas (getAt (x - 1) (y - 1), getAt (x + 1) (y + 1))
&& isMas (getAt (x + 1) (y - 1), getAt (x - 1) (y + 1)))
then 1
else 0))
0
l))
0
v
where
getAt x y = v V.! x V.! y
main :: IO ()
main = print . countAll . V.fromList . map V.fromList . lines
=<< readFile "data.txt"

View file

@ -39,6 +39,7 @@
text
text-icu
unordered-containers
universe-base
vector
]))
];