Created
October 20, 2011 03:21
-
-
Save harold/1300340 to your computer and use it in GitHub Desktop.
Imperative code in functional languages is fun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let d = 4 | |
let a = Array.create (d*d) 0 | |
let results = System.Collections.Generic.Dictionary<int,int>() | |
for n=0 to (1<<<d*d)-1 do | |
// make a new a | |
for i=0 to d*d-1 do | |
if ((n &&& (1<<<i)) <> 0) then | |
a.[i] <- 1 | |
else | |
a.[i] <- 0 | |
// count greens | |
let mutable greens = 0 | |
for y=0 to d-1 do | |
for x=0 to d-1 do | |
let mutable neighbors = 0 | |
if x>0 && a.[(x-1)+y*d]=1 then neighbors <- neighbors + 1 | |
if x<d-1 && a.[(x+1)+y*d]=1 then neighbors <- neighbors + 1 | |
if y>0 && a.[x+(y-1)*d]=1 then neighbors <- neighbors + 1 | |
if y<d-1 && a.[x+(y+1)*d]=1 then neighbors <- neighbors + 1 | |
if (neighbors%2)=0 then greens <- greens + 1 | |
if results.ContainsKey greens then | |
results.[greens] <- (results.[greens]+1) | |
else | |
results.Add( greens, 1 ) | |
// print results | |
let keys = Array.sort (Array.ofSeq results.Keys) | |
for key in keys do | |
printfn "%O: %O" key results.[key] | |
// 0: 16 | |
// 2: 256 | |
// 3: 512 | |
// 4: 1472 | |
// 5: 4608 | |
// 6: 7936 | |
// 7: 11264 | |
// 8: 13408 | |
// 9: 11264 | |
// 10: 7936 | |
// 11: 4608 | |
// 12: 1472 | |
// 13: 512 | |
// 14: 256 | |
// 16: 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment