-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP68.jl
149 lines (128 loc) · 3.29 KB
/
P68.jl
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
140
141
142
143
144
145
146
147
148
149
using Combinatorics
N=16
gon=5
digits=1:10
# N=9
# gon=3
# digits=1:6
function main(N::Int,gon::Int,digits::Vector{Int})::Int128
sets=combinations(digits, 3)
legalsets=[]
for s in sets
if sum(s) == N
push!(legalsets, s)
end
end
println("number of 16-sum wings:", length(legalsets))
pentagonsets=[]
for p in combinations(legalsets, gon)
flatten = vcat(p...)
if all([i in flatten for i in digits])
legal=true
ninternal=0
nexternal=0
for i in digits
count=sum([i in wing for wing in p])
if count==1
nexternal+=1
elseif count==2
ninternal+=1
else
legal=false
break
end
end
if ninternal==nexternal==gon
else
legal=false
end
if legal
push!(pentagonsets,p)
end
end
end
println("number of possible pentagon sets:", length(pentagonsets))
function swap(i::Int, j::Int, vec::Vector)
vec[i],vec[j]=vec[j],vec[i]
end
function representation(magicgon::Vector{Vector{Int}})
n=length(magicgon)
# determine external and
internal=[]
external=[]
for i in digits
if sum([i in wing for wing in magicgon])==2
push!(internal,i)
else
push!(external,i)
end
end
# find start wing
rep=[]
w1=zeros(3)
p1=minimum(external)
for w in magicgon
if p1 in w
for i in 1:3
if w[i]==p1
swap(i,1,w)
end
end
w1=w
break
end
end
if w1[2]<w1[3]
swap(2,3,w1)
end
push!(rep, w1)
plast=w1[2]
pnext=w1[3]
count=0
while length(rep)<n+1
for w in magicgon
w=copy(w)
if !(plast in w) && pnext in w
for i in 1:3
if w[i]==pnext
swap(i,2,w)
break
end
end # exchange order of middle
if w[1] in internal
swap(1,3,w)
end
push!(rep, w)
plast=w[2]
pnext=w[3]
break
end
end
count+=1
if count>2n
return 0#collect(repeat(zeros(Int8,3),gon))
end
end
if rep[end]!=rep[1]
return 0#collect(repeat(zeros(Int8,3),gon))
end
magicgon=rep[1:gon]
str=vcat(magicgon...)|>join
if length(str)>16
return 0
else
return parse(Int128, str)
end
end
if pentagonsets!=[]
reps=map(representation, pentagonsets)
return maximum(reps)
else
return 0
end
end
results=[]
for N in 12:20
push!(results,main(N,5,collect(1:10)))
end
println(maximum(results))