SASでWORDLE
TwitterのTLに流れてきたこのツイートで、おお、SASでWordleか、ということでやってみました。
#SASUsers If you were to build the #Wordle game in a SAS program, what technique would you use to check guesses?
— Chris Hemedinger (@cjdinger) 2022年3月15日
(UIに関して追記:https://japelin.hatenablog.com/entry/2022/03/16/154301)
WORDLEについてはググって試して見るのが一番かと思いますので、とりあえずコードを。
まずは、解答用のデータセットを作成しています。
と同時に、文字数を指定するkeycharと、resobsでどのobsを解答として採用するかを指定しています。
(実際にはresobsはランダムに割り当てればいいと思いますがとりあえず。)
%let keychars=5; %let resobs=4; data list; length wordle $&keychars.; input wordle; datalines; PIZZA QUICK QUAKE CRAZY CHICK WALTZ ; run;
続いてマクロです。
ちょっと力技ですが、文字+結果 という変数を作って、その結果からセルの色を変更し、結果だけを出力対象としています。
%Macro Wordle(input); options nomprint nomlogic nonotes; title ; %if %length(&input)^=&keychars %then %do; %put not &keychars charactors!; %end;%else %do; data res; set list(firstobs=&resobs obs=&resobs); %do i=1 %to &keychars; %let kw&i.=%substr(%upcase(&input),&i.,1); res&i=cats("&&kw&i",index(wordle,"&&kw&i")); keep res:; %end; run; ods html; proc report data=res nowd noheader style(column)={FONT_SIZE=12pt WIDTH=20 TEXTALIGN=center FONT_WEIGHT=bold COLOR=white}; column res: ; %do i=1 %to &keychars; compute res&i; if substr(res&i,2)="0" then call define (_COL_,'style','style={background=gray}'); else if substr(res&i,2)="&i" then call define (_COL_,'style','style={background=green}'); else call define (_COL_,'style','style={background=D1C513}'); res&i=substr(res&i,1,1); endcomp; %end; run; %end; %Mend Wordle;
で、例えば、
%Wordle(ABCDZ)
とすると、こんな結果。
%Wordle(CRZDA)
で、最後は
%Wordle(CRAZY)
こんな感じです。
宣言はしていないですが、ほぼarrayの書き方ですね。
普段ods全く使わないので、結果の区切り文字が消えればもっときれいだと思うんですが。
ただ、これだとインターフェースがないのがイマイチですかね。
%window使おうとしたら、マクロコンパイルの関係でうまく動かなかったので、他の方法で何かしらUI用意したいところです。