2022-12-08 07:31:49 +01:00
import Data.List
main :: IO ()
main = do
2022-12-11 12:03:12 +01:00
input <- map ( map ( subtract 48 . fromEnum ) ) . lines <$> readFile " input "
print $ sum $ day8 ( fromEnum . any ( /= 0 ) ) walkLine1 input
2022-12-08 07:31:49 +01:00
print $ maximum $ day8 product walkLine2 input
walkLine2 :: Int -> [ Int ] -> Int
2022-12-11 12:03:12 +01:00
walkLine2 x ( y : ys ) =
1 +
if y < x
then walkLine2 x ys
else 0
2022-12-08 07:31:49 +01:00
walkLine2 _ [] = 0
walkLine1 :: Int -> [ Int ] -> Int
2022-12-11 12:03:12 +01:00
walkLine1 x ( y : ys ) =
if y < x
then walkLine1 x ys
else 0
2022-12-08 07:31:49 +01:00
walkLine1 _ [] = 1
2022-12-11 12:03:12 +01:00
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
2022-12-08 07:31:49 +01:00
day8 :: ( [ Int ] -> Int ) -> ( Int -> [ Int ] -> Int ) -> [ [ Int ] ] -> [ Int ]
2022-12-11 12:03:12 +01:00
day8 f1 f2 field =
[ f1 $ checkPoint f2 ( x , y ) field
| x <- [ 0 .. ( subtract 1 $ length $ head field ) ]
, y <- [ 0 .. ( subtract 1 $ length field ) ]
]