-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb059.jl
43 lines (32 loc) · 896 Bytes
/
pb059.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
module Problem059
function score(code, key)
decrypt = (xor(c, key[mod1(i, 3)]) for (i, c) in enumerate(code))
s = 0
for c in decrypt
Int('a') ≤ c ≤ Int('z') && (s += 2)
Int('A') ≤ c ≤ Int('Z') && (s += 1)
c == Int(' ') && (s += 10)
end
return s
end
"""
problem059()
Problem 059 of Project Euler.
https://projecteuler.net/problem=059
"""
function problem059(filename="txt/pb059.txt")
code = [parse(Int, n) for n in split(readline(filename), ",")]
maxkey = (0, 0, 0)
maxscore = 0
iter = Int('a'):Int('z')
for a in iter, b in iter, c in iter
key = (a, b, c)
s = score(code, key)
s > maxscore && (maxscore = s; maxkey = key)
end
return sum(xor(c, maxkey[mod1(i, 3)]) for (i, c) in enumerate(code))
end
export problem059
end # module Problem059
using .Problem059
export problem059