Beta

Advanced cow farm

Description:

History

Farmer-programmer John, who has not worked in his specialty for a long time, faced the problem that there are too many cows on his farm and it became almost impossible to distinguish them. So a plan matured in his head - to write a program that will encode information into a unique image for each cow on his farm which will store all the necessary information. John had been thinking over the generation algorithm for days, and after he finished, he realized that he had forgotten how to write code... image title

Task

Write a function that will generate an image based on data object and return it in base64 format.

Cow information format

{
    /**
    * Cow id — a string of small and large
    * latin letters and numbers, strictly 10 characters
    */
    id: 'string';
    /**
     * Flag with information about whether a cow is purchased or not(true || false)
    */
    isPurchased: 'boolean';
    /**
    * Cow name — a string of small and large
    * latin letters and spaces (from 0 to 10 characters)
    */
    name: 'string';
    /**
    * Cow's birthday - date in YYYY-MM-DD HH:MM format(ex. 2019-06-11 10:10)
    */
    dateOfBirth: 'string';
}

Image

Image has a fixed size — 156 by 164 pixels. There is a 10px wide white border around the perimeter of the barcode. Inside the frames is the barcode content, consisting of 18 lines of 17 black or white squares per line. The size of each square is 8 by 8 pixels. White squares in the content encode 0, black squares encode 1.

Algorithm

At the intersection of the first row and the first column of content, a square is drawn encoding information about whether it is a purchased cow or not. The value false is encoded by zero (white color), true - by one (black color).

Next, a string of the form <id><name><dateOfBirth> is formed from the fields - id, name, dateOfBirth. The name field is padded with spaces at the end of up to 10 characters.

The resulting string is converted into a byte array — each character of the string is assigned a corresponding ASCII code (a number from 0 to 255).

Then each element of the resulting array is translated into binary notation (eight characters 0 or 1) and encoded with a sequence of eight squares (0 is a white square, 1 is a black square). Squares are drawn in the barcode content sequentially and line by line.

The last line of the content contains the benchmark information

Algorithm of calculating benchmark information

Each square in the benchmark information row determines the parity of the sum of the content values in the corresponding column. If the sum of zeros and ones in the column is even, then a white square is drawn in the control information, otherwise — black.

Example

For this object:

{
    id: '3h13A8dH39';
    isPurchased: true;
    name: 'Candle-Fly';
    dateOfBirth: '2019-06-11 13:23';
}

the result will be: image title

Graphics
Algorithms
Design Patterns
Fundamentals
Lists

Similar Kata:

More By Author:

Check out these other kata created by vldnvsk

Stats:

CreatedOct 10, 2022
PublishedOct 11, 2022
Warriors Trained21
Total Skips0
Total Code Submissions48
Total Times Completed8
JavaScript Completions8
Total Stars4
% of votes with a positive feedback rating50% of 3
Total "Very Satisfied" Votes1
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes1
Total Rank Assessments3
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • vldnvsk Avatar
Ad