11#include < chrono>
2- #include < iomanip>
32#include < random>
4- #include < sstream>
5- #include < unordered_set>
63
74#include " baffling_birthdays.h"
85
96namespace baffling_birthdays {
107
11- bool shared_birthday (const std::vector<std::string>& birthdates) {
12- std::unordered_set<std::string> seen;
13- for (auto const & date : birthdates) {
14- if (date.size () < 10 ) continue ;
15- std::string month_day = date.substr (5 , 2 ) + " -" + date.substr (8 , 2 );
16- if (seen.count (month_day)) {
17- return true ;
18- }
19- seen.insert (month_day);
8+ Date parse_date (const std::string& iso) {
9+ Date d{0 , 0 , 0 };
10+ if (iso.size () < 10 ) return d;
11+ try {
12+ d.year = std::stoi (iso.substr (0 , 4 ));
13+ d.month = std::stoi (iso.substr (5 , 2 ));
14+ d.day = std::stoi (iso.substr (8 , 2 ));
15+ } catch (...) {
16+ d = {0 , 0 , 0 };
17+ }
18+ return d;
19+ }
20+
21+ std::string to_string (const Date& d) {
22+ std::ostringstream oss;
23+ oss << std::setw (4 ) << std::setfill (' 0' ) << d.year << ' -' << std::setw (2 )
24+ << std::setfill (' 0' ) << d.month << ' -' << std::setw (2 )
25+ << std::setfill (' 0' ) << d.day ;
26+ return oss.str ();
27+ }
28+
29+ bool shared_birthday (const std::vector<Date>& dates) {
30+ std::unordered_set<int > seen_md;
31+ seen_md.reserve (dates.size ());
32+ for (auto const & d : dates) {
33+ if (d.month < 1 || d.month > 12 || d.day < 1 || d.day > 31 ) continue ;
34+ int key = d.month * 100 + d.day ;
35+ if (seen_md.count (key)) return true ;
36+ seen_md.insert (key);
2037 }
2138 return false ;
2239}
2340
24- std::vector<std::string> random_birthdates (std::size_t group_size) {
41+ std::vector<Date> random_birthdates_dates (std::size_t group_size) {
2542 static std::mt19937_64 gen (
2643 std::chrono::system_clock::now ().time_since_epoch ().count ());
2744 std::uniform_int_distribution<int > month_dist (1 , 12 );
2845 const int days_in_month[12 ] = {31 , 28 , 31 , 30 , 31 , 30 ,
2946 31 , 31 , 30 , 31 , 30 , 31 };
30-
31- std::vector<std::string> dates;
47+ std::vector<Date> dates;
3248 dates.reserve (group_size);
3349 for (std::size_t i = 0 ; i < group_size; ++i) {
34- int month = month_dist (gen);
35- std::uniform_int_distribution<int > day_dist (1 ,
36- days_in_month[month - 1 ]);
37- int day = day_dist (gen);
38- int year = 2001 ;
39- std::ostringstream oss;
40- oss << year << " -" << std::setw (2 ) << std::setfill (' 0' ) << month << " -"
41- << std::setw (2 ) << std::setfill (' 0' ) << day;
42- dates.push_back (oss.str ());
50+ int m = month_dist (gen);
51+ std::uniform_int_distribution<int > day_dist (1 , days_in_month[m - 1 ]);
52+ dates.push_back (Date{2001 , m, day_dist (gen)});
4353 }
4454 return dates;
4555}
@@ -51,8 +61,7 @@ double estimated_probability_of_shared_birthday(std::size_t group_size) {
5161 for (std::size_t i = 0 ; i < group_size; ++i) {
5262 unique_prob *= (365.0 - static_cast <double >(i)) / 365.0 ;
5363 }
54- double shared_prob = (1.0 - unique_prob) * 100.0 ;
55- return shared_prob;
64+ return (1.0 - unique_prob) * 100.0 ;
5665}
5766
5867} // namespace baffling_birthdays
0 commit comments