Skip to content

Instantly share code, notes, and snippets.

@shanecharles
Last active December 7, 2017 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecharles/2c0fae810cd8e6ee51157d74f334b616 to your computer and use it in GitHub Desktop.
Save shanecharles/2c0fae810cd8e6ee51157d74f334b616 to your computer and use it in GitHub Desktop.
let wrapGet (arr : int []) = function
| x when x < 0 -> arr.[arr.Length + x]
| x -> arr.[x]
let generalIndexOffset rowLength location = 2 * (location / rowLength) + 1
let (|LastItem|_|) ((_, previousRing : int [], currentRing : int []), i) =
if i = currentRing.Length - 1
then Some (currentRing.[i - 1] + currentRing.[0] + previousRing.[previousRing.Length - 1])
else None
let (|BeforeLastItem|_|) ((_, previousRing : int [], currentRing : int []), i) =
if i = currentRing.Length - 2
then Some (currentRing.[i - 1] + currentRing.[0] + previousRing.[previousRing.Length - 1] + previousRing.[previousRing.Length - 2])
else None
let (|FirstItem|_|) ((_, previousRing : int [], _ : int []), i) =
if i = 0
then Some (previousRing.[previousRing.Length - 1] + previousRing.[0])
else None
let (|Corner|_|) ((rowLength, previousRing : int [], currentRing : int []), i) =
if (i + 1) % rowLength = 0
then
let offset = 2 * ((i + 1) / rowLength)
Some (currentRing.[i - 1] + previousRing.[i - offset])
else None
let (|BeforeCorner|_|) ((rowLength, previousRing : int [], currentRing : int []), i) =
if (i + 2) % rowLength = 0
then
let offset = generalIndexOffset rowLength i
Some (currentRing.[i - 1] + previousRing.[i - offset] + previousRing.[i - 1 - offset])
else None
let (|AfterCorner|_|) ((rowLength, previousRing : int [], currentRing : int []), i) =
if i % rowLength = 0
then
let offset = generalIndexOffset rowLength i
Some (currentRing.[i - 1] + currentRing.[i - 2] + previousRing.[i - offset] + previousRing.[i + 1 - offset])
else None
let calculateMiddle ((rowLength, previousRing : int [], currentRing : int []), i) =
let offset = generalIndexOffset rowLength i
let get = wrapGet previousRing
currentRing.[i - 1] + get (i - 1 - offset) + get (i - offset) + get (i + 1 - offset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment