Skip to content

Instantly share code, notes, and snippets.

@gvaughn
Last active November 27, 2015 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvaughn/b295e69b4eb302a4fab5 to your computer and use it in GitHub Desktop.
Save gvaughn/b295e69b4eb302a4fab5 to your computer and use it in GitHub Desktop.
Caesar Cipher ElixirGolf
# for an offset range of -26 to + 26
# 114 characters
[n,p]=IO.gets("")|>String.split",";IO.puts for c<-to_char_list(p),do: c<97&&c||97+rem c-71-String.to_integer(n),26
# for any offset range
# 122 characters
[n,p]=IO.gets("")|>String.split",";IO.puts for c<-to_char_list(p),do: c<97&&c||97+rem c-71-rem(String.to_integer(n),26),26
# with @MPAherns use of a binary generator in the list comprehension
# 103 characters
[n,p]=IO.gets("")|>String.split",";IO.puts for<<c<-p>>,do: c<97&&c||97+rem c-71-String.to_integer(n),26
@gvaughn
Copy link
Author

gvaughn commented Nov 21, 2015

To be fair, the posted solution only works for offsets up to 26. If we want larger ones, then I can do it in 123 chars:

[n,p]=IO.gets("")|>String.split",";IO.puts for c<-to_char_list(p),do: c<97&&c||97+rem(c-71-rem(String.to_integer(n),26),26)

@gvaughn
Copy link
Author

gvaughn commented Nov 21, 2015

The posted solution in the gist is 122 characters and works for any offset. I still consider that my own work before looking at how Henrick blended our approaches. That would also let me do 114 characters for an offset of -26 to +26.

@emson
Copy link

emson commented Nov 23, 2015

Great work... the rules don't state anything about offsets over 26 chars so, it's perfectly valid. Thanks for writing the descriptions I'll be sure to link to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment