-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb093.jl
82 lines (58 loc) · 1.7 KB
/
pb093.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
module Problem093
using .Iterators
const reachable = Int[]
function last_reachable(A)
find_reachable([n // 1 for n in A])
sort!(reachable)
unique!(reachable)
I = first(i for i in countfrom() if i > lastindex(reachable) || i != reachable[i]) - 1
empty!(reachable)
return I
end
function find_reachable(S)
if isone(length(S))
n = S[1]
isone(denominator(n)) && n > 0 && push!(reachable, numerator(n))
else
newS = Rational{Int}[]
for (i, n1) in enumerate(S), (j, n2) in collect(enumerate(S))[i+1:end]
copy!(newS, S)
deleteat!(newS, (i, j))
find_reachable(push!(newS, n1 + n2))
pop!(newS)
find_reachable(push!(newS, n1 - n2))
pop!(newS)
find_reachable(push!(newS, n2 - n1))
pop!(newS)
find_reachable(push!(newS, n1 * n2))
pop!(newS)
n2 ≠ 0 && find_reachable(push!(newS, n1 // n2))
n2 ≠ 0 && pop!(newS)
n1 ≠ 0 && find_reachable(push!(newS, n2 // n1))
n1 ≠ 0 && pop!(newS)
end
end
end
"""
problem093()
Problem 093 of Project Euler.
https://projecteuler.net/problem=093
You can get rid of the need to use brackets
by defining two additional operators,
one for each of the non-commutative operators division and subtraction.
"""
function problem093()
ans = 0
maxlength = 0
for a in 1:6, b in a+1:7, c in b+1:8, d in c+1:9
I = last_reachable([a, b, c, d])
I > maxlength || continue
maxlength = I
ans = 1000a + 100b + 10c + d
end
return ans
end
export problem093
end # module Problem093
using .Problem093
export problem093