-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d
66 lines (51 loc) · 1.44 KB
/
main.d
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
import std.stdio;
import std.file;
import std.getopt;
import utils;
import ConwayCommon;
void writeListStateToFile(ConwayState state, File file) {
auto range = state.getBoundingRange2D();
range.minRow = 0;
range.minCol = 0;
int arrCols = range.maxCol - range.minCol + 1;
byte[] asArr = state.getAsByteArray(range);
for (int row=range.minRow; row<=range.maxRow; row++) {
for (int col=range.minCol; col<=range.maxCol; col++) {
byte cell = asArr[(row - range.minRow) * arrCols + (col - range.minCol)];
file.write(cell == 1 ? '1' : '-');
}
file.writeln();
}
}
void conwayStateFromCharArr(ConwayState state, char[] data) {
int row=0, col=0;
for (int i=0; i<data.length; i++)
if (data[i] == '\n') {
row ++;
col = 0;
}
else if (data[i] != '\r') {
if (data[i] == '1')
state.setAlive(row, col);
col ++;
}
}
void main(string[] args) {
string inputFilePath = "in.txt";
string outputFilePath = "";
int numIterations = 1;
auto helpInformation = getopt(
args,
"input|i" , &inputFilePath ,
"number-of-iterations|n" , &numIterations ,
"output|o" , &outputFilePath
);
char[] strInput = [];
if (exists(inputFilePath))
strInput = cast(char[]) read(inputFilePath);
File outFile = (outputFilePath != "" ? File(outputFilePath, "w") : stdout);
auto state = new ConwayState();
conwayStateFromCharArr(state, strInput);
state.iterate(numIterations);
writeListStateToFile(state, outFile);
}