オフラインリアルタイムどう書く E27の参考問題「灯りと鏡」の実装例を、R言語 で。
問題 : http://nabetani.sakura.ne.jp/hena/orde27ligmir/
実装リンク集 : https://qiita.com/Nabetani/items/0b2f459ec128c89948f4
オフラインリアルタイムどう書くの現場で Rを使った人はまだいなかったと思う。
でももちろん、 Rでの参戦も歓迎。
で。私の実装
R言語
SIZE<-5
impl<-function(src){
you<-regexpr( "Y", src )[1]-1
y <- you %/% (SIZE+1)
x <- you %% (SIZE+1)
r<-matrix(c(x,y),1,2)
pos <- c(x,y)
dir <- c(0,-1)
repeat{
if ( pos[1]<0 || SIZE<=pos[1] || pos[2]<0 || SIZE<=pos[2] ){
break
}
charpos <- pos[1]+pos[2]*(SIZE+1) + 1
cell <- substr( src, charpos, charpos )
if ( cell=="x" ){
break
}
if ( cell=="1" ){
dir <- -rev(dir)
} else if ( cell=="0" ){
dir <- rev(dir)
}
r<-rbind(r, pos)
pos <- pos+dir
if ( all(pos==c(x,y)) && all(dir==c(0,-1)) ){
break
}
}
return( r )
}
solve<-function(src){
visited<-impl(src)
r<-c()
for( row in 1:nrow(visited) ){
xy<-visited[row,]
r<-append( r, rawToChar( as.raw( xy[1]+xy[2]*SIZE+97 ) ) )
}
result <- paste(unique(sort(r)), collapse="")
return(result)
}
test<-function(src, expected ){
actual<-solve(src)
print( paste( ifelse( actual==expected, "ok", "**NG**" ), src, actual, expected, sep=" ") )
}
test( "x...x/.1.0./..0../.Y.../0..x.", "ghilnqs" )
test( "..Y../...../...../...../.....", "c" )
test( "..x../..Y../...../...../.....", "h" )
test( "..Y.x/..1x0/11.../....0/1..1.", "c" )
test( "....1/....Y/...../...../.....", "ej" )
test( ".10../x.1../x.1x./.Y.1./...0.", "bcghlq" )
test( "0.x10/00..x/x0x.0/....0/...Y1", "deinsx" )
test( "1.01./01Y.1/..1.1/..10./0.0..", "abcfgh" )
test( "x..../x1x../0...0/....Y/.1..0", "klmnot" )
test( "...../..10./.1Y1./.01../.....", "hilmnqr" )
test( "...../..10./x.11./...../..Y..", "hilmnrw" )
test( "...../x.10x/...../x.Y1x/.....", "himnqrs" )
test( "..010/...Y1/..0../0.x../.....", "defghij" )
test( "1.0../...../.0x../Y.1x./..1..", "abcfhkp" )
test( "...../101../0.0../..Y../.....", "fgklmqrv" )
test( "1.0../00.../.x..0/0.Y1./...10", "abcfghmr" )
test( "x101./1..../.Y.x./..01./.00.1", "bcghlmrs" )
test( "x11../x.x../.0.01/..x../...Y.", "bcglmnsx" )
test( "..1.0/x0.x./0.0../x...Y/.10.1", "cdehjmnot" )
test( "..x.0/.0.../1..0x/1..1./Y.00.", "klmnpqrsu" )
test( "0.1.0/.0.xY/0...0/01..1/x00.x", "cdehjmrwx" )
test( "...0./.0.0./..101/...10/..01Y", "mnpqrstwxy" )
test( "10..0/.Y.0./0..1./....x/000..", "abfghiklmn" )
test( "10..1/...../.1010/110.1/x..Yx", "lmnopqrstx" )
test( "110../....1/x1..x/0.0.0/....Y", "bcghlmrsty" )
test( "x.101/1..../..001/010Yx/..1.1", "cdehijmnos" )
test( "x.111/x10../...0./00.1x/x.Y.1", "ghklmnqrsw" )
test( "11.../....0/11..1/1.1../.Y..1", "fghijlmnoqv" )
test( "...x1/.1.0./11.1./.01../Y..x.", "cghiklmnpqru" )
test( ".0.../110x./11..0/01.x./..Y.x", "ghklmnopqrtw" )
test( ".01.0/.110x/0...0/.01Y./x.1x.", "cdeghilmnqrs" )
test( ".1100/..1.0/1.11Y/0..1./.0..0", "hijklmnopqrs" )
test( "1..00/..11./.100./1..Y1/.....", "abcdfhikmnps" )
test( "1.0../.11x0/.00.x/Y.10./.10x0", "abcfghklmpqr" )
test( "11110/11.../.x.../.0111/0.Y0.", "deijnorstwxy" )
test( "...1./.1.0x/10..0/0Y.11/.0.x0", "ghiklmnopqrst" )
test( "...10/x111./0x.11/.0.../0.0Y.", "dehijmnorswxy" )
test( ".1x../.x1.0/0x.x./x11.1/x0Y.1", "hijmoqrstvwxy" )
test( "x.x../x110./1.1.0/0.Y.1/0.00x", "hiklmnopqrstx" )
test( "...0./11.00/10..x/..0.1/Y0.10", "ghiklmnpqsuvwx" )
test( ".110./....0/x..../.0001/11.Y.", "cdfghijmnorstx" )
test( "1.00./....1/.1.../0...0/0..1Y", "abcfhkmpqrstwy" )
test( ".1.01/..x../..100/..Y../...01", "bcdgilmnoqrstvxy" )
test( "1...0/Y..../...../...../0...1", "abcdefjkoptuvwxy" )
test( "x1..0/1..0./.Yx../0...1/.0.1.", "bcdefghijklnopqrstvwx" )
test( "1...0/.1.0./..1../..01./Y0..1", "abcdefghijklmnopqrstuvwxy" )
仕事で稀にR言語使うけど、まだまだ初心者。調べながらじゃないと全然書けない。
この実装は、golang 版の移植。
visited
が無駄に matrix
になっている。
sort
と unique
があってよかった。まああるに決まっているか。
if ( all(pos==c(x,y)) && all(dir==c(0,-1)) ){略}
の部分でall
が必要であることに気づかず、往生した。
あと、src
から "Y"
を探す regexpr
。返戻値が 1 オリジンであることに最初気づかず、こちらも往生した。なんで 1 オリジンかなぁ。
整数除算演算子が %/%
と %%
なのが面白い。