diff --git a/archives/22/current b/archives/22/current new file mode 100755 index 0000000..e6f0c36 Binary files /dev/null and b/archives/22/current differ diff --git a/archives/22/src/main.c b/archives/22/src/main.c new file mode 100644 index 0000000..761c218 --- /dev/null +++ b/archives/22/src/main.c @@ -0,0 +1,103 @@ +#include +#include "utils.h" +#include +#include + +/* + Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. + + For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714. + + What is the total of all the name scores in the file? + + https://projecteuler.net/problem=22 +*/ + +int nameScore(int pos,char*str) { + int myScore=0; + int marker=0; + while (str[marker]!='\0') { + myScore+=str[marker]-'A'+1; + marker++; + } + return myScore*pos; +} + +int main(int argc,char**argv) { + FILE*file = fopen("p022_names.txt","r"); + int pointer=0; + int nameCount=0; + while (!feof(file)) { + if (fgetc(file)=='"') { + while (fgetc(file)!='"') { + } + nameCount++; + } + } + fseek(file,0,0); + char*names[nameCount]; + int currentCount=0; + while (!feof(file)) { + if (fgetc(file)=='"') { + //Start reading the name. + char*newName=malloc(1); + char c=' '; + int length=1; + while ((c=fgetc(file))!='"') { + newName=realloc(newName,++length); + newName[length-2]=c; + newName[length-1]='\0'; + } + if (currentCount<2) { + if (currentCount==1) { + if (strcmp(names[0],newName)<0) { + names[1]=newName; + } else { + names[1]=names[0]; + names[0]=newName; + } + } else { + names[0]=newName; + } + } else { + int low=0; + int high=currentCount; + int pos=0; + int mid=low+(high-low)/2; + while (low<=high) { + mid=low+(high-low)/2; + if (mid>=currentCount) { + break; + } + int diff=strcmp(names[mid],newName); + if (diff<0) { + low=mid+1; + //printf("%s is after %s... New low:%d\n",newName,names[mid],low); + } else { + high=mid-1; + //printf("%s is before %s... New high:%d\n",newName,names[mid],high); + } + } + pos=low+(high-low)/2; + //printf("Position of %s is %d Count:%d.\n",newName,pos,currentCount); + if (pos==currentCount) { + names[currentCount]=newName; + } else { + for (int j=currentCount;j>=pos;j--) { + names[j]=names[j-1]; + } + names[pos]=newName; + } + } + currentCount++; + } + } + long nameScores=0; + for (int i=0;i +#include + +struct String mult(struct String numb1, struct String numb2) { + struct String n1 = numb1; + struct String n2 = numb2; + byte carryover = 0; + if (numb2.length>numb1.length) { + n1=numb2; + n2=numb1; + } + int addends[n2.length][n1.length+1]; + for (int i=0;i=0;i--) { + carryover=0; + for (int j=n1.length-1;j>=0;j--) { + int mult = (n1.str[j]-'0')*(n2.str[i]-'0')+((carryover!=0)?carryover:0); + //printf(" %d/%d\n",mult,carryover); + carryover=0; + if (mult>=10) { + carryover=mult/10; + mult=mult%10; + } + addends[(n2.length-1)-i][j+1]=mult; + } + if (carryover>0) { + addends[(n2.length-1)-i][0]=carryover; + } + } + //printIntDoubleArr(n2.length,n1.length+1,addends); + struct String sum = {1,"0"}; + for (int i=0;i=numb2.length) { + for (int offset=0;offsetoffset) { + //printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]); + int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0); + if (sum>=10) { + carryover=1; + sum-=10; + } + str[offset]=sum+'0'; + } else { + str[offset]=numb1.str[numb1.length-offset-1]+((carryover>0)?carryover--:0); + } + //str[offset+1]='\0'; + } + } else { + for (int offset=0;offsetoffset) { + //printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]); + int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0); + if (sum>=10) { + carryover=1; + sum-=10; + } + str[offset]=sum+'0'; + } else { + str[offset]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0); + } + //str[offset+1]='\0'; + } + } + if (carryover>0) { + str = realloc(str,++digitCount); + str[digitCount-1]='1'; + //str[digitCount]='\0'; + } + for (int i=0;i #include "utils.h" #include +#include /* Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. @@ -12,6 +13,16 @@ https://projecteuler.net/problem=22 */ +int nameScore(int pos,char*str) { + int myScore=0; + int marker=0; + while (str[marker]!='\0') { + myScore+=str[marker]-'A'+1; + marker++; + } + return myScore*pos; +} + int main(int argc,char**argv) { FILE*file = fopen("p022_names.txt","r"); int pointer=0; @@ -37,12 +48,56 @@ int main(int argc,char**argv) { newName[length-2]=c; newName[length-1]='\0'; } - //printf("%s\n",newName); - names[currentCount++]=newName; + if (currentCount<2) { + if (currentCount==1) { + if (strcmp(names[0],newName)<0) { + names[1]=newName; + } else { + names[1]=names[0]; + names[0]=newName; + } + } else { + names[0]=newName; + } + } else { + int low=0; + int high=currentCount; + int pos=0; + int mid=low+(high-low)/2; + while (low<=high) { + mid=low+(high-low)/2; + if (mid>=currentCount) { + break; + } + int diff=strcmp(names[mid],newName); + if (diff<0) { + low=mid+1; + //printf("%s is after %s... New low:%d\n",newName,names[mid],low); + } else { + high=mid-1; + //printf("%s is before %s... New high:%d\n",newName,names[mid],high); + } + } + pos=low+(high-low)/2; + //printf("Position of %s is %d Count:%d.\n",newName,pos,currentCount); + if (pos==currentCount) { + names[currentCount]=newName; + } else { + for (int j=currentCount;j>=pos;j--) { + names[j]=names[j-1]; + } + names[pos]=newName; + } + } + currentCount++; } } + long nameScores=0; for (int i=0;i