diff --git a/challenges/euler/readme.md b/challenges/euler/readme.md index ada645d..6ef1d5d 100644 --- a/challenges/euler/readme.md +++ b/challenges/euler/readme.md @@ -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 diff --git a/challenges/euler/scripts/generate.js b/challenges/euler/scripts/generate.js index b1184cd..964636d 100644 --- a/challenges/euler/scripts/generate.js +++ b/challenges/euler/scripts/generate.js @@ -58,10 +58,10 @@ inquirer fs.writeFileSync( path.join(src, `${fileName}.ts`), `${problemContent} - export = {}; +export = {}; - // Output - console.log();` +// Output +console.log();` ); // Generate the thoughts file diff --git a/challenges/euler/src/19 - Counting Sundays.ts b/challenges/euler/src/19 - Counting Sundays.ts new file mode 100644 index 0000000..cb037ba --- /dev/null +++ b/challenges/euler/src/19 - Counting Sundays.ts @@ -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);