2021 challenge 1
[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 pbs = pb.split()
114 cat(pbs[i+1][0] for i, w in enumerate(pbs) if w == 'stop')
115 ```
116
117 ```python Collapsed="false"
118 wcat(segment(cat(w[0] for w in pbs)))
119 ```
120
121 ```python Collapsed="false"
122 wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))
123 ```
124
125 ```python Collapsed="false"
126 wcat(segment(cat(w[-1] for w in reversed(pbs))))
127 ```
128
129 ```python Collapsed="false"
130 max([len(w) for w in pbs])
131 ```
132
133 ```python Collapsed="false"
134 [len(s.strip().split()) for s in pb.split('stop')]
135 ```
136
137 ```python Collapsed="false"
138 tt = ''.maketrans('rdf', 'RDF')
139 pbcap = wcat(pb.split()).translate(tt)
140 spbcap = wcat(spb.split()).translate(tt)
141 pbcap
142 ```
143
144 ```python Collapsed="false"
145 pbcap2 = cat(('\n' + c) if c.isupper() else c for c in pbcap)
146 print(pbcap2)
147 ```
148
149 ```python Collapsed="false"
150 wcat(chunks(spb, 5))
151 ```
152
153 ```python Collapsed="false"
154 cat(w[-1] for w in chunks(spb, 5))
155 ```
156
157 ```python Collapsed="false"
158 locR = [i for i, c in enumerate(spb) if c == 'r']
159 locD = [i for i, c in enumerate(spb) if c == 'd']
160 locF = [i for i, c in enumerate(spb) if c == 'f']
161 len(locR), len(locD), len(locF)
162 ```
163
164 ```python Collapsed="false"
165 triples = []
166 for r in locR:
167 for d in locD:
168 for f in locF:
169 if d > r:
170 if d - r == f - d:
171 triples += [(r, d, f, d - r)]
172 triples
173 ```
174
175 ```python Collapsed="false"
176 t = cat(c if c.isupper() else '.' for c in pbscap)
177 col = 305 % 32
178 tcs = chunks(spb, 32)
179 stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]
180 print(lcat(stcs))
181 ```
182
183 ```python Collapsed="false"
184 results = []
185 for l in range(1, 40):
186 for i in range(l):
187 cs = chunks(spb, l, fillvalue=' ')
188 t = cat(c[i] for c in cs)
189 f = Ptrigrams(t)
190 results += [{'l': l, 'i': i, 't': t, 'f': f * l}]
191 len(results)
192 ```
193
194 ```python Collapsed="false"
195 list(sorted(results, key=lambda r: r['f']))[-4:]
196 ```
197
198 ```python Collapsed="false"
199 rdfresults = [r for r in results if 'rdf' in r['t']]
200 len(rdfresults)
201 ```
202
203 ```python Collapsed="false"
204 rdfresults
205 ```
206
207 ```python Collapsed="false"
208
209 ```