-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.fsx
77 lines (60 loc) · 2 KB
/
interpreter.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(*
LinqPad style for results:
body
{
margin: 0.3em 0.3em 0.4em 0.4em;
font-family: Consolas;
font-size: 80%;
background: white;
line-height: 57%;
}
*)
let random = new Random()
let size = 200
let ruleNumber = random.Next(0, 256)
ruleNumber.Dump();
"".Dump();
let iterations = size / 2
let buildBoolArray n =
[0..n] |> Seq.map (fun idex -> match random.Next(0, 100) with
| x when x >= 10 -> true
| _ -> false)
|> Seq.toArray
let computeRule number =
let hasBit num bit =
num &&& (1 <<< bit) > 0
let toBool n =
(hasBit n 2, hasBit n 1, hasBit n 0), hasBit number n
[0..7] |> Seq.map toBool
|> dict
let toString row =
let convert x =
match x with
| true -> "■"
| _ -> " "
let strs = row |> Seq.map convert
String.Join("", strs)
let go firstRow (ruleSet:IDictionary<(bool * bool * bool), bool>) step =
let getTriplets (row:bool[]) =
let toTriplet index =
match index with
| 0 -> (row.[0], row.[0], row.[1])
| x when x = row.Length - 1 -> (row.[x - 1], row.[x], row.[x])
| _ -> (row.[index - 1], row.[index], row.[index + 1])
[0..(row.Length - 1)] |> Seq.map toTriplet
let rec goInternal row index =
seq {
yield row
let newRow = row |> getTriplets
|> Seq.map (fun x -> ruleSet.Item(x))
|> Seq.toArray
match index with
| 1 -> yield newRow
| _ -> yield! goInternal newRow (index - 1)
}
goInternal firstRow step
let rule = ruleNumber |> computeRule
let firstRow = buildBoolArray size
let results = go firstRow rule iterations
let printable = String.Join(Environment.NewLine, results |> Seq.map toString)
printable.Dump();