Last active
February 2, 2018 10:13
-
-
Save shiracamus/c47f65397c4317d182647d5817838a23 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く F09 のC言語実装例 https://qiita.com/Nabetani/items/61e13fa5cf0abe5979be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
typedef char Digit; | |
typedef struct { | |
Digit digits[3]; | |
} Row; | |
typedef union { | |
Digit digits[9]; | |
Row rows[3]; | |
} Numbers; | |
typedef union { | |
char string[12]; | |
struct { | |
Row row; | |
char delimiter; | |
} rows[3]; | |
} Answer; | |
void rotate(Numbers *numbers, int times) { | |
while (--times >= 0) { | |
Numbers old = *numbers; | |
for (int i = 0; i < 9; i++) | |
numbers->digits[i] = old.digits["741852963"[i] - '1']; | |
} | |
} | |
void process(Numbers *numbers, int position) { | |
rotate(numbers, position / 3); | |
Row *row = &numbers->rows[position % 3]; | |
Row rotated = { row->digits[1], row->digits[2], row->digits[0] }; | |
*row = rotated; | |
rotate(numbers, 4 - position / 3); | |
} | |
Answer solve(const char *positions) { | |
Numbers numbers = { .digits="123456789" }; | |
for ( ; *positions; positions++) | |
process(&numbers, *positions - 'a'); | |
Answer answer = { .rows={ { numbers.rows[0], '/' }, | |
{ numbers.rows[1], '/' }, | |
{ numbers.rows[2], '\0' } } }; | |
return answer; | |
} | |
void test(const char *positions, const char *expected) { | |
Answer answer = solve(positions); | |
printf("%s %s %s %s\n", strcmp(answer.string, expected) ? "NG" : "OK", | |
positions, expected, answer.string); | |
} | |
int main(void) | |
{ | |
/*0*/ test( "aegj", "286/435/971" ); | |
/*1*/ test( "a", "231/456/789" ); | |
/*2*/ test( "e", "183/426/759" ); | |
/*3*/ test( "g", "123/456/978" ); | |
/*4*/ test( "j", "126/459/783" ); | |
/*5*/ test( "bb", "123/645/789" ); | |
/*6*/ test( "jjj", "123/456/789" ); | |
/*7*/ test( "bd", "723/164/589" ); | |
/*8*/ test( "ah", "231/645/789" ); | |
/*9*/ test( "bj", "124/569/783" ); | |
/*10*/ test( "db", "723/561/489" ); | |
/*11*/ test( "dh", "723/615/489" ); | |
/*12*/ test( "dl", "123/456/789" ); | |
/*13*/ test( "hc", "123/645/897" ); | |
/*14*/ test( "gf", "128/453/976" ); | |
/*15*/ test( "hl", "623/745/189" ); | |
/*16*/ test( "ja", "261/459/783" ); | |
/*17*/ test( "ld", "123/456/789" ); | |
/*18*/ test( "ki", "315/486/729" ); | |
/*19*/ test( "lfa", "294/753/186" ); | |
/*20*/ test( "kga", "531/486/972" ); | |
/*21*/ test( "dbi", "372/561/489" ); | |
/*22*/ test( "che", "193/625/847" ); | |
/*23*/ test( "iea", "823/416/759" ); | |
/*24*/ test( "gbl", "523/964/178" ); | |
/*25*/ test( "egj", "186/425/973" ); | |
/*26*/ test( "jcf", "127/456/839" ); | |
/*27*/ test( "djh", "726/915/483" ); | |
/*28*/ test( "hld", "123/645/789" ); | |
/*29*/ test( "leeh", "453/678/129" ); | |
/*30*/ test( "heja", "851/629/743" ); | |
/*31*/ test( "cakh", "251/649/837" ); | |
/*32*/ test( "bhjik", "652/489/713" ); | |
/*33*/ test( "eabji", "483/269/751" ); | |
/*34*/ test( "cdbch", "823/156/974" ); | |
/*35*/ test( "ckgajc", "536/492/817" ); | |
/*36*/ test( "ggchha", "231/564/978" ); | |
/*37*/ test( "gfbkeg", "128/534/697" ); | |
/*38*/ test( "agfbcbf", "239/148/765" ); | |
/*39*/ test( "ekahijf", "123/645/789" ); | |
/*40*/ test( "hajdjbe", "789/432/615" ); | |
/*41*/ test( "elgililj", "976/325/814" ); | |
/*42*/ test( "chffefif", "317/629/845" ); | |
/*43*/ test( "ilbbihak", "462/587/319" ); | |
/*44*/ test( "abcdefghijkl", "123/456/789" ); | |
/*45*/ test( "hkijbglfaced", "768/125/493" ); | |
/*46*/ test( "dfkbjiechlga", "256/387/419" ); | |
/*47*/ test( "hgfkbidlajce", "186/745/239" ); | |
/*48*/ test( "baciefjhgkdl", "153/482/796" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment