Skip to content

Instantly share code, notes, and snippets.

@haseeb-heaven
Created July 8, 2025 10:20
Show Gist options
  • Save haseeb-heaven/c3798aa0fd75911c41109c002499ae33 to your computer and use it in GitHub Desktop.
Save haseeb-heaven/c3798aa0fd75911c41109c002499ae33 to your computer and use it in GitHub Desktop.
Clever way of Gemini pro to use factory map for print ASCII art from input.
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <functional>
#include <stdexcept>
#include <algorithm>
#include <utility>
#include <cctype>
class AsciiArt {
public:
explicit AsciiArt(std::vector<std::string> artLinesVector = {})
: artLines(std::move(artLinesVector)) {
artHeight = artLines.size();
artWidth = 0;
if (artHeight > 0) {
for (const auto& line : artLines) {
if (line.length() > artWidth) {
artWidth = line.length();
}
}
}
}
const std::vector<std::string>& getLines() const { return artLines; }
size_t getWidth() const { return artWidth; }
size_t getHeight() const { return artHeight; }
private:
std::vector<std::string> artLines;
size_t artWidth = 0;
size_t artHeight = 0;
};
std::ostream& operator<<(std::ostream& outputStream, const AsciiArt& artToPrint) {
for (const auto& line : artToPrint.getLines()) {
outputStream << line << '\n';
}
return outputStream;
}
AsciiArt operator+(const AsciiArt& leftHandSide, const AsciiArt& rightHandSide) {
std::vector<std::string> combinedLines;
const size_t maxHeight = std::max(leftHandSide.getHeight(), rightHandSide.getHeight());
combinedLines.reserve(maxHeight);
for (size_t i = 0; i < maxHeight; ++i) {
std::string leftPart = (i < leftHandSide.getHeight()) ? leftHandSide.getLines()[i] : std::string(leftHandSide.getWidth(), ' ');
std::string rightPart = (i < rightHandSide.getHeight()) ? rightHandSide.getLines()[i] : std::string(rightHandSide.getWidth(), ' ');
if (leftPart.length() < leftHandSide.getWidth()) {
leftPart.append(leftHandSide.getWidth() - leftPart.length(), ' ');
}
combinedLines.push_back(leftPart + rightPart);
}
return AsciiArt(combinedLines);
}
namespace ArtFactory {
using ArtCreator = std::function<AsciiArt()>;
const std::map<char, ArtCreator> artMap = {
// Uppercase
{'A', []{ return AsciiArt({" # ", " # # ", " # # ", "#######", "# #", "# #", "# #"}); }},
{'B', []{ return AsciiArt({"###### ", "# ##", "# ##", "###### ", "# ##", "# ##", "###### "}); }},
{'C', []{ return AsciiArt({" #### ", " ## ##", "## ", "## ", "## ", " ## ##", " #### "}); }},
{'D', []{ return AsciiArt({"##### ", "## ## ", "## ##", "## ##", "## ##", "## ## ", "##### "}); }},
{'E', []{ return AsciiArt({"#######", "## ", "## ", "###### ", "## ", "## ", "#######"}); }},
{'F', []{ return AsciiArt({"#######", "## ", "## ", "###### ", "## ", "## ", "## "}); }},
{'G', []{ return AsciiArt({" ##### ", "## ##", "## ", "## ####", "## ##", "## ##", " ##### "}); }},
{'H', []{ return AsciiArt({"# #", "# #", "# #", "#######", "# #", "# #", "# #"}); }},
{'I', []{ return AsciiArt({"#######", " ### ", " ### ", " ### ", " ### ", " ### ", "#######"}); }},
{'J', []{ return AsciiArt({" ####", " ###", " ###", " ###", "## ###", "## ###", " #### "}); }},
{'K', []{ return AsciiArt({"# ##", "# ## ", "# ## ", "### ", "# ## ", "# ## ", "# ##"}); }},
{'L', []{ return AsciiArt({"## ", "## ", "## ", "## ", "## ", "## ", "#######"}); }},
{'M', []{ return AsciiArt({"# #", "## ##", "# # # #", "# # #", "# #", "# #", "# #"}); }},
{'N', []{ return AsciiArt({"# #", "## #", "# # #", "# # #", "# # #", "# ##", "# #"}); }},
{'O', []{ return AsciiArt({" ### ", " # # ", "# #", "# #", "# #", " # # ", " ### "}); }},
{'P', []{ return AsciiArt({"###### ", "## ##", "## ##", "###### ", "## ", "## ", "## "}); }},
{'Q', []{ return AsciiArt({" ##### ", "## ##", "## ##", "## ##", "## # #", "## # ", " #### #"}); }},
{'R', []{ return AsciiArt({"###### ", "## ##", "## ##", "###### ", "## ## ", "## ##", "## ##"}); }},
{'S', []{ return AsciiArt({" #### ", "## #", "## ", " ### ", " ##", "# ##", " #### "}); }},
{'T', []{ return AsciiArt({"#######", "#######", " ### ", " ### ", " ### ", " ### ", " ### "}); }},
{'U', []{ return AsciiArt({"# #", "# #", "# #", "# #", "# #", " # # ", " ### "}); }},
{'V', []{ return AsciiArt({"# #", "# #", "# #", " # # ", " # # ", " # # ", " # "}); }},
{'W', []{ return AsciiArt({"# #", "# #", "# # #", "# # # #", "## ##", "## ##", "# #"}); }},
{'X', []{ return AsciiArt({"# #", " # # ", " # # ", " # ", " # # ", " # # ", "# #"}); }},
{'Y', []{ return AsciiArt({"# #", " # # ", " # # ", " # ", " # ", " # ", " # "}); }},
{'Z', []{ return AsciiArt({"#######", " ##", " ## ", " ## ", " ## ", " ## ", "#######"}); }},
// Lowercase
{'a', []{ return AsciiArt({" ", " ", " #### ", " # ##", " #####", "## ##", " ##### "}); }},
{'b', []{ return AsciiArt({"# ", "# ", "# #### ", "## ##", "# ##", "# ##", "###### "}); }},
{'c', []{ return AsciiArt({" ", " ", " #### ", " ## ", " ## ", " ## ", " #### "}); }},
{'d', []{ return AsciiArt({" # ", " ##", " #### #", "## ##", "## ##", "## ##", " ##### "}); }},
{'e', []{ return AsciiArt({" ", " ", " ##### ", "## ##", "#######", "## ", " ##### "}); }},
{'f', []{ return AsciiArt({" ## ", " # ", " #### ", " # ", " # ", " # ", " # "}); }},
{'g', []{ return AsciiArt({" ", " ##### ", "## ##", "## ##", " ##### ", " ##", " ##### "}); }},
{'h', []{ return AsciiArt({"# ", "# ", "## ### ", "### ##", "# ##", "# ##", "# ##"}); }},
{'i', []{ return AsciiArt({" ### ", " ", " ### ", " ### ", " ### ", " ### ", " ### "}); }},
{'j', []{ return AsciiArt({" ### ", " ", " ### ", " ### ", " ### ", "## ### ", " #### "}); }},
{'k', []{ return AsciiArt({"# ", "# ", "# ## ", "# ## ", "### ", "# ## ", "# ## "}); }},
{'l', []{ return AsciiArt({" ### ", " ### ", " ### ", " ### ", " ### ", " ### ", " ## "}); }},
{'m', []{ return AsciiArt({" ", " ", "## # ##", "### ###", "# # # #", "# # # #", "# # # #"}); }},
{'n', []{ return AsciiArt({" ", " ", "## ### ", "### ##", "# ##", "# ##", "# ##"}); }},
{'o', []{ return AsciiArt({" ", " ", " ##### ", "## ##", "## ##", "## ##", " ##### "}); }},
{'p', []{ return AsciiArt({" ", "###### ", "## ##", "## ##", "###### ", "## ", "## "}); }},
{'q', []{ return AsciiArt({" ", " ##### ", "## ##", "## ##", " ##### ", " ####", " ###"}); }},
{'r', []{ return AsciiArt({" ", " ", "## ### ", "### ##", "# ", "# ", "# "}); }},
{'s', []{ return AsciiArt({" ", " ", " ##### ", "## ", " ##### ", " ##", "##### "}); }},
{'t', []{ return AsciiArt({" # ", " # ", " #### ", " # ", " # ", " # # ", " ## "}); }},
{'u', []{ return AsciiArt({" ", " ", "# ##", "# ##", "# ##", "## ##", " ##### "}); }},
{'v', []{ return AsciiArt({" ", " ", "# #", "# #", " # # ", " # # ", " # # "}); }},
{'w', []{ return AsciiArt({" ", " ", "# #", "# # # #", "# # # #", "## ##", "# #"}); }},
{'x', []{ return AsciiArt({" ", " ", "# ##", " # # ", " ## ", " # # ", "# ##"}); }},
{'y', []{ return AsciiArt({" ", "# #", " # # ", " # # ", " # ", " # ", " # "}); }},
{'z', []{ return AsciiArt({" ", " ", "#######", " ## ", " ## ", " ## ", "#######"}); }},
// Numbers
{'0', []{ return AsciiArt({" ##### ", "## ##", "## # ##", "## # ##", "### ##", "## ##", " ##### "}); }},
{'1', []{ return AsciiArt({" ## ", " ### ", " ## ", " ## ", " ## ", " ## ", "###### "}); }},
{'2', []{ return AsciiArt({" ##### ", "## ##", " ## ", " ## ", " ## ", " ## ", "#######"}); }},
{'3', []{ return AsciiArt({" ##### ", "## ##", " ## ", " #### ", " ## ", "## ##", " ##### "}); }},
{'4', []{ return AsciiArt({"## ##", "## ##", "## ##", "#######", " ##", " ##", " ##"}); }},
{'5', []{ return AsciiArt({"#######", "## ", "###### ", " ##", " ##", "## ##", " ##### "}); }},
{'6', []{ return AsciiArt({" ##### ", "## ", "## ", "###### ", "## ##", "## ##", " ##### "}); }},
{'7', []{ return AsciiArt({"#######", "## ##", " ## ", " ## ", " ## ", " ## ", "## "}); }},
{'8', []{ return AsciiArt({" ##### ", "## ##", "## ##", " ##### ", "## ##", "## ##", " ##### "}); }},
{'9', []{ return AsciiArt({" ##### ", "## ##", "## ##", " ######", " ##", " ##", " ##### "}); }},
{' ', []{ return AsciiArt({" ", " ", " ", " ", " ", " ", " "}); }}
};
AsciiArt createArtForChar(char character) {
auto it = artMap.find(character);
if (it != artMap.end()) {
return it->second();
}
throw std::runtime_error("Unsupported character in input string: " + std::string(1, character));
}
}
AsciiArt createArtFromString(const std::string& message) {
if (message.empty()) {
return AsciiArt();
}
AsciiArt finalArt;
bool firstChar = true;
for (char character : message) {
AsciiArt charArt = ArtFactory::createArtForChar(character);
if (firstChar) {
finalArt = charArt;
firstChar = false;
} else {
finalArt = finalArt + charArt;
}
}
return finalArt;
}
int main() {
std::string userInput;
std::cout << "Message for ASCII art: ";
std::getline(std::cin, userInput);
try {
AsciiArt finalArt = createArtFromString(userInput);
std::cout << "===================================================================================================\n";
std::cout << finalArt;
std::cout << "===================================================================================================\n";
} catch (const std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Please use only supported characters (A-Z, a-z, 0-9, space)." << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment