r/dailyprogrammer • u/fvandepitte 0 0 • Jun 01 '16
[2016-06-01] Challenge #269 [Intermediate] Mirror encryption
Description
We are going to encrypt and decrypt with a mirror field.
It works like this:
We align letters to a mirror field:
ab
A \c
B\ d
CDEvery letter has now a mirror image
For example A has as mirror image D
A-\
|
DThe / and \ act as a mirror that will turn the line 90 degrees like you would if you had a laserpointer pointed to a mirror.
The full letter grid will look like this (without the seperators):
|a|b|c|d|e|f|g|h|i|j|k|l|m|
-----------------------------
A| | | | | | | | | | | | | |n
-----------------------------
B| | | | | | | | | | | | | |o
-----------------------------
C| | | | | | | | | | | | | |p
-----------------------------
D| | | | | | | | | | | | | |q
-----------------------------
E| | | | | | | | | | | | | |r
-----------------------------
F| | | | | | | | | | | | | |s
-----------------------------
G| | | | | | | | | | | | | |t
-----------------------------
H| | | | | | | | | | | | | |u
-----------------------------
I| | | | | | | | | | | | | |v
-----------------------------
J| | | | | | | | | | | | | |w
-----------------------------
K| | | | | | | | | | | | | |x
-----------------------------
L| | | | | | | | | | | | | |y
-----------------------------
M| | | | | | | | | | | | | |z
-----------------------------
|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|Formal Inputs & Outputs
Input description
You'll get a grid of 13 by 13 with mirrors and a word.
\\ /\
\
/
\ \
\
/ /
\ / \
\
\/
/
\
\/
/ /
TpnQSjdmZdpoohdOutput description
Return the encrypted word
DailyProgrammerBonus
Use the mirrors as a encryption key file and make you program encrypt in realtime (as you type)
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
Edit
Thanks to you all for pointing out the typo. Fixed it now.
Special thanks to /u/skeeto to provide us with an animated version http://i.imgur.com/uML0tJK.gif
19
u/SPIDERS_IN_PEEHOLE Jun 01 '16
I honestly don't get it. How does this work? Are there more examples of it in action? Step by step? I just don't understand.
9
u/skeeto -9 8 Jun 01 '16 edited Jun 01 '16
C, just tracing out the grid. If I was concerned about speed, I'd precompute a lookup table.
I animated it to help me debug: http://i.imgur.com/uML0tJK.gif (this code not included)
5
u/chachee Jun 01 '16
I think the test input will yield "DaolyProgrammer".
Test input should be "TpnQSjdmZdpoohd"
5
u/glenbolake 2 0 Jun 02 '16
Python 3, with turtle. Because why not.
Turtle in action
Final result
1
u/itsme86 Jun 02 '16
It might be cool if a mirror flips every time it's hit. That way you don't get D every time you type T, etc.
3
u/Godspiral 3 3 Jun 01 '16
an all in 1 "key file" (with J code)
3
u/Godspiral 3 3 Jun 02 '16
in J, input has i wrong
k is combined keymap
DaolyProgrammer
DailyProgrammer
3
u/Scroph 0 0 Jun 02 '16 edited Jun 02 '16
At first I started coding this in C++, but then my C instincts kicked in so I ended up writing it in C instead :
Edit : removed boolean-related lines of code and the string.h header.
3
u/The_Jare Jun 02 '16
Rust:
2
u/itsme86 Jun 01 '16
C#
2
u/ShaharNJIT 0 1 Jun 02 '16 edited Jun 02 '16
JavaScript, Fiddle: https://jsfiddle.net/am2h3o3a/1/ (bonus included)
2
u/blondepianist Jun 03 '16
Objective C:
2
u/bibko Jun 07 '16
Java with very simple GUI interface in swing (created with eclipse gui builder).
The whole source is here: https://github.com/schwarzwald/public/tree/master/mirror
You can specify in the configuration file the alphabet of the grid and also the key (size of the grid is also configurable)
The main part is:
2
u/Starcast Jul 08 '16 edited Jul 08 '16
Python 2.x
1
u/chunes 1 2 Jun 02 '16
I enjoyed this challenge.
Java:
1
u/a_Happy_Tiny_Bunny Jun 02 '16
Haskell
It does the bonus: just paste the normal input, including the output, and then type.
Not the tersest way to go around solving the problem, but a very straightforward one.
I believe all the FFI stuff wouldn't be necessary on Linux. In Windows, GHC seems unable to do nonbuffered IO with the simplest method. I also think the program may not run on Linux because of the
conio.himport.1
u/Daanvdk 1 0 Jun 02 '16
Java solution, converts the grid into a
Map<Character,Character>using some functions to recursively go through the field and then uses that map to encode.+/u/CompileBot Java
Input:
1
u/jnd-au 0 1 Jun 02 '16
Scala with bonus (unknown chars are passed through unaltered). Interactive output:
Challenge:
Bonus:
1
u/5900 Jun 02 '16
Ruby
1
u/InProx_Ichlife Jun 02 '16 edited Jun 02 '16
Python 3
A lot of ord() action. Kinda hard to follow, but I used it to basically locate where a letter lies on the 13x13 grid. Moved across the grid using the 'direct' variable, reflecting in respectively when a mirror is countered.
Without bonus.
1
u/DeLangzameSchildpad Jun 02 '16
Python 3
I decided to make a class for the encryption, so if you want to reuse the grid, you only have to parse it once, and then you can directly use the mapping.
1
u/FlammableMarshmallow Jun 02 '16
Python3.5
This code works, but I'm not sure why.
The logic for the mirrors is backwards as how it seems it should be, but it works... Could somebody enlighten me as to why?
1
u/Nevindy Jun 02 '16
Python
I have the input mirror and encrypted word saved in a text document.
1
u/vesche Jun 02 '16
Python 2.7, without using a lookup table
input:
code:
1
u/MRSantos Jun 02 '16 edited Jun 02 '16
Python 2.7.
1
u/FrankRuben27 0 1 Jun 02 '16
In C; quick enough for bonus even without pre-computing the decoder table:
1
u/fbWright Jun 03 '16
Python 3, animated
A bit rough around the edges, but I wrote it at 2:40 AM and it works. It has pwetty
os.system("clear"); print(stuff)animations, too!1
u/ih8uh8me Jun 03 '16
Java
input:
1
u/ddionnehb Jun 03 '16
Python 3.5 with bonus if run in terminal, IDLE has no way of doing a getch-like function so don't try running this in IDLE.
Also, the choice of slashes was unfortunate for the bonus part of this challenge since it is trying to escape things all over the place...
I left this with the test map by default and all you have to do is type things and see it encode in real-time. If you want to try and put in a custom mapping you need to uncomment the line near the end (i call it out in the comment) and make sure you are escaping this correctly.
1
u/Albaforia Jun 03 '16 edited Jun 03 '16
Used C++11, and also did it by creating classes instead of tackling it all in one file. The mirror layouts were inserted as a file. The grid is a 1D array, for optimization purposes (locality and allocation).
grid.h:
grid.cpp:
main.cpp:
1
u/[deleted] Jun 03 '16 edited Jun 11 '17
deleted 0.1400 What is ^^^this?
1
u/thorwing Jun 03 '16 edited Jun 03 '16
Java
I create an initial map, mapping every key "a-zA-Z" to it's opposite. I calculate while iterating over the slashes where every mapping should be redirected too. Had quite some fun figuring out a fast and simple algorithm for that.
output of map is the knowledge of each opposite:
So it's a simple maprequest afterwards = DailyProgrammer
I replaced '' and '/' with 'b' and 'f' because of their regex-like behaviour in strings.
1
u/LordJackass Jun 03 '16
C++, no bonus yet.
1
u/thorwing Jun 04 '16
Java
I did some reducements for fun...
1
u/voice-of-hermes Jun 05 '16
This implementation is O(n) in memory and O(nm) in time for n columns and m rows; it updates state for each character read from the grid rather than trying to ray trace after the grid is read, and holds state for all the (optical) connections from the top/left/right until it gets to the bottom. Pretty fun stuff.
Bonus not included (but would be pretty trivial to add).
1
u/plargato Jun 05 '16 edited Jun 05 '16
c#
1
u/gju_ Jun 05 '16
Go
Probably not a very efficient method. I'm basically tracing the "beam" through the grid. My index calculation function seems a bit goofy to me... I'm bad at index magic. :)
1
u/termifire Jun 05 '16 edited Jun 05 '16
Python, no bonus. First submission. I really liked the challenge, I appreciate all feedback.
On GitHub: code and tests.
1
u/weekendblues Jun 05 '16
My solution in Java
Stores pathways through mirror key in a hashmap for increased efficiency. Doesn't check for input errors.
Output:
1
u/Hannoii Jun 05 '16
C++ No bonus since I'm just using the console.
Output:
1
u/ultrasu Jun 06 '16
Racket:
output:
1
u/EtDecius Jun 07 '16
C++: Bonus not included. Reads input from file and creates mirror grid object.
MirrorEncryption.cpp:
MirrorGrid.hpp
MirrorGrid.cpp
Output:
1
u/sanfly Jun 08 '16
Java, no bonus
I'm quite new to Java/OOP so any constructive feedback appreciated. I feel like my code could definitely be more efficient
message: TpnQSjdmZdpoohd output: DailyProgrammer
message: ypiuQSLfQY output: Check it and see!
1
u/crintus Jun 10 '16
Python. Pretty new to python, so I'd like some feedback from you peepz on how I can improve :)
1
u/OmniSable Jun 14 '16
Python3.4 with numpy https://gist.github.com/Highstaker/09913478cdb10977272a115586d3ba2a#file-2016-06-01-challenge-269-intermediate-mirror-encryption-py
1
u/[deleted] Jun 19 '16
Poweshell
1
u/Gobbedyret 1 0 Jun 24 '16 edited Jun 24 '16
Python 3.5
This might have been the most difficult challenge I've done here.
I quickly realized that this is perfectly doable keeping only 14 unknown placeholders characters in memory (in addition to the already mapped pairs of characters) and be done in one pass. Writing the code to do it that way, however, was awful. I've also written a simple two-way dictionary class for this task.
BUT! My code is blisteringly fast (150 µs for challenge input, of which almost all the time is spent building the lookup table) and consumes practically no memory.
1
u/SlowerPhoton Jul 18 '16 edited Jul 18 '16
C++ solution