-
Notifications
You must be signed in to change notification settings - Fork 2
/
PolyformGenerator.hx
88 lines (79 loc) · 3.05 KB
/
PolyformGenerator.hx
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
78
79
80
81
82
83
84
85
86
87
88
package net.rezmason.polyform;
import net.rezmason.polyform.Step.*;
using net.rezmason.polyform.PolyformPlotter;
class PolyformGenerator {
static function main() {
var limit = 5;
#if !(js || flash)
var args = Sys.args();
if (args.length > 0) limit = Std.parseInt(args[0]);
#end
generate(limit, true);
}
public static function generate(limit:Int, ?verbose:Bool) {
var rules = [
[R, R] => [S, R, R, S],
[S, R] => [L, R, R, S],
[R, S] => [S, R, R, L],
[S, S] => [L, R, R, L],
[R, L, R] => [S, R, S],
[R, L, S] => [S, R, L],
];
var stringRules = new Map();
for (key in rules.keys()) stringRules[key.join('')] = rules[key].join('');
var polyominoes = [];
var lastMatches:Array<Polyform> = null;
for (ike in 0...limit + 1) {
var matches:Array<Polyform> = [];
var matchMap:Map<String, Polyform> = new Map();
if (ike == 0) {
matches.push(Polyform.nihiloform());
} else if (ike == 1) {
matches.push(Polyform.monoform(4));
} else {
for (poly in lastMatches) for (expansion in poly.expand(stringRules)) matchMap[expansion] = expansion;
for (poly in matchMap) {
if (poly.winding() != 4) throw 'Invalid: $poly'; // This actually tests the rules, not the pieces.
if (ike >= 7 && hasCoincidentPerimeter(poly)) continue;
matches.push(poly);
}
}
matches.sort(Polyform.sortFunction);
if (verbose) printPolyforms(ike, matches);
polyominoes.push(matches.copy());
lastMatches = matches;
}
return polyominoes;
}
inline static function printPolyforms(size:Int, polys:Array<Polyform>) {
var freeCount = 0;
var fixedCount = 0;
for (poly in polys) {
freeCount++;
fixedCount += poly.numReflections() * poly.numRotations();
println('<$poly> (${poly.numReflections()},${poly.numRotations()})');
println(poly.render().print());
}
println('There are $freeCount free polyominoes of size $size with no holes.');
println('There are $fixedCount fixed polyominoes of size $size with no holes.');
}
inline static function hasCoincidentPerimeter(poly:Polyform) {
var found = false;
if (poly.toString().substr(0, 3) == '$L$L$L') {
found = true;
} else {
var points = [];
poly.march(points, []);
var pointsVisited = new Map();
for (point in points) {
if (pointsVisited['$point'] != null) {
found = true;
break;
}
pointsVisited['$point'] = true;
}
}
return found;
}
inline static function println(str:Dynamic) { #if !(js || flash) Sys.println(str); #end }
}