view src/requests.tp @ 41:4750da71cae6

parsing myproblems.json, needs fix
author William Morgan <bill@mrgn.org>
date Sun, 11 Aug 2013 02:16:55 -0700
parents c42d3c8b1d75
children e9e01cd64993
line wrap: on
line source

#{

	strJoin <- :str arr {
		acc <- ""
		arr foreach: :i el {
			if: i = 0 {
				acc <- (string: el)
			} else: {
				acc <- acc . ", " . (string: el)
			}
		}
		acc
		//arr fold: "" with: :acc el {acc . el}
	}

	quote <- :str {
		"\"" . str . "\""
	}

	println <- :str {
		print: str . "\n"
	}

	fixArgs <- :args {
		args map: :el {
			"\"0x" . (hex: el) . "\""
		}
	}


	evalId <- :id args {
		args <- fixArgs: args
		#{
			string <- {
				idStr <- (quote: "id") . ":" . (quote: id)
				argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]"
				"{" . idStr . "," . argsStr . "}"
			}
			sendWithKey <- :key {
				print: "Sending: " . string . "\n"
				cli <- http client: "icfpc2013.cloudapp.net"
				resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json"
				evalResponse: resp
			}
		}
	}

	evalProgram <- :program args {
		args <- fixArgs: args
		#{
			string <- {
				progStr <- (quote: "program") . ":" . (quote: program)
				argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]"
				"{" . progStr . "," . argsStr . "}"
			}
			sendWithKey <- :key {
				print: "Sending: " . string . "\n"
				cli <- http client: "icfpc2013.cloudapp.net"
				resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json"
				evalResponse: resp
			}
		}
	}

	guess <- :id :prog {
		#{
			string <- {
				idStr <- "\"id\":\"" . id . "\""
				progStr <- "\"program\":\"" . prog . "\""
				"{" . idStr . "," . progStr . "}"
			}
			sendWithKey <- :key {
				print: "Sending: " . string . "\n"
				cli <- http client: "icfpc2013.cloudapp.net"
				resp <- cli post: string toPath: "/guess?auth=" . key withType: "application/json"
				guessResponse: resp
			}
		}
	}

	evalResponse <- :httpResp {
		_code <- httpResp statusCode
		bod <- httpResp body
		print: "Response code: " . (string: _code) . "\n"
		print: bod . "\n"
		decoded <- json decode: bod
		_status <- "error"
		if: _code = 200 {
			_status <- decoded get: "status" withDefault: "error"
			if: _status = "ok" {
				_outputs <- (decoded get: "outputs" withDefault: #[]) map: :num {
					(num from: 2) parseHex64
				}
				#{
					status <- { "ok" }
					outputs <- { _outputs }
					string <- {
						str <- "OK:"
						foreach: _outputs :idx val {
							str <- str . "\n" . (string: idx) . ": 0x" . (hex: val)
						}
						str
					}
					print <- {
						print: string . "\n"
					}
				}
			} else: {
				_message <- decoded get: "message" withDefault: ""
				#{
					status <- { "error" }
					message <- { _message }
					string <- {
						"Error: " . _message
					}
					print <- {
						print: string . "\n"
					}
				}
			}
		} else: {
			#{print <- print: "HTTP response gave error! code was: " . _code . "\n"}
			
		}
	}

	guessResponse <- :httpResp {
		_code <- httpResp statusCode
		bod <- httpResp body
		print: "Response code: " . (string: _code) . "\n"
		print: bod . "\n"
		decoded <- json decode: bod
		_status <- "error"
		if: _code = 200 {
			_status <- decoded get: "status" withDefault: "error"
			if: _status = "win" {
				#{
					status <- { "win" }
					string <- { "OK: win" }
					print <- {
						print: string . "\n"
					}
				}
			} else: { if: _status = "mismatch" {

				_values <- (decoded get: "values" withDefault: #[]) map: :num {
					(num from: 2) parseHex64
				}
				#{
					status <- { "mismatch" }
					values <- { _values }
					string <- {
						str <- "OK:"
						foreach: _values :idx val {
							str <- str . "\n" . (string: idx) . ": 0x" . (hex: val)
						}
						str
					}
					print <- {
						print: string . "\n"
					}
				}
			} else: {
				_message <- decoded get: "message" withDefault: ""
				#{
					status <- { "error" }
					message <- { _message }
					string <- {
						"Error: " . _message
					}
					print <- {
						print: string . "\n"
					}
				}
			}} // end if
		} else: {
			#{print <- print: "HTTP response gave error! code was: " . _code . "\n"}
		}
	}

	_problem <- :decoded {
		#{
			id <- decoded get: "id" withDefault: -1
			size <- decoded get: "size" withDefault: -1
			operators <- decoded get: "operators" withDefault: #[]
			//solved <- decoded get: "solved" withDefault: false // todo: B ool up ean  this son of a j
			timeLeft <- decoded get: "timeLeft" withDefault: 301
			// training problems have a challenge, real ones do not.:
			challenge <- decoded get: "challenge" withDefault: ""
			string <- { 
						str <- "problem '" . id . "' has size '" . size . "' and operators:"
						foreach: operators :idx val {
							str <- str . "\n  " . (string: idx) . ": " . val
						}
						str . "\ntimeLeft: " . timeLeft
			}
			print <- {
				print: string . "\n"
			}
		}
	}

	problem <- :bod {
		decoded <- json decode: bod
		_problem: decoded
	}

	problems <- :bod {
		decoded <- json decode: bod
		decoded map: :idx el{
			_problem: el
		}
	}

	main <- :args {
		print: "Program Start."
		testId0 <- "wdThP1AgVrS2rp7q6qt9mLqp" // TRAINING PROGRAM ID!  not real one. response in data folder.
		testId1 <- "QwhG7ZpaVsfXiLRvbJfIfxl8" // TRAINING PROGRAM ID!  not real one. response in data folder.
		someProblem <- "{\"id\":\"QwhG7ZpaVsfXiLRvbJfIfxl8\",\"size\":15,\"operators\":[\"and\",\"if0\",\"shl1\",\"shr16\",\"tfold\"]}"
		someProgram <- "(lambda (input) (shl1 input))"

		//print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n"
		//print: ((guess: "someId" "someProg") string) . "\n"
		//print: ((problem: someProblem) string) . "\n"

		file <- os open: "/home/wbm25/Desktop/icfp2013/data/myproblems.json" (os O_RDONLY)
		fstr <- (os read: file 400 * 1024)  // file was 276k before bonus problems...
		os close: file

		print: (((problems: fstr) get: 0) string )



		if: (args length) > 1 {
			key <- args get: 1

			//print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
			//print: ((evalId: testId0 #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key)
			//print: ((guess: testId0 someProgram) sendWithKey: key)
			println: "derp"
		}
	}
}