Mercurial > repos > icfp2012
comparison src/sim.tp @ 33:efa82c5e95c2
must commit, too late. does not compile.
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sun, 15 Jul 2012 03:05:36 -0700 |
parents | 0a55ee387d69 |
children | ac0df071afe7 |
comparison
equal
deleted
inserted
replaced
32:0a55ee387d69 | 33:efa82c5e95c2 |
---|---|
1 { | 1 { |
2 null <- #{} | |
3 | |
2 eachbyte <- :string action { | 4 eachbyte <- :string action { |
3 strLen <- string byte_length: | 5 strLen <- string byte_length: |
4 index <- 0 | 6 index <- 0 |
5 while: {index < strLen} do: { | 7 while: {index < strLen} do: { |
6 element <- (string byte: index) | 8 element <- (string byte: index) |
33 wall <- new: "#" | 35 wall <- new: "#" |
34 empty <- new: " " | 36 empty <- new: " " |
35 earth <- new: "." | 37 earth <- new: "." |
36 rock <- new: "*" | 38 rock <- new: "*" |
37 lambda <- new: "\\" | 39 lambda <- new: "\\" |
38 closedlift <- new: "L" | 40 closedLift <- new: "L" |
39 openlift <- new: "O" | 41 openLift <- new: "O" |
40 newline <- new: "\n" | 42 newline <- new: "\n" |
41 robot <- { | 43 robot <- { |
42 #{ | 44 ret <- #{ |
43 id <- ("R" byte: 0) | 45 id <- ("R" byte: 0) |
44 str <- "R" | 46 str <- "R" |
45 isrobot <- { true } | |
46 heldBreath <- 0 | |
47 x <- 0 | 47 x <- 0 |
48 y <- 0 | 48 y <- 0 |
49 move <- :cmd mine { | 49 isrobot <- { true } |
50 writeMove <- :xPrime yPrime { | 50 collected <- 0 |
51 heldBreath <- 0 | |
52 razors <- 0 | |
53 busted <- false | |
54 mine <- null | |
55 doCmd <- :cmd { | |
56 action <- commands get: cmd | |
57 action: | |
58 } | |
59 move <- :xDelta yDelta { | |
60 xPrime <- x + xDelta | |
61 yPrime <- y + yDelta | |
62 | |
63 writeMove <- { | |
51 mine setCell: xPrime yPrime self | 64 mine setCell: xPrime yPrime self |
52 mine setCell: x y empty | 65 mine setCell: x y empty |
53 x <- xPrime | 66 x <- xPrime |
54 y <- yPrime | 67 y <- yPrime |
55 } | 68 } |
56 writeMove: x (y + 1) | 69 |
70 navigable <- { | |
71 // need "any" and "all" functions... | |
72 if: self = empty {true} else: { | |
73 if: self = earth {true} else: { | |
74 if: self = lambda {true} else: { | |
75 if: self = openLift {true} else: { | |
76 false }}}} | |
77 } | |
78 | |
79 consequenceOf <- { | |
80 if: self = lambda {collected <- collected + 1} | |
81 if: self = openLift {mine succeeded!: true} | |
82 } | |
83 | |
84 destination <- mine getCell: xPrime yPrime | |
85 | |
86 if: (destination navigable: ) { | |
87 consequenceOf: destination | |
88 writeMove: | |
89 } else: { | |
90 if: destination = rock { | |
91 xPrimePrime <- xDelta * 2 + x | |
92 rockDestination <- mine getCell: xPrimePrime y | |
93 if: rockDestination = empty { | |
94 mine setCell: xPrimePrime y rock | |
95 writeMove: | |
96 } | |
97 } | |
98 } | |
99 | |
57 } | 100 } |
58 } | 101 } |
102 commands <- dict linear | |
103 commands set: "L" {move: -1 0 } | |
104 commands set: "R" {move: 1 0 } | |
105 commands set: "U" {move: 0 1 } | |
106 commands set: "D" {move: 0 -1 } | |
107 //commands set: "A" {mine ended!: true} | |
108 ret | |
59 } | 109 } |
60 } | 110 } |
61 } | 111 } |
62 #{ | 112 #{ |
63 | 113 |
64 cellTypes <- makeCellTypes: | 114 cellTypes <- makeCellTypes: |
65 | 115 |
66 state <- #{ | 116 state <- #{ |
67 new <- :in_grid in_width in_height { | 117 new <- :in_grid in_width in_height { |
68 nextGrid <- #[] | 118 nextGrid <- #[] |
69 robot <- false | 119 robot <- null |
70 endreached <- false | 120 endreached <- false |
71 ret <- #{ | 121 ret <- #{ |
72 grid <- in_grid | 122 grid <- in_grid |
73 width <- in_width | 123 width <- in_width |
74 height <- in_height | 124 height <- in_height |
86 obj y!: (calcY: Index) | 136 obj y!: (calcY: Index) |
87 } | 137 } |
88 water <- 0 | 138 water <- 0 |
89 flooding <- 0 | 139 flooding <- 0 |
90 waterproof <- 10 | 140 waterproof <- 10 |
91 collected <- 0 | |
92 moves <- 0 | 141 moves <- 0 |
93 ended <- {endreached} | 142 ended <- {endreached} |
143 succeeded <- false | |
94 doUpdate <- { | 144 doUpdate <- { |
95 true | 145 true |
96 } | 146 } |
97 advance <- :roboCmd { | 147 advance <- :roboCmd { |
98 endreached <- roboCmd = "A" | 148 endreached <- roboCmd = "A" |
99 robot move: roboCmd self | 149 robot doCmd: roboCmd inMine: self |
100 moves <- moves + 1 | 150 moves <- moves + 1 |
101 doUpdate: | 151 doUpdate: |
102 self | 152 self |
103 } | 153 } |
104 printGrid <- { | 154 printGrid <- { |
116 } | 166 } |
117 } | 167 } |
118 } | 168 } |
119 foreach: in_grid :index el{ | 169 foreach: in_grid :index el{ |
120 nextGrid append: el | 170 nextGrid append: el |
121 if: (el isrobot) { | 171 if: (el isrobot) { |
122 robot <- el | 172 robot <- el |
173 robot mine!: ret | |
123 ret updatePos: robot index | 174 ret updatePos: robot index |
124 } | 175 } |
176 | |
177 | |
178 // adding a 'new' method to robot and doing this instead | |
179 // wolud allow me to treat robots and other cellTypes equaly | |
180 // particularly for adding methods or state to other cellTypess. | |
181 // | |
182 // if: (el = (cellTypes robot)) { | |
183 // robot <- el new: | |
184 // (ret grid) set: index robot | |
185 // robot mine!: ret | |
186 // ret updatePos: robot index | |
187 // nextGrid append: el | |
188 // } else: { | |
189 // nextGrid append: el | |
190 // } | |
191 | |
125 } | 192 } |
126 ret | 193 ret |
127 } | 194 } |
128 | 195 |
129 fromStr <- :str { | 196 fromStr <- :str { |
134 lines <- #[] | 201 lines <- #[] |
135 curline <- #[] | 202 curline <- #[] |
136 eachbyte: str :index element { | 203 eachbyte: str :index element { |
137 if: element = nl { | 204 if: element = nl { |
138 col <- curline length | 205 col <- curline length |
139 maxCol <- if: col > maxCol {col} else: {maxCol} | 206 maxCol <- if: col > maxCol {col} else: {maxCol} |
140 lines append: curline | 207 lines append: curline |
141 curline <- #[] | 208 curline <- #[] |
142 } else: { | 209 } else: { |
143 curline append: (cellTypes find: element) | 210 curline append: (cellTypes find: element) |
144 } | 211 } |
159 } | 226 } |
160 new: grid maxCol (lines length) | 227 new: grid maxCol (lines length) |
161 } | 228 } |
162 } | 229 } |
163 | 230 |
164 testMoves <- { | |
165 myStep <- 0 | |
166 { | |
167 myStep <- myStep + 1 | |
168 if: myStep > 5 {"A"} else: {"W"} | |
169 } | |
170 } | |
171 | |
172 readFd <- :fd { | 231 readFd <- :fd { |
173 if: fd < 0 { "" } else: { | 232 if: fd < 0 { "" } else: { |
174 cur <- "" | 233 cur <- "" |
175 part <- "" | 234 part <- "" |
176 while: { | 235 while: { |
205 verbose <- true | 264 verbose <- true |
206 text <- readFile: (args get: 1) | 265 text <- readFile: (args get: 1) |
207 print: text | 266 print: text |
208 os close: 1 | 267 os close: 1 |
209 simState <- state fromStr: text | 268 simState <- state fromStr: text |
210 derp <- simState ended: | |
211 while: { not: (simState ended: ) } do: { | 269 while: { not: (simState ended: ) } do: { |
212 simState advance: (getMove: ) | 270 simState advance: (getMove: ) |
213 if: verbose { | 271 if: verbose { |
214 simState printGrid | 272 simState printGrid |
215 } | 273 } |