You encounter an intriguing binary file
The structure of the format is as follows: the first line contains the magic number that identifies the format. In hex notation, this is c5 c7 57. The next line specifies the number of referenced objects present in the file; call this number N. The next N lines are zero-padded positive integers (each has exactly 10 digits) specifying the absolute byte offsets at which the respective objects are defined. Each object's id is inferred from the order in which the offsets are enumerated, starting from 0. This part of the format is called the reference table. It may look like this:
3
0000002135
0000024352
0000099641
The remaining part of the document contains object definitions. An object in this format can have many forms. For the purpose of this task, we will define only a subset of them.
Objects come in two categories: atomic and composed. Atomic objects include:
- string is a sequence of characters enclosed in double quotes,
". Only the line feed character,\n, and ASCII printable characters are allowed. The backslash character,\, can be used for escaping: the literal sequence\c, for any characterc, must be treated as the single characterc. This is useful when double quotes or the backslash character itself need to be included in the string. - integer is written as one or more decimal digits, optionally preceded by a plus or minus sign.
- boolean may be one of two values:
trueorfalse. - nil is a special value indicating a lack of meaningful value, written as the sequence
nil.
Next, we will define two composed objects:
- map is a sequence of key-value pairs enclosed in curly braces
{...}. Each key must be a string, followed by an object. The sequence=>separates keys and values. Note that map entries are separated by whitespace; no additional separator is required between key-value pairs, e.g.:
{
"format" => "acw"
"size" => 6539
"content" => false
"inner" => {"value" => 12}
}
- array is an ordered sequence of objects enclosed in square brackets
[...], with elements separated by semicolons,;.
[11; -22; []; nil]
For readability, whitespace may be used around objects or lexical tokens. In this format, a whitespace consists only of the line feed character and the space character.
Your task is to compute the fingerprint checksum for an instance of this format. Iterate through each object in the reference table, compute its fingerprint, and multiply it by the object's id. To compute the fingerprint, use the following pseudocode:
func fp(obj: object) -> integer:
switch type(obj):
case string:
return len(obj) // note that len("\"") == 1
case integer:
return abs(obj) // absolute value
case boolean:
if obj:
return 1
else:
return 0
case nil:
return 31
case map:
return sum(fp(key)*fp(value) for key, value in obj.pairs()) + 3
case array:
return sum(fp(o) for o in obj) + 1
The checksum is the sum of these products of the fingerprint and the object's id.
Input format
A Base64-encoded file containing the structure defined above.
Output format
The fingerprint checksum of the file.
Sample input
xcdXCjQKMDAwMDAwMDA1MQowMDAwMDAwMDU0CjAwMDAwMDAxNDQKMDAwMDAwMDE4MQoKMAoKewogICAgImtpbmQiID0+ICJyb29cdCIKICAgICJzaXplIiA9PiAxMTEKICAgICJjb250ZW50IiA9PiBmYWxzZQogICAgImVuY29kaW5nIiA9PiBuaWwKfQoKW3RydWU7IGZhbHNlOyB0cnVlOyB0cnVlOyB0cnVlOyAtMV0KCiJhY3ciCg==
Sample output
732