Skip to content

Instantly share code, notes, and snippets.

@AtharvaVaidya
Last active March 27, 2016 00:44
Show Gist options
  • Save AtharvaVaidya/769b54cff2802db82c25 to your computer and use it in GitHub Desktop.
Save AtharvaVaidya/769b54cff2802db82c25 to your computer and use it in GitHub Desktop.
//
// main.cpp
// Problem
//
// Created by Tarun Sudhams on 2016-03-10.
// Copyright © 2016 Tarun Sudhams. All rights reserved.
//
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
bool isLeap(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0));
}
int increaseDate(int date) // 20160310
{
unsigned int day = date % 100; // 10
unsigned int month = (((date % 10000) - (date % 100)) / 100); // 03
unsigned int year = ((date % int(pow(10, 8))) - month) / pow(10, 4); // 2016
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10)
{
if (day == 31)
{
date = ((year * pow(10, 4)) + ((month + 1) * 100) + 1);
}
else
++date;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
date = day == 30 ? ((year * pow(10, 4)) + ((month + 1) * 100) + 1) : ++date;
}
else if (month == 2)
{
if (isLeap(year))
{
date = day == 29 ? (year * pow(10, 4)) + ((month + 1) * 100) + 1 : ++date;
}
else
{
date = day == 28 ? (year * pow(10, 4)) + ((month + 1) * 100) + 1 : ++date;
}
}
else
{
date = day == 31 ? ((year + 1) * pow(10, 4)) + (100) + 1 : ++date;
}
return date;
}
int increaseDateBySeven(int date)
{
for (int i = 0; i < 7; i++)
{
date = increaseDate(date);
}
return date;
}
bool isMonday(int date, int start_day_of_Week, int start_date)
{
int n = start_day_of_Week;
for (int i = start_date; i < date ; i = increaseDate(i))
{
n++;
}
return n % 7 == 1;
}
bool isFriday(int date, int start_day_of_week, int start_date)
{
int n = start_day_of_week;
for (int i = start_date; i < date; i = increaseDate(i))
{
n++;
}
return n % 7 == 5;
}
bool isWorkingDay(int date, int start_day_of_week, int start_date)
{
int n = start_day_of_week;
for (int i = start_date; i < date; i = increaseDate(i))
{
n++;
}
return (n % 7 != 6 && n % 7 != 0);
}
unsigned int convertDateToInt(int year, int month, int day)
{
return (year * 10000) + (month * 100) + day; // 2016 * 1000 = 20160000; 20160000 + (800)= 20160800 + 1 = 20160801
}
int main()
{
unsigned int start_date, end_date, day_of_week, n_public, n_annual_leaves, start_day, start_month, start_year, end_day, end_month, end_year, saturdays[100], sundays[100], counter_for_saturdays = 0, counter_of_sundays = 0, public_day, public_month, public_year, public_holidays[n_public];
char buffer;
cout << "Enter the start date: ";
cin >> start_year; // 2016
cin >> buffer;
cin >> start_month; // 8
cin >> buffer;
cin >> start_day;// 1
start_date = convertDateToInt(start_year, start_month, start_day);
cout << "Enter the end date: ";
cin >> end_year;
cin >> buffer;
cin >> end_month;
cin >> buffer;
cin >> end_day;
end_date = convertDateToInt(end_year, end_month, end_day);
cout << "Enter the day of the week: ";
cin >> day_of_week;
cout << "Enter the number of public holidays in that timeframe: ";
cin >> n_public;
for (int i = 0; i < n_public; i++)
{
cin >> public_year;
cin >> buffer;
cin >> public_month;
cin >> buffer;
cin >> public_day;
public_holidays[i] = convertDateToInt(public_year, public_month, public_day);
}
cout << "Enter the number of days of annual leaves you plan to take: ";
cin >> n_annual_leaves;
int days[420];
int counter = 0;
for (int i = start_date; i <= end_date; i = increaseDate(i))
{
days[counter] = i;
++counter;
}
int holidays[420];
int counter_for_holidays = 0;
int first_saturday = start_date, first_sunday = start_date;
if (day_of_week == 7)
{
for (int i = 0; i < 6; i++)
{
first_saturday = increaseDate(first_saturday);
}
}
else
{
for (int i = day_of_week; i < 6; i++)
{
first_saturday = increaseDate(first_saturday);
}
}
if (first_saturday > end_date)
{
first_saturday = 0;
}
else
{
for (int i = first_saturday; i < end_date; i = increaseDateBySeven(i))
{
holidays[counter_for_holidays] = i;
++counter_for_holidays;
saturdays[counter_for_saturdays] = i;
++counter_for_saturdays;
}
}
for (int i = day_of_week; i < 7; i++)
{
first_sunday = increaseDate(first_sunday);
}
if (first_sunday > end_date)
{
first_sunday = 0;
}
else
{
for (int i = first_sunday; i < end_date; i = increaseDateBySeven(i))
{
holidays[counter_for_holidays] = i;
++counter_for_holidays;
sundays[counter_of_sundays] = i;
++counter_of_sundays;
}
}
for (int i = 0; i < n_public; i++)
{
holidays[counter_for_holidays] = public_holidays[i];
++counter_for_holidays;
}
int final_holidays[counter_for_holidays];
for (int i = 0; i < counter_for_holidays; i++)
{
final_holidays[i] = holidays[i];
}
sort(final_holidays, final_holidays + counter_for_holidays);
int n_holidays = 1, max_holidays = 1;
int dates[420], dates_counter = 0;
for (int i = start_date; i < end_date; i = increaseDate(i))
{
if ((isWorkingDay(i, day_of_week, start_date)))
{
dates[dates_counter] = 0;
}
else
{
dates[dates_counter] = 1;
}
dates_counter++;
}
int new_dates_counter = 0, c = 0;
for (int i = start_date; i < end_date; i = increaseDate(i))
{
for (int j = 0; j < n_public; j++)
{
if (i == public_holidays[j])
{
dates[c] = 1;
new_dates_counter++;
break;
}
}
c++;
}
int start_of_sequence = 0, end_point = 0;
for (int i = 0; i < c; i++)
{
if (dates[i] == 1)
{
n_holidays++;
}
else
{
if (n_holidays - 1 > max_holidays)
{
max_holidays = n_holidays - 1;
end_point = i;
start_of_sequence = end_point - max_holidays;
int additional_sequence_start_point = end_point + n_annual_leaves;
int additional_sequences_begin_point = start_of_sequence - n_annual_leaves - 1;
if (additional_sequence_start_point < c && dates[additional_sequence_start_point] == 1)
{
int k = additional_sequence_start_point;
while (dates[k] == 1)
{
n_holidays++;
k++;
}
max_holidays = n_holidays - 1;
}
if (additional_sequences_begin_point < c && dates[additional_sequences_begin_point] == 1)
{
int k = additional_sequences_begin_point;
while (dates[k] == 1)
{
n_holidays++;
k--;
}
max_holidays = n_holidays - 1;
}
}
else
{
n_holidays = 1;
}
}
}
if (n_holidays - 1 > max_holidays)
{
max_holidays = n_holidays - 1;
end_point = c;
start_of_sequence = end_point - n_holidays - 1;
}
if (n_holidays == 1 && max_holidays == 1)
{
max_holidays = 0;
}
max_holidays += n_annual_leaves;
unsigned int day = days[start_of_sequence] % 100; // 10
unsigned int month = (((days[start_of_sequence] % 10000) - (days[start_of_sequence] % 100)) / 100); // 03
unsigned int year = ((days[start_of_sequence] % int(pow(10, 8))) - month) / pow(10, 4); // 2016
cout << "The maximum number of consecutive holidays you can take is: " << max_holidays << " \nStarting on: " << year << "/" << month << "/" << day << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment