aboutsummaryrefslogtreecommitdiff
path: root/0011/main.hs
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-04-13 01:27:02 +0800
committerMistivia <i@mistivia.com>2025-04-13 01:27:02 +0800
commit609a4795fa59775f51127400d626d625de828c76 (patch)
treee68c34ca6f784120a46766ade445504ed49c662f /0011/main.hs
parentbc76d979fd60b31d11aa2adea52a03a36b5c2277 (diff)
Largest Product in a Grid
Diffstat (limited to '0011/main.hs')
-rw-r--r--0011/main.hs19
1 files changed, 19 insertions, 0 deletions
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
+