2024 day 4
This commit is contained in:
parent
d2892f1a3b
commit
605e8076bd
23
2024/4/Main1.hs
Normal file
23
2024/4/Main1.hs
Normal 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
36
2024/4/Main2.hs
Normal 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"
|
Loading…
Reference in a new issue