Created
January 18, 2017 21:33
-
-
Save loremIpsum1771/fc09a70996492ae8ae5314c2e0a156c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<vector> | |
#include<string> | |
#include<iostream> | |
#include<unordered_map> | |
#include<sstream> | |
#include<utility> | |
using namespace std; | |
class Day { | |
bool weekday = false; | |
public: | |
Day() {} | |
unordered_map<string, int> prices; | |
}; | |
class Hotel { | |
public: | |
int rating = 0; | |
Day weekend; | |
Day weekday; | |
string name; | |
Hotel(string newName): name(newName) | |
{} | |
}; | |
unordered_map<string, int> dayMap = { | |
{"mon", 0},{ "tue", 1 },{ "wed", 2 },{ "thu", 3 },{ "fri", 4 },{ "sat", 5 },{ "sun", 6 },{ "Regular", 0 },{ "Rewards", 1 } | |
}; | |
vector<int> parseInput(string inputString) { | |
string customerType; | |
vector<int> input_info; | |
int lastIdx = 0; | |
//Get customer type | |
for (int i = 0; i < inputString.size(); i++) { | |
if (inputString[i] != ':') { | |
customerType += inputString[i]; | |
} | |
else { | |
input_info.emplace_back(dayMap[customerType]); | |
lastIdx = i; | |
break; | |
} | |
} | |
//Get days | |
string day; | |
int dayVal = 0; | |
for (int i = lastIdx; i < inputString.size(); i++) { | |
if (inputString[i] != '(') { | |
day += inputString[++i]; | |
day += inputString[++i]; | |
day += inputString[++i]; | |
input_info.emplace_back(dayMap[day]); | |
} | |
} | |
return input_info; | |
} | |
//"Regular: 20Mar2009(fri), 21Mar2009(sat), 22Mar2009(sun)" | |
/*INPUT FORMAT : | |
Customer Types: Regular, Rewards | |
Lakewood - | |
weekday - regular (110), rewards (80) | |
weekend - regular ( 90) , rewards (80) | |
rating - 3 | |
Bridgewood - | |
weekday - regular (160), rewards (110) | |
weekend - regular ( 60) , rewards (50) | |
rating - 4 | |
Ridgewood - | |
weekday - regular (220), rewards (100) | |
weekend - regular ( 150) , rewards (40) | |
rating - 5 | |
*/ | |
//Lakewood: 110 +90 + 90 , Bridgewood: 160 + 60 + 60 Ridgewood: 220 + 150 + 150 | |
string cheapestHotel(vector<Hotel> hotels, vector<int> customer_info) { | |
unordered_map<Hotel,int> scores; | |
int sum = 0; | |
int customerType = customer_info[0]; | |
for (Hotel eachHotel : hotels) { | |
for (int i = 1; i < customer_info.size(); i++) { | |
if (customer_info[i] < 5) { | |
if (!customerType) | |
sum += eachHotel.weekday.prices["Regular"]; | |
else | |
sum += eachHotel.weekday.prices["Rewards"]; | |
} | |
else { | |
if (!customerType) | |
sum += eachHotel.weekend.prices["Regular"]; | |
else | |
sum += eachHotel.weekend.prices["Rewards"]; | |
} | |
} | |
scores[eachHotel.name] = sum; | |
sum = 0; | |
} | |
Hotel null("null"); | |
pair<Hotel, int> min = { null,0 }; | |
unordered_map<Hotel, int>::iterator it; | |
for (it; it != scores.end(); it++) { | |
if (it->second < min.second) { | |
min.first = it->first; | |
min.second = it->second; | |
} | |
else if (it->second == min.second) { | |
if (it->first.rating > min.first.rating) { | |
min.first = it->first; | |
min.second = it->second; | |
} | |
} | |
} | |
return min.first.name; | |
} | |
int main() { | |
Day weekday; | |
weekday.prices["regular"] = 110; | |
weekday.prices["rewards"] = 80; | |
Day weekend; | |
weekend.prices["regular"] = 90; | |
weekend.prices["rewards"] = 80; | |
Hotel lakewood("lakewood"); | |
lakewood.weekend = weekend; | |
lakewood.weekday = weekday; | |
lakewood.rating = 4; | |
Day weekday1; | |
weekday1.prices["regular"] = 160; | |
weekday1.prices["rewards"] = 110; | |
Day weekend1; | |
weekend1.prices["regular"] = 60; | |
weekend1.prices["rewards"] = 50; | |
Hotel Bridgewood("bridgewood"); | |
Bridgewood.weekend = weekend; | |
Bridgewood.weekday = weekday; | |
Bridgewood.rating = 4; | |
Day weekday2; | |
weekday2.prices["regular"] = 220; | |
weekday2.prices["rewards"] = 100; | |
Day weekend2; | |
weekend2.prices["regular"] = 150; | |
weekend2.prices["rewards"] = 40; | |
Hotel ridgewood("ridgewood"); | |
ridgewood.weekend = weekend; | |
ridgewood.weekday = weekday; | |
ridgewood.rating = 4; | |
vector<Hotel> hotels = { lakewood, Bridgewood, ridgewood }; | |
string inputString = "Regular: 20Mar2009(fri), 21Mar2009(sat), 22Mar2009(sun)"; | |
vector<int> customer_info = parseInput(inputString); | |
string cheapest = cheapestHotel(hotels, customer_info); | |
cout << "cheapest: " << cheapest << endl; | |
/*INPUT FORMAT : | |
Customer Types: Regular, Rewards | |
Lakewood - | |
weekday - regular (110), rewards (80) | |
weekend - regular ( 90) , rewards (80) | |
rating - 3 | |
Bridgewood - | |
weekday - regular (160), rewards (110) | |
weekend - regular ( 60) , rewards (50) | |
rating - 4 | |
Ridgewood - | |
weekday - regular (220), rewards (100) | |
weekend - regular ( 150) , rewards (40) | |
rating - 5 | |
IN: Regular : 20Mar2009(fri), 21Mar2009(sat), 22Mar2009(sun) | |
Lakewood with a rating of 3 has weekday rates as 110$ for regular customer and 80$ for rewards customer. | |
The weekend rates are 90$ for regular customer and 80$ for a rewards customer. | |
<customer_type> : <date1>, <date2>, <date3>, ... | |
OUTPUT FORMAT : | |
<name_of_the_cheapest_hotel> | |
INPUT 1 : | |
Regular : 16Mar2009(mon), 17Mar2009(tue), 18Mar2009(wed) | |
OUTPUT 1 : | |
Lakewood | |
INPUT 2 : | |
Regular : 20Mar2009(fri), 21Mar2009(sat), 22Mar2009(sun) | |
OUTPUT 2 : | |
Bridgewood | |
INPUT 3 : | |
Rewards : 26Mar2009(thu), 27Mar2009(fri), 28Mar2009(sat) | |
OUTPUT 3 : | |
Ridgewood*/ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment