This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
forked from droundy/icfp-2017-contest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battle-royale.py
57 lines (52 loc) · 1.99 KB
/
battle-royale.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
#!/usr/bin/env python2
from __future__ import division
import sys, os, subprocess, argparse, glob, itertools
import arena, rwjson
def rank_scores(scores):
ranks = {}
for i in range(len(scores)):
ranks[scores[i][1]] = i
for j in range(i+1,len(scores)):
if scores[i][0] == scores[j][0]:
ranks[scores[i][1]] = j
return ranks
def battle(max_size, programs):
permutations = itertools.permutations(programs)
pairs = set([])
triples = set([])
cumulative = {}
for p in programs:
cumulative[p] = 0
for p in permutations:
pairs.add(tuple(sorted(p[:2])))
if len(programs) > 2:
triples.add(tuple(sorted(p[:3])))
for mapfile in glob.glob('maps/*.json'):
with open(mapfile) as f:
themap = rwjson.readJson(f)
if len(themap['rivers']) > max_size:
print mapfile,'is too long with size', len(themap['rivers'])
continue
print '\ntesting map', mapfile, 'with size', len(themap['rivers'])
for pair in pairs:
scores = arena.arena(mapfile, pair)
ranks = rank_scores(scores)
print 'ranks are', ranks, 'and scores are', scores
for p in ranks:
cumulative[p] += ranks[p]
for pair in triples:
scores = arena.arena(mapfile, pair)
ranks = rank_scores(scores)
for p,v in ranks:
cumulative[p] += v
print 'cumulative so far:', cumulative
print 'cumulative score:', cumulative
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Compete some punters')
parser.add_argument('--max', metavar='NODES', type=int, nargs=1,
default=100,
help='the largest size map to test')
parser.add_argument('programs', metavar='PUNTER', nargs=argparse.REMAINDER,
help='the programs to compete')
args = parser.parse_args()
battle(args.max, args.programs)