
2023 · Task 2
Christmas Letters
Ivan's mum Marinka has worked for Santa for years. Since she had decided to make a mountain of sarma (cabbage rolls) this year, she hoped Santa would give her a few days off around Christmas, but he wasn't having any of it. Apparently the happiness of a bunch of random children matters more than her Ivan's.
To teach him a lesson, Marinka decided to encrypt every letter Santa receives and then base64-encode it (see the code example below), and let him struggle with the decryption. Everyone knows the internet at the North Pole is dreadful, so Marinka wants the letters to be as short as possible, letting her sneak off home from work a little earlier. Programming was never her strong point, so she is asking you for help.
You are a good friend of Marinka's and don't want to let her down, but you also have the well-being of children all over the world at heart. Caught in this conflict of interest, you have decided to write a program that can run in two modes:
- compress (compresses the input text) - a mode that compresses (encrypts) the input file and grants Marinka's wish
- decompress (unpacks the compressed text) - a mode that decompresses (decrypts) the input file (which you will secretly hand to Santa once he gets desperate)
You have at your disposal a small portion of the text of all the letters written to Santa this year, from which you can try to extract statistics. For some reason unknown to you (and to us), the letters contain LaTeX formulas. Assume that the rest of the letters' text behaves the same way as your sample.
Example program
You may use any of these languages: C++, Java, Python, C...
import base64
mode = input()
if mode == "compress":
data = input()
data = base64.b64decode(data.encode()).decode() # this converts the input into LaTeX form
# compress data
print(base64.b64encode(data.encode()).decode(), flush=True)
elif mode == "decompress":
data = input()
data = base64.b64decode(data.encode()).decode()
# decompress data
print(base64.b64encode(data.encode()).decode(), flush=True)
# YOU MUST ADD THIS AT THE END:
while True:
input()
Input
The first line contains the mode - "compress" or "decompress".
It is followed by the base64-encoded text of the letters that you need to compress / decompress (the size of the text is not known).
Output
Print the base64-encoded text after compressing / decompressing (depending on the mode). The better the compression, i.e. the smaller the compressed text, the higher your score
Scoring
Note: You may NOT use any compression libraries (e.g. gzip). Your final score is based on the length of the compressed file in bytes (shorter is better). Naturally, it is a requirement that text compressed in “compress” mode can be unpacked back into the original text using “decompress” mode. Otherwise, your solution to this problem is not valid.