Thanks to your help in the Mark My Mail challenge, PPCG-Post has successfully stamped all of its parcels with the generated barcodes!
Now, it's time to decode them.
In this challenge your program will, given a barcode generated from the Mark My Mail challenge, decode it and return the encoded integer.
But watch out! The barcode might be upside down...
4-state barcodes
In the case you missed the encoding challenge you'll need to know what kind of barcodes we're talking about. A 4-state barcode is a row of bars with four possible states, each representing a base-4 integer:
| |
Bar: | | | |
| |
Digit: 0 1 2 3
Rendered in ASCII, the barcodes will take up three lines of text, using the pipe (|
) character to represent part of a bar, and a space () to represent an empty section. There will be a single space in between each bar. An example barcode may look like this:
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
To convert a barcode back to the integer it encodes, map each bar to its corresponding base-4 digit, concatenate these, and convert it to decimal.
As each barcode will also represent a different barcode when upside down, we implement a start/stop sequence so the orienation can be calculated. 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, given an ASCII 4-state barcode, parse it and return the integer it encodes - essentially the reverse of Mark My Mail.
But to spice things up, there's a catch - the barcode may be given upside down. As in the real world, it will be left to the barcode reader (your program) to determine the correct orientation using the start/stop sequence.
Example:
Given the following barcode:
| | | | | | | | | | | | | | | | | | | |
We can clearly see that the first and last pairs of digits are 0, 2
and not 1, 0
. This means that the barcode is upside down - so we must rotate it by 180 degrees (not just flip each bar) to achieve the correct orientation:
| | | | | | | | | | | | | | | | | | | |
Now, we can begin the decoding. We map each bar to its corresponding base-4 digit, ignoring the start/stop sequences as they do not encode the data.
| | | | | | | | | | | | | | | | | | | | - - 2 1 0 3 0 2 3 - -
We concatenate this to the base-4 integer 2103023
, then convert it to its decimal representation 9419
for the final result.
Rules
- The input will always be a valid, 4-state barcode, rendered in ASCII as set out above, with the described start/stop sequence.
- You may request trailing spaces, or stripped lines, as well as a trailing newline - whichever format suits your golfing.
- It may or may not be in the correct orientation - your program must determine whether to read it upside down, by using the start/stop sequence.
- It will not encode leading zero-digits in the base-4 integer.
- You may take the input as a list of lines, or a string with newlines.
- The output should be an integer in your language's standard integer base, representing the data that was encoded by the barcode.
- As postage stamps are small and can fit very little code on them, your code will need to be as short as possible: this is a code-golf - so the shortest program (in bytes) wins!
Test Cases
| | | | | | | | | | | | | |
= 4096 (flipped)
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
= 7313145 (flipped)
| | | | | | | | | | | | | | | | | | | |
= 9419 (flipped)
| | | | | | | | | | | | | | | | | | |
= 990 (not flipped)
| | | | | | | | | | | | | | | | | | |
= 12345 (not flipped)
[String]
,[{#Char}]
,[{Char}]
,[[Char]]
?, given thatString
is equivalent to{#Char}
\$\endgroup\$