Giter Club home page Giter Club logo

elixir_kata's People

Contributors

gvaughn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

cpjk epoch

elixir_kata's Issues

Erlang code of "Poker hands" problem

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}.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.