Mercurial > repos > icfp2012
view src/sim.tp @ 11:f28e465e9ee6
tried to unf*ck, ashamed rebarfing
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sat, 14 Jul 2012 02:21:32 -0700 |
parents | 370a1eeb8812 |
children | 6ef6dc8ab95e |
line wrap: on
line source
{ true <- #{ if:else <- :self trueblock :elseblock { trueblock: } } false <- #{ if:else <- :self trueblock :elseblock { elseblock: } } makeCellTypes <- { allstr <- #[] allobj <- #[] new <- :idstr { ret <- #{ id <- (idstr byte: 0) isrobot <- { false } } 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 <- 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 } } } } ttrue <- true tfalse <- false #{ // utilities true <- {ttrue} false <- {tfalse} foreach <- :string action { strLen <- string byte_length: index <- 0 while: {index < strLen} do: { element <- (string byte: index) action: index element index <- index + 1 } } // end utilities cellTypes <- makeCellTypes: state <- #{ new <- :in_grid in_width in_height { nextGrid <- #[] robot <- 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 <- 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: maxRow <- 0 curRow <- 0 foreach: str :index element { if: element = ((cellTypes newline) id) { maxRow <- if: curRow > maxRow {curRow} else: {maxRow} curRow <- 0 } else: { curRow <- curRow + 1 } } foreach: str :index element { if: element = ((cellTypes newline) id) { // add spaces curRow <- 0 } 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: ) } } } } }