Fixed wordbreaks in the plaintext
[cipher-challenge.git] / 2020 / 2020-challenge5.md
1 ---
2 jupyter:
3 jupytext:
4 formats: ipynb,md
5 text_representation:
6 extension: .md
7 format_name: markdown
8 format_version: '1.2'
9 jupytext_version: 1.3.4
10 kernelspec:
11 display_name: Python 3
12 language: python
13 name: python3
14 ---
15
16 ```python Collapsed="false"
17 from szyfrow.keyword_cipher import *
18 from szyfrow.column_transposition import *
19 from szyfrow.support.text_prettify import *
20 ```
21
22 ```python Collapsed="false"
23 challenge_number = 5
24 plaintext_a_filename = f'plaintext.{challenge_number}a.txt'
25 plaintext_b_filename = f'plaintext.{challenge_number}b.txt'
26 ciphertext_a_filename = f'ciphertext.{challenge_number}a.txt'
27 ciphertext_b_filename = f'ciphertext.{challenge_number}b.txt'
28 ```
29
30 ```python Collapsed="false"
31 ca = open(ciphertext_a_filename).read()
32 sca = sanitise(ca)
33 cb = open(ciphertext_b_filename).read()
34 scb = sanitise(cb)
35 ```
36
37 ```python Collapsed="false"
38 (word_a, wrap_a), score_a = keyword_break_mp(sca, fitness=Ptrigrams)
39 print(word_a, wrap_a, '\n')
40 pa = keyword_decipher(ca, word_a, wrap_a)
41 print(pa)
42 ```
43
44 ```python Collapsed="false"
45 keyword_decipher(ca, 'akela', wrap_a)
46 ```
47
48 ```python Collapsed="false"
49 word_a, score_a = simulated_annealing_break(sca, fitness=Ptrigrams,
50 plain_alphabet=string.ascii_lowercase, cipher_alphabet=keyword_cipher_alphabet_of('akkad', wrap_alphabet=KeywordWrapAlphabet.from_largest))
51 print(word_a, '\n')
52 pa = keyword_decipher(ca, word_a)
53 print(pa)
54 ```
55
56 ```python Collapsed="false"
57 pa = prettify(keyword_decipher(sca, 'akela', wrap_alphabet=KeywordWrapAlphabet.from_a))
58 print(pa)
59 ```
60
61 ```python Collapsed="false"
62 open(plaintext_a_filename, 'w').write(pa)
63 ```
64
65 ```python Collapsed="false"
66 (word_b, wrap_b), score_b = keyword_break_mp(scb, fitness=Ptrigrams)
67 print(word_b, wrap_b, '\n')
68 pb = keyword_decipher(cb, word_b, wrap_b)
69 print(pb)
70 ```
71
72 ```python Collapsed="false"
73 pb = prettify(keyword_decipher(scb, word_b, wrap_b))
74 print(pb)
75 ```
76
77 ```python Collapsed="false"
78 open(plaintext_b_filename, 'w').write(pb)
79 ```
80
81 ```python Collapsed="false"
82 pb = """dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and
83 found it most informative stop the beautiful cathedral almost justified our visit on its own and our
84 hosts shared with us some interesting drawings of the towers you mentioned stop they graciously
85 allowed me to copy the sketches and explained much about how the towers are built and why stop as
86 usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure
87 their safe delivery stop really i must congratulate our hosts in the local scout groups for the
88 marvellous way in which they have organised our travel though some of the scouts have been rather
89 more solicitous than we had expected asking rather a lot of questions about our plans stop generally
90 i hope they were satisfied with the answers that we gave but we feel that we are at risk of
91 overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our
92 time together there has been some debate among the group about where we should visit next stop there
93 are so many interesting sites to visit along the majestic thames estuary so we have decided to split
94 into two groups stop ralf will lead one party on a tour of the kent coast while i am very much
95 looking forward to exploring the essex marshes stop do let me know if you have a better idea but i
96 have been told that canewdons fifteenth century church affords an elevation with a wide view of
97 the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating
98 tower which i will be certain to sketch for you stop finally if you have any further requests for
99 specific information then perhaps you could leave a message for me at the post office there stop karl
100 message ends"""
101 ```
102
103 ```python Collapsed="false"
104 spb = sanitise(pb)
105 ```
106
107 ```python Collapsed="false"
108 pbs = pb.split()
109 [pbs[i+1] for i, w in enumerate(pbs) if w == 'stop']
110 ```
111
112 ```python Collapsed="false"
113 wcat(segment(cat(w[0] for w in pbs)))
114 ```
115
116 ```python Collapsed="false"
117 wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))
118 ```
119
120 ```python Collapsed="false"
121 wcat(segment(cat(w[-1] for w in reversed(pbs))))
122 ```
123
124 ```python Collapsed="false"
125 max([len(w) for w in pbs])
126 ```
127
128 ```python Collapsed="false"
129 [len(s.strip().split()) for s in pb.split('stop')]
130 ```
131
132 ```python Collapsed="false"
133 tt = ''.maketrans('rdf', 'RDF')
134 pbcap = wcat(pb.split()).translate(tt)
135 spbcap = wcat(spb.split()).translate(tt)
136 pbcap
137 ```
138
139 ```python Collapsed="false"
140 pbcap2 = cat(('\n' + c) if c.isupper() else c for c in pbcap)
141 print(pbcap2)
142 ```
143
144 ```python Collapsed="false"
145 wcat(chunks(spb, 5))
146 ```
147
148 ```python Collapsed="false"
149 cat(w[-1] for w in chunks(spb, 5))
150 ```
151
152 ```python Collapsed="false"
153 locR = [i for i, c in enumerate(spb) if c == 'r']
154 locD = [i for i, c in enumerate(spb) if c == 'd']
155 locF = [i for i, c in enumerate(spb) if c == 'f']
156 len(locR), len(locD), len(locF)
157 ```
158
159 ```python Collapsed="false"
160 triples = []
161 for r in locR:
162 for d in locD:
163 for f in locF:
164 if d > r:
165 if d - r == f - d:
166 triples += [(r, d, f, d - r)]
167 triples
168 ```
169
170 ```python Collapsed="false"
171 t = cat(c if c.isupper() else '.' for c in pbscap)
172 col = 305 % 32
173 tcs = chunks(spb, 32)
174 stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]
175 print(lcat(stcs))
176 ```
177
178 ```python Collapsed="false"
179 results = []
180 for l in range(1, 40):
181 for i in range(l):
182 cs = chunks(spb, l, fillvalue=' ')
183 t = cat(c[i] for c in cs)
184 f = Ptrigrams(t)
185 results += [{'l': l, 'i': i, 't': t, 'f': f * l}]
186 len(results)
187 ```
188
189 ```python Collapsed="false"
190 list(sorted(results, key=lambda r: r['f']))[-4:]
191 ```
192
193 ```python Collapsed="false"
194 rdfresults = [r for r in results if 'rdf' in r['t']]
195 len(rdfresults)
196 ```
197
198 ```python Collapsed="false"
199 rdfresults
200 ```
201
202 ```python Collapsed="false"
203
204 ```