comparison src/solver.tp @ 38:cde3f5943cd4

Solver works!
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Aug 2013 23:34:57 -0700
parents b00904b36aca
children 1cadb591eef1
comparison
equal deleted inserted replaced
37:c94beafc8d1e 38:cde3f5943cd4
7 _hasFirst? <- false 7 _hasFirst? <- false
8 _val <- vals get: startIdx 8 _val <- vals get: startIdx
9 #{ 9 #{
10 input <- { _val } 10 input <- { _val }
11 valmap <- { _dict } 11 valmap <- { _dict }
12 allInputs <- { vals }
12 constantProgs <- { _const } 13 constantProgs <- { _const }
13 append <- :tree { 14 append <- :tree {
14 if: child? && (tree constant?) { 15 if: child? && (tree constant?) {
15 _const append: tree 16 _const append: tree
16 } else: { 17 } else: {
39 length <- { 40 length <- {
40 len <- _dict length 41 len <- _dict length
41 if: len = 0 && _hasFirst? { 42 if: len = 0 && _hasFirst? {
42 len <- 1 43 len <- 1
43 } 44 }
44 len + _const length 45 len + (_const length)
45 } 46 }
46 printwithIndent <- :indent { 47 printwithIndent <- :indent {
47 print: indent . "Input: " . (hex: _val) . "\n" 48 print: indent . "Input: " . (hex: _val) . "\n"
48 nextindent <- indent . " " 49 nextindent <- indent . " "
49 if: (_const length) > 0 { 50 if: (_const length) > 0 {
64 } 65 }
65 } 66 }
66 print <- { 67 print <- {
67 printwithIndent: "" 68 printwithIndent: ""
68 } 69 }
70 findMatches:at <- :outputs :startIdx {
71 outVal <- outputs get: startIdx
72 sub <- _dict get: outVal withDefault: #{
73 length <- { 0 }
74 }
75 res <- #[]
76 if: (sub length) > 0 {
77 res <- sub findMatches: outputs at: (startIdx + 1)
78 cps <- sub constantProgs
79 if: (cps length) > 0 {
80 isConstant <- true
81 cur <- 0
82 while: { isConstant && cur < (cps length)} do: {
83 isConstant <- outVal = (outputs get: cur)
84 cur <- cur + 1
85 }
86 if: isConstant {
87 foreach: cps :idx tree {
88 res append: cps
89 }
90 }
91 }
92 }
93 res
94 }
69 } 95 }
70 } else: { 96 } else: {
71 _arr <- #[] 97 _arr <- #[]
72 #{ 98 #{
73 append <- :tree { 99 append <- :tree {
74 _arr append: tree 100 _arr append: tree
75 } 101 }
76 length <- { _arr length } 102 length <- { _arr length }
103 constantProgs <- { #[] }
77 printwithIndent <- :indent { 104 printwithIndent <- :indent {
78 print: indent . "No more values for these:\n" 105 print: indent . "No more values for these:\n"
79 indent <- indent . " " 106 indent <- indent . " "
80 foreach: _arr :idx val { 107 foreach: _arr :idx val {
81 print: indent . (string: val) . "\n" 108 print: indent . (string: val) . "\n"
82 } 109 }
83 } 110 }
84 print <- { 111 print <- {
85 printwithIndent: "" 112 printwithIndent: ""
113 }
114 findMatches:at <- :outputs :startIdx {
115 _arr
86 } 116 }
87 } 117 }
88 } 118 }
89 } 119 }
90 #{ 120 #{
101 root append: tree 131 root append: tree
102 } 132 }
103 root 133 root
104 } 134 }
105 135
136 solve:withAuth:andInfo:andProg <- :progId :authKey :info :prog {
137 resp <- (requests evalId: progId (info allInputs)) sendWithKey: authKey
138 if: (resp status) = "ok" {
139 matches <- info findMatches: (resp outputs) at: 0
140 noSuccess <- true
141 cur <- 0
142 while: { noSuccess && cur < (matches length) } do: {
143 prog root!: (matches get: cur)
144 gresp <- (requests guess: progId (string: prog)) sendWithKey: authKey
145 if: (gresp status) = "win" {
146 noSuccess <- false
147 } else: {
148 if: (gresp status) = "mismatch" {
149 failInput <- (gresp values) get: 0
150 failOutput <- (gresp values) get: 1
151 filtered <- #[]
152 foreach: matches :idx tree {
153 prog root!: tree
154 if: (prog run: failInput) = failOutput {
155 filtered append: tree
156 }
157 }
158 matches <- filtered
159 if: (matches length) = 0 {
160 print: "None of our programs actually matched 0x" . (hex: failOutput) ." with input 0x" . (hex: failInput) ." :(\n"
161 }
162 } else: {
163 print: "Got message: " . (gresp message) . ", moving on\n"
164 cur <- cur + 1
165 }
166 }
167 }
168 } else: {
169 print: resp
170 }
171 }
172
106 main <- :args { 173 main <- :args {
107 size <- 3 174 size <- 3
108 if: (args length) > 1 { 175 if: (args length) > 1 {
109 size <- int32: (args get: 1) 176 size <- int32: (args get: 1)
110 } 177 }
121 if: (args length) > 3 { 188 if: (args length) > 3 {
122 ops <- (args get: 3) splitOn: "," 189 ops <- (args get: 3) splitOn: ","
123 trees <- prog filterTrees: trees ops 190 trees <- prog filterTrees: trees ops
124 } 191 }
125 info <- classify: prog trees numTests 192 info <- classify: prog trees numTests
126 print: info 193 if: (args length) > 5 {
127 } 194 progId <- (args get: 4)
195 authKey <- (args get: 5)
196
197 solve: progId withAuth: authKey andInfo: info andProg: prog
198 } else: {
199 print: info
200 }
201 }
202 0
128 } 203 }
129 } 204 }
130 } 205 }