Skip to content

Instantly share code, notes, and snippets.

@lastland lastland/clock.c
Created Mar 25, 2017

Embed
What would you like to do?
#include <stdio.h>
#include <string.h>
#define MAX_SQUARE 57521883
#define C1 4617
#define C2 73
#define C3 36936
#define C4 262657
#define C5 2134536
#define C6 16810048
#define C7 2363904
#define C8 19136512
#define C9 18911232
typedef long square;
#define PRINT(x)\
while (s##x--)\
{\
sprintf(tmp, #x " ");\
strcat(result, tmp);\
}
#define ITER_HEAD(x, y)\
for (s##x = 0, r##x = r##y; s##x < 4; s##x++)\
{\
now += s##x;\
if (now > mins)\
{\
now -= s##x;\
break;\
}
#define ITER_END(x)\
turn(&r##x, C##x);\
now -= s##x;\
}
void turn(square *des, square clock)
{
*des = (*des + clock) & MAX_SQUARE;
}
char match(square a)
{
return a == 0;
}
int main()
{
FILE *fin, *fout;
int s1, s2, s3, s4, s5, s6, s7, s8, s9;
square r0 = 0, r1, r2, r3, r4, r5, r6, r7, r8, r9;
int mins = 28, now = 0;
char result[55] = {0}, tmp[3], p_result[55] = {0};
fin = fopen("clocks.in", "r");
fout = fopen("clocks.out", "w");
for (s1 = 0; s1 < 3; s1++)
for (s2 = 0; s2 < 3; s2++)
{
fscanf(fin, "%d", &s3);
r0 += ((s3 % 12) / 3) << ((s1 * 3 + s2) * 3);
}
ITER_HEAD(1, 0)
ITER_HEAD(2, 1)
ITER_HEAD(3, 2)
ITER_HEAD(4, 3)
ITER_HEAD(5, 4)
ITER_HEAD(6, 5)
ITER_HEAD(7, 6)
ITER_HEAD(8, 7)
ITER_HEAD(9, 8)
if (match(r9))
{
PRINT(1)
PRINT(2)
PRINT(3)
PRINT(4)
PRINT(5)
PRINT(6)
PRINT(7)
PRINT(8)
PRINT(9)
if (now < mins)
{
mins = now;
strcpy(p_result, result);
}
else if (now == mins && strcmp(result, p_result) < 0)
strcpy(p_result, result);
}
ITER_END(9)
ITER_END(8)
ITER_END(7)
ITER_END(6)
ITER_END(5)
ITER_END(4)
ITER_END(3)
ITER_END(2)
ITER_END(1)
p_result[strlen(p_result) - 1] = '\0';
fprintf(fout, "%s\n", p_result);
fclose(fin);
fclose(fout);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.