Completed 18!
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
046318072f
commit
9f14ac1de8
@ -7,7 +7,7 @@
|
|||||||
How many such routes are there through a 20×20 grid?
|
How many such routes are there through a 20×20 grid?
|
||||||
|
|
||||||
|
|
||||||
https://projecteuler.net/problem=14
|
https://projecteuler.net/problem=15
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void move(int x,int y,long*pathCount) {
|
void move(int x,int y,long*pathCount) {
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
How many such routes are there through a 20×20 grid?
|
How many such routes are there through a 20×20 grid?
|
||||||
|
|
||||||
|
|
||||||
https://projecteuler.net/problem=14
|
https://projecteuler.net/problem=15
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void move(int x,int y,long*pathCount) {
|
void move(int x,int y,long*pathCount) {
|
||||||
|
BIN
archives/18/current
Executable file
BIN
archives/18/current
Executable file
Binary file not shown.
76
archives/18/src/main.c
Normal file
76
archives/18/src/main.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#define TARGET 2
|
||||||
|
|
||||||
|
/*
|
||||||
|
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||||
|
|
||||||
|
3
|
||||||
|
7 4
|
||||||
|
2 4 6
|
||||||
|
8 5 9 3
|
||||||
|
|
||||||
|
That is, 3 + 7 + 4 + 9 = 23.
|
||||||
|
|
||||||
|
Find the maximum total from top to bottom of the triangle below:
|
||||||
|
|
||||||
|
75
|
||||||
|
95 64
|
||||||
|
17 47 82
|
||||||
|
18 35 87 10
|
||||||
|
20 04 82 47 65
|
||||||
|
19 01 23 75 03 34
|
||||||
|
88 02 77 73 07 63 67
|
||||||
|
99 65 04 28 06 16 70 92
|
||||||
|
41 41 26 56 83 40 80 70 33
|
||||||
|
41 48 72 33 47 32 37 16 94 29
|
||||||
|
53 71 44 65 25 43 91 52 97 51 14
|
||||||
|
70 11 33 28 77 73 17 78 39 68 17 57
|
||||||
|
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||||
|
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||||
|
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||||
|
|
||||||
|
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||||
|
https://projecteuler.net/problem=18
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main(int argc,char**argv) {
|
||||||
|
int rows[][15] = {
|
||||||
|
{75}
|
||||||
|
,{95,64}
|
||||||
|
,{17,47,82}
|
||||||
|
,{18,35,87,10}
|
||||||
|
,{20,4,82,47,65}
|
||||||
|
,{19,1,23,75,3,34}
|
||||||
|
,{88,2,77,73,7,63,67}
|
||||||
|
,{99,65,4,28,6,16,70,92}
|
||||||
|
,{41,41,26,56,83,40,80,70,33}
|
||||||
|
,{41,48,72,33,47,32,37,16,94,29}
|
||||||
|
,{53,71,44,65,25,43,91,52,97,51,14}
|
||||||
|
,{70,11,33,28,77,73,17,78,39,68,17,57}
|
||||||
|
,{91,71,52,38,17,14,91,43,58,50,27,29,48}
|
||||||
|
,{63,66,4,68,89,53,67,30,73,16,69,87,40,31}
|
||||||
|
,{4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}};
|
||||||
|
|
||||||
|
int max=14;
|
||||||
|
for (int row=14;row>0;row--) {
|
||||||
|
for (int col=0;col<max;col++) {
|
||||||
|
int numb1=rows[row][col];
|
||||||
|
int numb2=rows[row][col+1];
|
||||||
|
//printf("Picked numbers: %d %d\n",numb1,numb2);
|
||||||
|
rows[row-1][col]+=(numb1>numb2)?numb1:numb2;
|
||||||
|
}
|
||||||
|
max--;
|
||||||
|
}
|
||||||
|
for (int i=0;i<15;i++) {
|
||||||
|
for (int j=0;j<15;j++) {
|
||||||
|
printf("%d\t",rows[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nYour maximum route is %d",rows[0][0]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
54
archives/18/src/utils.c
Normal file
54
archives/18/src/utils.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct String add(struct String numb1, struct String numb2){
|
||||||
|
byte carryover=0;
|
||||||
|
int digitCount=0;
|
||||||
|
char*str = malloc(0);
|
||||||
|
if (numb1.length>=numb2.length) {
|
||||||
|
for (int offset=0;offset<numb1.length;offset++) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
//printf("Digit count is now %d\n",digitCount);
|
||||||
|
if (numb2.length>offset) {
|
||||||
|
//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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int offset=0;offset<numb2.length;offset++) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
//printf("Digit count is now %d\n",digitCount);
|
||||||
|
if (numb1.length>offset) {
|
||||||
|
//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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (carryover>0) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
str[digitCount-1]='1';
|
||||||
|
}
|
||||||
|
for (int i=0;i<digitCount/2;i++) {
|
||||||
|
char c = str[i];
|
||||||
|
char c2 = str[digitCount-i-1];
|
||||||
|
str[digitCount-i-1]=c;
|
||||||
|
str[i]=c2;
|
||||||
|
}
|
||||||
|
struct String newStr = {digitCount,str};
|
||||||
|
return newStr;
|
||||||
|
}
|
9
archives/18/src/utils.h
Normal file
9
archives/18/src/utils.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
#define boolean char
|
||||||
|
#define byte char
|
||||||
|
struct String{
|
||||||
|
int length;
|
||||||
|
char*str;
|
||||||
|
};
|
||||||
|
struct String add(struct String numb1, struct String numb2);
|
210
src/main.c
210
src/main.c
@ -1,168 +1,76 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#define TARGET 2
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||||
|
|
||||||
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
3
|
||||||
|
7 4
|
||||||
|
2 4 6
|
||||||
|
8 5 9 3
|
||||||
|
|
||||||
|
That is, 3 + 7 + 4 + 9 = 23.
|
||||||
|
|
||||||
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
|
Find the maximum total from top to bottom of the triangle below:
|
||||||
|
|
||||||
https://projecteuler.net/problem=17
|
75
|
||||||
|
95 64
|
||||||
|
17 47 82
|
||||||
|
18 35 87 10
|
||||||
|
20 04 82 47 65
|
||||||
|
19 01 23 75 03 34
|
||||||
|
88 02 77 73 07 63 67
|
||||||
|
99 65 04 28 06 16 70 92
|
||||||
|
41 41 26 56 83 40 80 70 33
|
||||||
|
41 48 72 33 47 32 37 16 94 29
|
||||||
|
53 71 44 65 25 43 91 52 97 51 14
|
||||||
|
70 11 33 28 77 73 17 78 39 68 17 57
|
||||||
|
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||||
|
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||||
|
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
||||||
|
|
||||||
|
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
|
||||||
|
https://projecteuler.net/problem=18
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main(int argc,char**argv) {
|
int main(int argc,char**argv) {
|
||||||
int totalCount=0;
|
int rows[][15] = {
|
||||||
int counter=1;
|
{75}
|
||||||
int i=1;
|
,{95,64}
|
||||||
int hundredsDigit=-1;
|
,{17,47,82}
|
||||||
int tensDigit=-1;
|
,{18,35,87,10}
|
||||||
while (i<=1000) {
|
,{20,4,82,47,65}
|
||||||
hundredsDigit=-1;
|
,{19,1,23,75,3,34}
|
||||||
tensDigit=-1;
|
,{88,2,77,73,7,63,67}
|
||||||
counter=i;
|
,{99,65,4,28,6,16,70,92}
|
||||||
int currentCount=totalCount;
|
,{41,41,26,56,83,40,80,70,33}
|
||||||
if (counter==1000) {
|
,{41,48,72,33,47,32,37,16,94,29}
|
||||||
//onethousand (11)
|
,{53,71,44,65,25,43,91,52,97,51,14}
|
||||||
totalCount+=11;
|
,{70,11,33,28,77,73,17,78,39,68,17,57}
|
||||||
counter-=1000;
|
,{91,71,52,38,17,14,91,43,58,50,27,29,48}
|
||||||
printf("onethousand");
|
,{63,66,4,68,89,53,67,30,73,16,69,87,40,31}
|
||||||
|
,{4,62,98,27,23,9,70,98,73,93,38,53,60,4,23}};
|
||||||
|
|
||||||
|
int max=14;
|
||||||
|
for (int row=14;row>0;row--) {
|
||||||
|
for (int col=0;col<max;col++) {
|
||||||
|
int numb1=rows[row][col];
|
||||||
|
int numb2=rows[row][col+1];
|
||||||
|
//printf("Picked numbers: %d %d\n",numb1,numb2);
|
||||||
|
rows[row-1][col]+=(numb1>numb2)?numb1:numb2;
|
||||||
}
|
}
|
||||||
if (counter>=100) {
|
max--;
|
||||||
hundredsDigit=counter/100;
|
|
||||||
switch (hundredsDigit) {
|
|
||||||
case 1:
|
|
||||||
case 2:
|
|
||||||
case 6:{
|
|
||||||
//onehundred (10)
|
|
||||||
totalCount+=10;
|
|
||||||
printf("%s",hundredsDigit==1?"one":hundredsDigit==2?"two":"six");
|
|
||||||
}break;
|
|
||||||
case 3:
|
|
||||||
case 7:
|
|
||||||
case 8:{
|
|
||||||
//threehundred (12)
|
|
||||||
totalCount+=12;
|
|
||||||
printf("%s",hundredsDigit==3?"three":hundredsDigit==7?"seven":"eight");
|
|
||||||
}break;
|
|
||||||
case 4:
|
|
||||||
case 5:
|
|
||||||
case 9:{
|
|
||||||
//fourhundred (11)
|
|
||||||
totalCount+=11;
|
|
||||||
printf("%s",hundredsDigit==4?"four":hundredsDigit==5?"five":"nine");
|
|
||||||
}break;
|
|
||||||
}
|
}
|
||||||
printf("hundred");
|
for (int i=0;i<15;i++) {
|
||||||
counter-=hundredsDigit*100;
|
for (int j=0;j<15;j++) {
|
||||||
|
printf("%d\t",rows[i][j]);
|
||||||
}
|
}
|
||||||
if (counter>=10) {
|
printf("\n");
|
||||||
tensDigit=counter/10;
|
|
||||||
if (tensDigit!=0) {
|
|
||||||
if (hundredsDigit!=-1) {
|
|
||||||
//and (3)
|
|
||||||
totalCount+=3;
|
|
||||||
printf("and");
|
|
||||||
}
|
}
|
||||||
if (counter<20) {
|
|
||||||
switch (counter) {
|
printf("\nYour maximum route is %d",rows[0][0]);
|
||||||
case 10:{
|
|
||||||
//ten (3)
|
|
||||||
totalCount+=3;
|
|
||||||
printf("ten");
|
|
||||||
}break;
|
|
||||||
case 11:{
|
|
||||||
//eleven (6)
|
|
||||||
totalCount+=6;
|
|
||||||
printf("eleven");
|
|
||||||
}break;
|
|
||||||
case 12:{
|
|
||||||
//twelve (5)
|
|
||||||
totalCount+=5;
|
|
||||||
printf("twelve");
|
|
||||||
}break;
|
|
||||||
case 13:
|
|
||||||
case 14:
|
|
||||||
case 19:{
|
|
||||||
//thirteen (8)
|
|
||||||
totalCount+=8;
|
|
||||||
printf("%s",counter==13?"thirteen":counter==14?"fourteen":"nineteen");
|
|
||||||
}break;
|
|
||||||
case 15:
|
|
||||||
case 16:{
|
|
||||||
//fifteen (7)
|
|
||||||
totalCount+=7;
|
|
||||||
printf("%s",counter==15?"fifteen":"sixteen");
|
|
||||||
}break;
|
|
||||||
case 17:
|
|
||||||
case 18:{
|
|
||||||
//seventeen (9)
|
|
||||||
totalCount+=9;
|
|
||||||
printf("%s",counter==17?"seventeen":"eighteen");
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
counter=0;
|
|
||||||
} else {
|
|
||||||
switch (tensDigit) {
|
|
||||||
case 2:
|
|
||||||
case 3:
|
|
||||||
case 8:
|
|
||||||
case 9:{
|
|
||||||
//twenty (6)
|
|
||||||
totalCount+=6;
|
|
||||||
printf("%s",tensDigit==2?"twenty":tensDigit==3?"thirty":tensDigit==8?"eighty":"ninety");
|
|
||||||
}break;
|
|
||||||
case 4:
|
|
||||||
case 5:
|
|
||||||
case 6:{
|
|
||||||
//forty (5)
|
|
||||||
totalCount+=5;
|
|
||||||
printf("%s",tensDigit==4?"forty":tensDigit==5?"fifty":"sixty");
|
|
||||||
}break;
|
|
||||||
case 7:{
|
|
||||||
//seventy (7)
|
|
||||||
totalCount+=7;
|
|
||||||
printf("seventy");
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
counter-=tensDigit*10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (counter>0) {
|
|
||||||
if (tensDigit==-1&&hundredsDigit!=-1) {
|
|
||||||
//and (3)
|
|
||||||
totalCount+=3;
|
|
||||||
printf("and");
|
|
||||||
}
|
|
||||||
switch (counter) {
|
|
||||||
case 1:
|
|
||||||
case 2:
|
|
||||||
case 6:{
|
|
||||||
//one (3)
|
|
||||||
totalCount+=3;
|
|
||||||
printf("%s",counter==1?"one":counter==2?"two":"six");
|
|
||||||
}break;
|
|
||||||
case 3:
|
|
||||||
case 7:
|
|
||||||
case 8:{
|
|
||||||
//three (5)
|
|
||||||
totalCount+=5;
|
|
||||||
printf("%s",counter==3?"three":counter==7?"seven":"eight");
|
|
||||||
}break;
|
|
||||||
case 4:
|
|
||||||
case 5:
|
|
||||||
case 9:{
|
|
||||||
//four (4)
|
|
||||||
totalCount+=4;
|
|
||||||
printf("%s",counter==4?"four":counter==5?"five":"nine");
|
|
||||||
}break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf(" %d:%d\n",i,totalCount-currentCount);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
printf("%d",totalCount);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user