-
Notifications
You must be signed in to change notification settings - Fork 0
/
getscore.sml
executable file
·150 lines (132 loc) · 5.29 KB
/
getscore.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
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
structure GetScore =
struct
val file = Params.param ""
(SOME("-file", "A file containing JSON input as sent to the server."))
"file"
val scriptp = Params.param ""
(SOME("-script", "The command sequence (as characters) to run."))
"script"
val scriptfilep = Params.param ""
(SOME("-scriptfile", "A file containing (only) the command sequence to run."))
"scriptfile"
val problemp = Params.param "11"
(SOME("-problem", "Problem number to load."))
"problem"
val seedp = Params.param "0"
(SOME("-seed", "Seed *value* to use. Not an index."))
"seed"
fun get_score problema scripta seeda =
let
val seed = Word32.fromInt seeda
val problem = Board.fromjson
("qualifiers/problem_" ^ problema ^ ".json")
val state = Board.resetwithseed (problem, seed)
val full_script = explode (scripta)
fun replay { score, lines, phrases, script : char list } =
case script of
nil => { score = score, lines = lines, phrases = phrases,
leftover = 0, fate = "SCRIPT_EXHAUSTED" }
| #"\n" :: rest =>
replay { score = score, lines = lines, phrases = phrases,
script = rest }
| #"\r" :: rest =>
replay { score = score, lines = lines, phrases = phrases,
script = rest }
| #"\t" :: rest =>
replay { score = score, lines = lines, phrases = phrases,
script = rest }
| c :: rest =>
let
val legalchar : Board.legalchar = Board.legalize c
val Board.M { scored, lines = new_lines, locked = _,
new_phrases, status } =
Board.move (state, legalchar)
val score = score + scored
val lines = lines + new_lines
val phrases = phrases + new_phrases
datatype status = datatype Board.status
in
case status of
CONTINUE => replay { score = score, lines = lines,
phrases = phrases, script = rest }
| GAMEOVER why => { score = score,
lines = lines,
phrases = phrases,
leftover = length rest,
fate = (case why of
Board.COMPLETE => "COMPLETE"
| Board.NO_SPACE => "NO_SPACE") }
| ERROR => { score = 0,
lines = lines,
phrases = phrases,
leftover = length rest,
fate = "ERROR_STUTTER" }
end
in
replay { score = 0, lines = 0, phrases = 0, script = full_script }
handle Board.Board s =>
let in
TextIO.output(TextIO.stdErr, "Board exn: " ^ s ^ "\n");
{ score = 0, lines = 0, phrases = 0, leftover = 0,
fate = "EXCEPTION" }
end
end
fun fromjson s =
let
datatype json = datatype JSON.value
fun error_handle (msg,pos,data) =
raise Fail ("Error: " ^ msg ^ " near " ^ Int.toString pos)
val j = JSONParser.parseFile s
fun entry e =
(Int.toString (JSONUtils.Int (e, "problemId")),
JSONUtils.String (e, "solution"),
JSONUtils.Int (e, "seed"))
in
List.map entry (JSONUtils.UnList j)
handle JSONUtils.JSONUtils s =>
(TextIO.output (TextIO.stdErr,
"Uncaught JSON parse error: " ^ s ^ "\n");
[])
end
fun main () =
let
fun jsonline (k, v) = "\"" ^ k ^ "\": " ^ v
fun print_score { score, lines, phrases, leftover, fate } =
("{\n " ^
StringUtil.delimit ",\n "
(map jsonline
[("score", Int.toString score),
("distinct_phrases", Int.toString phrases),
("fate", "\"" ^ fate ^ "\""),
("commands_left", Int.toString leftover)]) ^
"\n}\n")
in
(if !file = "" then
(if !scriptfilep = "" then
print (print_score (get_score (!problemp) (!scriptp)
(Params.asint 0 seedp)))
else
print (print_score (get_score
(!problemp)
(StringUtil.readfile (!scriptfilep))
(Params.asint 0 seedp)))
)
else
(print "[";
print
(String.concatWith ",\n"
(List.map
(fn (p, sc, sd) => print_score (get_score p sc sd))
(fromjson (!file))));
print "]\n")
) (*; (* This next line can't be right. -rjs *)
print "]" *)
end
end
val () = Params.main0
("This program takes no arguments (but uses -flags). It prints out a simple " ^
"JSON object with statistics about the -script executed on the -problem " ^
"number with the given -seed value. Information about phrases of power, " ^
"including their impact on the score, is only relative to the phrases " ^
"known in phrases.sml.")
GetScore.main