Skip to content

Instantly share code, notes, and snippets.

@amoshyc
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save amoshyc/01ed19040dcbac6f99d3 to your computer and use it in GitHub Desktop.

Select an option

Save amoshyc/01ed19040dcbac6f99d3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int min(int a, int b) {
return ((a > b) ? b: a);
}
int calculate_steps(int data[][3], const char* combination) {
int steps = 0;
int i;
for (i=0; i<3; i++) {
if (combination[i] == 'B')
steps = steps + (data[(i+1)%3][0] + data[(i+2)%3][0]);
else if (combination[i] == 'G')
steps = steps + (data[(i+1)%3][1] + data[(i+2)%3][1]);
else /* combination[i] = 'C'*/
steps = steps + (data[(i+1)%3][2] + data[(i+2)%3][2]);
}
return steps;
}
int main() {
const char* combinations[6] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"};
int input[3][3]; /* [[B0, G0, C0], [B1, G1, C1], [B2, G2, C2]]*/
while (scanf("%d", &input[0][0]) != EOF) {
int i;
for(i=1; i<9; i++)
scanf("%d", &input[i/3][i%3]);
int min_steps = calculate_steps(input, combinations[0]);
int min_idx = 0;
for (i=1; i<6; i++) {
int steps = calculate_steps(input, combinations[i]);
if (steps < min_steps) {
min_steps = steps;
min_idx = i;
}
}
printf("%s %d\n", combinations[min_idx], min_steps);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment