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.

Recommended Posts

Offline real-time how to write F03 ruby and C implementation example
Offline real-time how to write F06 implementation example
How to write offline real time Implementation example by ruby and C99 of F04
How to write offline real-time Java implementation example of F01 problem
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
[Ruby] How to write blocks
[Java] How to output and write files!
[Easy] How to upgrade Ruby and bundler
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to write offline 15th reference question answer
How to write code that thinks object-oriented Ruby
Ruby How to convert between uppercase and lowercase
Javaer tries to summarize how to write properties in C #
[Ruby on Rails] How to write enum in Japanese
How to write Rails
[Java] Types of comments and how to write them
How to write dockerfile
How to write docker-compose
How to write Mockito
How to handle TSV files and CSV files in Ruby
How to write migrationfile
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to write and notes when migrating from VB to JAVA
How to deal with different versions of rbenv and Ruby
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
How to write good code
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
[Ruby] How to comment out
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
Ruby C extension and volatile
Ruby: How to use cookies
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
How to write Rails routing
Basics of Java development ~ How to write programs (variables and types) ~
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
How to write ruby if in one line Summary by beginner
How to get and add data from Firebase Firestore in Ruby
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
How to use StringBurrer and Arrays.toString.
Vuze plugin to write and enjoy
How to iterate infinitely in Ruby
Write Ruby methods using C (Part 1)
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
How to install ruby through rbenv
How to use Ruby on Rails
How to call classes and methods
Baseball ball count (how to write)
How to use equality and equality (how to use equals)
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
How to write a ternary operator