Created
July 31, 2023 23:28
-
-
Save pyoneerC/0ffff7e39300c6a68ccff1410fbc2d04 to your computer and use it in GitHub Desktop.
CT1M String Generator
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 <iostream> | |
#include <string> | |
#include <limits> | |
std::string getPalindromeSum(int num) { | |
if (1 == num) { | |
return "0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + " | |
+ std::to_string(num) + | |
" + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 2) && (num <= 10)) { | |
num -= 2; | |
return "0 + 0 + 0 + 0 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + " | |
+ std::to_string(num) + | |
" + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 11) && (num <= 100)) { | |
num -= 11; | |
return "0 + 0 + 0 + 0 + 2.5 + 2 + 1 + " | |
+ std::to_string(num) + | |
" + 1 + 2 + 2.5 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 101) && (num <= 1'000)) { | |
num -= 101; | |
return "0 + 0 + 0 + 0 + 40.5 + 2 + 2 + 2 + 2 + 2 + " | |
+ std::to_string(num) + | |
" + 2 + 2 + 2 + 2 + 2 + 40.5 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 1'001) && (num <= 10'000)) { | |
num -= 1'001; | |
return "0 + 0 + 1 + 0 + 0 + 74.1 + 99.4 + 299 " | |
"+ 1 + 26 + " + std::to_string(num) + | |
" + 26 + 1 + 299 + 99.4 + 74.1 + 0 + 0 + 1 + 0 + 0"; | |
} else if ((num >= 10'001) && (num <= 50'000)) { | |
num -= 9'974; | |
return | |
"0 + 0 + 0 + 0 + 1 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 999 +" | |
" 999 + 999 + 333 + 333 + 0 + 0 + 0 + 222 + 37 + 37 + 30 + 7 + " + | |
std::to_string(num) + | |
" + 7 + 30 + 37 + 37 + 222 + 0 + 0 + 0 + 333 + 333 + 999 + 999 + 999 + 99 " | |
"+ 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 1 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 50'001) && (num <= 100'000)) { | |
num -= 9'974; | |
return | |
"0 + 0 + 0 + 0 + 0 + 0 + 1 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 999 +" | |
" 999 + 999 + 333 + 333 + 222 + 37 + 37 + 15 + 7 + 15 + " + | |
std::to_string(num) + | |
" + 15 + 7 + 15 + 37 + 37 + 222 + 333 + 333 + 999 + 999 + 999 + 99 " | |
"+ 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 1 + 0 + 0 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 100'001) && (num <= 500'000)) { | |
num -= 9'974; | |
return | |
"0 + 0 + 0 + 0 + 1 + 23 + 0 + 1 + 75 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 999 +" | |
" 999 + 999 + 333 + 333 + 222 + 37 + 37 + 30 + 7 + " + | |
std::to_string(num) + | |
" + 7 + 30 + 37 + 37 + 222 + 333 + 333 + 999 + 999 + 999 + 99 " | |
"+ 99 + 99 + 99 + 99 + 99 + 99 + 99 + 99 + 75 + 1 + 0 + 23 + 1 + 0 + 0 + 0 + 0"; | |
} else if ((num >= 500'001) && (num <= 1'000'000)) { | |
num -= 100'001; | |
return | |
"0 + 0 + 0 + 0 + 0 + 0.5 + 14909 + 14909 + 6969 + 4693 + 850 + 850 + 1700 +" | |
" 1700 + 1700 + 340 + 340 + 340 + 340 + 340 + 10 + 5 + 2.5 + 2.5 + " + | |
std::to_string(num) + | |
" + 2.5 + 2.5 + 5 + 10 + 340 + 340 + 340 + 340 + 340 + 1700 + 1700 + 1700 + 850 + 850 +" | |
" 4693 + 6969 + 14909 + 14909 + 0.5 + 0 + 0 + 0 + 0 + 0"; | |
} | |
return "Number must be between 1 and 1'000'000"; | |
} | |
[[nodiscard]] bool isValidInput(const int num, const int minRange, | |
const int maxRange) { | |
return ((num >= minRange) && (num <= maxRange)); | |
} | |
[[nodiscard]] bool isPalindrome(int num) { | |
int reversedNum = 0; | |
const int originalNum = num; | |
while (num > 0) { | |
const int digit = num % 10; | |
reversedNum = ((reversedNum * 10) + digit); | |
num /= 10; | |
} | |
return (originalNum == reversedNum); | |
} | |
int main() { | |
int input{}; | |
const int minRange = 1; | |
const int maxRange = 1'000'000; | |
std::cout << "Enter numbers between 1 and 1,000,000 until EOF:\n"; | |
while (std::cin >> input) { | |
if ((!std::cin.fail()) && (::isValidInput(input, minRange, maxRange))) { | |
if ((input >= 100) && (::isPalindrome(input))) { | |
std::cout << input << " is palindrome :)" << '\n'; | |
} | |
std::cout << input << " -> " << ::getPalindromeSum(input) << '\n'; | |
} else { | |
std::cout << "Invalid input. Please enter a valid number between " | |
<< minRange << " and " << maxRange << ".\n"; | |
std::cin.clear(); | |
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | |
} | |
} | |
if (std::cin.eof()) { | |
std::cout << "EOF encountered. Exiting the program.\n"; | |
} else { | |
std::cout << "An error occurred while reading input. " | |
"Exiting the program.\n"; | |
} | |
return 0; | |
} | |
////Better C++ Discord Coding competition: CT1M String Generator | |
////Input(stdin): an integer in the range [1,1 000 000] | |
////Output(stdout): an expression that evaluates to the number and | |
/// can be recognized by the bot in count-to-a-million, while also | |
/// being a palindrome.(e.g. count-to-a-million) | |
////Repeat the input/output loop until EOF is encountered. Inputs | |
/// are delimited by a single \n. | |
////Use either C++ or Python. | |
////Restrictions: | |
////C++ code: | |
////Must compile in g++ with -std=c++23 and -m64. Warnings are | |
/// allowed. | |
////Relying on g++-specific behavior when the standard says | |
/// something is "implementation-defined" is allowed. | |
////The program must be conforming. | |
////Language-independent restrictions: | |
////Must run in a reasonable amount of time(max. 5 seconds to | |
/// evaluate all of 1-1 000 000) | |
////Must NOT do I/O except for the required inputs and outputs. | |
////Must NOT intentionally lag/harm/abuse the host computer, | |
/// including but not limited to: spawning other processes, | |
/// using too many threads, creating popups, creating a virus, | |
/// using system calls. | |
////Must use less than 4GB of RAM. | |
////Ranking: | |
////The program will be run with all numbers from 1 to one | |
/// million(inclusive). Programs are sorted by their average | |
/// output length over all inputs(shorter=better). | |
////Judge machine(mine): Windows 10 with MSYS2 Mingw64 G++ | |
/// 13.1.0 and CPython 3.10.10 | |
////Submissions are accepted until 8/1/2023 UTC.(MM/DD/YYYY) | |
//// Example input/output: | |
//// 696969 -> 0 + 0 + 0.5 + 14909 + 14909 + 6969 + 4693 + | |
/// 850 + 850 + 1700 + 1700 + 1700 + 1700 + 10 + 5 + 2.5 + | |
/// 2.5 + 596968 + 2.5 + 2.5 + 5 + 10 + 1700 + 1700 + 1700 | |
/// + 1700 + 850 + 850 + 4693 + 6969 + 14909 + 14909 + 0.5 + 0 + 0 | |
////Outputs must be on individual lines as well. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment