How to write offline real time Implementation example by ruby and C99 of F04

Problem: http://nabetani.sakura.ne.jp/hena/ordf04octsp/ Implementation links: http://qiita.com/Nabetani/items/8ead5622e192d9655cf5

As always, from the ruby implementation, which omits most of the test data:

def solve(src0)
  src = src0.to_i
  Array.new(8){ |s|
    if src[s]==1
      w = (1..8).find{ |x| src[(s+x)%8]==1 }
      w + (w==4 ? 1 : 2)
    end
  }.compact.sort.join
end

DATA.all?{ |line|
  num, src, expected = line.split(/\s+/ )
  actual = solve( src )
  okay = expected == actual
  puts( "%s %s:%s->%s ( %s )" % [(okay ? "ok" : "**NG**"), num, src, actual, expected] )
  okay
}.tap{ |x| puts( x ? "everything is okay" : "something wrong" ) }

__END__
0 165 3445
1 80  48
2 255 33333333

A straightforward implementation that you can spin around using the remainder. It's easier to handle bits with ruby.

The part where the number of vertices changes when the center becomes a straight line is ad hoc.

so.

The following is a port of this to C99 as it is:

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

int compare( const void * a, const void * b ){
  const char * ac = (const char * )a;
  const char * bc = (const char * )b;
  return *ac-*bc;
}

char * solve_impl( int src )
{
  char buffer[9]={0};
  char * pos=buffer;
  for( int s=0 ; s<8 ; ++s ){
    if ( src & (1<<s ) ){
      int w=1;
      for( ; w<=8 ; ++w ){
        if ( src & (1<<((w+s)%8)) ){
          break;
        }
      }
      *pos=w+( w==4 ? '1' : '2' );
      ++pos;
    }
  }
  qsort( buffer, pos-buffer, 1, compare );
  return strdup( buffer );
}

char * solve( char const * src )
{
  return solve_impl( atoi( src ) );
}

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

int main()
{
  /*0*/ test( "165", "3445" );    
  /*1*/ test( "80", "48" );    
  /*2*/ test( "255", "33333333" );
  return 0;
}

Sorting is troublesome. It's really annoying. Other than that, I think it's like this. I think that the area where the character string is created by adding '1' and '2' is like C language.

Recommended Posts

How to write offline real time Implementation example by ruby and C99 of F04
Offline real-time how to write F03 ruby and C implementation example
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to write offline real-time Java implementation example of F01 problem
Offline real-time how to write F06 implementation example
JDBC promises and examples of how to write
[Java] Types of comments and how to write them
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Ruby] How to write blocks
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to deal with different versions of rbenv and Ruby
Basics of Java development ~ How to write programs (variables and types) ~
How to write ruby if in one line Summary by beginner
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
Summary of how to write annotation arguments
Explanation of Ruby Time and Date objects
[Java] How to output and write files!
[Easy] How to upgrade Ruby and bundler
How to write and explain Dockerfile, docker-compose
[Android 9.0 Pie] Example of how to call strings.xml other than Activity and Fragment
Basics of Java development ~ How to write a program (flow and conditional branching) ~
How to display today's date and time by default: Remove seconds or less
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
Read and write line by line from buffer with TCP communication between C and Ruby
How to find the cause of the Ruby error
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to write offline 15th reference question answer
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
[Ruby] How to use gsub method and sub method
[Ruby] "Reference to object" and "Contents of variable"
How to write code that thinks object-oriented Ruby
Ruby How to convert between uppercase and lowercase
How to request by passing an array to the query with HTTP Client of Ruby
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
Javaer tries to summarize how to write properties in C #
[Ruby on Rails] How to write enum in Japanese
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
How to handle TSV files and CSV files in Ruby
Implementation example of simple LISP processing system (Ruby version)
How to boot by environment with Spring Boot of Maven
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
Handling of date and time in Ruby. Use Date and Time properly.
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
Implementation method of linking multiple images to one post and posting at the same time