-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug-life.pas
199 lines (166 loc) · 3.62 KB
/
bug-life.pas
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Program buglife;
{$MODE OBJFPC} {$COPERATORS ON}
Type
PTile = ^TTile;
TTile = (
TILE_EMPTY,
TILE_BUG
);
PMap = ^TMap;
TMap = Object
Private
_data: Array[0..24] of TTile;
Function Offset(X, Y: NativeInt):NativeInt;
Function GetTile(X, Y: NativeInt):TTile;
Procedure SetTile(X, Y: NativeInt; T:TTile);
Public
Procedure Print();
Function GetBiodiversity():NativeUInt;
Function Equals(Const Other:TMap):Boolean;
Property Tile[X, Y: NativeInt]: TTile
read GetTile;
Constructor CreateFromString(Const Str:String);
Constructor CreateSimulateOther(Const Other: TMap);
Destructor Destroy();
end;
Function TMap.Offset(X, Y: NativeInt):NativeInt;
Begin
Result := (Y * 5) + X
End;
Function TMap.GetTile(X, Y: NativeInt):TTile;
Begin
If (X < 0) or (X >= 5) then
Result := TILE_EMPTY
Else If (Y < 0) or (Y >= 5) then
Result := TILE_EMPTY
Else
Result := _data[Offset(X, Y)]
End;
Procedure TMap.SetTile(X, Y: NativeInt; T:TTile);
Begin
_data[Offset(X, Y)] := T
End;
Function TMap.Equals(Const Other: TMap):Boolean;
Var
Idx: NativeInt;
Begin
Result := True;
For Idx := 0 to 24 do
If Self._data[Idx] <> Other._data[Idx] then begin
Result := False;
Break
end
End;
Procedure TMap.Print();
Var
Idx: NativeInt;
Begin
For Idx := 0 to 24 do begin
If _data[Idx] = TILE_BUG then
Write('#')
else
Write('.');
If ((Idx + 1) mod 5) = 0 then Writeln()
end;
End;
Function TMap.GetBiodiversity():NativeUInt;
Var
Idx, Power: NativeInt;
Begin
Result := 0;
Power := 1;
For Idx := 0 to 24 do begin
If _data[Idx] = TILE_BUG then Result += Power;
Power *= 2
end;
End;
Constructor TMap.CreateSimulateOther(Const Other: TMap);
Var
X, Y: NativeInt;
Neighbours: NativeInt;
Begin
For Y := 0 to 4 do
For X := 0 to 4 do begin
Neighbours := 0;
If(Other.GetTile(X, Y-1) = TILE_BUG) then Neighbours += 1;
If(Other.GetTile(X+1, Y ) = TILE_BUG) then Neighbours += 1;
If(Other.GetTile(X, Y+1) = TILE_BUG) then Neighbours += 1;
If(Other.GetTile(X-1, Y ) = TILE_BUG) then Neighbours += 1;
If Other.GetTile(X, Y) = TILE_BUG then begin
If Neighbours <> 1 then
Self.SetTile(X, Y, TILE_EMPTY)
else
Self.SetTile(X, Y, TILE_BUG)
end else begin
If (Neighbours = 1) or (Neighbours = 2) then
Self.SetTile(X, Y, TILE_BUG)
else
Self.SetTile(X, Y, TILE_EMPTY)
end
end;
End;
Constructor TMap.CreateFromString(Const Str:String);
Var
Idx: NativeInt;
T: TTile;
Begin
For Idx := 0 to 24 do begin
If Str[Idx+1] = '#' then
T := TILE_BUG
else
T := TILE_EMPTY;
_data[Idx] := T
end;
End;
Destructor TMap.Destroy();
Begin
// nothing to do here
End;
Var
Current: TMap;
History: Array of TMap;
HistCount, HistLength: NativeInt;
Const
HISTORY_LENGTH_STEP = 4096 div SizeOf(TMap);
Function SeenBefore():Boolean;
Var
Idx: NativeInt;
Begin
Result := False;
For Idx := 0 to (HistCount - 1) do
If Current.Equals(History[Idx]) then begin
Result := True;
Break
end
End;
Function GetInput():String;
Var
Idx: NativeInt;
Line: String;
Begin
Result := '';
For Idx := 0 to 4 do begin
Readln(Line);
Result += Line
end
End;
Begin
HistCount := 0;
HistLength := HISTORY_LENGTH_STEP;
SetLength(History, HistLength);
Current.CreateFromString(GetInput());
Current.Print();
Writeln();
While Not SeenBefore() do begin
If HistCount = HistLength then begin
HistLength += HISTORY_LENGTH_STEP;
SetLength(History, HistLength)
end;
History[HistCount] := Current;
Current.CreateSimulateOther(History[HistCount]);
HistCount += 1;
Current.Print();
Writeln()
end;
Writeln('Biodiversity rating: ', Current.GetBiodiversity())
End.