This is a not so complicated C++ program (yes, though you do not see {} for scoping and [] for indexing!). Please take a moment and read through the program and findout the password to unlock it :stuck_out_tongue_winking_eye:

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
 %:include <iostream>
 %:include <array>
 %:include <algorithm>

 #define INITIALIZE_LOGGER(name, stream)                                        \
     void log_##name(std::string arg) <%                                        \
         stream << "[" << #name << "]: " << arg << '\n';                        \
     %>

 INITIALIZE_LOGGER(treasure, std::cout);
 INITIALIZE_LOGGER(info, std::cout);
 INITIALIZE_LOGGER(error, std::cerr);

 std::array<int, 10> differences<%65, 43, 3, -7, 7, -2, 2, 3, -17, -64%>;

 struct Key <%
     std::string password;
     bool unlock();

     Key(std::string pass) : password(pass)<%%>;
     compl Key() = default;
     Key(const Key bitand) = delete;

   private:
     bool checkLength();
     bool checkSum();
     bool checkDifferences();
 %>;

 bool Key::checkLength() <%
     if (password.length() not_eq 10) <%
         log_error("Password length does not match: " +
                   std::to_string(password.length()));
         return false;
     %>

     log_info("Password matches the length!");
     return true;
 %>

 bool Key::checkSum() <%
     struct Sum <%
         void operator()(char i) <%
             auto get_ascii = <:bitand:>() <% return int(i); %>;
             value += get_ascii();
         %>

         int value<%0%>;
     %>;

     Sum s = std::for_each(password.begin(), password.end(), Sum());

     if (s.value not_eq 963) <%
         log_error("Password has invalid sum: " + std::to_string(s.value));
         return false;
     %>

     log_info("Password has the correct sum!");
     return true;
 %>

 bool Key::checkDifferences() <%
     auto get_ascii = <::>(char i) <% return int(i); %>;

     for (unsigned i = 1; i < password.length(); i++) <%
         int diff = get_ascii(password[i]) - get_ascii(password[i - 1]);
         if (diff not_eq differences[i]) <%
             return false;
         %>
     %>

     log_info("Password has the correct differences!");
     return true;
 %>

 bool Key::unlock() <%
     if (checkLength() and checkSum() and checkDifferences()) <%
         log_treasure("Hello world :P");
         return true;
     %>

     return false;
 %>

 int main(int argc, char **argv) <%
     Key("").unlock();
     return 0;
 %>

Okay! You really are interested in knowing the password. Let me give you a hint, if you are a Potterhead, you should know how to unlock a door!!! (If you are not a Potterhead, then Google will definetly help you :smile:!)

Peculiariaties

You might be already wondering how this program works without the scoping ({}) and indexing ([]) characters. Also, why do we see Python-like syntax in conditionals :thinking: (for example, if (checkLength() and checkSum() and checkDifferences()) or if (s.value not_eq 963)). What the heck are %:, <%, %>, <:, :>, compl, bitand, and, not_eq and ## in the macro INITIALIZE_LOGGER :fearful:!!! The explanation is as follows,

Alternative Operators (<%, %>, <:, :>, compl, bitand)

These are the alternative operator representations in C++. This allows programmers to use these alternative representations to represent the characters that are not present in the character set (for example, DIN 66003). The following table shows the primary representation of the alternative representations used in the program:

Alternative Primary
%: #
<% {
%> }
<: [
:> ]
compl ~
bitand &
and &&
not_eq !=
   

Hopefully, now you can imagine how the ususal version of this program looks like :slightly_smiling_face:!

Concatenation or Token Pasting (##)

Let us look into the INITIALIZE_LOGGER macro,

/// syntax:
/// #define identifier(parameters) replacement-list
#define INITIALIZE_LOGGER(name, stream)                                        \
    void log_##name(std::string arg) <%                                        \
        stream << "[" << #name << "]: " << arg << '\n';                        \
    %>

A macro in C++ just replaces the identifier with the corresponding text. C++ allows macros to take parameters (function-like macros). In the above example, we have a function-like macro which contains a peculiar operator, ##. This operator is used for concatenation of two identifiers in the replacement-list with the corresponding parameter. Let us consider the first call to the macro in the program,

INITIALIZE_LOGGER(treasure, std::cout);

Here, the macro INITIALIZE_LOGGER is called with two parameters, ‘treasure’ as name and ‘std::cout’ as stream. During the macro expansion, the preprocessor looks for the identifier after ## in the parameter list and replaces it with its value. Therefore, the macro expands as follows,

void log_treasure(std::string arg) <%
    stream << "[" << "treasure" << "]: " << arg << '\n';
%>

Password

If you have already read until here, firstly, I thank you :smiley:. Secondly, I will give you the password.

Did you try typing in the spell (suggested by Google? :stuck_out_tongue_winking_eye:) And it did not work right! Of course there is some case sensitive magic going on here! Let us try to decipher the password!

So the spell to unlock a door in is alohomora. The length of this spell is 9 characters. But the program mandates the password to be 10 characters long. You can use the array differences to find out what the last character is. The array contains the difference between each character with the previous character except for the first one. So reversing this computation, should give you the ASCII value of the 10th character. You can also already see the ASCII value of first character in the same array, which is, 65.

So, our password to unlock the program is “Alohomora!” :joy:

You can see the output of the program in this link. Thanks again for reading!!!