-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileUtils.cs
More file actions
108 lines (93 loc) · 2.56 KB
/
TileUtils.cs
File metadata and controls
108 lines (93 loc) · 2.56 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
namespace Rule110;
public class TileUtils
{
public static int BodyLength(int[][] strips, int n)
{
var count = strips.Length;
var total = TotalLength(strips);
var rem = FirstNLength(strips, n % count);
return n / count * total + rem;
}
public static int FirstNLength(int[][] strips, int n)
{
return strips.Take(n + 1).Sum(s => s.Length);
}
public static int TotalLength(int[][] strips)
{
return strips.Sum(s => s.Length);
}
public static int[][] ParseStrips(string[] strips)
{
var res = new int[strips.Length][];
for (int i = 0; i < strips.Length; i++)
{
res[i] = ParseStrip(strips[i]);
}
return res;
}
public static int[] ParseStrip(string strip)
{
var res = new int[strip.Length];
for (int i = 0; i < strip.Length; i++)
{
res[i] = strip[i] == '*' ? 1 : 0;
}
return res;
}
public static Tile ParseTile(string[] stars, int nextRow)
{
var len = stars.Length;
var per = stars[0].Length;
var arr = new int[per * len];
for (int i = 0; i < len; i++)
{
for (int j = 0; j < stars[i].Length; j++)
{
arr[i * per + j] = stars[i][j] == '*'
? 1 : 0;
}
}
var tile = new Tile {
XPeriod = per,
YPeriod = len,
Arr = arr,
NextRow = nextRow
};
return tile;
}
public static TilePrefix ParsePrefix(string str, int entrance)
{
var prefix = new int[str.Length];
for (int j = 0; j < str.Length; j++)
{
prefix[j] = str[j] == '*' ? 1 : 0;
}
var tilePrefix = new TilePrefix {
Arr = prefix,
TileEntrance = entrance
};
return tilePrefix;
}
public static TileSuffix ParseSuffix((string, int)[] stars)
{
var len = stars.Length;
var entr = new int[len];
var opt = new List<int[]>();
for (int i = 0; i < len; i++)
{
var (str, entrance) = stars[i];
entr[i] = entrance;
var suffix = new int[str.Length];
for (int j = 0; j < str.Length; j++)
{
suffix[j] = str[j] == '*' ? 1 : 0;
}
opt.Add(suffix);
}
var tileSuffix = new TileSuffix {
Options = opt,
EtherEntrances = entr
};
return tileSuffix;
}
}