diff --git a/archives/19/current b/archives/19/current new file mode 100755 index 0000000..e2467db Binary files /dev/null and b/archives/19/current differ diff --git a/archives/19/src/main.c b/archives/19/src/main.c new file mode 100644 index 0000000..74ce739 --- /dev/null +++ b/archives/19/src/main.c @@ -0,0 +1,89 @@ +#include +#include "utils.h" + +/* + You are given the following information, but you may prefer to do some research for yourself. + + 1 Jan 1900 was a Monday. + Thirty days has September, + April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. + How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? + + https://projecteuler.net/problem=19 +*/ + +enum Month {JANUARY=1,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER}; + +int DaysInMonth(enum Month month,int year) { + switch (month) { + case JANUARY: + case MARCH: + case MAY: + case JULY: + case AUGUST: + case OCTOBER: + case DECEMBER:{ + return 31; + }break; + case APRIL: + case JUNE: + case SEPTEMBER: + case NOVEMBER:{ + return 30; + }break; + case FEBRUARY:{ + return ((year%100==0)&&(year%400!=0))?28:(year%4==0)?29:28; + }break; + } +} + +int main(int argc,char**argv) { + int day=1; + enum Month month=1; + int year=1900; + enum Weekday{ + SUNDAY, + MONDAY, + TUESDAY, + WEDNESDAY, + THURSDAY, + FRIDAY, + SATURDAY, + } weekday=MONDAY; + + int sundayCount=0; + + while((day!=31)||(month!=12)||(year!=2000)) { + printf("Date: %d/%d/%d\n",month,day,year); + if (year>=1901) { + //Start counting Sundays. + if (weekday==SUNDAY&&day==1) { + sundayCount++; + } + } + //Increment the day. + if (day+1<=DaysInMonth(month,year)) { + day++; + } else + if (month+1<=12){ + //It's a new month. + month++; + day=1; + } else { + //It's a new year! + year++; + day=1; + month=1; + //printf("The year is now %d...\n",year); + } + weekday=((weekday+1)<=SATURDAY)?weekday+1:SUNDAY; + } + + printf("\n\n%d Sundays were on the 1st of the month.",sundayCount); + return 0; +} \ No newline at end of file diff --git a/archives/19/src/utils.c b/archives/19/src/utils.c new file mode 100644 index 0000000..90b55a9 --- /dev/null +++ b/archives/19/src/utils.c @@ -0,0 +1,72 @@ +#include "utils.h" +#include +#include + +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;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]; + } + } + } 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]; + } + } + } + if (carryover>0) { + str = realloc(str,++digitCount); + str[digitCount-1]='1'; + } + for (int i=0;i=1901) { + //Start counting Sundays. + if (weekday==SUNDAY&&day==1) { + sundayCount++; + } + } + //Increment the day. + if (day+1<=DaysInMonth(month,year)) { + day++; + } else + if (month+1<=12){ + //It's a new month. + month++; + day=1; + } else { + //It's a new year! + year++; + day=1; + month=1; + //printf("The year is now %d...\n",year); + } + weekday=((weekday+1)<=SATURDAY)?weekday+1:SUNDAY; + } + + printf("\n\n%d Sundays were on the 1st of the month.",sundayCount); return 0; } \ No newline at end of file