Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <time.h>
- #define filename "input_23_1.txt"
- #define CHARCNT ((uint16_t)('z'-'a'+1))
- #define MAPLEN (CHARCNT*CHARCNT)
- bool conn[MAPLEN][MAPLEN];
- char results[22222][150]; /* Must not be more than 22222 3-way networks
- and final result must not be longer than 50 nodes (adjust according to your data) */
- uint16_t getIndex(char * node)
- {
- return ((uint16_t)(node[0]-'a') * CHARCNT) + (uint16_t)(node[1]-'a');
- }
- int printIndex(uint16_t index, char * ptr)
- {
- if (ptr) return sprintf(ptr, "%c%c,", (char)((index/CHARCNT)+'a'), (char)((index%CHARCNT)+'a'));
- else return printf( "%c%c,", (char)((index/CHARCNT)+'a'), (char)((index%CHARCNT)+'a'));
- }
- int main()
- {
- FILE * f;
- int result1 = 0;
- int strcount = 0;
- int num;
- f = fopen(filename, "r");
- if (f != NULL) {
- char line[160] = {0};
- while (!feof(f)) {
- if ((NULL != fgets(line, sizeof(line), f)) && (strlen(line) > 1)) {
- uint16_t node1 = getIndex(&line[0]);
- uint16_t node2 = getIndex(&line[3]);
- conn[node1][node2] = true;
- conn[node2][node1] = true;
- }
- }
- fclose(f);
- }
- uint16_t ta = getIndex("ta");
- uint16_t tz = getIndex("tz");
- for (uint16_t x = 0; x < MAPLEN; x++) {
- for (uint16_t i = x+1; i < MAPLEN; i++) {
- if (conn[x][i]) {
- for (uint16_t t = i+1; t < MAPLEN; t++) {
- if (conn[t][i] && conn[t][x]) {
- int len=0;
- len+=printIndex(x, &results[strcount][len]);
- len+=printIndex(i, &results[strcount][len]);
- printIndex(t, &results[strcount][len]);
- strcount++;
- if (((t>=ta) && (t<=tz)) ||
- ((x>=ta) && (x<=tz)) ||
- ((i>=ta) && (i<=tz))) {
- result1++;
- /*
- printf("connection between: ");
- printIndex(x,0);
- printIndex(i,0);
- printIndex(t,0);
- printf("\n\r");
- fflush(stdout);
- */
- }
- }
- }
- }
- }
- }
- printf("Result 1 (3-node networks with t-node): %i\n\r", result1);
- printf("Total count of networks with 3+ nodes: %i\n\r", strcount);
- int lastAddedIndex = 0;
- int lookNext = 3;
- int lookCount;
- do {
- lookCount = lookNext;
- for (int i = 0; i < strcount; i++) {
- if (strlen(results[i]) == lookCount*3) {
- uint16_t lastIndex = getIndex(&results[i][(lookCount-1)*3]);
- for (uint16_t newIndex = lastIndex+1; newIndex < MAPLEN; newIndex++) {
- // check whether newIndex is connected to all existing items in this results[i] string
- bool match = true;
- for (uint16_t look = 0; look < lookCount; look++) {
- uint16_t testIndex = getIndex(&results[i][look*3]);
- if (!conn[newIndex][testIndex]) {
- match = false;
- break;
- }
- }
- if (match) {
- // add newIndex into array
- int len=strlen(results[i]);
- printIndex(newIndex, &results[i][len]);
- lookNext = lookCount+1;
- lastAddedIndex = i;
- break;
- }
- }
- }
- }
- } while(lookNext > lookCount);
- printf("Nodes in biggest network: %i\n\r", lookCount);
- printf("Result 2: %.*s\n\r", (int)(strlen(results[lastAddedIndex])-1), results[lastAddedIndex]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement