Advertisement
Nividica

TIS-100: Luhn Summation

Jun 21st, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. function get_name()
  2.   return "Luhn Summation"
  3. end
  4.  
  5. function get_description()
  6.   return { "ADD EVEN DIGITS.",
  7.   "DOUBLE ODD DIGITS, IF DOUBLED IS GREATER THAN 9, SUBTRACT 9",
  8.   "IF SUM MULTIPLE OF 10, WRITE 0, ELSE WRITE (10-UNIT DIGIT) TO CHK",
  9.   "SEQUENCES ARE -1 TERMINATED"}
  10. end
  11.  
  12. -- Luhn summation
  13. function luhn( input, output, check, count)
  14.   -- The first element in this sequence
  15.   local head = #input + 1
  16.   -- The last element in this sequence
  17.   local tail = head + count - 1
  18.   -- Sum of the number
  19.   local sum = 0
  20.   -- If true the number doubled
  21.   local doDouble = true
  22.  
  23.   -- Generate the sequence
  24.   for i = head,tail do
  25.     -- Generate a number
  26.     local inp = math.random( 0, 9 )
  27.     -- Insert into the input
  28.     table.insert( input, inp )
  29.     -- Doubling?
  30.     if( doDouble ) then
  31.       inp = inp * 2
  32.       -- Bigger than 9?
  33.       if( inp > 9 ) then
  34.         inp = inp - 9
  35.       end
  36.     end
  37.     -- Add to sum
  38.     sum = sum + inp    
  39.     -- Flip doubling flag
  40.     doDouble = not doDouble
  41.   end
  42.   -- Append the sequence terminator
  43.   table.insert( input, -1 )
  44.  
  45.   -- Add the sum, and terminator, to the output
  46.   table.insert( output, sum )
  47.   table.insert( output, -1 )
  48.  
  49.   -- Add check digit
  50.   table.insert( check, ( sum * 9 % 10 ) )
  51.   table.insert( check, -1 )
  52. end
  53.  
  54. function get_streams()
  55.   -- Maximum number of items to place into the input
  56.   local MAX_COUNT = 38
  57.  
  58.   -- Numbers sent to the TIS-100
  59.   input = {}
  60.  
  61.   -- Summations expected to get back from the TIS-100
  62.   output = {}
  63.  
  64.   -- Check digits expected to get back from the TIS-100
  65.   check = {}
  66.  
  67.   -- How many items are in the input
  68.   local count = 0
  69.  
  70.   while( count < MAX_COUNT ) do
  71.     -- Random sequence length
  72.     local nextLen = math.random( 1, 8 )
  73.    
  74.     -- Update count
  75.     count = count + nextLen + 1
  76.     -- Bounds check
  77.     if( count > MAX_COUNT ) then
  78.       -- Too big, reduce sequence length
  79.       nextLen = MAX_COUNT - ( count - nextLen - 1 )
  80.      
  81.       -- Set count to max
  82.       count = MAX_COUNT
  83.     end
  84.    
  85.     -- Add sequences to the input, and luhn sum to output
  86.     luhn( input, output, check, nextLen )
  87.   end
  88.  
  89.   return {
  90.     { STREAM_INPUT, "IN.D", 1, input },
  91.     { STREAM_OUTPUT, "OUT.CHK", 0, check },
  92.     { STREAM_OUTPUT, "OUT.SUM", 2, output },
  93.   }
  94. end
  95.  
  96. function get_layout()
  97.   return {
  98.     TILE_COMPUTE,   TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
  99.     TILE_COMPUTE,   TILE_DAMAGED, TILE_COMPUTE, TILE_COMPUTE,
  100.     TILE_COMPUTE,   TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
  101.   }
  102. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement