forked from pcewing/programming-elixir-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.exs
73 lines (62 loc) · 1.56 KB
/
caesar.exs
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
defprotocol Encryptable do
def encrypt(string, shift)
def rot13(string)
end
defimpl Encryptable, for: BitString do
def encrypt(string, shift) do
string
|> String.to_char_list
|> Enum.map(&Char.shift(&1, shift))
|> List.to_string
end
def rot13(string) do
encrypt(string, 13)
end
end
defimpl Encryptable, for: List do
def encrypt(string, shift) do
string
|> Enum.map(&Char.shift(&1, shift))
end
def rot13(string) do
encrypt(string, 13)
end
end
defmodule Char do
def shift(char, shift) when char < 0x5b and char > 0x40 do
case char + shift do
new_char when new_char >= 0x5b -> new_char - 26
new_char when new_char <= 0x40 -> new_char + 26
new_char -> new_char
end
end
def shift(char, shift) when char < 0x7b and char > 0x60 do
case char + shift do
new_char when new_char >= 0x7b -> new_char - 26
new_char when new_char <= 0x60 -> new_char + 26
new_char -> new_char
end
end
def shift(char, _), do: char
end
defmodule Test do
def test do
words = "words.txt"
|> File.stream!
|> Stream.map(&String.strip(&1))
|> Enum.reduce(MapSet.new, fn(word, acc) -> MapSet.put(acc, word) end)
words
|> Enum.reduce([], fn(word, acc) -> update_results(word, words, acc) end)
|> IO.inspect
end
def rotation_exists?(word, words) do
rotated = Encryptable.rot13(word)
MapSet.member?(words, rotated)
end
def update_results(word, words, acc) do
case rotation_exists?(word, words) do
true -> [word | acc]
false -> acc
end
end
end