From bc9a7f513738afb878d4265f0fd02728d00a1c63 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Tue, 19 Jul 2022 18:56:13 +0000 Subject: [PATCH] Completed 22 with a binary search and insert! Co-authored-by: sigonasr2 --- archives/22/current | Bin 0 -> 21488 bytes archives/22/src/main.c | 103 +++++++++++++++++++++++++++++ archives/22/src/utils.c | 140 ++++++++++++++++++++++++++++++++++++++++ archives/22/src/utils.h | 14 ++++ current | Bin 21448 -> 21488 bytes src/main.c | 61 ++++++++++++++++- 6 files changed, 315 insertions(+), 3 deletions(-) create mode 100755 archives/22/current create mode 100644 archives/22/src/main.c create mode 100644 archives/22/src/utils.c create mode 100644 archives/22/src/utils.h diff --git a/archives/22/current b/archives/22/current new file mode 100755 index 0000000000000000000000000000000000000000..e6f0c3639fca030ad42b9c6a28f645462efc21c2 GIT binary patch literal 21488 zcmeHP4{%$>nP1sX;y|zr7fK4W5ovQx2r*8Y(3mvEO7g-pRlp5#frC^ivK+hkUuEf< zbduDdi1X`EWABX#D&9ffbRG0 z+fROamZ8(@+;ncVGt#&F`~K|iw}0O5N^ifa`8JifT!KTX_>7>`!SgMoRxzr5Dgsg~ z)`;`)y-<8q%tpFQViv#FBH&u#WJx|PS9mEX=`Em00eYzg3#ME`qNF!dxLIYuR8(4$ zq&JJAD($2f&bQ>4GC!AJwZ#_>I-=^w)UHRqXBwqO37*X;D0t+z5Kg_Dm7bGsRPszI4+=NF7-{@{@~c;Rw<$k5X~Z%w zm~wufgC6y^9ewZU8nI&5oKIc!l|}#fFITe- z%9CU$kUVz@N>iNjAsMUwcOo{`jgNpa5^t4EVjkJq6@xIw)j^yio}M z%nbOB8SvX@z-wl}`+h2P&52?X!(2b=HkHw9W+MQ1b;Yz~A)I234Y?Ffp_a7%lnNi>Bz@U{ zIQ3PFS~2p43y2Gp$K(8#mh-5W?^Rgz;+DCj)kDGvJWulc)U6t`Q_X{n*DAcc-qK^7 z(CKL`=18w|q*vS1D_*TLj(~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&F|uUB$> z{=k%eH>E)O+AkO}CpF`iH^q3}ZYQjH<}`yl-#5hNOj{{i;2% zKGWGyw(IBiAGMYi=fgHwkJIy@Oc>|S>*2~;OTsBndD+AHD*tLbh&bynbKtCRr@6jo zldY|_68*|?A3>7#5?mghf9V{P%Wi2{-)FA1=t2jR^JQ5&u5gG*mix1o1l +#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'; + } + }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$iN0C;7sTefFpdy(1b zdze^9q6u+6AOA#-2?Kk+AsSzaM9)PN?VOlosLMCl(ototJ&95>_%#Z?8Q@8vld3?0 zBCr)0LN^A~@;JDjpGfZmEilk}Cec02+CIeq9f={jIYqkz6Fm`1KKW5V_X~MeUqGFS zQ`Dn@qCXJ;uzYzzks~K}L#id!k_MX`8F0jrEk1O7 zz=^d*T@+7Pzak|hBe2#C(Xk?!<1UB8TTV;p?B3&X?{nruRFDqhi*+#R)an2C5d?in mioVUNv^k5{#F7$;(hevoO%J(Npoo!hpfsHuD})QBiT?mUCQxty diff --git a/src/main.c b/src/main.c index e0b9f39..761c218 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #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. @@ -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