-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb098.jl
65 lines (48 loc) · 1.29 KB
/
pb098.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
module Problem098
using ..ProjectEulerLibrary
"""
problem098()
Problem 098 of Project Euler.
https://projecteuler.net/problem=098
"""
function problem098(filename="txt/pb098.txt")
anagrams = Dict{String,Vector{String}}()
for word in sort(split(replace(readline(filename), "\"" => ""), ","); by=length)
key = join(sort(collect(word)))
if key in keys(anagrams)
push!(anagrams[key], word)
else
anagrams[key] = [word]
end
end
ans = 0
for words in values(anagrams)
for (i, w1) in enumerate(words), w2 in words[i+1:end]
ans = max(ans, square_anagram(w1, w2))
end
end
return ans
end
function square_anagram(w1::AbstractString, w2::AbstractString)
l = length(w1)
ans = -1
for n = isqrt(10^(l - 1) - 1)+1:isqrt(10^l - 1)
x = string(n^2)
for (d, c) in zip(string(n^2), w1)
x = replace(x, d => c)
end
x ≠ w1 && continue
y = w2
for (d, c) in zip(string(n^2), w1)
y = replace(y, c => d)
end
y[1] == '0' && continue
m = parse(Int, y)
issquare(m) && (ans = max(ans, m, n^2))
end
return ans
end
export problem098
end # module Problem098
using .Problem098
export problem098