Created
November 29, 2019 09:26
-
-
Save shariq/ecb812cb69c4d607893cbe1ff324a273 to your computer and use it in GitHub Desktop.
fail to solve nato tide challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
the gif with the challenge image can be found here: | |
https://www.act.nato.int/images/stories/events/2018/tide-hackathon/EA-FN.gif | |
EA contains the message "Keep :) Hacking" | |
not sure what FN contains | |
more details on the challenge: | |
https://www.act.nato.int/ar16 | |
below is really bad code with combinatorial ideas for what to do... but it didn't work :( | |
''' | |
s = '''00320030003 | |
1xxxx1x22x3 | |
0x0330xx1x0 | |
1xxx00x3xx1 | |
0x0230x13x1 | |
1x2200x11x0 | |
13201202000''' | |
[['0', '0', '3', '2', '0', '0', '3', '0', '0', '0', '3'], ['1', 'x', 'x', 'x', 'x', '1', 'x', '2', '2', 'x', '3'], ['0', 'x', '0', '3', '3', '0', 'x', 'x', '1', 'x', '0'], ['1', 'x', 'x', 'x', '0', '0', 'x', '3', 'x', 'x', '1'], ['0', 'x', '0', '2', '3', '0', 'x', '1', '3', 'x', '1'], ['1', 'x', '2', '2', '0', '0', 'x', '1', '1', 'x', '0'], ['1', '3', '2', '0', '1', '2', '0', '2', '0', '0', '0']] | |
['13201202000\n1x2200x11x0\n0x0230x13x1\n1xxx00x3xx1\n0x0330xx1x0\n1xxxx1x22x3\n00320030003', '3301100\n0xxxxx0\n021x310\n02x3112\n3xxxxx0\n0100002\n0x30301\n2x3x220\n3x0x022\n0xxxxx3\n0101011', '30003002300\n3x22x1xxxx1\n0x1xx0330x0\n1xx3x00xxx1\n1x31x0320x0\n0x11x0022x1\n00020210231', '1101010\n3xxxxx0\n220x0x3\n022x3x2\n10303x0\n2000010\n0xxxxx3\n2113x20\n013x120\n0xxxxx0\n0011033', '0101011\n0xxxxx3\n3x0x022\n2x3x220\n0x30301\n0100002\n3xxxxx0\n02x3112\n021x310\n0xxxxx0\n3301100', '00020210231\n0x11x0022x1\n1x31x0320x0\n1xx3x00xxx1\n0x1xx0330x0\n3x22x1xxxx1\n30003002300', '00320030003\n1xxxx1x22x3\n0x0330xx1x0\n1xxx00x3xx1\n0x0230x13x1\n1x2200x11x0\n13201202000', '0011033\n0xxxxx0\n013x120\n2113x20\n0xxxxx3\n2000010\n10303x0\n022x3x2\n220x0x3\n3xxxxx0\n1101010'] | |
#03 is always 003? | |
from textwrap import wrap | |
import requests | |
import urllib2 | |
big_text = requests.get('https://norvig.com/big.txt').text | |
big_text = urllib2.urlopen('https://norvig.com/big.txt').read() | |
big_nums = [ord(c) for c in big_text] | |
freq_dict = dict(zip(range(256), [big_nums.count(c) / float(len(big_nums)) for c in range(256)])) | |
def find_offsets(nums, number_offsets): | |
offset_to_error = {} | |
for offset in range(256): | |
new_nums = [(num+offset)%256 for num in nums] | |
error = 0 | |
for c in range(256): | |
new_f = new_nums.count(c) / float(len(new_nums)) | |
right_f = freq_dict[c] | |
error += abs(new_f - right_f) ** 2 | |
offset_to_error[offset] = error | |
sorted_offsets = [y[0] for y in sorted(offset_to_error.items(), key=lambda x: x[1])] | |
return sorted_offsets[:number_offsets] | |
def ff(s, number_offsets=3): | |
s = s.replace(' ', '').replace('\n', '') | |
for word in [2, 3, 4]: | |
for x in ['', '0', '1', '2', '3']: | |
nums = [int(num, 4) % 256 for num in wrap(s.replace('x', x), word)] | |
for offset in find_offsets(nums, number_offsets): | |
output = ''.join([chr((num+offset)%256) for num in nums]) | |
print(word, x, offset, output) | |
transpose = lambda l: [list(i) for i in zip(*l)] | |
rotate1 = lambda l: transpose(l[::-1]) | |
rotate2 = lambda l: transpose(l)[::-1] | |
flip1 = lambda l: l[::-1] | |
flip2 = lambda l: [L[::-1] for L in l] | |
def transform(a): | |
ops = [random.choice([transpose, rotate1, rotate2, flip1, flip2]) for i in range(random.randint(0, 10))] | |
if ops: | |
new_a = ops[0](a) | |
for op in ops[1:]: | |
new_a = op(new_a) | |
return new_a | |
return a | |
# dumb way to find out there are only 8 ways to look at a 2d array without changing it too much | |
def all_combos(a, iter=10000): | |
res = {} | |
for i in xrange(iter): | |
new_a = '\n'.join([''.join(e) for e in transform(a)]) | |
res[new_a] = True | |
return res.keys() | |
def shuby(a): | |
wow = all_combos(a) | |
[ff(s) for s in wow] | |
# attempt to use the gif sequence vs just the final image... | |
s0 = ''' 0 3 0 | |
1xxxx1x 2x3 | |
x xx x | |
xxx 0x xx | |
0x02 x 3x | |
x 0x x0 | |
1 2 1 02 0 ''' | |
s1 = ''' 0 003 0 | |
1xxxx1x22x3 | |
x0 0xx x0 | |
1xxx00x3xx | |
0x02 0x 3x | |
x2 0x1 x0 | |
1 201 0200 ''' | |
s2 = '''00320030003 | |
1xxxx1x22x3 | |
0x0330xx1x0 | |
1xxx00x3xx1 | |
0x0230x13x1 | |
1x2200x11x0 | |
13201202000''' | |
def remove(initial, bad_stuff, include_x=False): | |
out = '' | |
for c1, c2 in zip(initial, bad_stuff): | |
if c1 == '\n': | |
out += c1 | |
continue | |
if c1 == 'x' and include_x: | |
out += c1 | |
continue | |
out += c1.replace(c2, ' ') | |
return out | |
def all_sequences(): | |
sequences = [s2.replace('\n', ''), (s0+s1+s2).replace('\n', '')] # original sequences | |
sequences.append((s0 + remove(s1, s0) + remove(s2, s1)).replace('x', '').replace(' ', '').replace('\n', '')) # get rid of all x | |
sequences.append((s0 + remove(s1, s0) + remove(s2, s1)).replace(' ', '').replace('\n', '')) # keep initial x | |
sequences.append((s0 + remove(s1, s0, True) + remove(s2, s1, True)).replace(' ', '').replace('\n', '')) # keep all x | |
return sequences | |
def all_arrays(): | |
sequences = all_sequences() | |
return [[[c for c in l] for l in wrap(seq, 11)] for seq in sequences] | |
def shuby2(): | |
for a in all_arrays(): | |
wow = all_combos(a) | |
for s in wow: | |
print(s) | |
ff(s, 3) | |
# in future, try using the weird thing on the left also? | |
# 0 other reasonable ideas at this point :( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment