Task: UPC




Go: package main

import (
    "fmt"
    "regexp"
)

var bits = []string{
    "0 0 0 1 1 0 1 ",
    "0 0 1 1 0 0 1 ",
    "0 0 1 0 0 1 1 ",
    "0 1 1 1 1 0 1 ",
    "0 1 0 0 0 1 1 ",
    "0 1 1 0 0 0 1 ",
    "0 1 0 1 1 1 1 ",
    "0 1 1 1 0 1 1 ",
    "0 1 1 0 1 1 1 ",
    "0 0 0 1 0 1 1 ",
}

var (
    lhs = make(map[string]int)
    rhs = make(map[string]int)
)

var weights = []int{3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1}

const (
    s = "# #"
    m = " # # "
    e = "# #"
    d = "(?:#| ){7}"
)

func init() {
    for i := 0; i <= 9; i++ {
        lt := make([]byte, 7)
        rt := make([]byte, 7)
        for j := 0; j < 14; j += 2 {
            if bits[i][j] == '1' {
                lt[j/2] = '#'
                rt[j/2] = ' '
            } else {
                lt[j/2] = ' '
                rt[j/2] = '#'
            }
        }
        lhs[string(lt)] = i
        rhs[string(rt)] = i
    }
}

func reverse(s string) string {
    b := []byte(s)
    for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
        b[i], b[j] = b[j], b[i]
    }
    return string(b)
}

func main() {
    barcodes := []string{
        "         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #       ",
        "        # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         ",
        "         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         ",
        "       # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #        ",
        "         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #          ",
        "          # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         ",
        "         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #        ",
        "        # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         ",
        "         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #       ",
        "        # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         ",
    }

    
    
    expr := fmt.Sprintf(`^\s*%s(%s)(%s)(%s)(%s)(%s)(%s)%s(%s)(%s)(%s)(%s)(%s)(%s)%s\s*$`,
        s, d, d, d, d, d, d, m, d, d, d, d, d, d, e)
    rx := regexp.MustCompile(expr)
    fmt.Println("UPC-A barcodes:")
    for i, bc := range barcodes {
        for j := 0; j <= 1; j++ {
            if !rx.MatchString(bc) {
                fmt.Printf("%2d: Invalid format\n", i+1)
                break
            }
            codes := rx.FindStringSubmatch(bc)
            digits := make([]int, 12)
            var invalid, ok bool 
            for i := 1; i <= 6; i++ {
                digits[i-1], ok = lhs[codes[i]]
                if !ok {
                    invalid = true
                }
                digits[i+5], ok = rhs[codes[i+6]]
                if !ok {
                    invalid = true
                }
            }
            if invalid { 
                if j == 0 { 
                    bc = reverse(bc)
                    continue
                } else {
                    fmt.Printf("%2d: Invalid digit(s)\n", i+1)
                    break
                }
            }
            sum := 0
            for i, d := range digits {
                sum += weights[i] * d
            }
            if sum%10 != 0 {
                fmt.Printf("%2d: Checksum error\n", i+1)
                break
            } else {
                ud := ""
                if j == 1 {
                    ud = "(upside down)"
                }
                fmt.Printf("%2d: %v %s\n", i+1, digits, ud)
                break
            }
        }
    }
}





VB: 

