Skip to content

Instantly share code, notes, and snippets.

@lucarinelli
Last active January 5, 2018 17:19
Show Gist options
  • Save lucarinelli/b5c64975cdde092e1335764a343d08d6 to your computer and use it in GitHub Desktop.
Save lucarinelli/b5c64975cdde092e1335764a343d08d6 to your computer and use it in GitHub Desktop.
My first solution of the 18 points exam 20150202
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct props_list_item_t props_list_item_t;
#define ABS(a) (a)<0?(-(a)):(a)
struct props_list_item_t{
int val;
props_list_item_t *next;
};
int *readProperties(FILE *input, int *n);
props_list_item_t **distribute(int heirs, int *properties_values, int n);
void distribute_r(props_list_item_t **inheritances, int h, int *p_values, int n, int k,
props_list_item_t ***best_dist, int *best_diff);
props_list_item_t *addToInheritance(props_list_item_t *inheritance, int value);
props_list_item_t *popFrom(props_list_item_t *inheritance);
int computeDiff(props_list_item_t **inheritance, int h);
props_list_item_t **copyDist(props_list_item_t **inheritances, int h);
void copyList(props_list_item_t *source, props_list_item_t **copy);
void freeDist(props_list_item_t **inheritances, int h);
void freeList(props_list_item_t *list);
void printInheritances(FILE *file, props_list_item_t **inheritances, int h);
void *allocateOrDie(size_t size);
int main(int argc, char **argv){
int *properties_values, num_of_properties, num_of_heirs;
props_list_item_t **inheritances;
FILE *file;
if(argc!=4){
printf("Wrong number of arguments.\n");
exit(EXIT_FAILURE);
}
file=fopen(argv[1],"r");
if(file==NULL){
printf("Error opening input file.\n");
exit(EXIT_FAILURE);
}
properties_values=readProperties(file,&num_of_properties);
fclose(file);
num_of_heirs=atoi(argv[3]);
inheritances=distribute(num_of_heirs,properties_values,num_of_properties);
file=fopen(argv[2],"w");
if(file==NULL){
printf("Error opening output file.\n");
exit(EXIT_FAILURE);
}
printInheritances(file,inheritances,num_of_heirs);
fclose(file);
freeDist(inheritances,num_of_heirs);
return 0;
}
int *readProperties(FILE *input, int *n){
int *props,i;
fscanf(input,"%d",n);
props=(int*)allocateOrDie((*n)*(sizeof(int)));
for(i=0;i<*n;i++)fscanf(input,"%d",&props[i]);
return props;
}
props_list_item_t **distribute(int heirs, int *properties_values, int n){
props_list_item_t **inheritances, **best_distribution=NULL;
int best_diff=INT_MAX,i;
inheritances=(props_list_item_t**)allocateOrDie(sizeof(props_list_item_t*)*n);
for(i=0;i<heirs;i++)inheritances[i]=NULL;
distribute_r(inheritances,heirs,properties_values,n,0,&best_distribution,&best_diff);
return best_distribution;
}
void distribute_r(props_list_item_t **inheritances, int h, int *p_values, int n, int k,
props_list_item_t ***best_dist, int *best_diff){
int i,diff;
if(k==n){
diff=computeDiff(inheritances,h);
if(diff<*best_diff){
*best_diff=diff;
freeDist(*best_dist,h);
*best_dist=NULL;
*best_dist=copyDist(inheritances,h);
}
return;
}
for(i=0;i<h;i++){
inheritances[i]=addToInheritance(inheritances[i],p_values[k]);
distribute_r(inheritances,h,p_values,n,k+1,best_dist,best_diff);
inheritances[i]=popFrom(inheritances[i]);
}
}
props_list_item_t *addToInheritance(props_list_item_t *inheritance, int value) {
props_list_item_t *new;
new=(props_list_item_t*)allocateOrDie(sizeof(props_list_item_t));
new->val=value;
new->next=inheritance;
return new;
}
props_list_item_t *popFrom(props_list_item_t *inheritance){
props_list_item_t *tmp;
tmp=inheritance->next;
free(inheritance);
return tmp;
}
int computeDiff(props_list_item_t **inheritance, int h){
int i,j,diff=0,*sums;
props_list_item_t *tmp;
sums=(int*)allocateOrDie(sizeof(int)*h);
for(i=0;i<h;i++){
sums[i]=0;
for(tmp=inheritance[i];tmp!=NULL;tmp=tmp->next)sums[i]+=tmp->val;
}
for(i=0;i<h;i++)
for(j=i+1;j<h;j++)
diff+=ABS(sums[i]-sums[j]);
return diff;
}
props_list_item_t **copyDist(props_list_item_t **inheritances, int h){
props_list_item_t **copy;
int i;
copy=(props_list_item_t**)allocateOrDie(sizeof(props_list_item_t*)*h);
for(i=0;i<h;i++){
copy[i]=NULL;
copyList(inheritances[i],&copy[i]);
}
return copy;
}
void copyList(props_list_item_t *source, props_list_item_t **copy){
if(source==NULL)return;
props_list_item_t *new;
copyList(source->next,copy);
new=(props_list_item_t*)allocateOrDie(sizeof(props_list_item_t));
new->next=*copy;
new->val=source->val;
*copy=new;
}
void freeDist(props_list_item_t **inheritances, int h){
int i;
if(inheritances==NULL)return;
for(i=0;i<h;i++)
freeList(inheritances[i]);
free(inheritances);
}
void freeList(props_list_item_t *list){
if(list==NULL)return;
freeList(list->next);
free(list);
}
void printInheritances(FILE *file, props_list_item_t **inheritances, int h){
int i;
props_list_item_t *tmp;
for(i=0;i<h;i++){
for(tmp=inheritances[i];tmp!=NULL;tmp=tmp->next)fprintf(file,"%d ",tmp->val);
fprintf(file,"\n");
}
}
void *allocateOrDie(size_t size){
void *ptr=malloc(size);
if(ptr==NULL){
printf("ERROR allocating memory. EXITING.\n");
exit(EXIT_FAILURE);
}
return ptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment