fork download
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <numeric>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. class HexPic {
  11. protected:
  12. std::string hex_to_string(unsigned int value) {
  13. return std::bitset<8>(value).to_string();
  14. }
  15.  
  16. template <typename Iter>
  17. void binary_to_stream(Iter begin, Iter end, std::ostream& stream) const {
  18. char on = display_on_;
  19. char off = display_off_;
  20.  
  21. if (inverted_) std::swap(on, off);
  22.  
  23. std::string temp = std::accumulate(begin, end, std::string(""),
  24. [&](std::string& res, char ch) {
  25. return res += std::string(static_cast<int>(magnification_), (ch == '0' ? off : on));
  26. });
  27.  
  28. for (int i = 0; i < magnification_; ++i) {
  29. stream << temp << '\n';
  30. }
  31. }
  32.  
  33.  
  34. public:
  35. enum RotateDirection { top, right, bottom, left };
  36.  
  37. friend HexPic::RotateDirection& operator++(HexPic::RotateDirection& r) {
  38. if (static_cast<int>(r) == 3) return r = HexPic::RotateDirection(0);
  39. return r = HexPic::RotateDirection((static_cast<int>(r) + 1));
  40. }
  41.  
  42. friend HexPic::RotateDirection& operator--(HexPic::RotateDirection& r) {
  43. if (static_cast<int>(r) == 0) return r = HexPic::RotateDirection(3);
  44. return r = HexPic::RotateDirection((static_cast<int>(r) - 1));
  45. }
  46.  
  47. explicit HexPic() : inverted_{false}, magnification_{1.0},
  48. direction_{RotateDirection::top},
  49. display_on_{'X'}, display_off_{'.'} {}
  50.  
  51. void add_hexrow(const unsigned int hexval) {
  52. bitmap_.push_back(hex_to_string(hexval));
  53. }
  54.  
  55. void draw(std::ostream& stream) const {
  56. switch (direction_) {
  57. case top: {
  58. for (const auto& row : bitmap_) {
  59. binary_to_stream(row.begin(), row.end(), stream);
  60. }
  61. break;
  62. }
  63. case right: {
  64. std::vector<std::string> columns(bitmap_[0].size());
  65.  
  66. std::for_each(bitmap_.rbegin(),
  67. bitmap_.rend(),
  68. [&](const std::string& row) {
  69. std::transform(row.begin(),
  70. row.end(),
  71. columns.begin(),
  72. columns.begin(),
  73. [](char ch, std::string str) {
  74. return str + ch;
  75. });
  76. });
  77.  
  78. for (const auto& col : columns) {
  79. binary_to_stream(col.begin(), col.end(), stream);
  80. }
  81.  
  82. break;
  83. }
  84. case bottom: {
  85. std::for_each(bitmap_.rbegin(),
  86. bitmap_.rend(),
  87. [&](const std::string& row) {
  88. binary_to_stream(row.rbegin(), row.rend(), stream);
  89. });
  90. break;
  91. }
  92. case left: {
  93. std::vector<std::string> columns(bitmap_[0].size());
  94.  
  95. std::for_each(bitmap_.begin(),
  96. bitmap_.end(),
  97. [&](const std::string& row) {
  98. std::transform(row.rbegin(),
  99. row.rend(),
  100. columns.begin(),
  101. columns.begin(),
  102. [](char ch, std::string str) {
  103. return str + ch;
  104. });
  105. });
  106.  
  107. for (const auto& col : columns) {
  108. binary_to_stream(col.begin(), col.end(), stream);
  109. }
  110.  
  111. break;
  112. }
  113. }
  114.  
  115. stream << '\n';
  116. }
  117.  
  118. void rotate_cw() {
  119. ++direction_;
  120. }
  121.  
  122. void rotate_ccw() {
  123. --direction_;
  124. }
  125.  
  126. void zoom(double multiplier) {
  127. magnification_ = std::max(1.0, magnification_ * multiplier);
  128. }
  129.  
  130. void invert_display_bits() {
  131. inverted_ = !inverted_;
  132. }
  133.  
  134. friend std::istream& operator>>(std::istream& stream, HexPic& pic) {
  135. HexPic temp;
  136. std::string input;
  137. std::getline(stream, input);
  138. std::istringstream iss(input);
  139.  
  140. unsigned int value;
  141. while (iss >> std::hex >> value) {
  142. temp.add_hexrow(value);
  143. }
  144.  
  145. pic = temp;
  146. return stream;
  147. }
  148.  
  149. private:
  150. std::vector<std::string> bitmap_;
  151. RotateDirection direction_;
  152. char display_off_;
  153. char display_on_;
  154. bool inverted_;
  155. double magnification_;
  156. };
  157.  
  158. int main() {
  159. std::vector<HexPic> pics((std::istream_iterator<HexPic>(std::cin)),
  160. std::istream_iterator<HexPic>());
  161.  
  162. for (auto& pic : pics) {
  163. pic.draw(std::cout);
  164.  
  165. pic.zoom(2);
  166. pic.rotate_cw();
  167. pic.zoom(2);
  168. pic.invert_display_bits();
  169. pic.zoom(0.5);
  170. pic.draw(std::cout);
  171. }
  172. }
Success #stdin #stdout 0s 3448KB
stdin
FF 81 BD A5 A5 BD 81 FF
AA 55 AA 55 AA 55 AA 55
3E 7F FC F8 F8 FC 7F 3E
93 93 93 F3 F3 93 93 93
stdout
XXXXXXXX
X......X
X.XXXX.X
X.X..X.X
X.X..X.X
X.XXXX.X
X......X
XXXXXXXX

................
................
..XXXXXXXXXXXX..
..XXXXXXXXXXXX..
..XX........XX..
..XX........XX..
..XX..XXXX..XX..
..XX..XXXX..XX..
..XX..XXXX..XX..
..XX..XXXX..XX..
..XX........XX..
..XX........XX..
..XXXXXXXXXXXX..
..XXXXXXXXXXXX..
................
................

X.X.X.X.
.X.X.X.X
X.X.X.X.
.X.X.X.X
X.X.X.X.
.X.X.X.X
X.X.X.X.
.X.X.X.X

XX..XX..XX..XX..
XX..XX..XX..XX..
..XX..XX..XX..XX
..XX..XX..XX..XX
XX..XX..XX..XX..
XX..XX..XX..XX..
..XX..XX..XX..XX
..XX..XX..XX..XX
XX..XX..XX..XX..
XX..XX..XX..XX..
..XX..XX..XX..XX
..XX..XX..XX..XX
XX..XX..XX..XX..
XX..XX..XX..XX..
..XX..XX..XX..XX
..XX..XX..XX..XX

..XXXXX.
.XXXXXXX
XXXXXX..
XXXXX...
XXXXX...
XXXXXX..
.XXXXXXX
..XXXXX.

XXXX........XXXX
XXXX........XXXX
XX............XX
XX............XX
................
................
................
................
................
................
......XXXX......
......XXXX......
....XXXXXXXX....
....XXXXXXXX....
XX..XXXXXXXX..XX
XX..XXXXXXXX..XX

X..X..XX
X..X..XX
X..X..XX
XXXX..XX
XXXX..XX
X..X..XX
X..X..XX
X..X..XX

................
................
XXXXXX....XXXXXX
XXXXXX....XXXXXX
XXXXXX....XXXXXX
XXXXXX....XXXXXX
................
................
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
................
................
................
................