Option Explicit
Const m_limit ="# #"
Const m_middle=" # # "
Dim a,bnum,i,check,odic
a=array("         # #   # ##  #  ## #   ## ### ## ### ## #### # # # ## ##  #   #  ##  ## ###  # ##  ## ### #  # #       ",_
         "        # # #   ##   ## # #### #   # ## #   ## #   ## # # # ###  # ###  ##  ## ###  # #  ### ###  # # #         ",_
        "         # #    # # #  ###  #   #    # #  #   #    # # # # ## #   ## #   ## #   ##   # # #### ### ## # #         ",_
          "       # # ##  ## ##  ##   #  #   #  # ###  # ##  ## # # #   ## ##  #  ### ## ## #   # #### ## #   # #        ",_
        "         # # ### ## #   ## ## ###  ##  # ##   #   # ## # # ### #  ## ##  #    # ### #  ## ##  #      # #          ",_
       "          # #  #   # ##  ##  #   #   #  # ##  ##  #   # # # # #### #  ##  # #### #### # #  ##  # #### # #         ",_
        "         # #  #  ##  ##  # #   ## ##   # ### ## ##   # # # #  #   #   #  #  ### # #    ###  # #  #   # #        ",_
         "        # # #    # ##  ##   #  # ##  ##  ### #   #  # # # ### ## ## ### ## ### ### ## #  ##  ### ## # #         ",_
        "         # # ### ##   ## # # #### #   ## # #### # #### # # #   #  # ###  #    # ###  # #    # ###  # # #       ",_
         "        # # # #### ##   # #### # #   ## ## ### #### # # # #  ### # ###  ###  # # ###  #    # #  ### # #         ")


bnum=Array("0001101","0011001","0010011","0111101","0100011"," 0110001","0101111","0111011","0110111","0001011")

Set oDic = WScript.CreateObject("scripting.dictionary")
For i=0 To 9: 
  odic.Add bin2dec(bnum(i),Asc("1")),i+1 
  odic.Add bin2dec(bnum(i),Asc("0")),-i-1  
Next

For i=0 To UBound(a) : print pad(i+1,-2) & ": "& upc(a(i)) :Next
  WScript.Quit(1)
  
  Function bin2dec(ByVal B,a) 
    Dim n
    While len(b)
      n =n *2 - (asc(b)=a)  
      b=mid(b,2) 
    Wend
    bin2dec= n And 127
  End Function
  
  Sub print(s): 
    On Error Resume Next
    WScript.stdout.WriteLine (s)  
    If  err= &h80070006& Then WScript.Echo " Please run this script with CScript": WScript.quit
  End Sub
  function pad(s,n) if n<0 then pad= right(space(-n) & s ,-n) else  pad= left(s& space(n),n) end if :end function
   
  Function iif(t,a,b)  If t Then iif=a Else iif=b End If :End Function
  
  Function getnum(s,r) 
    Dim n,s1,r1 
    
    s1=Left(s,7)
    s=Mid(s,8)
    r1=r
    Do
      If r Then s1=StrReverse(s1) 
      n=bin2dec(s1,asc("#"))
      If odic.exists(n) Then
        getnum=odic(n)
        Exit Function
      Else
        If r1<>r Then getnum=0:Exit Function
        r=Not r
      End If
    Loop 
  End Function          
  
  Function getmarker(s,m) 
    getmarker= (InStr(s,m)= 1)
    s=Mid(s,Len(m)+1)
  End Function
  
  Function checksum(ByVal s)
    Dim n,i : n=0
    do
       n=n+(Asc(s)-48)*3
       s=Mid(s,2)
       n=n+(Asc(s)-48)*1
       s=Mid(s,2)
    Loop until Len(s)=0
    checksum= ((n mod 10)=0)
  End function      
      
  Function upc(ByVal s1)
    Dim i,n,s,out,rev,j 
    
    
    s=Trim(s1)
    If getmarker(s,m_limit)=False  Then upc= "bad start marker ":Exit function
    rev=False
    out=""
    For j= 0 To 1
      For i=0 To 5
        n=getnum(s,rev)
        If n=0 Then upc= pad(out,16) & pad ("bad code",-10) & pad("pos "& i+j*6+1,-11): Exit Function
        out=out & Abs(n)-1
      Next
      If j=0 Then If getmarker(s,m_middle)=False  Then upc= "bad middle marker " & out :Exit Function
    Next  
    If getmarker(s,m_limit)=False  Then upc= "bad end marker "  :Exit function
    If rev Then out=strreverse(out)
    upc= pad(out,16) &  pad(iif (checksum(out),"valid","not valid"),-10)&  pad(iif(rev,"reversed",""),-11)
  End Function