-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem19.js
35 lines (25 loc) · 997 Bytes
/
problem19.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const {range, when}= require('./eloquent');
const months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
// function to see if a year is a leap year
const isLeap = year => year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
// function to set months var based on if it's a leap year
const setLeap = year => months[1] = isLeap(year) ? 29 : 28;
// days since Jan 1st 1900
const daysGoneBy = (day, month, year) => {
return range(1900, year-1).reduce((sum, val) => sum + (!isLeap(val) ? 365 : 366), 0) + dayOfYear(day, month, year);
}
// need a func for day of year by date
const dayOfYear = (day, month, year) => {
setLeap(year);
return months.slice(0, month-1).reduce((sum, val) => sum + val, 0) + day - 1
}
const sunday = (day, month, year) => {
return (daysGoneBy(day, month, year) + 1) % 7 == 0;
}
let count = 0;
range(1901, 2000).forEach(year => {
range(1, 12).forEach(month => {
if(sunday(1, month, year)) count += 1;
})
})
console.log(count);