Robert Mion

Posted on

# Space Image Format

## Task: Solve for X where...

### Part 1

``````X = the number of 1 digits multiplied by the number of 2 digits in the layer that contains the fewest 0 digits
``````

### Part 2

``````X = the BIOS password
``````

### Example input

``````123456789012
``````

It represents

• A picture of a password
• Signified as a series of single digits
• Each digit represents the color of a single pixel

## Part 1

1. Understanding the composition of the image
2. Doing just enough to succeed
3. A written description of my working algorithm
4. A visual depiction of my working algorithm

### Understanding the composition of the image

The image dimensions are known:

• In the example, they are `3x2`
• For my puzzle input, they are `25x6`

The tricky part:

Each image actually consists of a series of identically-sized layers that are filled in

The example input contains 12 digits

``````123456789012
``````

Since the area of the image is 6...
...we can divide the number of digits by the area...
...to determine the number of identically-sized layers

`````` 12 [digits]
/ 6 [area: 3 x 2]
---
2 [identically-sized layers]
``````

My puzzle input contains 15,000 digits

`````` 15000 [digits]
/  150 [area: 25 x 6]
---
100 [identically-sized layers]
``````

### Doing just enough to succeed

• The task is to find the layer with the fewest 0s
• There is no need to identify the rows within each layer, as the sample diagram depicts
• Instead, it's enough to extract the characters representing each layer

### A written description of my working algorithm

``````Accept three parameters:
1. Digits
2. Width
3. Height

Calculate the area by multiplying width and height
Create a 2-element array:
1. A string containing as many 0s as equivalent to the area
2. Infinity

For i from 0 to the number of digits
Store a string containing only the digits at locations from i up to but not including i + area
Count the number of 0s in that string by turning the string into an array of digits and filtering to include only the 0s
If the number of zeros is less than the value stored in the 2-element array
Update the 2-element array such that:
1. The newly stored string created in this iteration of the for-loop
2. The count of 0s in the string

Return the product of the following operation performed twice on the string now stored in the 2-element array:
1. Turn the string into an array of digits and filter to include only the 1s - return the length
2. Turn the string into an array of digits and filter to include only the 2s - return the length
``````

## Part 2

1. I knew this was coming, and thought it would be harder
2. A written description of my working algorithm
3. A visual depiction of my working algorithm
4. An image-encoding simulator

### I knew this was coming, and thought it would be harder

The instruction from Part 1 stated:

Images are sent as a series of digits that each represent the color of a single pixel.

• Noting that the digits were 0,1,2 - I anticipated they would represent `RGB` and require a complex algorithm to determine what the image depicts
• To my surprise and delight, they represent `transparent`, `white` and `black` - whichever color is visible when viewed top-down is the winning pixel

### A written description of my working algorithm

``````Accept three parameters:
1. Digits
2. Width
3. Height

Calculate the area by multiplying width and height

Create an empty array, layers

For i from 0 to the number of digits in the input
Add to layers a string containing only the digits at locations from i up to but not including i + area

Using the first string in layers
Split the string into an array of stringified digits
Update each digit according to the following operations:
For each character and its location in the array
Set new variable, pixels, to an ordered list containing each digit from each layer's string at the current location
Set new variable, position, to 0
While the digit in pixels at position is '2'
Increment position
Return the digit at position in pixels
Merge all characters back into a string

Create an empty array, image

For i from 0 to the number of digits in the updated first layer
Add to image a string containing only the digits at locations from i up to but not including i + width

Merge each string in image using a newline character to simulate a 25x6 grid of digits
Replace all '0's with space characters to make the '1's pop out and reveal letters
Print the grid of digits to see the password
``````

### An image-encoding simulator

• Easy to build
• Added bonus of slowly rendering image using code from my Jurassic Jigsaw and simulator

## I did it!!

• Both parts solved!
• Two GIFs designed!
• A simulator built!

I appreciated this fun, breezy puzzle as a break between Intcode computer puzzles.

Bring it on, Day 9!