39
6
4-state barcodes
Many postal services (Royal Mail UK, Canada Post, US Mail, etc) use a 4-state barcode to encode information about their mail. Rendered in ASCII, it may look something like this:
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
A 4-state barcode is a row of bars. Each bar can be extended upwards, downwards, or both, allowing 4 possibilities. This means that each bar essentially represents a base 4 digit:
| | Bar: | | | | | | Digit: 0 1 2 3
The problem with this symbology is that it each barcode is a valid, different barcode upside down: drastically changing the meaning if the orientation is incorrect. Therefore, a start and stop sequence are normally implemented so the scanner can calculate which way it is supposed to be read.
For the purpose of this challenge, we will be using the start/stop sequence specified by Australia Post: each barcode begins and ends with a 1 0
sequence.
The Challenge
Your task is to write a program or function which, given a positive integer N
, converts it to an ASCII 4-state barcode, where each bar (except for the start/stop sequences) represents a digit in the base-4 representation of N
.
Example:
Given the integer 19623
, we would first convert it to its base-4 representation, 10302213
.
We would then map each digit to the corresponding bar:
1 0 3 0 2 2 1 3 | | | | | | | | | | | | | | | |
Finally, we would add the start/stop sequences:
Start: End: 1 0 1 0 | | | | | | | | | | | | | | | | | | | | | |
The resulting barcode should be the program's output.
Rules:
- The input will be a positive integer, within the range of your language's standard integer size.
- The output:
- May be either a list of lines, or a string containing newlines.
- May contain leading or trailing newlines/spaces, as long as the shape remains intact.
- Should show the barcode with the above format - it must use the pipe character (
|
) and space character () when drawing bars, and there should be 1 space in between each upright bar.
- This is code-golf, so the shortest program (in bytes) wins!
Test Cases
4095:
| | | | | | | | | | | | | | | | | | | | | | | |
4096:
| | | | | | | | | | | | | |
7313145:
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Leading spaces allowed? ;) – Erik the Outgolfer – 2017-12-01T20:16:13.607
@FlipTack The problem with this symbology - you haven't seen The Boondock Saints, have you? – Lord Farquaad – 2017-12-01T21:48:56.127
@EriktheOutgolfer As long as the actual barcode, as a 2D matrix of characters, is intact, it can have as many spaces before or after it as necessary. – FlipTack – 2017-12-01T22:24:03.260
Other barcode related challenges: 1, 2, 3
– FlipTack – 2017-12-01T22:30:42.917Can the output have leading zeros? – user230118 – 2017-12-01T23:34:46.147
@user230118 sorry, but the only leading/trailing characters should be spaces or newlines. – FlipTack – 2017-12-02T09:30:25.420
@FlipTack Are we allowed to output the barcode upside down? This comment seems to suggest that this would be acceptable.
– wizzwizz4 – 2017-12-02T13:01:49.413@wizzwizz4 It should be the right way around, as in the test cases. – FlipTack – 2017-12-02T13:56:25.387