# Offline real-time how to write F03 ruby and C implementation example

Problem: http://nabetani.sakura.ne.jp/hena/ordf03triom/ Implementation links: http://qiita.com/Nabetani/items/becb4c4d309b4fe8ce0f

It's easy because you only have to normalize the translation.

First of all, ruby

``````class Array
def x; self[0]; end
def y; self[1]; end
end

def solve( src )
poss0 = src.chars.uniq.map{ |x| (x.ord - ?a.ord).divmod(5) }.sort
return "-" unless poss0.size==3
poss = poss0.map{ |p| [p.x-poss0[0].x, p.y-poss0[0].y] }.join(",")
case poss
when "0,0,0,1,0,2"; "B"
when "0,0,0,1,1,1"; "T"
when "0,0,1,0,1,1"; "L"
when "0,0,0,1,1,0"; "R"
when "0,0,1,0,2,0"; "I"
when "0,0,1,-1,1,0"; "J"
else "-"
end
end

if \$0==__FILE__
DATA.each do |line|
num, src, expected = line.split(/\s+/)
actual = solve( src )
ok = actual==expected
p [ ok ? "ok" : "**NG**", num, src, expected, actual ]
end
end

__END__
0 cba B
1 yam -
2 aaa -
``````

And C

#### `C99`

``````
//clang -std=c99 -Wall
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int xval( char c ){  return (c-'a') % 5;}
int yval( char c ){  return (c-'a') / 5;}
int min( int a, int b ){  return a<b ? a : b; }

char solve( char const * src )
{
int xmin=9;
int ymin=9;
for( int i=0 ; i<3 ; ++i ){
xmin = min(xmin, xval(src[i]));
ymin = min(ymin, yval(src[i]));
}
int bits=0;
for( int i=0 ; i<3 ; ++i ){
bits |= 1<<(xval(src[i])-xmin + (yval(src[i])-ymin)*5 );
}
switch(bits){
default:
return '-';
case 0x7:   return 'B';
case 0x23:  return 'R';
case 0x43 : return 'T';
case 0x61:  return 'L';
case 0x62:  return 'J';
case 0x421: return 'I';
}
}

void test( char const * src, char const * expected )
{
char actual = solve( src );
_Bool ok = actual == *expected;
printf( "%s : %s / %s / %c\n", (ok  ? "ok" : "**NG**"), src, expected, actual );
}

int main()
{
/*0*/ test( "cba", "B" );
/*1*/ test( "yam", "-" );
/*2*/ test( "aaa", "-" );
/*3*/ test( "def", "-" );
return 0;
}

``````

ruby is sorted and the rest is appropriate. Since sort is troublesome in C, the set is represented by bits.

Since I created a function called `min`, do I need NOMINMAX in visual studio?

Isn't it easy?

Next time is E13 (https://yhpg.doorkeeper.jp/events/58541). I will ask you again. Make it harder than this.