csaboka

PRIME FACTOR CALCULATOR

Jul 7th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. -- The function get_name() should return a single string that is the name of the puzzle.
  2. --
  3. function get_name()
  4.     return "PRIME FACTOR CALCULATOR"
  5. end
  6.  
  7. -- The function get_description() should return an array of strings, where each string is
  8. -- a line of description for the puzzle. Puzzle descriptions are not automatically paginated,
  9. -- so you can use the string "<PAGE>" to start a new page. Surrounding text with asterisks
  10. -- will cause it to be rendered in bold, something we use when mentioning a signal by name.
  11. --
  12. function get_description()
  13.     return {
  14.         "*keypad* is an XBus input connected to a numeric keypad.",
  15.         "*factors* is an XBus output connected to a display device.",
  16.         "When a value is received from the *keypad*, send the prime factors of that number, in increasing order, to *factors*",
  17.         "Inputs are always between 2 and 99, inclusive.",
  18.     }
  19. end
  20.  
  21. -- The function get_board() allows you to specify an 18x7 "ASCII art" grid to customize
  22. -- the layout of the board and the placement of the input and output terminals. Generally
  23. -- speaking, inputs are placed on the left of boards, while outputs are placed on the right.
  24. --
  25. -- For empty space, use the '.' character.
  26. -- For buildable board space, use the '#' character.
  27. -- For an input or output terminal, use characters '0' through '9' no more than once each.
  28. -- For the bottom-left corner of the radio, use the 'R' character.
  29. -- For the bottom-left corners of dials, use characters 'A', 'B', and 'C'.
  30. --
  31. function get_board()
  32.     return [[
  33.         ...##############.
  34.         ..################
  35.         #################1
  36.         ##################
  37.         0#################
  38.         #############.....
  39.         #############.....
  40.     ]]
  41. end
  42.  
  43. -- The function get_data() is called both to establish information about the puzzle (such as what
  44. -- the inputs and outputs are) and to generate the random test cases. Signal levels and XBus data
  45. -- should change from call to call, but information like the names and types of terminals should
  46. -- not change at all.
  47. --
  48. -- To create a standard input or output terminal, call create_terminal(). Valid terminal types are
  49. -- TYPE_SIMPLE, TYPE_XBUS, and TYPE_XBUS_NONBLOCKING. Valid terminal directions are DIR_INPUT and
  50. -- DIR_OUTPUT. Valid data for a simple I/O signal is an array of integers, 0 - 100 inclusive. Valid
  51. -- data for an XBus signal is an array of integer arrays, each with values -999 to 999 inclusive.
  52. --
  53. --       create_terminal(name, board_character, type, direction, data)
  54. --
  55. -- To create a radio, call create_radio(). You may only create one radio in each puzzle. Since radios
  56. -- are XBus-only, the only valid data for data_rx and data_tx are arrays of integer arrays, each with
  57. -- values -999 to 999 inclusive. You cannot customize the signal names for a radio.
  58. --
  59. -- By default the radio will be placed in the bottom-left corner of the screen. However, if you use an
  60. -- 'R' character in your board layout, the bottom-left corner of the radio will be placed there instead.
  61. --
  62. --       create_radio(data_rx, data_tx)
  63. --
  64. -- To create a dial, call create_dial(). You may create up to three dials in each puzzle. The names
  65. -- of dials should be kept short, as there is not much room to display them visually. A valid value
  66. -- is an integer between 0 and 99, inclusive.
  67. --
  68. -- By default dials will be placed in the bottom-left corner of the screen. However, if you use an
  69. -- 'A', 'B', or 'C' character in your board layout, the bottom-left corners of the first, second, and
  70. -- third dials will be placed there, respectively.
  71. --
  72. --       create_dial(name, value)
  73. --
  74. -- NOTE: To generate random values you should use math.random(). However, you SHOULD NOT seed
  75. --       the random number generator with a new seed value, as that is how the game ensures that
  76. --       the first test run is consistent for all users, and thus something that allows for the
  77. --       comparison of cycle scores.
  78. --
  79. -- NOTE: Fun fact! Arrays in Lua are implemented as tables (dictionaries) with integer keys that
  80. --       start at 1 by convention. Contrast this with nearly every other programming language, in
  81. --       which arrays start with an index of 0. Because of this, the 60 "time slices" that make
  82. --       up a test case are indexed from 1 to 60 inclusive.
  83. --
  84. function get_data()
  85.     keypad = {}
  86.     factors = {}
  87.     time = math.random(1, 5)
  88.  
  89.     while time <= 60 do
  90.         input = math.random(2, 99)
  91.         keypad[time] = {input}
  92.        
  93.         current_factors = {}
  94.         factor_index = 1
  95.         factor = 2
  96.         while input ~= 1 do
  97.             if (input % factor) == 0 then
  98.                 current_factors[factor_index] = factor
  99.                 factor_index = factor_index + 1
  100.                 input = input / factor
  101.             else
  102.                 factor = factor + 1 -- this is inefficient, but shouldn't make a difference with such tiny numbers
  103.             end
  104.         end
  105.        
  106.         factors[time] = current_factors
  107.        
  108.         -- Add a delay long enough to make sure there is no overlap in the output display, plus some random delay
  109.         time = time + (factor_index - 1) + math.random(0, 3)
  110.     end
  111.  
  112.     create_terminal("keypad", "0", TYPE_XBUS, DIR_INPUT, keypad)
  113.     create_terminal("factors", "1", TYPE_XBUS, DIR_OUTPUT, factors)
  114. end
Advertisement
Add Comment
Please, Sign In to add comment