Minify YAML

View leaderboard

Task

YAML is a data format, similar to JSON, but more flexible and easier for people to read. Here is a simple YAML document:

name: Ada
age: 30

languages:
  - Python
  - YAML

profile:
  role: Engineer
  experience_years: 12
  location:
    city: Bremen
    country: Germany

The same data can be written in YAML in many different ways. For example, the document above can also be written as:

{name: Ada,age: 30,languages: [Python,YAML],profile: {role: Engineer,experience_years: 12,location: {city: Bremen,country: Germany}}}

Write a program that takes a YAML document and returns an equivalent version that is as short as possible.

Correctness

Your output must contain a valid YAML document, and nothing else. It must represent exactly the same data as the input document. The checker parses both documents with PyYAML's safe loader and checks yaml.safe_load(input) == yaml.safe_load(output).

Optimisation

Your goal is to generate output that is as short as possible. Every test has its own target length. Meeting that target passes the test and earns 1 point; producing an even shorter document earns bonus points. If your output is a little longer than the target, you can still earn partial points: they are awarded when its length is greater than the target but less than 1.1 × target length.

Trailing newlines and whitespace are part of your output, so do not emit them unless they are useful.

Submission

Submit one Python source file. It is run once for each YAML input. Your program must read from stdin and write the transformed document to stdout.

Limits

Each input YAML document is at most 100 KB.

The time limit is 5 seconds per test. The memory limit is 512 MiB per test. Your submitted Python source file must not be larger than 256 KiB.

Examples

Most tests are hidden. Here are some inputs you can download and try locally.