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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| # encoding: utf-8
require 'rmagick'
def cross_two_num(l, n)
co = "1379"
co.include? l.to_s and co.include? n.to_s or n.to_i == 10 - l.to_i
end
def num_possible(num, l)
((1..9).to_a - num - [l]).map do |n|
if not cross_two_num(l, n) or num.include?((l + n)/2)
n
end
end.compact
end
def check(pt, route)
if route.size > 0
last = route.last
if cross_two_num(last, pt) and not route.include?((last + pt)/2)
return false
end
end
return true
end
def dfs(pt, route=[], take=->(r) { yield(r) })
if check(pt, route)
route.push(pt)
if route.size >= 4
take.call(route)
end
(1..9).each do |npt|
if not route.include?(npt)
dfs(npt, route, take)
end
end
route.pop
end
end
def produce_rand(length)
num = [rand(1..9)]
length.times do
num << num_possible(num, num[-1]).sample
end
num.join
end
def num_check(num)
(0..num.size-2).each do |i|
l = num[i]
n = num[i+1]
if cross_two_num(l, n)
m = (l.to_i + n.to_i)/2
return false if not num[0..i].include?(m.to_s)
end
end
end
def draw_line(num, scnum)
def location(i)
base = i.to_i.divmod(3)
if base[1] == 0
x = base[1] + 2
y = base[0] - 1
else
x = base[1] - 1
y = base[0]
end
return [ 50 + 100 * x - 5, 50 + 100 * y ]
end
canvas = Magick::Image.new(300, 300)
text = Magick::Draw.new
text.font_family = 'TW-Sung'
text.pointsize = 14
text.gravity = Magick::CenterGravity
text.annotate(canvas, 0, 0, -5, -125, num)
text.annotate(canvas, 0, 0, -5, 140, scnum)
draw = Magick::Draw.new
spinner = Magick::ImageList.new
(1..9).each do |i|
x, y = location(i)
draw.circle(x, y, x + 6, y + 6)
draw.draw(canvas)
end
spinner << canvas.copy
draw.draw(spinner)
num.split(//).take(num.size-1).each_with_index do |n, i|
o = num[i + 1]
draw.fill('red')
ax, ay = location(n)
bx, by = location(o)
draw.line(ax, ay, bx, by)
if i == 0
draw.fill('blue')
draw.circle(ax, ay, ax + 6, ay + 6)
elsif i == num.length-2
draw.fill('yellow')
draw.circle(bx, by, bx + 6, by + 6)
5.times do
spinner << canvas.copy
draw.draw(spinner)
end
end
spinner << canvas.copy
draw.draw(spinner)
end
spinner.delay = 30
spinner.compression = Magick::LZWCompression
spinner.write('/tmp/unlockpattern.gif')
end
#===method 1
t = Time.now
all = []
(1..9).map do |i|
dfs(i) do |num|
all << num
end
end
p all.size
p Time.now - t
#===method 2
t = Time.now
a = (4..9).map{|e| "123456789".split(//).permutation(e).map &:join }.flatten
all = a.map do |num|
num if num_check(num)
end.compact
p all.size
p Time.now - t
#rand
sc = '零一二三四五六七八九'
p num = produce_rand(9)
puts scnum = num.split(//).map{|i| sc[i.to_i] }.join
draw_line(num, scnum)
|