added local to remote repo
This commit is contained in:
commit
dcf31c72d2
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
project(hexagon)
|
||||
#set(SOURCE_FILES src/main.c)
|
||||
add_executable(mklist src/list.c)
|
||||
add_executable(findsolution src/solution.c)
|
4
CMakeLists.txt~
Normal file
4
CMakeLists.txt~
Normal file
@ -0,0 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
project(hexagon)
|
||||
set(SOURCE_FILES src/main.c)
|
||||
add_executable(hexagon ${SOURCE_FILES})
|
1
src/.#list.c
Symbolic link
1
src/.#list.c
Symbolic link
@ -0,0 +1 @@
|
||||
huck@huck.13424:1688088982
|
33
src/list.c
Normal file
33
src/list.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#define ASIZE 6
|
||||
|
||||
int array[ASIZE] = {1,2,3,4,5,6};//,7,8,9,10,11,12,13,14,15,16,17,18,19};
|
||||
|
||||
void permute(int l){
|
||||
int tmp;
|
||||
if(l==ASIZE-1){
|
||||
for(int i=0; i<ASIZE; ++i){
|
||||
printf("%d_",array[i]);
|
||||
} putchar('\n');
|
||||
}
|
||||
for(int i=l; i<ASIZE; ++i){
|
||||
tmp = array[i];
|
||||
array[i] = array[l];
|
||||
array[l] = tmp;
|
||||
permute(l+1);
|
||||
tmp = array[i];
|
||||
array[i] = array[l];
|
||||
array[l] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
clock_t t;
|
||||
t=clock();
|
||||
|
||||
permute(0);
|
||||
|
||||
printf("completed in %f ms",(double)(clock()-t)/(CLOCKS_PER_SEC/1000));
|
||||
}
|
89
src/list.c~
Normal file
89
src/list.c~
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
int array [1..19]
|
||||
loop:
|
||||
- shift array by one thing until it is every possible version
|
||||
- each time check if it is the solution
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HEX_MAX 18
|
||||
#define HEX_GOAL 38
|
||||
|
||||
int* createHexagon(){
|
||||
int* hexagon = malloc(sizeof(int)*HEX_MAX);
|
||||
if(hexagon == NULL){
|
||||
printf("buy more ram lol\n");
|
||||
exit(1);
|
||||
}
|
||||
for(int i=1; i<=HEX_MAX; ++i){
|
||||
hexagon[i-1] = i;
|
||||
}
|
||||
return hexagon;
|
||||
}
|
||||
|
||||
int isSolution(int* h){
|
||||
if(HEX_MAX!=18||HEX_GOAL!=38)
|
||||
return 0;
|
||||
if(h[0]+h[1]+h[2]==HEX_GOAL
|
||||
&& h[0]+h[3]+h[7]==HEX_GOAL //3
|
||||
&& h[2]+h[6]+h[11]==HEX_GOAL
|
||||
&& h[7]+h[12]+h[16]==HEX_GOAL
|
||||
&& h[11]+h[15]+h[18]==HEX_GOAL
|
||||
&& h[16]+h[17]+h[18]==HEX_GOAL
|
||||
&& h[3]+h[4]+h[5]+h[6]==HEX_GOAL //4
|
||||
&& h[12]+h[13]+h[14]+h[15]==HEX_GOAL
|
||||
&& h[3]+h[8]+h[15]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[5]+h[10]+h[15]==HEX_GOAL
|
||||
&& h[6]+h[10]+h[14]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[4]+h[8]+h[12]==HEX_GOAL
|
||||
&& h[7]+h[8]+h[9]+h[10]+h[11]==HEX_GOAL //5
|
||||
&& h[0]+h[4]+h[9]+h[14]+h[18]==HEX_GOAL
|
||||
&& h[2]+h[5]+h[9]+h[13]+h[16]==HEX_GOAL
|
||||
)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printHexagon(int* h){
|
||||
printf(
|
||||
" == HEXAGON ==\n"
|
||||
" %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
"%d, %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d\n\n",
|
||||
h[0],h[1],h[2],h[3],h[4],h[5],h[6],h[7],h[8],h[9],h[10],h[11],h[12],h[13],h[14],h[15],h[16],h[17],h[18]);
|
||||
}
|
||||
|
||||
void solveHexagon(int* h, int l, int* count){
|
||||
int tmp;
|
||||
if(l==HEX_MAX-1){
|
||||
printf("hexagon #%d\n",*count);
|
||||
++*count;
|
||||
if(isSolution(h)){
|
||||
printf("found solution!\n");
|
||||
printHexagon(h);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(int i=l; i<HEX_MAX; ++i){
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
solveHexagon(h, l+1, count);
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int* my_hexagon = createHexagon();
|
||||
int count = 1;
|
||||
solveHexagon(my_hexagon,0,&count);
|
||||
free(my_hexagon);
|
||||
|
||||
return 0;
|
||||
}
|
84
src/main.c.bak
Normal file
84
src/main.c.bak
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
int array [1..19]
|
||||
loop:
|
||||
- shift array by one thing until it is every possible version
|
||||
- each time check if it is the solution
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HEX_MAX 5
|
||||
|
||||
int* createHexagon(){
|
||||
int* hexagon = malloc(sizeof(int)*(HEX_MAX+1));
|
||||
if(hexagon == NULL){
|
||||
printf("buy more ram lol\n");
|
||||
exit(1);
|
||||
}
|
||||
for(int i=1; i<=HEX_MAX+1; ++i){
|
||||
hexagon[i-1] = i;
|
||||
}
|
||||
return hexagon;
|
||||
}
|
||||
|
||||
int isSolution(int* h){
|
||||
return 1;
|
||||
}
|
||||
|
||||
void swapPlaces(int* h, int a, int b){
|
||||
int bb = h[b];
|
||||
h[b] = h[a];
|
||||
h[a] = bb;
|
||||
}
|
||||
|
||||
void printHexagon(int* h){
|
||||
printf("\n== HEXAGON ==\n");
|
||||
for(int i=0; i<=HEX_MAX; ++i){
|
||||
printf("%d_",h[i]);
|
||||
} putchar('\n');
|
||||
}
|
||||
|
||||
void solveHexagon(int* h, int n){
|
||||
if(isSolution(h)){
|
||||
printf("found solution");
|
||||
printHexagon(h);
|
||||
//return;
|
||||
}
|
||||
|
||||
int i,j,tmp;
|
||||
if(n == 1){
|
||||
printf("sorting finished\n");
|
||||
return;
|
||||
}
|
||||
for(i=0; i<n; ++i){
|
||||
solveHexagon(h, n-1);
|
||||
j = (n % 2 == 1) ? n-1 : 0;
|
||||
//swapPlaces(h, n-1, j);
|
||||
tmp = h[n-1];
|
||||
h[n-1] = h[j];
|
||||
h[j] = tmp;
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
ABC
|
||||
|
||||
CBA
|
||||
CAB
|
||||
|
||||
BAC
|
||||
BCA
|
||||
|
||||
ACB
|
||||
ABC
|
||||
|
||||
*/
|
||||
|
||||
int main(){
|
||||
int* my_hexagon = createHexagon();
|
||||
solveHexagon(my_hexagon,HEX_MAX+1);
|
||||
free(my_hexagon);
|
||||
|
||||
return 0;
|
||||
}
|
84
src/solution.c
Normal file
84
src/solution.c
Normal file
@ -0,0 +1,84 @@
|
||||
// solution.c
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HEX_MAX 18
|
||||
#define HEX_GOAL 38
|
||||
|
||||
int* createHexagon(){
|
||||
int* hexagon = malloc(sizeof(int)*HEX_MAX);
|
||||
if(hexagon == NULL){
|
||||
printf("buy more ram lol\n");
|
||||
exit(1);
|
||||
}
|
||||
for(int i=1; i<=HEX_MAX; ++i){
|
||||
hexagon[i-1] = i;
|
||||
}
|
||||
return hexagon;
|
||||
}
|
||||
|
||||
int isSolution(int* h){
|
||||
if(HEX_MAX!=18||HEX_GOAL!=38)
|
||||
return 0;
|
||||
if(h[0]+h[1]+h[2]==HEX_GOAL
|
||||
&& h[0]+h[3]+h[7]==HEX_GOAL //3
|
||||
&& h[2]+h[6]+h[11]==HEX_GOAL
|
||||
&& h[7]+h[12]+h[16]==HEX_GOAL
|
||||
&& h[11]+h[15]+h[18]==HEX_GOAL
|
||||
&& h[16]+h[17]+h[18]==HEX_GOAL
|
||||
&& h[3]+h[4]+h[5]+h[6]==HEX_GOAL //4
|
||||
&& h[12]+h[13]+h[14]+h[15]==HEX_GOAL
|
||||
&& h[3]+h[8]+h[15]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[5]+h[10]+h[15]==HEX_GOAL
|
||||
&& h[6]+h[10]+h[14]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[4]+h[8]+h[12]==HEX_GOAL
|
||||
&& h[7]+h[8]+h[9]+h[10]+h[11]==HEX_GOAL //5
|
||||
&& h[0]+h[4]+h[9]+h[14]+h[18]==HEX_GOAL
|
||||
&& h[2]+h[5]+h[9]+h[13]+h[16]==HEX_GOAL
|
||||
)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printHexagon(int* h){
|
||||
printf(
|
||||
" == HEXAGON ==\n"
|
||||
" %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
"%d, %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d\n\n",
|
||||
h[0],h[1],h[2],h[3],h[4],h[5],h[6],h[7],h[8],h[9],h[10],h[11],h[12],h[13],h[14],h[15],h[16],h[17],h[18]);
|
||||
}
|
||||
|
||||
void solveHexagon(int* h, int l, int* count){
|
||||
int tmp;
|
||||
if(l==HEX_MAX-1){
|
||||
printf("hexagon #%d\n",*count);
|
||||
++*count;
|
||||
if(isSolution(h)){
|
||||
printf("found solution!\n");
|
||||
printHexagon(h);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(int i=l; i<HEX_MAX; ++i){
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
solveHexagon(h, l+1, count);
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int* my_hexagon = createHexagon();
|
||||
int count = 1;
|
||||
solveHexagon(my_hexagon,0,&count);
|
||||
free(my_hexagon);
|
||||
|
||||
return 0;
|
||||
}
|
89
src/solution.c~
Normal file
89
src/solution.c~
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
int array [1..19]
|
||||
loop:
|
||||
- shift array by one thing until it is every possible version
|
||||
- each time check if it is the solution
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HEX_MAX 18
|
||||
#define HEX_GOAL 38
|
||||
|
||||
int* createHexagon(){
|
||||
int* hexagon = malloc(sizeof(int)*HEX_MAX);
|
||||
if(hexagon == NULL){
|
||||
printf("buy more ram lol\n");
|
||||
exit(1);
|
||||
}
|
||||
for(int i=1; i<=HEX_MAX; ++i){
|
||||
hexagon[i-1] = i;
|
||||
}
|
||||
return hexagon;
|
||||
}
|
||||
|
||||
int isSolution(int* h){
|
||||
if(HEX_MAX!=18||HEX_GOAL!=38)
|
||||
return 0;
|
||||
if(h[0]+h[1]+h[2]==HEX_GOAL
|
||||
&& h[0]+h[3]+h[7]==HEX_GOAL //3
|
||||
&& h[2]+h[6]+h[11]==HEX_GOAL
|
||||
&& h[7]+h[12]+h[16]==HEX_GOAL
|
||||
&& h[11]+h[15]+h[18]==HEX_GOAL
|
||||
&& h[16]+h[17]+h[18]==HEX_GOAL
|
||||
&& h[3]+h[4]+h[5]+h[6]==HEX_GOAL //4
|
||||
&& h[12]+h[13]+h[14]+h[15]==HEX_GOAL
|
||||
&& h[3]+h[8]+h[15]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[5]+h[10]+h[15]==HEX_GOAL
|
||||
&& h[6]+h[10]+h[14]+h[17]==HEX_GOAL
|
||||
&& h[1]+h[4]+h[8]+h[12]==HEX_GOAL
|
||||
&& h[7]+h[8]+h[9]+h[10]+h[11]==HEX_GOAL //5
|
||||
&& h[0]+h[4]+h[9]+h[14]+h[18]==HEX_GOAL
|
||||
&& h[2]+h[5]+h[9]+h[13]+h[16]==HEX_GOAL
|
||||
)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printHexagon(int* h){
|
||||
printf(
|
||||
" == HEXAGON ==\n"
|
||||
" %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
"%d, %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d, %d,\n"
|
||||
" %d, %d, %d\n\n",
|
||||
h[0],h[1],h[2],h[3],h[4],h[5],h[6],h[7],h[8],h[9],h[10],h[11],h[12],h[13],h[14],h[15],h[16],h[17],h[18]);
|
||||
}
|
||||
|
||||
void solveHexagon(int* h, int l, int* count){
|
||||
int tmp;
|
||||
if(l==HEX_MAX-1){
|
||||
printf("hexagon #%d\n",*count);
|
||||
++*count;
|
||||
if(isSolution(h)){
|
||||
printf("found solution!\n");
|
||||
printHexagon(h);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(int i=l; i<HEX_MAX; ++i){
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
solveHexagon(h, l+1, count);
|
||||
tmp = h[i];
|
||||
h[i] = h[l];
|
||||
h[l] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int* my_hexagon = createHexagon();
|
||||
int count = 1;
|
||||
solveHexagon(my_hexagon,0,&count);
|
||||
free(my_hexagon);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user