Tuesday, March 9, 2010

Longest Golf ever

More golfing. I'm always torn with the golf stuff. It seems like a bad idea to practice writing unreadable code but at the same time it's so much fun and such a good warmup to just attack some fifteen minute problem. Mostly it's fun to deploy to a remote server without access to the error messages and try to figure out how they're running the program and how they're reading the input.

A little of the joy is gone now that I know the stdins and outs of anarchy golf, but the weird little exercises are still there.


-module(keypad).
-export([m/0]).
m()->
io:format(parse(tokenize([],[],io:get_line([])))).
tokenize(Ts,T,[H|Tail])->
case Tail of
[$1]->[[H|T]|Ts];
_->
case H of
$1->tokenize([T|Ts],[],Tail);
_->tokenize(Ts,[H|T],Tail)
end
end.
parse(Ts)->
lists:reverse(
lists:map(
fun([C|_]=T)->
case C of
$0->32;
_->key(C)+length(T)-1
end
end,Ts)).
key(I)->
case I of
$8->$t;
$9->$w;
_->97+3*(I-50)
end.

No comments:

Post a Comment