57 lines
1.3 KiB
FSharp
57 lines
1.3 KiB
FSharp
module _11
|
|
|
|
open System
|
|
open System.IO
|
|
|
|
let rightTrans = (0, 1)
|
|
let downTrans = (1, 0)
|
|
let sidewayRightTrans = (1, 1)
|
|
let sidewayLeftTrans = (1, -1)
|
|
|
|
let len = 4
|
|
|
|
let maximum max v = if v > max then v else max
|
|
|
|
let maxProduct =
|
|
|
|
let getData =
|
|
let lines = File.ReadAllLines(@"11.txt")
|
|
|
|
let convertDataRow(line:string) =
|
|
line.Split(' ') |> Array.map int
|
|
|
|
lines |> Array.map convertDataRow
|
|
|
|
let rows = getData
|
|
|
|
let rowLen = rows.Length
|
|
let colLen = rows.[0].Length
|
|
|
|
let getAdjacentCells (r, c) (rInc, cInc) =
|
|
if (r + rInc*len > rowLen
|
|
|| c + cInc*len > colLen
|
|
|| c + cInc*len < 0) then
|
|
Seq.empty
|
|
else
|
|
Seq.init len (fun i -> rows.[r + rInc*i].[c + cInc*i])
|
|
|
|
seq{
|
|
for r in 0..rowLen-1 do
|
|
for c in 0..colLen-1 do
|
|
let getCells = getAdjacentCells (r, c)
|
|
|
|
let product = Seq.fold (*) 1
|
|
|
|
yield getCells rightTrans |> product
|
|
yield getCells downTrans |> product
|
|
yield getCells sidewayRightTrans |> product
|
|
yield getCells sidewayLeftTrans |> product
|
|
}
|
|
|> Seq.fold maximum -1
|
|
|
|
(*
|
|
for row in rows do
|
|
for cell in row do
|
|
printf "%u " cell
|
|
printf "\n"
|
|
*) |