-
Notifications
You must be signed in to change notification settings - Fork 0
/
towers
executable file
·76 lines (64 loc) · 1.46 KB
/
towers
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
73
74
75
76
#!/usr/bin/env ruby
require 'httparty'
require 'json'
require 'thor'
require_relative 'lib/ruby/towers'
require_relative 'lib/ruby/constrained_towers'
INTERVAL = 0.3
def hit_phat towers
url = 'http://localhost:5000/lights'
payload = {
data: towers.matrix
}
headers = {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
HTTParty.patch(url, body: payload.to_json, headers: headers)
end
def display towers
s = ''
towers.stacks.each do |stack|
s += stack.to_s
s += "\n"
end
s += '---'
puts s
end
class TowersSolver < Thor
desc "phat", "solve the towers against the pHAT's webserver"
option :constrained, type: :boolean
def phat
while true do
towers = Towers.new 5
if options[:constrained]
towers = ConstrainedTowers.new 5
end
until towers.solved do
hit_phat towers
towers.move
sleep INTERVAL
end
hit_phat towers
sleep INTERVAL * 3
end
end
desc "console", "solve the towers on the console"
option :discs, default: 3, type: :numeric
option :constrained, type: :boolean
def console
moves = 0
towers = Towers.new options[:discs]
if options[:constrained]
towers = ConstrainedTowers.new options[:discs]
end
until towers.solved do
display towers
towers.move
moves += 1
end
display towers
puts "%s moves to solve %s discs" % [moves, options[:discs]]
end
end
TowersSolver.start