-
Notifications
You must be signed in to change notification settings - Fork 3
/
GlobSolver.py
68 lines (56 loc) · 2.51 KB
/
GlobSolver.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
from src.datamodel.Competition import Competition
from src.functions.queries.grid import *
from src.settings import settings
from src.solver_multimodel.core.Solver import Solver
class GlobSolver(Solver):
""" Create a lookup table of all previously seen input/output pairs """
verbose = True
debug = True
solutions = {}
cache = {}
def __init__(self, tests_only=True):
super().__init__()
self.tests_only = tests_only
self.init_cache()
def init_cache(self):
if len(self.cache): return
competition = Competition()
for dataset_name, dataset in competition.items():
if dataset_name == 'test': continue # exclude test from the cache
for task in dataset:
for name, problemset in task.items():
for problem in problemset:
try:
if len(problem) == 0: continue
if problem['input'] is None or problem['output'] is None: continue
hash = problem['input'].tobytes()
self.solutions[hash] = (task.filename, problem['output'])
except Exception as exception:
pass
def detect(self, task):
if task.filename in self.cache: return True
if self.tests_only and 'test' not in task.filename: return False # We would get 100% success rate otherwise
# Loop through the all the inputs, as see if it is in our public database
for name, problemset in task.items():
inputs = [ problem['input'] for problem in problemset if problem ]
for input in inputs:
hash = input.tobytes()
if hash in self.solutions:
filename, solutions = self.solutions[hash]
self.cache[task.filename] = (filename,) # for logging purposes
return True
return False
def solve_grid(self, grid: np.ndarray, filename:str=None, task=None, *args):
"""If we have seen the input before, then propose the same output"""
hash = grid.tobytes()
if hash in self.solutions:
filename, solutions = self.solutions[hash]
return solutions
else:
return None
if __name__ == '__main__' and not settings['production']:
solver = GlobSolver(tests_only=True)
solver.verbose = True
competition = Competition()
competition.map(solver.solve_dataset)
print(competition)