24
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)
-1 the titles on the two posts aren't the same format. ("P the P" vs "M my M", pick either 'the' or 'my' :p) – Rɪᴋᴇʀ – 2017-12-18T18:56:10.977
1Would "list of lines" be any of
[String]
,[{#Char}]
,[{Char}]
,[[Char]]
?, given thatString
is equivalent to{#Char}
– Οurous – 2017-12-18T20:48:24.4973@Ouros What notation is that? I strongly suspect the answer is "yes", because data types are very different between languages, so coding challenges generally accept "whatever the equivalent is in your language", but I've no idea what different representations you're trying to represent. – IMSoP – 2017-12-19T09:38:03.703
1What's next? Emit the email? Transmit the Telegram? Process the Post? – Sanchises – 2017-12-19T13:15:56.823
1@Ourous yes, data type is flexible, as IMSoP described. – FlipTack – 2017-12-19T16:43:34.107
It's odd that you even have the middle line of characters, they will always be filled with
|
. – Magic Octopus Urn – 2017-12-19T21:56:51.730@MagicOctopusUrn It's most likely for ease of scanning by real-life barcode machines - my guess is that the scanner works best when there's always a bar, so in the case that there is neither an ascender (top part) nor a descender (bottom part), there is still the central section (called the tracker) of a bar. – FlipTack – 2017-12-19T22:00:55.427
@FlipTack am I allowed to remove that as part of the input or nah? Saves me 1 byte – Magic Octopus Urn – 2017-12-19T22:04:57.243
@MagicOctopusUrn You can't remove it, it's a part of the barcode. Parsing the necessary information from the barcode is part of the challenge (that everyone else has managed to complete without complaint). – FlipTack – 2017-12-19T22:10:37.443
@FlipTack guess I more importantly should've asked if it can be taken as an array of 3 strings. That's what I really wanted to ask. – Magic Octopus Urn – 2017-12-19T22:58:25.717
@MagicOctopusUm As stated in the question: You may take the input as a list of lines, or a string with newlines. – FlipTack – 2017-12-20T09:13:38.770