From 609a4795fa59775f51127400d626d625de828c76 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sun, 13 Apr 2025 01:27:02 +0800 Subject: Largest Product in a Grid --- 0011/main.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0011/main.hs diff --git a/0011/main.hs b/0011/main.hs new file mode 100644 index 0000000..6b2a415 --- /dev/null +++ b/0011/main.hs @@ -0,0 +1,19 @@ +numAt :: Int -> Int -> String -> Int +numAt x y contents = read $ (map words $ lines contents) !! y !! x + +rows = [[(x,y), (x+1, y), (x+2,y), (x+3, y)] | x <- [0..16], y <- [0..19]] +cols = [[(x,y), (x, y+1), (x,y+2), (x, y+3)] | x <- [0..19], y <- [0..16]] +ldiag = [[(x,y), (x+1, y+1), (x+2, y+2), (x+3, y+3)] | x <- [0..16], y <-[0..16]] +rdiag = [[(x,y), (x+1, y-1), (x+2, y-2), (x+3, y-3)] | x <- [0..16], y <-[3..19]] + +prodAt :: [(Int,Int)] -> String -> Int +prodAt lst contents = product $ map getNum lst + where getNum (x,y) = numAt x y contents + +result contents = foldl1 max $ map (flip prodAt contents) allTuples + where allTuples = (rows ++ cols ++ ldiag ++ rdiag) + +main = do + contents <- readFile "input" + putStrLn $ show $ result contents + -- cgit v1.0