-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.rs
362 lines (342 loc) · 10.5 KB
/
state.rs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import option::{option,some,none};
import geom::*;
import mine::*;
enum cmd { move(dir), wait, shave }
type score = i64;
type state = {
mine: mine,
rloc: point,
time: area,
lgot: area,
wdmg: area,
water: coord,
razors: area,
rolling: area,
rollrect: rect,
collected: bool,
c: @state_const
};
type state_const = {
tlim: area,
lamb: area,
flood: area,
wproof: area,
growth: area,
trampmap: ~[u8],
tramploc: ~[point],
targetloc: ~[point],
};
enum outcome {
cont,
died,
toolong,
won
}
pure fn rock_change(img: mine_image, here: point) -> option<point> {
if !img.get(here).is_rock() { ret none }
let hd = here.step(down);
let hl = here.step(left);
let hr = here.step(right);
let hdl = hd.step(left);
let hdr = hd.step(right);
if img.get(hd) == empty {
ret some(hd);
} else if img.get(hd).is_rock()
&& img.get(hr) == empty && img.get(hdr) == empty {
ret some(hdr);
} else if img.get(hd).is_rock()
// "is not empty or" entailed by "else if"
&& img.get(hl) == empty && img.get(hdl) == empty {
ret some(hdl);
} else if img.get(hd) == lambda
&& img.get(hr) == empty && img.get(hdr) == empty {
ret some(hdr);
} else {
ret none;
}
}
impl state for state {
fn step(cmd: cmd) -> (outcome, state) {
step(self, cmd)
}
pure fn score(outcome: option<outcome>) -> score {
let lgot = self.lgot as score;
let base = 25 * lgot - (self.time as score);
alt outcome {
none { base + 25 * lgot }
some(won) { base + 50 * lgot }
_ { base }
}
}
pure fn bonkp(bonk: &[point]) -> bool {
let airspace = self.rloc.step(up);
let painful = alt self.mine.get(airspace) {
rock | horock | lambda { true }
_ { false }
};
ret painful && vec::contains(bonk, airspace);
}
fn print() -> ~[str] { print(self) }
pure fn useful(cmd: cmd) -> bool {
alt cmd {
wait { self.rolling > 0 }
move(dir) {
let rl1 = self.rloc.step(dir);
alt self.mine.get(rl1) {
empty | earth | open_lift | lambda | tramp(_) | razor { true }
rock | horock {
alt dir {
left | right { self.mine.get(rl1.step(dir)) == empty }
up | down { false }
}
}
_ { false }
}
}
shave {
if self.razors > 0 {
let mut bp = false;
unchecked { // As pure as its callback....
for self.rloc.radiating |there| {
if self.mine.get(there) == beard { bp = true; break }
}
}
bp
} else {
false
}
}
}
}
}
fn step(state: state, cmd: cmd) -> (outcome, state) {
let mut state = { time: state.time + 1 with state };
let mut completing = false;
let mut collected = false;
// 2.2 Robot Movement
assert(state.mine.get(state.rloc) == robot);
alt cmd {
wait { }
move(d) {
let mut edits = ~[];
let mut nrloc = state.rloc.step(d);
let that = state.mine.get(nrloc);
let moved = alt that {
empty { true }
earth { true }
open_lift { completing = true; true }
lambda { state = {lgot: state.lgot + 1 with state};
collected = true; true }
rock | horock {
alt d {
left | right {
let rollto = nrloc.step(d);
if state.mine.get(rollto) == empty {
edits += ~[{ where: rollto, what: that }];
true
} else {
false
}
}
_ { false }
}
}
tramp(x) {
let y = state.c.trampmap[x];
assert(y < 16);
nrloc = state.c.targetloc[y];
for uint::range(0,15) |xx| {
if state.c.trampmap[xx] == y {
edits += ~[{ where: state.c.tramploc[xx], what: empty }];
}
}
true
}
razor {
state = { razors: state.razors + 1 with state };
true
}
_ { false }
};
if moved {
edits += ~[{ where: nrloc, what: robot },
{ where: state.rloc, what: empty }];
let rollrect = do vec::foldl(state.rollrect, edits) |r,e| {
r + e.where.box()
};
state = {rloc: nrloc, mine: state.mine.edit(edits),
rollrect: rollrect with state};
}
}
shave {
if state.razors > 0 {
let mut edits = ~[];
for state.rloc.radiating |there| {
if state.mine.get(there) == beard {
edits += ~[{ where: there, what: empty }];
}
}
state = {razors: state.razors - 1,
mine: state.mine.edit(edits) with state};
}
}
}
// 2.3 Map Update
let mut edits = ~[];
let mut bonk = ~[];
let mut nrr = state.rloc.box();
let growp = state.c.growth > 0 && state.time % state.c.growth == 0;
do state.mine.read |img| {
let rollrect = if growp { img.box () } else {
state.rollrect.grow(1, 1, 0, 1) * img.box()
};
do rollrect.iter |here| {
alt rock_change(img, here) {
none {
if growp && img.get(here) == beard {
for here.radiating |there| {
if img.get(there) == empty {
edits += ~[{ where: there, what: beard }]
}
}
}
}
some(there) {
let payload = alt check img.get(here) {
horock if img.get(there.step(down)) != empty { lambda }
whatever { whatever }
};
edits += ~[{ where: here, what: empty },
{ where: there, what: payload }];
bonk += ~[there];
nrr += here.box();
nrr += there.box();
}
}
}
}
if (collected || state.c.lamb == 0) && state.lgot == state.c.lamb {
// Note: if state.c.lamb == 0, best move is immediate abort.
// (So worry not about efficiency for that case.)
do state.mine.read |img| {
do img.box().iter |here| {
if img.get(here) == closed_lift {
edits += ~[{ where: here, what: open_lift }];
}
}
}
}
let water = state.water + if state.c.flood > 0
&& state.time % state.c.flood == 0 { 1 } else { 0 };
let wdmg = if water >= state.rloc.y { state.wdmg + 1 } else { 0 };
state = {mine: state.mine.edit(edits),
rolling: bonk.len() as area, rollrect: nrr,
collected: collected, water: water, wdmg: wdmg,
with state};
// 2.4 Ending Conditions
if completing {
ret (won, state);
} else if state.wdmg > state.c.wproof || state.bonkp(bonk) {
ret (died, state);
} else if state.time >= state.c.tlim {
ret (toolong, state);
} else {
ret (cont, state);
}
}
fn print(state: state) -> ~[str] {
assert(state.mine.get(state.rloc) == robot);
mine::print(state.mine)
}
fn parse(lines: &[str]) -> state {
let (img, metalines) = mine::parse(lines);
let mut water = -1;
let mut flood = 0;
let mut wproof = 10;
let mut rloc = none;
let mut lamb = 0;
let mut rolling = 0;
let mut growth = 25;
let mut razors = 0;
let mut beardp = false;
let trampmap = vec::to_mut(vec::from_elem(16, 16));
let tramploc = vec::to_mut(vec::from_elem(16, { x: -1, y: -1 }));
let targetloc = vec::to_mut(vec::from_elem(16, { x: -1, y: -1 }));
do img.iteri |y,line| {
do line.iteri |x,cell| {
let here = {x: x as coord, y: y as coord};
alt space_show_(cell) {
robot { rloc = some(here) }
lambda { lamb += 1 }
rock { rolling += 1 }
horock { lamb += 1; rolling += 1 }
tramp(x) { tramploc[x] = here; }
target(y) { targetloc[y] = here; }
beard { beardp = true; }
_ { }
}
}
}
for metalines.each |line| {
log(debug, line);
if line == "" { again } // Sigh.
let words = str::split_nonempty(line, char::is_whitespace);
alt check words[0].to_lower() {
"water" { water = (int::from_str(words[1]).get() - 1) as coord }
"flooding" { flood = int::from_str(words[1]).get() as area }
"waterproof" { wproof = int::from_str(words[1]).get() as area }
"trampoline" if words[2] == "targets" {
trampmap[alt check
space_of_char(str::char_at(words[1], 0)) { tramp(x) { x }}]
= alt check
space_of_char(str::char_at(words[3], 0)) { target(y) { y }};
}
"growth" { growth = int::from_str(words[1]).get() as area }
"razors" { razors = int::from_str(words[1]).get() as area }
}
}
{mine: new_mine(copy img),
rloc: option::get(rloc),
time: 0,
lgot: 0,
wdmg: 0,
water: water,
razors: razors,
rolling: rolling,
rollrect: img.box(),
collected: false,
c: @{tlim: img.box().area(),
lamb: lamb,
flood: flood,
wproof: wproof,
growth: if beardp { growth } else { 0 },
trampmap: vec::from_mut(trampmap),
tramploc: vec::from_mut(tramploc),
targetloc: vec::from_mut(targetloc)
}
}
}
pure fn cmd_of_char(c: char) -> cmd {
option::get(cmd_opt_of_char(c))
}
pure fn cmd_opt_of_char(c: char) -> option<cmd> {
alt c {
'L' { some(move(left)) }
'R' { some(move(right)) }
'U' { some(move(up)) }
'D' { some(move(down)) }
'W' { some(wait) }
'S' { some(shave) }
_ { none }
}
}
pure fn char_of_cmd(c: cmd) -> char {
alt c {
move(left) { 'L' }
move(right) { 'R' }
move(up) { 'U' }
move(down) { 'D' }
wait { 'W' }
shave { 'S' }
}
}