Mercurial > repos > icfp2012
comparison src/sim.tp @ 13:c92633098f1d
simulator now properly parses map and accepts input until A is sent to it over stdin
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 14 Jul 2012 05:15:00 -0700 |
parents | 6ef6dc8ab95e |
children | 26cfb964fe81 |
comparison
equal
deleted
inserted
replaced
12:6ef6dc8ab95e | 13:c92633098f1d |
---|---|
20 index <- index + 1 | 20 index <- index + 1 |
21 } | 21 } |
22 } | 22 } |
23 | 23 |
24 makeCellTypes <- { | 24 makeCellTypes <- { |
25 allstr <- #[] | 25 allid <- #[] |
26 allobj <- #[] | 26 allobj <- #[] |
27 new <- :idstr { | 27 new <- :idstr { |
28 ret <- #{ | 28 ret <- #{ |
29 id <- (idstr byte: 0) | 29 id <- (idstr byte: 0) |
30 isrobot <- { false } | 30 isrobot <- { false } |
31 } | 31 } |
32 allobj append: ret | 32 allobj append: ret |
33 allstr append: idstr | 33 allid append: (ret id) |
34 ret | 34 ret |
35 } | 35 } |
36 #{ | 36 #{ |
37 find <- :idstr { | 37 find <- :id { |
38 if: idstr = "R" { robot: } else: { | 38 if: id = ("R" byte: 0) { robot: } else: { |
39 index <- 0 | 39 index <- 0 |
40 while: { | 40 while: { |
41 if: index < (allstr length) { | 41 if: index < (allid length) { |
42 (allstr get: index) != idstr | 42 (allid get: index) != id |
43 } else: {false} | 43 } else: {false} |
44 } do: { | 44 } do: { |
45 index <- index + 1 | 45 index <- index + 1 |
46 } | 46 } |
47 if: index < (allstr length) { | 47 if: index < (allid length) { |
48 allobj get: index | 48 allobj get: index |
49 } else: { | 49 } else: { |
50 empty | 50 empty |
51 } | 51 } |
52 } | 52 } |
62 robot <- { | 62 robot <- { |
63 #{ | 63 #{ |
64 id <- ("R" byte: 0) | 64 id <- ("R" byte: 0) |
65 isrobot <- { true } | 65 isrobot <- { true } |
66 heldBreath <- 0 | 66 heldBreath <- 0 |
67 move <- :cmd { | |
68 cmd | |
69 } | |
67 } | 70 } |
68 } | 71 } |
69 } | 72 } |
70 } | 73 } |
71 ttrue <- true | 74 ttrue <- true |
84 | 87 |
85 state <- #{ | 88 state <- #{ |
86 new <- :in_grid in_width in_height { | 89 new <- :in_grid in_width in_height { |
87 nextGrid <- #[] | 90 nextGrid <- #[] |
88 robot <- false | 91 robot <- false |
92 endreached <- false | |
89 foreach: in_grid :index el{ | 93 foreach: in_grid :index el{ |
90 nextGrid append: el | 94 nextGrid append: el |
91 if: (el isrobot) { robot <- el } else: { true } | 95 if: (el isrobot) { |
96 print: "found robot\n" | |
97 robot <- el | |
98 } else: { true } | |
92 } | 99 } |
93 #{ | 100 #{ |
94 grid <- in_grid | 101 grid <- in_grid |
95 width <- in_width | 102 width <- in_width |
96 height <- in_height | 103 height <- in_height |
104 water <- 0 | 111 water <- 0 |
105 flooding <- 0 | 112 flooding <- 0 |
106 waterproof <- 10 | 113 waterproof <- 10 |
107 collected <- 0 | 114 collected <- 0 |
108 moves <- 0 | 115 moves <- 0 |
109 ended <- false | 116 ended <- {endreached} |
110 doUpdate <- { | 117 doUpdate <- { |
111 true | 118 true |
112 } | 119 } |
113 advance <- :roboCmd { | 120 advance <- :roboCmd { |
114 ended <- roboCmd = "A" | 121 endreached <- roboCmd = "A" |
115 robot move: roboCmd | 122 robot move: roboCmd |
116 moves <- moves + 1 | 123 moves <- moves + 1 |
117 doUpdate: | 124 doUpdate: |
118 self | 125 self |
119 } | 126 } |
121 } | 128 } |
122 } | 129 } |
123 fromStr <- :str { | 130 fromStr <- :str { |
124 | 131 |
125 strLen <- str byte_length: | 132 strLen <- str byte_length: |
126 maxRow <- 0 | 133 maxCol <- 0 |
127 curRow <- 0 | 134 col <- 0 |
135 rows <- 0 | |
136 nl <- (cellTypes newline) id | |
137 blank <- cellTypes empty | |
128 eachbyte: str :index element { | 138 eachbyte: str :index element { |
129 if: element = ((cellTypes newline) id) { | 139 if: element = nl { |
130 maxRow <- if: curRow > maxRow {curRow} else: {maxRow} | 140 maxCol <- if: col > maxCol {col} else: {maxCol} |
131 curRow <- 0 | 141 col <- 0 |
142 rows <- rows + 1 | |
132 } else: { | 143 } else: { |
133 curRow <- curRow + 1 | 144 col <- col + 1 |
134 } | 145 } |
135 } | 146 } |
136 | 147 |
148 grid <- #[] | |
137 eachbyte: str :index element { | 149 eachbyte: str :index element { |
138 if: element = ((cellTypes newline) id) { | 150 if: element = nl { |
139 // add spaces | 151 // add spaces |
140 curRow <- 0 | 152 while: { col < maxCol } do: { |
153 grid append: blank | |
154 col <- col + 1 | |
155 } | |
156 col <- 0 | |
141 } else: { | 157 } else: { |
142 curRow = curRow + 1 | 158 grid append: (cellTypes find: element) |
143 } | 159 col = col + 1 |
144 } | 160 } |
145 grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] | 161 } |
146 fresh <- new: grid 3 3 | 162 new: grid maxCol rows |
147 fresh | |
148 } | 163 } |
149 } | 164 } |
150 | 165 |
151 testMoves <- { | 166 testMoves <- { |
152 myStep <- 0 | 167 myStep <- 0 |