[Question]
  [
Thanks to your help in the [Mark My Mail](https://codegolf.stackexchange.com/questions/149672/mark-my-mail-ascii-barcodes) 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](https://codegolf.stackexchange.com/questions/149672/mark-my-mail-ascii-barcodes) 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](https://codegolf.stackexchange.com/questions/149672/mark-my-mail-ascii-barcodes).
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](/questions/tagged/code-golf "show questions tagged 'code-golf'") - so the shortest program (in bytes) wins!
---
# Test Cases
```
| | | | | | | | | | |
  |             |   |
```
**= 4096** (flipped)
```
      | | | |     | | | |
| | | | | | | | | | | | | | | |
  | |   | |   | |   | |   |   |
```
**= 7313145** (flipped)
```
    | |   |     |
| | | | | | | | | | |
  | |     |   |     |
```
**= 9419** (flipped)
```
|   | | | |   |   
| | | | | | | | | 
    | |   | |     
```
**= 990** (not flipped)
```
|   |       |   | |   
| | | | | | | | | | | 
    |       | |       
```
**= 12345** (not flipped)
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 18 bytes
```
>⁶m2Zm2UFUi¡1ṫ4Ḅ:⁴
```
[Try it online!](https://tio.run/##y0rNyan8/9/uUeO2XKOoXKNQt9DMQwsNH@5cbfJwR4vVo8Yt////j@ZSUgCDGihEZyso6XAp1SjghSAlClAd2EggVuKKBQA "Jelly – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 16 bytes
```
ḋṁẊ=hhttĊ2T§▼↔m↔
```
[Try it online!](https://tio.run/##yygtzv6v8Kip8dC2/w93dD/c2fhwV5dtRkZJyZEuo5BDyx9N2/OobUouEP///18BDGqgEJ2twFWjgBdyKUDVYiOBGAA "Husk – Try It Online")
Input is a list of lines (the TIO link uses a multiline string for clarity).
The lines must have equal lengths, and there must not be extra trailing spaces.
## Explanation
```
ḋṁẊ=hhttĊ2T§▼↔m↔  Input is a list of strings x.
           §▼     Lexicographic minimum of
             ↔    x reversed and
              m↔  x with each line reversed.
          T       Transpose. Now we have a list of columns.
        Ċ2        Get every second column, removing the blank ones.
    hhtt          Remove first 2 and last 2 (the orientation markers).
 ṁ                Map and concatenate
  Ẋ=              equality of adjacent pairs.
                  This turns a column like "|| " into [1,0], and these pairs are concatenated.
ḋ                 Convert from binary to integer.
```
[Answer]
# [SOGL V0.12](https://github.com/dzaima/SOGLOnline), ~~40~~ 32 [bytes](https://github.com/dzaima/SOGL/blob/master/chartable.md)
```
K@=«I⁷G@Κ¹;⌡I2n{j_{@≠}«+}¹jkjk4│
```
[Try it Here!](https://dzaima.github.io/SOGLOnline/?code=S0AlM0QlQUJJJXUyMDc3R0AldTAzOUElQjklM0IldTIzMjFJMm4lN0JqXyU3QkAldTIyNjAlN0QlQUIrJTdEJUI5amtqazQldTI1MDI_,inputs=JTdDJTIwJTIwJTIwJTdDJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdDJTIwJTIwJTIwJTdDJTIwJTdDJTIwJTIwJTIwJTBBJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTdDJTIwJTBBJTIwJTIwJTIwJTIwJTdDJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTdDJTIwJTdDJTIwJTIwJTIwJTIwJTIwJTIwJTIw,v=0.12)
[Answer]
# [Python 2](https://docs.python.org/2/), ~~113~~ 105 bytes
```
lambda x:int(''.join([`2*(c>'#')+(a>'#')`for a,b,c in zip(*x[::1-2*(x[-1]>'#')].split('\n'))][4:-4:2]),4)
```
[Try it online!](https://tio.run/##fZLhboMgEID/@xSX7AfQYVOUdZHEvQiS1HY1Y7Fo1CVu8d0daI22sYXkuIPju@OO8rf5KkzQZ3HS5@nl@JlCK7RpMELb70IbLA/BBp8@0Asirzgd1kNWVJDSIz2BNvCnS7xppRDMt56t9JkavNS2LnNtQYlBhCjJhc9FoAjlpG/OdVPHUiJYG4npYGUmBuyyHM7qEOW7aK9o4k246ca9/gh8H6KDdTkEew9ZyPjbHG8@vcqn6c9@Vw3RiLNoxHU3CXerKIe5TQwsItotCYvqPKAsSXPRYKSxILQPVJ5rs@sUhepc/@SN6/bQOeFBWdlPMliTnmFnkTgenem04fX/ "Python 2 – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 96 bytes
```
lambda a:int(`[2*(r==q)+(p==q)for p,q,r in zip(*a[::(a>'|')*2-1].split('\n'))[4:-4:2]]`[1::3],4)
```
[Try it online!](https://tio.run/##fUzLCsIwEPyVvWVTU6Exp4X2R5JAI1IM1G0ae1H677FKTyLOMI/DMOmxXCfWZWhdGcPtfAkQKPKCvdUV5rad5QHTO4YpQ1KzyhAZnjFhFSwRhk6sQla6bvzxnsa4oHAspLSGakPa@942RCevjCwpb88woIAP1p3fHRyv8JeOYV//8k1Clhc "Python 2 – Try It Online")
[Answer]
# [Pip](https://github.com/dloscutoff/pip), ~~46~~ ~~43~~ 42 bytes
```
IsQ@@gg:RV*RVgY^gR'|1(J@UW2*y@2+@y)TM2FB:4
```
Takes the lines of the barcode as three command-line arguments. The first and third lines must be padded to the length of the second line with spaces. [Try it online!](https://tio.run/##K8gs@P/fszjQwSE93SooTCsoLD0yLj1IvcZQw8shNNxIq9LBSNuhUjPE18jNycrk////CmBQA4XobIX/NQp44X8FqFpsJBADAA "Pip – Try It Online")
### Explanation
First some prepwork:
```
IsQ@@gg:RV*RVg Y^gR'|1
                        g is cmdline args; s is space (implicit)
IsQ                     If space equals
   @@g                  the first character of the first line, then:
           RVg           Reverse the order of the rows in g
        RV*              then reverse the characters in each row
      g:                 and assign the result back to g
                 gR'|1  In g, replace pipe character with 1
                ^       Split each row into a list of characters
               Y        Yank the result into y
```
Now observe that if we ignore the middle row and treat `|` as 1 and  as 0, each bar is just a 2-bit binary number:
```
(J@UW2*y@2+@y)TM2FB:4
       y@2             Third row
     2*                Multiply by 2, turning 1 into 2 and space into 0
           @y          First row
          +            Add
   UW                  Unweave: creates a list of two lists, the first containing all
                       even-indexed elements (in our case, the actual data), the second
                       containing all odd-indexed elements (the space separators)
  @                    First item of that list
 J                     Join the list of digits into a string
(            )TM2      Trim 2 characters from the beginning and end
                 FB:4  Convert from base 4 (: makes the precedence lower than TM)
                       Autoprint
```
[Answer]
# [Retina](https://github.com/m-ender/retina), 71 bytes
```
(.).
$1
sO$^`^\|.*|.
^..|..¶.*¶..|..$
¶
\|
 |
+`\| 
 ||||
.*¶$
$&$&
\|
```
[Try it online!](https://tio.run/##hU0xDsIwDNzvFR5MVYpkiTmP4ANRFAYGFgZg9Lv6gH4s2GpKOgRxkeOzzr573t73x7UsMwJCgPXDmL2XUY4CPuN14ZRTVJlUgCSiIsssk5VT9puoIMUpRyUjBrjO4IEHE0uhHqDUeSD79/BJgZUqbbL@sWhblQFaLb426J22mDUCzYOa8DN5v1vZBw "Retina – Try It Online") Link includes smaller test cases. Requires the first and last lines to be space-padded to the length of the middle line. Explanation:
```
(.).
$1
```
Delete the unnecessary spaces.
```
sO$^`^\|.*|.
```
Reverse the characters in the code, but if the bar code begins with a `|`, then select the entire code, otherwise split it in to characters. Then, reverse them. This flips the code if it begins with a `0`.
```
^..|..¶.*¶..|..$
¶
```
Delete the start/stop sequence and the middle row (which is of no use to us).
```
\|
 |
+`\| 
 ||||
```
Convert the spaces and `|`s from base 4 to unary.
```
.*¶$
$&$&
```
Double the last line.
```
\|
```
Convert to decimal.
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 181 160 bytes
Not too shabby for a java solution, I'm sure there are optimisations I can make, but I've already been staring at this for too long.
Cut down a few bytes by shortening the loop rather than using substring.
Golfed
```
c->{String r="";Integer i,j,l=c[0].length;for(i=4;i<l-4;i+=2){j=c[0][0]>32?i:l-1-i;r+=c[0][j]%8/4*(j==i?1:2)+c[2][j]%8/4*(j==i?2:1)+"";}return l.parseInt(r,4);}
```
[Try it online!](https://tio.run/##bY9fT8MgFMWf209xs8QE7B9t3YNZZYvxyQf34mPTB2Sso1LaUDazdP3sFTY1cSkQyD335ncOFT3QqGm5qjafI5O06@CNCtX7nlCG6y1lHNa9D56roURsR3Ve5AUwnPne4Pteu/@QgkFnqLHPoREbqC0BvRstVGknKbY0z1GhBgKKf50tkAN4fzyqy852e5rfF7FpXqz@rDU9IhwCzZMJLb3SBsd7P3aG13GzN3Fr/Y1UqI5VXCLHx7@Zf8KuQVnLkUXL/pIWNJnNslf785JrEGEVSsJcIMlVaXbZttFIkHkmnmRk74CkuK/OE/YsH9KVWMgoiUSmg4taFTePd/NbVBEiVskixQGzuf/L6SLBgbUdNDd7rUDGLdUdtymQDuc4G0Yv84dxhKk1nmBqTwzCleyq0zc "Java (OpenJDK 8) – Try It Online")
Ungolfed
```
String r = "";
Integer i, j, l = c[0].length;
for(i=4; i<l-4; i+=2){
    j = c[0][0]>32 ? i : l-1-i;
    r += c[0][j]%8/4 * (j==i?1:2) + c[2][j]%8/4 * (j==i?2:1) + "";
}
return l.parseInt(r, 4);
```
[Answer]
# [Java 8](http://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html), ~~208~~ ~~166~~ ~~157~~ 151 bytes
Trying it, probably can be better, reduced 42 due unnecessary checks, -9 removing variables, -6 thanks to Luke Stevens
Input is a `char[][3]`
```
(a)->{int i=2,c,s=0;c=(a[0][0]>32)?1:-1;for(;i<a.length-2;i++){s+=((a[i][1-c]>32?1:0)+(a[i][1+c]>32?2:0))*Math.pow(4,c==1?a.length-i-3:i-2);}return s;}
```
ungolfed:
```
int b(char[][] a) {
    int i=2,c,s=0;                      //i for looping, c to check if it's reversed, s is the sum, n the number
    c=(a[0][0]>32)?1:-1; //Check if must be reversed
    for(;i<a.length-2;i++){         //Looping elements
            //(Checking value for 1 + Checking value for 2) * Power, if reversed increasing, else decreasing
        s+=((a[i][1-c]>32?1:0)+(a[i][1+c]>32?2:0))*Math.pow(4,c==1?a.length-i-3:i-2);   
    }
    return s;
}
```
[Answer]
# [Clean](https://clean.cs.ru.nl), ~~191~~ ... ~~161~~ 144 bytes
```
import StdEnv
?' '=0;?_=1
r=reverse
$a b|hd a<'|'= $(r b)(r a)=[?(b!!n)*2+ ?(a!!n)\\n<-[4,6..length b-4]]
@[a,_,b]=sum[d*4^p\\d<- $a b&p<-[0..]]
```
[Try it online!](https://tio.run/##fY9NT8MwDIbP5Fd4YqLbaLsPJi4s6iTggMRtxzZMaRO2SGlSJekAqb@dLhmDA0LYkj9kPa/tSnKq@lqzVnKoqVC9qBttHGwce1QHlEUQ4dldtsVzZLDhB24sR0MKZbdnQFdRF2EYjgyUYx/oGOfZqBwM1HiyuIZsRENZFGqV5Mv4Nk0lVzu3hzJZEoLWOY23cUmwbeucTZYvTVGwVQJB/arxyCxNCek3jvp7plNwe2GBSqnfLFhdc604CMXfG24EVxVnvoP78BA4DY5bBwdqhG6tHzSts@jylAFDji7yCE7Wnf13DRGJw9a5l5F@TSA6@NfPxEKxHwLOen9FCAQE4sZ8EwRhWH@d239Wr5LubJ88PfcPH4rWorJH "Clean – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~39~~ 38 bytes
```
B4ththmȯ%4+3%5f≠192Ḟz+zṀ·*cN?↔m↔(='|←←
```
Takes input as a list of strings: [Try it online](https://tio.run/##yygtzv7/38mkJKMkI/fEelUTbWNV07RHnQsMLY0e7phXpV31cGfDoe1ayX72j9qm5AKxhq16zaO2CUD0////aKUaBQUFEFaA0jVgUklHqUYBGwRKKKDogLGUYgE "Husk – Try It Online") or [try the test-suite!](https://tio.run/##yygtzv6f@6ip8b@TSUlGSUbuifWqJtrGqqZpjzoXGFoaPdwxr0q76uHOhkPbtZL97B@1TckFYg1b9ZpHbROA6P///9HRSgrYgJKOUo0CFggUVwDSyADEq1GK1YGZBFOLzsZpJobpNQrYSWR7EGJQkoCLESqhLLBJNShurMFlDNgMVNcAVSIMQAoI3IagGIQIKmjYxMYCAA "Husk – Try It Online")
### Explanation
```
B4ththm(%4+3%5)f≠192Ḟz+zṀ·*cN?↔m↔(='|←←)
                             ?   (='|←←)  -- if the very first character == '|'
                              ↔           --   reverse the lines
                                          -- else
                               m↔         --   reverse each line
                       zṀ·*cN             -- zip the lines with [1,2,3..] under..
                        Ṁ·*c              --   convert each character to its codepoint and multiply by one of [1,2,3]
                    Ḟz+                   -- reduce the lines under zipWith(+) (this sums the columns)
               f≠192                      -- only keep elements ≠ 192 (gets rid of the separating lines)
-- for the example from the challenge we would now have:
--   [652,376,468,652,376,744,376,468,744,652,376]
      m(      )                           -- map the following function
        %4+3%5                            --   λx . ((x % 5) + 3) % 4
-- this gives us: [1,0,2,1,0,3,0,2,3,1,0]
    th                                    -- remove first & last element
  th                                      -- remove first & last element
B4                                        -- interpret as base4 number
```
[Answer]
# [Perl 5](https://www.perl.org/), 152 + 2 (`-F`) bytes
```
push@a,[@F]}{for$i(0..$#{$a[1]}){$_.=($a[0][$i]eq'|')+2*($a[2][$i]eq'|')}$_=reverse y/12/21/r if/^0/;s/^0*1000|10*$//g;s/.\K0//g;$\=$_+$\*4for/./g;say$\
```
[Try it online!](https://tio.run/##bY3NDoIwEITvPgWJm/An7ZbIyZBw4mJ8AoqEQ9EmRrBVE0J5dWvxxMGd7GT228MMQt0ya4eXvhbtrirKep66XoEMkBDYTtBWrJ7DCRqSB@7AugJZi4dv/DBOowWlKzRDkyvxFkoLb6QspSmjypMdPSM9aOcRQ0TDMAJKL44QfsQlAc@hiYFHe9dOyfJrR@DWboz3RxvP@XrMbz/98JT9XdvklBFkaJPyCw "Perl 5 – Try It Online")
[Answer]
# [Octave](https://www.gnu.org/software/octave/), ~~80~~ ~~75~~ 68 bytes
```
@(b)bi2de({rot90(t=~~(b-32)([3,1],[1:2:end]),2),t}{2-t(2)}(5:end-4))
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999BI0kzKdMoJVWjuii/xNJAo8S2rk4jSdfYSFMj2ljHMFYn2tDKyCo1LyVWU8dIU6ekttpIt0TDSLNWwxQkqmuiqfk/TSNaXQEIaoBQAYyhpLo1F4isUcACoXIwXQrIetVjNbm4QKYixGDyNSBTFYgwFlkjlAU09j8A "Octave – Try It Online")
Curiously, `bi2de` is default MSB on the right rather than the left, leading to some headaches while I made this... I think I should have the optimal way of flipping the array before indexing it, but there are *extremely* many ways to do it (either in the first indexing, or with `flipud`, `fliplr`, `rot90`, `'` (transpose), the final indexing...). Takes a rectangular array with spaces and `|`s (so, trailing spaces required)
```
@(b)                    % Define anonymous function taking input b
t=                      % Inline-define t,
~~(b-32)                % which is the input, converted to 1's and 0's,
([3 1],[1:2:end])       % but only keep the relevant rows (1 and 3 flipped vertically) and columns (only odd) 
{rot90(t,2),t}          % Flip 180 degrees
{2-t(2)                 % depending on whether the second element of t is 1 or 0.}
(5:end-4)               % Flatten array, discarding control bits
bi2de( ... )            % and convert from binary to decimal,
```
[Answer]
# JavaScript (ES6), ~~184~~ 181 bytes
I'm not an experienced golfer - I'm sure this can be improved, but I loved this challenge! I've always wondered about those marks.
Function `f` takes a list of strings with required trailing spaces. Newlines added to the code below for clarity (not included in byte count).
```
f=t=>((o=[[...t[0]],[...t[2]]])[0][0]=='|'?o:(z=a=>a.reverse())(o.map(z)))
.map((a,r)=>a.filter((_,i)=>~i%2).map(e=>(1+r)*(e=='|')).slice(2,-2))
.reduce((a,b)=>a+parseInt(b.join``,4),0)
```
## Usage
```
t=['                     ','| | | | | | | | | | |','  |             |   |']
console.log(f(t)) // 4096
```
## Ungolfed version with explanation
```
// take list of strings, t, as input:
f = t => ( 
  // create output variable o, discard middle row, turn other rows into lists:
  ( o = [ [...t[0]], [...t[2]] ] )
  // if top-left position isn't a pipe, rotate 180 degrees.
  // Alias [].reverse to save 3 bytes :D
  [0][0] == '|' ? o : ( z = a=> a.reverse() )( o.map(z) )
).map( (a,r) => 
  // remove even-numbered positions (non-encoding spaces):
  a.filter( (_,i) => ~i%2 )
  // convert non-pipes into zeros, and pipes into 1 or 2;
  // top row becomes the base4 1-component, bottom row is 2:
  .map( e => (1+r) * (e=='|') ).slice(2,-2)
// convert rows to base4 component strings, then decimal, then sum them:
).reduce( (a,b) => a + parseInt(b.join``,4),0)
```
]