-
Notifications
You must be signed in to change notification settings - Fork 13
/
retro-typewriter-art.cpp
69 lines (54 loc) · 1.94 KB
/
retro-typewriter-art.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
#include <algorithm>
#include <bits/stdc++.h>
static void trim(std::string &s)
{
s.erase(std::begin(s), std::find_if(std::begin(s), std::end(s), [](int ch) {
return !std::isspace(ch);
}));
s.erase(std::find_if(std::rbegin(s), std::rend(s), [](int ch) {
return !std::isspace(ch);
}).base(), std::end(s));
}
static std::vector<std::string> split(std::string s, const std::string& sep)
{
std::vector<std::string> res;
size_t pos{ 0 };
while ( std::string::npos != (pos = s.find(sep)) ) {
res.emplace_back(s.substr(0, pos));
s.erase(0, pos + std::size(sep));
}
if ( !std::empty(s) ) res.emplace_back(s);
return res;
}
static void typewrite(std::string& line) {
static const std::map<std::string, char> abbrevs = {
{ "sp", ' ' }, { "bS", '\\' }, { "sQ", '\'' }, { "nl", '\n' }
};
std::cerr << "line : " << line << '\n';
::trim(line);
if ( std::empty(line) ) { std::cout<< '\n'; return; }
for ( auto& chunk : ::split(line, " ") ) {
int nb;
std::string val;
auto first_non_digit = std::find_if(std::begin(chunk),
std::end(chunk),
[](char c){ return !std::isdigit(c); });
if ( std::end(chunk) == first_non_digit ) {
nb = stoi( std::string(std::begin(chunk), std::end(chunk) -1));
val = chunk.back();
} else {
nb = std::stoi(chunk.substr(0, *first_non_digit));
val = std::string(first_non_digit, std::end(chunk) );
}
std::cout << std::string(nb, ( std::end(abbrevs) != abbrevs.find(val) ) ? abbrevs.at(val) : val[0]);
}
std::cout << '\n';
}
int main()
{
std::string receipe;
getline(std::cin, receipe);
std::cerr << "receipe: " << receipe << '\n';
for ( auto& line : ::split(receipe, "nl") )
typewrite(line);
}