This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx_message.py
408 lines (336 loc) · 11.7 KB
/
x_message.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from typing import List
from enum import Enum
from x_helpers import Position
from copy import deepcopy
MESSAGE_LENGTH = 32
CHAR_BITS = 8
#
ANT_ID_BITS = 12
CHAT_KIND_BITS = 2
CELL_KIND_BITS = 5
# hashes
hash_index = 0
hash_index_to_pos = []
hash_pos_to_index = {}
for i in range(35):
for j in range(35):
hash_index_to_pos.append([i, j])
hash_pos_to_index[(i, j)] = hash_index
hash_index += 1
class ChatKind(Enum):
OBSERVATION_VALUE = '00' # [position + kind + value]
OBSERVATION_SIMPLE = '01' # [position + kind]
SINGLE_CELL_KIND = '10' # [kind]
END = '11' # not needed
class CellKind(Enum):
WALL = 0
GRASS = 1
BREAD = 2
INVALID = 3
SWAMP = 4
TRAP = 5
ENEMY_BASE = 6
ME_WORKER = 7
ME_SOLDIER = 8
WANT_TO_DEFEND = 9
WANT_TO_HARVEST = 10
WANT_TO_GATHER = 11
WANT_TO_EXPLORE = 12
# NO POSITION NEEDED
EXPLORER_DIED = 14
HELP_ME = 15
LETS_FUCK_THIS_SHIT = 16
ME_EXPLORER = 17
WORKER_BORN = 18
WORKER_DIED = 19
SOLDIER_BORN = 20
SOLDIER_DIED = 21
INVALID_FOR_WORKER = 22
DEFENDER_DIED = 23
@staticmethod
def get_value(kind: int):
if kind == 0:
return CellKind.WALL
if kind == 1:
return CellKind.GRASS
if kind == 2:
return CellKind.BREAD
if kind == 3:
return CellKind.INVALID
if kind == 4:
return CellKind.SWAMP
if kind == 5:
return CellKind.TRAP
if kind == 6:
return CellKind.ENEMY_BASE
if kind == 7:
return CellKind.ME_WORKER
if kind == 8:
return CellKind.ME_SOLDIER
if kind == 9:
return CellKind.WANT_TO_DEFEND
if kind == 10:
return CellKind.WANT_TO_HARVEST
if kind == 11:
return CellKind.WANT_TO_GATHER
if kind == 12:
return CellKind.WANT_TO_EXPLORE
if kind == 14:
return CellKind.EXPLORER_DIED
if kind == 15:
return CellKind.HELP_ME
if kind == 16:
return CellKind.LETS_FUCK_THIS_SHIT
if kind == 17:
return CellKind.ME_EXPLORER
if kind == 18:
return CellKind.WORKER_BORN
if kind == 19:
return CellKind.WORKER_DIED
if kind == 20:
return CellKind.SOLDIER_BORN
if kind == 21:
return CellKind.SOLDIER_DIED
if kind == 22:
return CellKind.INVALID_FOR_WORKER
if kind == 23:
return CellKind.DEFENDER_DIED
return None
def get_kind_score(kind: CellKind):
if kind == CellKind.WALL:
return 5
if kind == CellKind.GRASS:
return 5
if kind == CellKind.BREAD:
return 5
if kind == CellKind.INVALID:
return 100
if kind == CellKind.INVALID_FOR_WORKER:
return 100
if kind == CellKind.SWAMP:
return 10
if kind == CellKind.TRAP:
return 10
if kind == CellKind.ME_WORKER:
return 3
if kind == CellKind.ME_SOLDIER:
return 6
if kind == CellKind.ENEMY_BASE:
return 2000
if kind == CellKind.WANT_TO_DEFEND:
return 10
if kind == CellKind.WANT_TO_HARVEST:
return 10
if kind == CellKind.WANT_TO_GATHER:
return 1500
if kind == CellKind.WANT_TO_EXPLORE:
return 200
if kind == CellKind.HELP_ME:
return 150
if kind == CellKind.LETS_FUCK_THIS_SHIT:
return 3000
if kind == CellKind.ME_EXPLORER:
return 100
if kind == CellKind.WORKER_BORN:
return 250
if kind == CellKind.WORKER_DIED:
return 254
if kind == CellKind.SOLDIER_BORN:
return 251
if kind == CellKind.SOLDIER_DIED:
return 253
if kind == CellKind.DEFENDER_DIED:
return 250
if kind == CellKind.EXPLORER_DIED:
return 250
return 0
class Chat():
def __init__(self, type, data) -> None:
self.type: ChatKind = type
self.data = data
self.score = self.data.score
def __str__(self) -> str:
return f"({self.type} {self.data} sc={self.score})"
def __repr__(self) -> str:
return self.__str__()
class ChatObservationSimple():
def __init__(self, position=None, cell_kind=None) -> None:
self.position: Position = position
self.cell_kind: CellKind = cell_kind
self.score = 0
self.get_score()
# CONSTS
self.POSITION_BITS = 15
self.CELL_KIND_BITS = CELL_KIND_BITS
self.MESSAGE_BITS = self.POSITION_BITS + self.CELL_KIND_BITS
def get_score(self) -> int:
self.score = get_kind_score(self.cell_kind)
def __str__(self) -> str:
return f"{self.position} {self.cell_kind}"
def __repr__(self) -> str:
return self.__str__()
class ChatObservationValue():
def __init__(self, position=None, cell_kind=None, value=None) -> None:
self.position: Position = position
self.cell_kind: CellKind = cell_kind
self.value: int = value
self.score = 0
self.get_score()
# CONSTS
self.POSITION_BITS = 15
self.CELL_KIND_BITS = CELL_KIND_BITS
self.VALUE_BITS = 10
self.MESSAGE_BITS = self.POSITION_BITS + self.CELL_KIND_BITS + self.VALUE_BITS
def get_score(self) -> int:
self.score = get_kind_score(self.cell_kind)
def __str__(self) -> str:
return f"{self.position} {self.cell_kind} {self.value}"
def __repr__(self) -> str:
return self.__str__()
class ChatSingleCellKind():
def __init__(self, cell_kind=None) -> None:
self.cell_kind = cell_kind
self.score = 0
self.get_score()
# CONSTS
self.CELL_KIND_BITS = CELL_KIND_BITS
self.MESSAGE_BITS = self.CELL_KIND_BITS
def get_score(self) -> int:
self.score = get_kind_score(self.cell_kind)
def __str__(self) -> str:
return f"{self.cell_kind}"
def __repr__(self) -> str:
return self.__str__()
def to_bin_with_fixed_length(dec: int, goal_len: int):
"converts dec to bin and make it goal_len"
bin = f"{dec:b}"
bin = f"{'0'*(goal_len-len(bin))}{bin}"
return bin
def encode(ant_id, messages: List[Chat]) -> str:
"encode a message into multi-message format"
messages.sort(key=lambda x: x.score, reverse=True)
s = ''
total_score = 0
bits_remaining = MESSAGE_LENGTH * CHAR_BITS
# create ant id with ANT_ID_BITS bits
ant_id_bin = to_bin_with_fixed_length(ant_id, ANT_ID_BITS)
s += ant_id_bin
bits_remaining -= len(ant_id_bin)
for msg in messages:
# for OBSERVATION_SIMPLE
if msg.type == ChatKind.OBSERVATION_SIMPLE:
if bits_remaining >= msg.data.MESSAGE_BITS + CHAT_KIND_BITS:
s += ChatKind.OBSERVATION_SIMPLE.value
idx = hash_pos_to_index[(
msg.data.position.x, msg.data.position.y)]
s += to_bin_with_fixed_length(idx, msg.data.POSITION_BITS)
s += to_bin_with_fixed_length(msg.data.cell_kind.value,
msg.data.CELL_KIND_BITS)
bits_remaining -= msg.data.MESSAGE_BITS + CHAT_KIND_BITS
total_score += msg.data.score
# for OBSERVATION_VALUE
if msg.type == ChatKind.OBSERVATION_VALUE:
if bits_remaining >= msg.data.MESSAGE_BITS + CHAT_KIND_BITS:
s += ChatKind.OBSERVATION_VALUE.value
idx = hash_pos_to_index[(
msg.data.position.x, msg.data.position.y)]
s += to_bin_with_fixed_length(idx, msg.data.POSITION_BITS)
s += to_bin_with_fixed_length(msg.data.cell_kind.value,
msg.data.CELL_KIND_BITS)
s += to_bin_with_fixed_length(msg.data.value,
msg.data.VALUE_BITS)
bits_remaining -= msg.data.MESSAGE_BITS + CHAT_KIND_BITS
total_score += msg.data.score
# for SINGLE_CELL_KIND
if msg.type == ChatKind.SINGLE_CELL_KIND:
if bits_remaining >= msg.data.MESSAGE_BITS + CHAT_KIND_BITS:
s += ChatKind.SINGLE_CELL_KIND.value
s += to_bin_with_fixed_length(msg.data.cell_kind.value,
msg.data.CELL_KIND_BITS)
bits_remaining -= msg.data.MESSAGE_BITS + CHAT_KIND_BITS
total_score += msg.data.score
final = ''
while len(s) % CHAR_BITS != 0:
s += '0'
bits_remaining -= 1
cntr = len(s) // CHAR_BITS
for i in range(cntr):
char = s[i*CHAR_BITS:(i+1)*CHAR_BITS]
char = int(char, 2)
final += chr(char)
return final, total_score
def parser(bin):
ant_id = int(bin[:ANT_ID_BITS], 2)
bin = bin[ANT_ID_BITS:]
msgs = []
while len(bin) > CHAT_KIND_BITS:
chat_kind = bin[:CHAT_KIND_BITS]
bin = bin[CHAT_KIND_BITS:]
# OBSERVATION_SIMPLE
if chat_kind == ChatKind.OBSERVATION_SIMPLE.value:
chat = ChatObservationSimple()
if len(bin) < chat.MESSAGE_BITS:
break
pos = ord(chr(int(bin[:chat.POSITION_BITS], 2)))
pos = hash_index_to_pos[pos]
chat.position = Position(pos[0], pos[1])
bin = bin[chat.POSITION_BITS:]
kind = ord(chr(int(bin[:chat.CELL_KIND_BITS], 2)))
chat.cell_kind = CellKind(kind)
chat.get_score()
bin = bin[chat.CELL_KIND_BITS:]
msgs.append(
Chat(type=ChatKind.OBSERVATION_SIMPLE, data=deepcopy(chat)))
# OBSERVATION_VALUE
if chat_kind == ChatKind.OBSERVATION_VALUE.value:
chat = ChatObservationValue()
if len(bin) < chat.MESSAGE_BITS:
break
pos = ord(chr(int(bin[:chat.POSITION_BITS], 2)))
pos = hash_index_to_pos[pos]
chat.position = Position(pos[0], pos[1])
bin = bin[chat.POSITION_BITS:]
kind = ord(chr(int(bin[:chat.CELL_KIND_BITS], 2)))
chat.cell_kind = CellKind(kind)
chat.get_score()
bin = bin[chat.CELL_KIND_BITS:]
value = ord(chr(int(bin[:chat.VALUE_BITS], 2)))
chat.value = value
bin = bin[chat.VALUE_BITS:]
msgs.append(
Chat(type=ChatKind.OBSERVATION_VALUE, data=deepcopy(chat)))
# SINGLE_CELL_KIND
if chat_kind == ChatKind.SINGLE_CELL_KIND.value:
chat = ChatSingleCellKind()
if len(bin) < chat.MESSAGE_BITS:
break
kind = ord(chr(int(bin[:chat.CELL_KIND_BITS], 2)))
chat.cell_kind = CellKind(kind)
chat.get_score()
bin = bin[chat.CELL_KIND_BITS:]
msgs.append(
Chat(type=ChatKind.SINGLE_CELL_KIND, data=deepcopy(chat)))
return ant_id, msgs
def decode(msg: str):
"decode a message into multi-message format"
try:
s = ''
for m in msg:
s += to_bin_with_fixed_length(ord(m), CHAR_BITS)
return parser(s)
except Exception:
return 0, []
# tests
if __name__ == '__main__':
f_msg1 = Chat(type=ChatKind.OBSERVATION_SIMPLE,
data=ChatObservationSimple(
Position(15, 12), CellKind.ME_EXPLORER))
f_msg2 = Chat(type=ChatKind.OBSERVATION_VALUE,
data=ChatObservationValue(
Position(3, 4), CellKind.BREAD, 16))
f_msg3 = Chat(type=ChatKind.SINGLE_CELL_KIND,
data=ChatSingleCellKind(CellKind.HELP_ME))
m, sc = encode(
1337, [f_msg1, f_msg2, f_msg3, f_msg1, f_msg2])
id, e = decode(m)
print(id, e)