Mercurial > repos > icfp2012
comparison src/sim.tp @ 25:a224dc43877f
major progress! ...robot always moves down though
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sat, 14 Jul 2012 19:05:56 -0700 |
parents | 8435bf0360f8 |
children | 7a274d6026c8 |
comparison
equal
deleted
inserted
replaced
24:8435bf0360f8 | 25:a224dc43877f |
---|---|
25 action: index element | 25 action: index element |
26 index <- index + 1 | 26 index <- index + 1 |
27 } | 27 } |
28 } | 28 } |
29 | 29 |
30 debugLog <- :str { | |
31 os write: 2 str | |
32 } | |
33 | |
30 makeCellTypes <- { | 34 makeCellTypes <- { |
31 allid <- #[] | 35 allid <- #[] |
32 allobj <- #[] | 36 allobj <- #[] |
33 new <- :idstr { | 37 new <- :idStr { |
34 ret <- #{ | 38 ret <- #{ |
35 id <- (idstr byte: 0) | 39 id <- (idStr byte: 0) |
40 str <- idStr | |
36 isrobot <- { false } | 41 isrobot <- { false } |
37 } | 42 } |
38 allobj append: ret | 43 allobj append: ret |
39 allid append: (ret id) | 44 allid append: (ret id) |
40 ret | 45 ret |
66 openlift <- new: "O" | 71 openlift <- new: "O" |
67 newline <- new: "\n" | 72 newline <- new: "\n" |
68 robot <- { | 73 robot <- { |
69 #{ | 74 #{ |
70 id <- ("R" byte: 0) | 75 id <- ("R" byte: 0) |
76 str <- "R" | |
71 isrobot <- { true } | 77 isrobot <- { true } |
72 heldBreath <- 0 | 78 heldBreath <- 0 |
73 x <- 0 | 79 x <- 0 |
74 y <- 0 | 80 y <- 0 |
75 move <- :cmd mine { | 81 move <- :cmd mine { |
76 writeMove <- :xPrime yPrime { | 82 writeMove <- :xPrime yPrime { |
77 mine setCell: xPrime yPrime self | 83 mine setCell: xPrime yPrime self |
78 mine setCell: x y empty | 84 mine setCell: x y empty |
79 x!: xPrime | 85 x <- xPrime |
80 y!: yPrime | 86 y <- yPrime |
81 } | 87 } |
82 writeMove: x (y - 1) | 88 writeMove: x (y + 1) |
83 } | 89 } |
84 } | 90 } |
85 } | 91 } |
86 } | 92 } |
87 } | 93 } |
102 state <- #{ | 108 state <- #{ |
103 new <- :in_grid in_width in_height { | 109 new <- :in_grid in_width in_height { |
104 nextGrid <- #[] | 110 nextGrid <- #[] |
105 robot <- false | 111 robot <- false |
106 endreached <- false | 112 endreached <- false |
107 foreach: in_grid :index el{ | 113 ret <- #{ |
108 nextGrid append: el | |
109 if: (el isrobot) { | |
110 robot <- el | |
111 updatePos: robot index | |
112 } else: { true } | |
113 } | |
114 #{ | |
115 grid <- in_grid | 114 grid <- in_grid |
116 width <- in_width | 115 width <- in_width |
117 height <- in_height | 116 height <- in_height |
118 calcIndex <- :x y { x + y * width } | 117 calcIndex <- :x y { x + y * width } |
119 calcX <- :index {index % width} | 118 calcX <- :index {index % width} |
123 } | 122 } |
124 getCell <- :x y { | 123 getCell <- :x y { |
125 grid get: (calcIndex: x y) | 124 grid get: (calcIndex: x y) |
126 } | 125 } |
127 updatePos <- :obj Index { | 126 updatePos <- :obj Index { |
128 obj x! (calcX: Index) | 127 obj x!: (calcX: Index) |
129 obj y! (calcY: Index) | 128 obj y!: (calcY: Index) |
130 } | 129 } |
131 water <- 0 | 130 water <- 0 |
132 flooding <- 0 | 131 flooding <- 0 |
133 waterproof <- 10 | 132 waterproof <- 10 |
134 collected <- 0 | 133 collected <- 0 |
140 advance <- :roboCmd { | 139 advance <- :roboCmd { |
141 endreached <- roboCmd = "A" | 140 endreached <- roboCmd = "A" |
142 robot move: roboCmd self | 141 robot move: roboCmd self |
143 moves <- moves + 1 | 142 moves <- moves + 1 |
144 doUpdate: | 143 doUpdate: |
145 if: debugToStdErr { | 144 self |
146 grid foreach: :index value { | 145 } |
147 os write: 2 (string: (value id)) | 146 printGrid <- { |
147 grid foreach: :index value { | |
148 os write: 2 (value str) | |
149 if: index % width = width - 1 { | |
150 os write: 2 "\n" | |
148 } | 151 } |
149 } | 152 } |
150 self | 153 } |
151 } | 154 } |
152 | 155 foreach: in_grid :index el{ |
153 } | 156 nextGrid append: el |
157 if: (el isrobot) { | |
158 robot <- el | |
159 ret updatePos: robot index | |
160 } else: { true } | |
161 } | |
162 ret | |
154 } | 163 } |
155 fromStr <- :str { | 164 fromStr <- :str { |
156 | |
157 strLen <- str byte_length: | 165 strLen <- str byte_length: |
158 maxCol <- 0 | 166 maxCol <- 0 |
159 col <- 0 | 167 col <- 0 |
160 rows <- 0 | 168 rows <- 0 |
161 nl <- (cellTypes newline) id | 169 nl <- (cellTypes newline) id |
167 rows <- rows + 1 | 175 rows <- rows + 1 |
168 } else: { | 176 } else: { |
169 col <- col + 1 | 177 col <- col + 1 |
170 } | 178 } |
171 } | 179 } |
172 | 180 col <- 0 |
173 grid <- #[] | 181 grid <- #[] |
174 eachbyte: str :index element { | 182 eachbyte: str :index element { |
175 if: element = nl { | 183 if: element = nl { |
176 // add spaces | 184 // add spaces |
177 while: { col < maxCol } do: { | 185 while: { col < maxCol } do: { |
179 col <- col + 1 | 187 col <- col + 1 |
180 } | 188 } |
181 col <- 0 | 189 col <- 0 |
182 } else: { | 190 } else: { |
183 grid append: (cellTypes find: element) | 191 grid append: (cellTypes find: element) |
184 col = col + 1 | 192 col <- col + 1 |
185 } | 193 } |
186 } | 194 } |
187 new: grid maxCol rows | 195 new: grid maxCol rows |
188 } | 196 } |
189 } | 197 } |
219 | 227 |
220 getMove <- { | 228 getMove <- { |
221 os read: 0 1 | 229 os read: 0 1 |
222 } | 230 } |
223 | 231 |
224 debugToStdErr <- true | |
225 main <- :args { | 232 main <- :args { |
226 if: (args length) < 2 { | 233 if: (args length) < 2 { |
227 print: "usage: sim filename\n" | 234 print: "usage: sim filename\n" |
228 } else: { | 235 } else: { |
236 verbose <- true | |
229 text <- readFile: (args get: 1) | 237 text <- readFile: (args get: 1) |
230 print: text | 238 print: text |
231 os close: 1 | 239 os close: 1 |
232 simState <- state fromStr: text | 240 simState <- state fromStr: text |
233 while: { if: (simState ended) {false} else: {true} } do: { | 241 derp <- simState ended: |
242 while: { if: (simState ended: ) {false} else: {true} } do: { | |
234 simState advance: (getMove: ) | 243 simState advance: (getMove: ) |
244 if: verbose { | |
245 simState printGrid | |
246 } | |
235 } | 247 } |
236 } | 248 } |
237 } | 249 } |
238 | 250 |
239 } | 251 } |