/* Shortest code to encrypt string in Picat. http://codegolf.stackexchange.com/questions/20021/shortest-code-to-encrypt-a-string """ Using a language of your choice write a program that takes a string as an input, and returns the string encrypted. Algorithm: You should assume that each character is always encoded in a byte and is ASCII-based. The extended ASCII codes are mapped to whatever is the default code page in use by the system. You should encrypt the code in the following way: code of first character +1, code of second character +2, code of third character +3 ..... Extended ASCII codes are based on this table: http://www.asciitable.com/ Shortest code wins. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> S = "My secret password is 123456!", E = encrypt1(S), println(encrypt1=E), println(decrypt1=decrypt1(E)), nl. go => true. % 42 chars encrypt1(S) = % 1 2 3 4 42 chars % 123456789012345678901234567890123456789012 [chr(ord(C)+I):{C,I}in zip(S,1..S.length)]. % I:=0,[chr(ord(C)+I):C in S,I:=I+1]=E. % Error: assignment_in_condition::=/2 ["works" in Picat shell] % decrypt: 42 chars decrypt1(S) = [chr(ord(C)-I):{C,I}in zip(S,1..S.length)].