96 lines
3 KiB
Text
96 lines
3 KiB
Text
|
|
V LEFT_DIGITS = [
|
|||
|
|
‘ ## #’ = 0,
|
|||
|
|
‘ ## #’ = 1,
|
|||
|
|
‘ # ##’ = 2,
|
|||
|
|
‘ #### #’ = 3,
|
|||
|
|
‘ # ##’ = 4,
|
|||
|
|
‘ ## #’ = 5,
|
|||
|
|
‘ # ####’ = 6,
|
|||
|
|
‘ ### ##’ = 7,
|
|||
|
|
‘ ## ###’ = 8,
|
|||
|
|
‘ # ##’ = 9
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
V RIGHT_DIGITS = Dict(LEFT_DIGITS.items(), (k, v) -> (k.replace(‘ ’, ‘s’).replace(‘#’, ‘ ’).replace(‘s’, ‘#’), v))
|
|||
|
|
|
|||
|
|
V END_SENTINEL = ‘# #’
|
|||
|
|
V MID_SENTINEL = ‘ # # ’
|
|||
|
|
|
|||
|
|
F decodeUPC(input)
|
|||
|
|
F decode(candidate)
|
|||
|
|
V pos = 0
|
|||
|
|
V part = candidate[pos .+ :END_SENTINEL.len]
|
|||
|
|
I part == :END_SENTINEL
|
|||
|
|
pos += :END_SENTINEL.len
|
|||
|
|
E
|
|||
|
|
R (0B, [Int]())
|
|||
|
|
|
|||
|
|
[Int] output
|
|||
|
|
L 6
|
|||
|
|
part = candidate[pos .+ 7]
|
|||
|
|
pos += 7
|
|||
|
|
|
|||
|
|
I part C :LEFT_DIGITS
|
|||
|
|
output [+]= :LEFT_DIGITS[part]
|
|||
|
|
E
|
|||
|
|
R (0B, output)
|
|||
|
|
|
|||
|
|
part = candidate[pos .+ :MID_SENTINEL.len]
|
|||
|
|
I part == :MID_SENTINEL
|
|||
|
|
pos += :MID_SENTINEL.len
|
|||
|
|
E
|
|||
|
|
R (0B, output)
|
|||
|
|
|
|||
|
|
L 6
|
|||
|
|
part = candidate[pos .+ 7]
|
|||
|
|
pos += 7
|
|||
|
|
|
|||
|
|
I part C :RIGHT_DIGITS
|
|||
|
|
output [+]= :RIGHT_DIGITS[part]
|
|||
|
|
E
|
|||
|
|
R (0B, output)
|
|||
|
|
|
|||
|
|
part = candidate[pos .+ :END_SENTINEL.len]
|
|||
|
|
I part == :END_SENTINEL
|
|||
|
|
pos += :END_SENTINEL.len
|
|||
|
|
E
|
|||
|
|
R (0B, output)
|
|||
|
|
|
|||
|
|
V sum = 0
|
|||
|
|
L(v) output
|
|||
|
|
I L.index % 2 == 0
|
|||
|
|
sum += 3 * v
|
|||
|
|
E
|
|||
|
|
sum += v
|
|||
|
|
R (sum % 10 == 0, output)
|
|||
|
|
|
|||
|
|
V candidate = input.trim(‘ ’)
|
|||
|
|
V out = decode(candidate)
|
|||
|
|
I out[0]
|
|||
|
|
print(out[1])
|
|||
|
|
E
|
|||
|
|
out = decode(reversed(candidate))
|
|||
|
|
I out[0]
|
|||
|
|
print(out[1]‘ Upside down’)
|
|||
|
|
E
|
|||
|
|
I out[1].len == 12
|
|||
|
|
print(‘Invalid checksum’)
|
|||
|
|
E
|
|||
|
|
print(‘Invalid digit(s)’)
|
|||
|
|
|
|||
|
|
V barcodes = [
|
|||
|
|
‘ # # # ## # ## # ## ### ## ### ## #### # # # ## ## # # ## ## ### # ## ## ### # # # ’,
|
|||
|
|
‘ # # # ## ## # #### # # ## # ## # ## # # # ### # ### ## ## ### # # ### ### # # # ’,
|
|||
|
|
‘ # # # # # ### # # # # # # # # # # ## # ## # ## # ## # # #### ### ## # # ’,
|
|||
|
|
‘ # # ## ## ## ## # # # # ### # ## ## # # # ## ## # ### ## ## # # #### ## # # # ’,
|
|||
|
|
‘ # # ### ## # ## ## ### ## # ## # # ## # # ### # ## ## # # ### # ## ## # # # ’,
|
|||
|
|
‘ # # # # ## ## # # # # ## ## # # # # # #### # ## # #### #### # # ## # #### # # ’,
|
|||
|
|
‘ # # # ## ## # # ## ## # ### ## ## # # # # # # # # ### # # ### # # # # # ’,
|
|||
|
|
‘ # # # # ## ## # # ## ## ### # # # # # ### ## ## ### ## ### ### ## # ## ### ## # # ’,
|
|||
|
|
‘ # # ### ## ## # # #### # ## # #### # #### # # # # # ### # # ### # # # ### # # # ’,
|
|||
|
|
‘ # # # #### ## # #### # # ## ## ### #### # # # # ### # ### ### # # ### # # # ### # # ’
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
L(barcode) barcodes
|
|||
|
|
decodeUPC(barcode)
|