AoC/2022/day08/day08.hs
2022-12-11 12:03:12 +01:00

40 lines
1.0 KiB
Haskell

import Data.List
main :: IO ()
main = do
input <- map (map (subtract 48 . fromEnum)) . lines <$> readFile "input"
print $ sum $ day8 (fromEnum . any (/= 0)) walkLine1 input
print $ maximum $ day8 product walkLine2 input
walkLine2 :: Int -> [Int] -> Int
walkLine2 x (y:ys) =
1 +
if y < x
then walkLine2 x ys
else 0
walkLine2 _ [] = 0
walkLine1 :: Int -> [Int] -> Int
walkLine1 x (y:ys) =
if y < x
then walkLine1 x ys
else 0
walkLine1 _ [] = 1
checkPoint :: (Int -> [Int] -> Int) -> (Int, Int) -> [[Int]] -> [Int]
checkPoint f (x, y) field =
[f num line | line <- [lineX1, lineX2, lineY1, lineY2]]
where
num = field !! y !! x
lineX1 = drop (x + 1) $ field !! y
lineX2 = reverse $ take x $ field !! y
lineY1 = drop (y + 1) $ transpose field !! x
lineY2 = reverse $ take y $ transpose field !! x
day8 :: ([Int] -> Int) -> (Int -> [Int] -> Int) -> [[Int]] -> [Int]
day8 f1 f2 field =
[ f1 $ checkPoint f2 (x, y) field
| x <- [0 .. (subtract 1 $ length $ head field)]
, y <- [0 .. (subtract 1 $ length field)]
]