Skip to content

Instantly share code, notes, and snippets.

@MehdiNS
Last active July 24, 2016 12:19
Show Gist options
  • Save MehdiNS/786fae42de2add8c96ba734095c6df53 to your computer and use it in GitHub Desktop.
Save MehdiNS/786fae42de2add8c96ba734095c6df53 to your computer and use it in GitHub Desktop.
Euler19 - 10min to lose...
#include <iostream>
#include <string>
#include <vector>
enum yearTypeT
{
COMMON = 0, LEAP
};
using yearT = unsigned int;
enum monthT
{
JAN = 0, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, NB_MONTH_IN_YEAR
};
enum dayT
{
MON = 0, TUE, WED, THU, FRI, SAT, SUN, NB_DAY_IN_WEEK
};
unsigned int isLeapYear(yearT year)
{
if (year%4 != 0)
return COMMON;
else if (year%100 != 0)
return LEAP;
else if (year%400 != 0)
return COMMON;
else
return LEAP;
}
unsigned int nbDaysInYear(yearT year)
{
return 365 + isLeapYear(year);
}
unsigned int nbDaysInMonth(monthT month, yearT year)
{
unsigned int res;
switch(month)
{
case APR :
case JUN :
case SEP :
case NOV : res = 30; break;
case FEB : res = 28 + isLeapYear(year); break;
default : res = 31;
}
return res;
}
int main()
{
auto res = 0;
unsigned int currentDay = MON; //1 Jan 1900 was a Monday
currentDay = (currentDay + nbDaysInYear(1900)) % NB_DAY_IN_WEEK; // Day of 1 Jan 1901
for(yearT year = 1901; year <= 2000; ++year)
{
for(monthT month = JAN; month <= DEC; month=monthT(month+1) )
{
currentDay = (currentDay + nbDaysInMonth(month, year)) % NB_DAY_IN_WEEK;
if (currentDay==SUN)
res++;
}
}
std::cout << res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment