Mercurial > repos > icfp2012
view src/sim.tp @ 18:883ae0c1b7f6
merge
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sat, 14 Jul 2012 11:09:36 -0700 |
parents | 998ef2173f28 |
children | be6d1cf7b7d7 75a005b210c5 |
line wrap: on
line source
{ true <- #{ if:else <- :self trueblock :elseblock { trueblock: } } false <- #{ if:else <- :self trueblock :elseblock { elseblock: } } eachbyte <- :string action { strLen <- string byte_length: index <- 0 while: {index < strLen} do: { element <- (string byte: index) action: index element index <- index + 1 } } makeCellTypes <- { allid <- #[] allobj <- #[] new <- :idstr { ret <- #{ id <- (idstr byte: 0) isrobot <- { false } } allobj append: ret allid append: (ret id) ret } #{ find <- :id { if: id = ("R" byte: 0) { robot: } else: { index <- 0 while: { if: index < (allid length) { (allid get: index) != id } else: {false} } do: { index <- index + 1 } if: index < (allid length) { allobj get: index } else: { empty } } } wall <- new: "#" empty <- new: " " earth <- new: "." rock <- new: "*" lambda <- new: "\\" closedlift <- new: "L" openlift <- new: "O" newline <- new: "\n" robot <- { #{ id <- ("R" byte: 0) isrobot <- { true } heldBreath <- 0 move <- :cmd { cmd } } } } } ttrue <- true tfalse <- false #{ // utilities true <- ttrue false <- tfalse // end utilities cellTypes <- makeCellTypes: state <- #{ new <- :in_grid in_width in_height { nextGrid <- #[] robot <- false endreached <- false foreach: in_grid :index el{ nextGrid append: el if: (el isrobot) { robot <- el } else: { true } } #{ grid <- in_grid width <- in_width height <- in_height address <- :x y { x + y * width } setCell <- :x y val { grid set: (address: x y) val } getCell <- :x y { grid get: (address: x y) } water <- 0 flooding <- 0 waterproof <- 10 collected <- 0 moves <- 0 ended <- {endreached} doUpdate <- { true } advance <- :roboCmd { endreached <- roboCmd = "A" robot move: roboCmd moves <- moves + 1 doUpdate: self } } } fromStr <- :str { strLen <- str byte_length: maxCol <- 0 col <- 0 rows <- 0 nl <- (cellTypes newline) id blank <- cellTypes empty eachbyte: str :index element { if: element = nl { maxCol <- if: col > maxCol {col} else: {maxCol} col <- 0 rows <- rows + 1 } else: { col <- col + 1 } } grid <- #[] eachbyte: str :index element { if: element = nl { // add spaces while: { col < maxCol } do: { grid append: blank col <- col + 1 } col <- 0 } else: { grid append: (cellTypes find: element) col = col + 1 } } new: grid maxCol rows } } testMoves <- { myStep <- 0 { myStep <- myStep + 1 if: myStep > 5 {"A"} else: {"W"} } } readFile <- :path { fd <- os open: path (os O_RDONLY) if: fd < 0 { "" } else: { cur <- "" part <- "" while: { part <- os read: fd 128 part != "" } do: { cur <- cur . part } os close: fd cur } } getMove <- { os read: 0 1 } main <- :args { if: (args length) < 2 { print: "usage: sim filename\n" } else: { text <- readFile: (args get: 1) print: text os close: 1 simState <- state fromStr: text while: { if: (simState ended) {false} else: {true} } do: { simState advance: (getMove: ) } } } } }