Mercurial > repos > icfp2012
annotate src/sim.tp @ 32:0a55ee387d69
Change grid order
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 14 Jul 2012 23:03:04 -0700 |
parents | f7a1daaec925 |
children | efa82c5e95c2 |
rev | line source |
---|---|
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
1 { |
12 | 2 eachbyte <- :string action { |
3 strLen <- string byte_length: | |
4 index <- 0 | |
5 while: {index < strLen} do: { | |
6 element <- (string byte: index) | |
7 action: index element | |
8 index <- index + 1 | |
9 } | |
10 } | |
11 | |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
12 debugLog <- :str { |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
13 os write: 2 str |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
14 } |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
15 |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
16 makeCellTypes <- { |
31 | 17 typedict <- dict linear |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
18 new <- :idStr { |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
19 ret <- #{ |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
20 id <- (idStr byte: 0) |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
21 str <- idStr |
10 | 22 isrobot <- { false } |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
23 } |
31 | 24 typedict set: (ret id) ret |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
25 ret |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
26 } |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
27 #{ |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
28 find <- :id { |
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
29 if: id = ("R" byte: 0) { robot: } else: { |
31 | 30 typedict get: id withDefault: empty |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
31 } |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
32 } |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
33 wall <- new: "#" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
34 empty <- new: " " |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
35 earth <- new: "." |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
36 rock <- new: "*" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
37 lambda <- new: "\\" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
38 closedlift <- new: "L" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
39 openlift <- new: "O" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
40 newline <- new: "\n" |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
41 robot <- { |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
42 #{ |
23 | 43 id <- ("R" byte: 0) |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
44 str <- "R" |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
45 isrobot <- { true } |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
46 heldBreath <- 0 |
23 | 47 x <- 0 |
48 y <- 0 | |
49 move <- :cmd mine { | |
50 writeMove <- :xPrime yPrime { | |
51 mine setCell: xPrime yPrime self | |
52 mine setCell: x y empty | |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
53 x <- xPrime |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
54 y <- yPrime |
23 | 55 } |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
56 writeMove: x (y + 1) |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
57 } |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
58 } |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
59 } |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
60 } |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
61 } |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
62 #{ |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
63 |
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
64 cellTypes <- makeCellTypes: |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
65 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
66 state <- #{ |
6 | 67 new <- :in_grid in_width in_height { |
10 | 68 nextGrid <- #[] |
69 robot <- false | |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
70 endreached <- false |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
71 ret <- #{ |
8 | 72 grid <- in_grid |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
73 width <- in_width |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
74 height <- in_height |
23 | 75 calcIndex <- :x y { x + y * width } |
76 calcX <- :index {index % width} | |
77 calcY <- :index {index / width} | |
10 | 78 setCell <- :x y val { |
23 | 79 grid set: (calcIndex: x y) val |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
80 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
81 getCell <- :x y { |
23 | 82 grid get: (calcIndex: x y) |
83 } | |
84 updatePos <- :obj Index { | |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
85 obj x!: (calcX: Index) |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
86 obj y!: (calcY: Index) |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
87 } |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
88 water <- 0 |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
89 flooding <- 0 |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
90 waterproof <- 10 |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
91 collected <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
92 moves <- 0 |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
93 ended <- {endreached} |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
94 doUpdate <- { |
12 | 95 true |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
96 } |
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
97 advance <- :roboCmd { |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
98 endreached <- roboCmd = "A" |
23 | 99 robot move: roboCmd self |
6 | 100 moves <- moves + 1 |
7
1bd46f854dbb
progress on sim, celltypes as objects
William Morgan <bill@mrgn.org>
parents:
6
diff
changeset
|
101 doUpdate: |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
102 self |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
103 } |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
104 printGrid <- { |
32 | 105 cur <- (grid length) - width |
106 col <- 0 | |
107 while: {cur >= 0} do: { | |
108 os write: 2 ((grid get: cur) str) | |
109 cur <- cur + 1 | |
110 col <- col + 1 | |
111 if: col = width { | |
112 col <- 0 | |
113 cur <- cur - (width + width) | |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
114 os write: 2 "\n" |
23 | 115 } |
116 } | |
32 | 117 } |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
118 } |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
119 foreach: in_grid :index el{ |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
120 nextGrid append: el |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
121 if: (el isrobot) { |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
122 robot <- el |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
123 ret updatePos: robot index |
30
031d46ff76a9
minor clean up to use 'if' and 'not'
William Morgan <bill@mrgn.org>
parents:
29
diff
changeset
|
124 } |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
125 } |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
126 ret |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
127 } |
32 | 128 |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
129 fromStr <- :str { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
130 strLen <- str byte_length: |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
131 maxCol <- 0 |
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
132 nl <- (cellTypes newline) id |
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
133 blank <- cellTypes empty |
32 | 134 lines <- #[] |
135 curline <- #[] | |
12 | 136 eachbyte: str :index element { |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
137 if: element = nl { |
32 | 138 col <- curline length |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
139 maxCol <- if: col > maxCol {col} else: {maxCol} |
32 | 140 lines append: curline |
141 curline <- #[] | |
10 | 142 } else: { |
32 | 143 curline append: (cellTypes find: element) |
10 | 144 } |
145 } | |
13
c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
146 grid <- #[] |
32 | 147 cur <- (lines length) - 1 |
148 while: { cur >= 0 } do: { | |
149 curline <- (lines get: cur) | |
150 foreach: curline :idx el { | |
151 grid append: el | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
152 } |
32 | 153 extra <- maxCol - (curline length) |
154 while: { extra > 0 } do: { | |
155 grid append: blank | |
156 extra <- extra - 1 | |
157 } | |
158 cur <- cur - 1 | |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
159 } |
32 | 160 new: grid maxCol (lines length) |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
161 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
162 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
163 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
164 testMoves <- { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
165 myStep <- 0 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
166 { |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
167 myStep <- myStep + 1 |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
168 if: myStep > 5 {"A"} else: {"W"} |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
169 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
170 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
171 |
20
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
172 readFd <- :fd { |
8 | 173 if: fd < 0 { "" } else: { |
174 cur <- "" | |
175 part <- "" | |
176 while: { | |
177 part <- os read: fd 128 | |
178 part != "" | |
179 } do: { | |
180 cur <- cur . part | |
181 } | |
182 cur | |
183 } | |
184 } | |
185 | |
20
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
186 readFile <- :path { |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
187 fd <- os open: path (os O_RDONLY) |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
188 out <- readFd: fd |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
189 os close: fd |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
190 out |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
191 } |
50a456168c25
Split readFd out of readFile for use in lifter. Add code to read map from stdin to lifter using code in sim
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
192 |
8 | 193 getMove <- { |
29 | 194 ret <- os read: 0 1 |
195 while: {ret = "\n"} do: { | |
196 ret <- os read: 0 1 | |
197 } | |
198 ret | |
3
bb29dcd46cbf
Put dummy code in placeholder source files. Create makefile.
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
199 } |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
200 |
8 | 201 main <- :args { |
202 if: (args length) < 2 { | |
203 print: "usage: sim filename\n" | |
204 } else: { | |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
205 verbose <- true |
8 | 206 text <- readFile: (args get: 1) |
14 | 207 print: text |
17 | 208 os close: 1 |
8 | 209 simState <- state fromStr: text |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
210 derp <- simState ended: |
30
031d46ff76a9
minor clean up to use 'if' and 'not'
William Morgan <bill@mrgn.org>
parents:
29
diff
changeset
|
211 while: { not: (simState ended: ) } do: { |
8 | 212 simState advance: (getMove: ) |
25
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
213 if: verbose { |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
214 simState printGrid |
a224dc43877f
major progress! ...robot always moves down though
William Morgan <bill@mrgn.org>
parents:
24
diff
changeset
|
215 } |
8 | 216 } |
5
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
217 } |
be946b2a2cbc
initial barf into simulator file
William Morgan <bill@mrgn.org>
parents:
3
diff
changeset
|
218 } |
8 | 219 |
3
bb29dcd46cbf
Put dummy code in placeholder source files. Create makefile.
Mike Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
220 } |
11
f28e465e9ee6
tried to unf*ck, ashamed rebarfing
William Morgan <bill@mrgn.org>
parents:
10
diff
changeset
|
221 } |