gvaughn / elixir_kata Goto Github PK
View Code? Open in Web Editor NEWI'm learning elixir, y'all
I'm learning elixir, y'all
Hi!
I've found this Erlang solution of Euler's problem 54 "Poker hands", that seems to me very inspiring:
-module(problem54_poker).
-compile([export_all]).
-define(HC,0). % High Card: Highest value card.
-define(OP,1). % One Pair: Two cards of the same value.
-define(TP,2). % Two Pairs: Two different pairs.
-define(TK,3). % Three of a Kind: Three cards of the same value.
-define(ST,4). % Straight: All cards are consecutive values.
-define(FL,5). % Flush: All cards of the same suit.
-define(FH,6). % Full House: Three of a kind and a pair.
-define(FK,7). % Four of a Kind: Four cards of the same value.
-define(SF,8). % Straight Flush: All cards are consecutive values of same suit.
problem54_poker() ->
{ok,F} = file:open("C:\erl5.10.1\bin\poker.txt",[read]),
R = problem54_poker(F,file:read_line(F),0),
file:close(F),
R.
problem54_poker(_,eof,I) -> I;
problem54_poker(F,{ok,D},I) -> problem54_poker(F,file:read_line(F),I+readline(D)).
readline(S) ->
{S1,S2} = lists:split(15, S),
case (readline(S1,[],undefined) > readline(S2,[],undefined)) of
false -> 0;
true -> 1
end.
readline([],R,Colour) -> hand({lists:sort(R),Colour =/= false});
readline([C,K,|Q],R,K) -> readline(Q,[convert(C)|R],K);
readline([C,K,|Q],R,undefined) -> readline(Q,[convert(C)|R],K);
readline([C,,|Q],R,_) -> readline(Q,[convert(C)|R],false).
convert($2) -> 2;
convert($3) -> 3;
convert($4) -> 4;
convert($5) -> 5;
convert($6) -> 6;
convert($7) -> 7;
convert($8) -> 8;
convert($9) -> 9;
convert($T) -> 10;
convert($J) -> 11;
convert($Q) -> 12;
convert($K) -> 13;
convert($A) -> 14.
hand({[A,A,A,A,],}) -> {?FK,A};
hand({[,A,A,A,A],}) -> {?FK,A};
hand({[B,B,A,A,A],}) -> {?FH,A};
hand({[A,A,A,B,B],}) -> {?FH,A};
hand({[A,A,A,,],}) -> {?TK,A};
hand({[,A,A,A,],}) -> {?TK,A};
hand({[,,A,A,A],}) -> {?TK,A};
hand({[,B,B,A,A],}) -> {?TP,A};
hand({[B,B,,A,A],}) -> {?TP,A};
hand({[B,B,A,A,],}) -> {?TP,A};
hand({[A,A,,,],}) -> {?OP,A};
hand({[,A,A,,],}) -> {?OP,A};
hand({[,,A,A,],}) -> {?OP,A};
hand({[,,,A,A],}) -> {?OP,A};
hand({[2,3,4,5,14],true}) -> {?SF,5};
hand({[2,3,4,5,14],false}) -> {?ST,5};
hand({[A,,,,B],true}) when (B-A) == 4 -> {?SF,B};
hand({[A,,,,B],false}) when (B-A) == 4 -> {?ST,B};
hand({[,,,,A],true}) -> {?FL,A};
hand({[,,,,A],}) -> {?HC,A}.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.