-
Notifications
You must be signed in to change notification settings - Fork 0
/
david.sml
89 lines (72 loc) · 2.47 KB
/
david.sml
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
structure David =
struct
val tagp = Params.param "better_default_tag"
(SOME("-tag", "Value to put in the tag field."))
"tag"
val problemp = Params.param "14"
(SOME("-problem", "Problem number to load."))
("problem")
val timelimitp = Params.param "10"
(SOME("-timelimit", "Max number of seconds to spend per problem."))
("timelimit")
(* favor far-down squares *)
(* fun heuristic (x, y) = y *)
(*
fun do_seed (problemId, problem, seed_idx, seed) =
let
val state = Board.reset (problem, seed_idx)
val commands = ForwardChain.simple_heuristic_solver (state, heuristic)
in
(if seed_idx > 0
then print ",\n"
else ());
print "{\n";
print ("\"problemId\": " ^ Int.toString problemId ^ ",\n");
print ("\"seed\": " ^ Int.toString (Word32.toInt seed) ^ ",\n");
print ("\"tag\": \"" ^ (!tagp) ^ "\",\n");
print ("\"solution\": \"");
print (implode (List.map (Board.forgetlegal o Board.anychar) commands));
print "\"\n";
print "}\n"
end
*)
fun do_seed (problemId, problem, seed_idx, seed) =
let
val state = Board.reset (problem, seed_idx)
val heuristic = LockStep.simple_heuristic problem
val seconds = Params.asint 10 timelimitp
val steps = LockStep.play_to_end (state, heuristic, Time.fromSeconds (IntInf.fromInt seconds), true)
val commands = List.rev (List.concat
(List.map
(fn (LockStep.Step {commands, ...}) => commands)
steps))
in
(if seed_idx > 0
then print ",\n"
else ());
print "{\n";
print ("\"problemId\": " ^ Int.toString problemId ^ ",\n");
print ("\"seed\": " ^ Int.toString (Word32.toInt seed) ^ ",\n");
print ("\"tag\": \"" ^ (!tagp) ^ "\",\n");
print ("\"solution\": \"");
print (implode (List.map (Board.forgetlegal o Board.anychar) commands));
print "\"\n";
print "}\n"
end
fun main () =
let
val problemId = Params.asint 1 problemp
val problem = Board.fromjson
("qualifiers/problem_" ^ Int.toString problemId ^".json")
val seeds = Board.seeds problem
in
print "[\n";
Vector.appi (fn (idx, seed) => do_seed (problemId, problem, idx, seed)) seeds;
print "]\n"
end
handle Board.Board s =>
TextIO.output (TextIO.stdErr, "Uncaught Board: " ^ s ^ "\n")
| LockStep.LockStep s =>
TextIO.output (TextIO.stdErr, "Uncaught LockStep: " ^ s ^ "\n")
end
val () = Params.main0 "No arguments." David.main