Mercurial > repos > icfp2012
view src/sim.tp @ 9:66ea6fdd3fcb
Merge
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 14 Jul 2012 01:46:25 -0700 |
parents | 5941e6b3684c 1bd46f854dbb |
children | 370a1eeb8812 |
line wrap: on
line source
#{ true <- #{ if:else <- :self trueblock :elseblock { trueblock: } } false <- #{ if:else <- :self trueblock :elseblock { elseblock: } } cellTypes <- #{ allstr <- #[] allobj <- #[] make <- :idstr { ret <- #{ id <- (idstr byte: 0) } allobj append: ret allstr append: idstr ret } find <- :idstr { if: idstr = "R" { robot } else: { index <- 0 while: { if: index < (allstr length) { (allstr get: index) != idstr } else: {false} } do: { index <- index + 1 } if: index <- (allstr length) { allobj get: index } else: { empty } } } wall <- make: "#" empty <- make: " " earth <- make: "." rock <- make: "*" lambda <- make: "\\" closedlift <- make: "L" openlift <- make: "O" newline <- make: "\n" robot <- { #{ id <- ("R" byte: 0) heldBreath <- 0 } } state <- #{ new <- :in_grid in_width in_height { next_grid <- #[] foreach: in_grid :index el{ next_grid append: el } #{ grid <- in_grid width <- in_width height <- in_height robot <- cellTypes robot address <- :x y { x + y * width } setCell <- :x y cell { grid set: (address: x y) cell } getCell <- :x y { grid get: (address: x y) } water <- 0 flooding <- 0 waterproof <- 10 collected <- 0 moves <- 0 ended <- false doMove <- :roboMove { robo <- doMove r nexty <- doMove y ended <- roboMove = "A" } doUpdate <- { updateCell <- :x, y { if } } advance <- :roboCmd { robot move: roboCmd moves <- moves + 1 doUpdate: self } } } fromStr <- :str { strLen <- str byte_length: index <- 0 maxRow <- 0 curRow <- 0 while: {index < strLen} do: { curByte <- str byte: index if: curByte = (cellType newline id) { maxRow <- if: curRow > maxRow {curRow} else: {maxRow} } else: { curRow = curRow + 1 } } grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0) ( "#" byte: 0) (" " byte: 0) ("#" byte: 0) ( "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] fresh <- new: grid 3 3 fresh } } testMoves <- { myStep <- 0 { print: (string: myStep) myStep <- myStep + 1 if: myStep > 5 {"A"} else: {"W"} } } readFile <- :path { fd <- os open: path (os O_RDONLY) print: "fd: " . fd . "\n" if: fd < 0 { "" } else: { cur <- "" part <- "" while: { part <- os read: fd 128 print: "read: " . part . "\n" 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: { print: (args get: 1) . "\n" text <- readFile: (args get: 1) simState <- state fromStr: text while: { if: (simState ended) {false} else: {true} } do: { print: "step...\n" simState advance: (getMove: ) } } } }