Pixel Budget

10s 256 MB
Exclusive Easy+ 2.5
AlgorithmsBasics +1 Anthropic

Implement an ImageTransformer, a tool that can hold a set of images and apply some operations to transform them in various different ways.

You are given a sequence of grayscale images and a list of edit scripts. Each edit script is a sequence of named operations applied to one image to produce a new one. Your task is to apply each script and output the resulting image.

All images are grayscale. Each pixel is an integer in [0, 255]. Images are represented as H x W grids of integers. Pixel values are clamped to [0, 255] after every operation.

Each operation takes the current image as input and produces a new image of the same dimensions. Operations are applied in the order listed in the script. The output image is the result after all operations have been applied.

Name Arguments
BRIGHTNESS d (integer)
FLIP_H (none)
FLIP_V (none)
CROP r c h w
ROTATE_90 (none)
THRESHOLD t (integer)
  • BRIGHTNESS d — Add d to every pixel.
  • FLIP_H — Flip the image horizontally (mirror left-right).
  • FLIP_V — Flip the image vertically (mirror top-bottom).
  • CROP r c h w — Extract the h x w sub-image starting at row r, column c. All subsequent operations use this smaller image.
  • ROTATE_90 — Rotate the image 90° clockwise.
  • THRESHOLD t — Set each pixel to 255 if it is ≥ t, else 0.
Text
N
<image_0>
<image_1>
...
<image_(N-1)>
S
<script_0>
<script_1>
...
<script_(S-1)>

Each image is given as:

Text
H W
<row 0>
<row 1>
...
<row H-1>

Each script is given as:

Text
img_index K
op_1 [args]
op_2 [args]
...
op_K [args]

where img_index is the 0-indexed image to apply the script to, and K is the number of operations.

For each script (in order), output the resulting image:

Text
H W
<row 0>
...
<row H-1>

with a blank line between images.

  • Clamping is applied after each operation individually, not once at the end.
  • ROTATE_90 clockwise means row r, column c maps to row c, column (H-1-r) in the new image (new dimensions are W x H).
  • Scripts are independent — each starts from the original image at img_index, not from the result of a previous script.
  • Pixel values in output rows are space-separated.
  • 1 ≤ N ≤ 20
  • 1 ≤ S ≤ 50
  • 1 ≤ H, W ≤ 128
  • 1 ≤ K ≤ 20
  • CROP is always given valid arguments: r + h ≤ H and c + w ≤ W at the time of the crop.
  • No script produces an image smaller than 1 x 1.
Example 1
Input
1
4 4
100 120 130 140
80 90 100 110
60 70 80 90
40 50 60 70
2
0 2
BRIGHTNESS 30
FLIP_V
0 1
THRESHOLD 100
Output
4 4
70 80 90 100
90 100 110 120
110 120 130 140
130 150 160 170

4 4
255 255 255 255
0 0 255 255
0 0 0 0
0 0 0 0
Accepted 20/22
Acceptance 91%
Loading editor...
Input
1
4 4
100 120 130 140
80 90 100 110
60 70 80 90
40 50 60 70
2
0 2
BRIGHTNESS 30
FLIP_V
0 1
THRESHOLD 100
Expected Output
4 4
70 80 90 100
90 100 110 120
110 120 130 140
130 150 160 170

4 4
255 255 255 255
0 0 255 255
0 0 0 0
0 0 0 0