-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfinite_monkey.cpp
105 lines (80 loc) · 3.15 KB
/
Infinite_monkey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <random>
#include <deque>
void monkeyTypewriter(const std::string& sentence, double typingSpeed);
int main()
{
std::string sentence = "Hello, World!";
double typingSpeed = 100;
monkeyTypewriter(sentence, typingSpeed);
return 0;
}
void monkeyTypewriter(const std::string& sentence, double typingSpeed)
{
if (sentence.empty()) {
std::cout << "Error: Input sentence cannot be empty." << std::endl;
return;
}
std::deque<char> buffer;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(' ', '~'); // its common knowledge that monkeys can type only printable ASCII characters
auto start_time = std::chrono::steady_clock::now();
double sleep_ms = 1000.0 / typingSpeed;
unsigned long long total_attempts = 0;
size_t sentence_length = sentence.size();
while (true)
{
char j = static_cast<char>(distr(gen));
total_attempts++;
buffer.push_back(j);
if (buffer.size() > sentence_length)
{
buffer.pop_front();
}
std::cout << "\r";
for (char c : buffer)
std::cout << c;
std::flush(std::cout);
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(sleep_ms)));
if (std::string(buffer.begin(), buffer.end()) == sentence)
{
break;
}
}
auto end_time = std::chrono::steady_clock::now();
std::cout << std::endl;
std::chrono::duration<double> elapsed_seconds = end_time - start_time;
double elapsed_time = elapsed_seconds.count(); // in seconds
// Function to format time into years, months, days, hours, minutes, seconds that i stole from github
auto format_time = [](double total_seconds)
{
int years = static_cast<int>(total_seconds / (365 * 24 * 3600));
total_seconds = std::fmod(total_seconds, 365 * 24 * 3600);
int days = static_cast<int>(total_seconds / (24 * 3600));
total_seconds = std::fmod(total_seconds, 24 * 3600);
int hours = static_cast<int>(total_seconds / 3600);
total_seconds = std::fmod(total_seconds, 3600);
int minutes = static_cast<int>(total_seconds / 60);
total_seconds = std::fmod(total_seconds, 60);
int seconds = static_cast<int>(total_seconds);
std::string result;
if (years > 0)
result += std::to_string(years) + " years ";
if (days > 0)
result += std::to_string(days) + " days ";
if (hours > 0)
result += std::to_string(hours) + " hours ";
if (minutes > 0)
result += std::to_string(minutes) + " minutes ";
if (seconds > 0 || result.empty())
result += std::to_string(seconds) + " seconds";
return result;
};
std::string formatted_elapsed_time = format_time(elapsed_time);
std::cout << "\nThe monkey typed your sentence in " << formatted_elapsed_time << "!" << std::endl;
std::cout << "The monkey tried a total of " << total_attempts << " characters." << std::endl;
}