forked from stokarenko/it-duel-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_solver.rb
72 lines (56 loc) · 1.45 KB
/
net_solver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (c) 2015 Dmitriy Kiriyenko, Alexey Kudryashov, Sergey Tokarenko.
require 'httparty'
require 'benchmark'
module Tetris
class NetSolver
#TODO move to config
TOKEN = '0c07cedd-397b-491a-9173-370a1969dd97'
API_URL = 'http://tetro.andy128k.net/api/'
include HTTParty
base_uri API_URL
format :json
attr_reader :size, :timeout
def initialize(size, timeout)
@size, @timeout = size, timeout
end
def work
task = fetch
res = solve(task)
send(task, res) if res
end
def work!
while true
puts "=" * 42
puts
work rescue Net::ReadTimeout
end
end
private
def make_query(params)
params.merge(token: TOKEN)
end
def fetch
benchmark do
self.class.get("/puzzle", query: make_query(size: size)).parsed_response
end
end
def solve(task)
benchmark('Solved!') do
Solver.new(size: size, signature: task["signature"]).solve(timeout: timeout)
end
end
def send(task, result)
benchmark do
self.class.post("/puzzle", body: make_query(id: task["id"], solution: result.to_json)).parsed_response
end
end
def benchmark(message = nil)
res = nil
benchmark = Benchmark.measure {
res = yield
}
puts "[%.2fs] %s" % [benchmark.real, message || res]
res
end
end
end