feat(euler): 19 - counting sundays

-
This commit is contained in:
newt 2024-10-09 18:02:41 +01:00
parent 816dad857f
commit 35415ae25e
3 changed files with 66 additions and 4 deletions

View file

@ -30,7 +30,7 @@ The source code can be found in the [src](src) directory. My thoughts about some
- [x] [16 - Power digit sum](src/16%20-%20Power%20digit%20sum.ts)
- [x] [17 - Number letter counts](src/17%20-%20Number%20letter%20counts.ts)
- [x] [18 - Maximum path sum I](src/18%20-%20Maximum%20path%20sum%20I.ts)
- [ ] 19 - Counting Sundays
- [x] [19 - Counting Sundays](src/19%20-%20Counting%20Sundays.ts)
- [ ] 20 - Factorial digit sum
- [ ] 21 - Amicable numbers
- [ ] 22 - Names scores

View file

@ -0,0 +1,62 @@
// 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)?
export = {};
interface Date {
year: number;
month: number;
date: number;
}
/**
* Find all of the Sundays in a given
*/
const sundaysInAYear = (year: number): Date[] => {
const time = new Date(year, 0, 1);
while (time.getDay() != 0) {
time.setDate(time.getDate() + 1);
}
const dates = [];
while (time.getFullYear() === year) {
const month = time.getMonth() + 1;
const date = time.getDate();
dates.push({
year,
month,
date
});
time.setDate(time.getDate() + 7);
}
return dates;
};
// Output
let dates: Date[] = [];
for (let year = 1901; year <= 2000; year++) {
dates = [...dates, ...sundaysInAYear(year)];
}
let count = 0;
dates.forEach(({ date }) => {
if (date == 1) {
count += 1;
}
});
console.log(count);