Rating:

```python
#!/usr/bin/env python2
from pwn import remote, log
import itertools
import re
import random
from pprint import pprint

digest = re.compile(r"that .+=(\w+).")
game = re.compile(r"Current state of the game: (.+)\n")

r = remote("199.247.6.180", 14002)

def hash(target):
from hashlib import md5
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+;':\"<>?,./\\"
count = 100
for item in itertools.product(chars, repeat=count):
s = "".join(item)
h = md5(s.encode()).hexdigest()[:5]
if target in h:
return s

def nimsum(l):
n = l[0]
for i in range(1, len(l)):
n = n ^ l[i]
return n

# Input the pile:
# 7
# Input the quantity:
# 17
# Current state of the game: [0, 0, 0, 0, 0, 0, 0, 0, 54, 40, 64, 5, 38, 57, 85]
#
# The gnome's move is: pile = 8, quantity = 15
# Current state of the game: [0, 0, 0, 0, 0, 0, 0, 0, 39, 40, 64, 5, 38, 57, 85]
def play_pile(first=None):
if first is None:
line_list = r.recvuntil("]\n")
line_list = r.recvuntil("]\n")
log.info(line_list)
game_status = eval(game.findall(line_list)[0])
nim = nimsum(game_status)
log.warning(nim)
if nim != 0:
pile = next((i for i, x in enumerate(game_status) if x ^ nim < x),
None)
sendnum = game_status[pile] - (game_status[pile] ^ nim)
else:
pile = next((i for i, x in enumerate(game_status) if x > 1), None)
sendnum = random.randint(1, game_status[pile])
log.info(r.recvuntil("Input the pile:\n"))
log.warning(str(pile))
r.sendline(str(pile))
log.info(r.recvuntil("Input the quantity:\n"))
log.warning(sendnum)
r.sendline(str(sendnum))

d = r.recvuntil(".\n")
log.info(d)
r.sendline(hash(digest.findall(d)[0]))
log.info(r.recvuntil("of stones.\n"))
log.info(r.recvuntil("stone wins!\n"))
log.info(r.recvuntil("You start.\n"))

play_pile(first="si")
for i in range(0, 100):
try:
play_pile()
except Exception:
log.success(r.recvline())
break
```