# 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 |
This comment has been minimized.
This comment has been minimized.
I started with a basic working solution: defmodule Caesar do
import Stream
@a String.codepoints "abcdefghijklmnopqrstuvwxyz"
def encode(n, p) do
d = @a |> cycle |> drop(String.to_integer(n)) |> take(26) |> Enum.zip(@a)
Enum.map_join(String.codepoints(p), fn c -> d[c] || c end)
end
end
IO.puts apply(Caesar, :encode, IO.gets("--enter your code here--\n") |> String.split(","))
I went through some intermediate steps, but ultimately ended up at 168 characters (wrapped for readability here): import String;
IO.puts apply fn n,p->
Enum.map_join(
codepoints(p),
&(for c<-0..25,into: [],do:{<<97+rem(c+to_integer(n),26)>>,<<97+c>>})[&1]||&1)
end
,IO.gets("")|>split"," Further golfing steps:
Last minute change: |
This comment has been minimized.
This comment has been minimized.
Great, that's really helpful... Thanks Ben... |
This comment has been minimized.
This comment has been minimized.
Now I'm tweet sized by doing the calculation inline instead of calculating |
This comment has been minimized.
This comment has been minimized.
Oh man you are the one to beat now! |
This comment has been minimized.
This comment has been minimized.
115 characters |
This comment has been minimized.
This comment has been minimized.
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) |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
From http://elixirgolf.com/articles/the-elixir-caesar-cipher/