# HG changeset patch # User Mike Pavone # Date 1376202897 25200 # Node ID cde3f5943cd447e9b046776c22ab50f2b2f095c6 # Parent c94beafc8d1ea9629a3487dc6085e989e74d8afe Solver works! diff -r c94beafc8d1e -r cde3f5943cd4 src/solver.tp --- a/src/solver.tp Sat Aug 10 22:49:25 2013 -0700 +++ b/src/solver.tp Sat Aug 10 23:34:57 2013 -0700 @@ -9,6 +9,7 @@ #{ input <- { _val } valmap <- { _dict } + allInputs <- { vals } constantProgs <- { _const } append <- :tree { if: child? && (tree constant?) { @@ -41,7 +42,7 @@ if: len = 0 && _hasFirst? { len <- 1 } - len + _const length + len + (_const length) } printwithIndent <- :indent { print: indent . "Input: " . (hex: _val) . "\n" @@ -66,6 +67,31 @@ print <- { printwithIndent: "" } + findMatches:at <- :outputs :startIdx { + outVal <- outputs get: startIdx + sub <- _dict get: outVal withDefault: #{ + length <- { 0 } + } + res <- #[] + if: (sub length) > 0 { + res <- sub findMatches: outputs at: (startIdx + 1) + cps <- sub constantProgs + if: (cps length) > 0 { + isConstant <- true + cur <- 0 + while: { isConstant && cur < (cps length)} do: { + isConstant <- outVal = (outputs get: cur) + cur <- cur + 1 + } + if: isConstant { + foreach: cps :idx tree { + res append: cps + } + } + } + } + res + } } } else: { _arr <- #[] @@ -74,6 +100,7 @@ _arr append: tree } length <- { _arr length } + constantProgs <- { #[] } printwithIndent <- :indent { print: indent . "No more values for these:\n" indent <- indent . " " @@ -84,6 +111,9 @@ print <- { printwithIndent: "" } + findMatches:at <- :outputs :startIdx { + _arr + } } } } @@ -103,6 +133,43 @@ root } + solve:withAuth:andInfo:andProg <- :progId :authKey :info :prog { + resp <- (requests evalId: progId (info allInputs)) sendWithKey: authKey + if: (resp status) = "ok" { + matches <- info findMatches: (resp outputs) at: 0 + noSuccess <- true + cur <- 0 + while: { noSuccess && cur < (matches length) } do: { + prog root!: (matches get: cur) + gresp <- (requests guess: progId (string: prog)) sendWithKey: authKey + if: (gresp status) = "win" { + noSuccess <- false + } else: { + if: (gresp status) = "mismatch" { + failInput <- (gresp values) get: 0 + failOutput <- (gresp values) get: 1 + filtered <- #[] + foreach: matches :idx tree { + prog root!: tree + if: (prog run: failInput) = failOutput { + filtered append: tree + } + } + matches <- filtered + if: (matches length) = 0 { + print: "None of our programs actually matched 0x" . (hex: failOutput) ." with input 0x" . (hex: failInput) ." :(\n" + } + } else: { + print: "Got message: " . (gresp message) . ", moving on\n" + cur <- cur + 1 + } + } + } + } else: { + print: resp + } + } + main <- :args { size <- 3 if: (args length) > 1 { @@ -123,8 +190,16 @@ trees <- prog filterTrees: trees ops } info <- classify: prog trees numTests - print: info + if: (args length) > 5 { + progId <- (args get: 4) + authKey <- (args get: 5) + + solve: progId withAuth: authKey andInfo: info andProg: prog + } else: { + print: info + } } + 0 } } }