-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocols_2.exs
68 lines (57 loc) · 1.62 KB
/
protocols_2.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
defprotocol CaesarSalad do
def encrypt(string, shift)
def rot13(string)
end
defimpl CaesarSalad, for: List do
def encrypt(list, shift), do:
list |> Enum.map(&encrypt_char(&1, shift))
def rot13(list), do:
encrypt(list, 13)
def encrypt_char(char, shift) when char in 97..122, do:
97 + rem(char + shift - 97, 26)
def encrypt_char(char, shift) when char in 65..90, do:
65 + rem(char + shift - 65, 26)
def encrypt_char(32, _shift), do:
32
# Question mark for not matched char
def encrypt_char(_, _shift) do
63
end
end
defimpl CaesarSalad, for: BitString do
def encrypt(string, shift) do
String.to_char_list(string)
|> CaesarSalad.List.encrypt(shift)
|> List.to_string
end
def rot13(string), do:
encrypt(string, 13)
end
defmodule CaesarMacChecker do
def run do
mac_words = macintosh_dictionary()
inspect_words(mac_words, sorted_words(mac_words))
end
defp inspect_words(mac_words, words_by_size) do
Enum.each(mac_words, &match_word(&1, words_by_size))
end
defp match_word(word, words_by_size) do
encrypted = CaesarSalad.rot13(word)
case MapSet.member?(words_by_size[byte_size(word)], encrypted) do
true ->
IO.puts "#{word} is equal #{encrypted}"
_ ->
nil
end
end
defp macintosh_dictionary do
File.stream!("/usr/share/dict/words", [:read, :utf8], :line)
|> Enum.map(&String.rstrip(&1) |> String.downcase)
end
defp sorted_words(words) do
for {length, word_group} <- Enum.group_by(words, &byte_size/1), into: Map.new do
{length, Enum.into(word_group, MapSet.new)}
end
end
end
CaesarMacChecker.run