Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The function get_name() should return a single string that is the name of the puzzle.
- --
- function get_name()
- return "CREDIT CARD VERIFIER"
- end
- -- The function get_description() should return an array of strings, where each string is
- -- a line of description for the puzzle. Surrounding text with asterisks will cause it to
- -- be rendered in bold, something we use when mentioning a signal by name.
- --
- -- By using the syntax that we use in our puzzle descriptions, you can also create tables:
- --
- -- "| This is a table. | This is a second column. | This is a column with "
- -- "| | | multiple line of text."
- -- "----------------------------------------------------------------------"
- -- "| This is a second | | More text here! "
- -- "| table row. | | "
- --
- -- Puzzle descriptions are not automatically paginated, so you can use the string "<PAGE>"
- -- to start a new page of text. If you don't it will overflow and make Zach sad.
- --
- function get_description()
- return {
- "*card#* are XBus inputs connected to numeric keypads.",
- "*error* is a simple output connected to a LED next to the keypad.",
- "15-digit credit card numbers are entered on the keypads connected to *card#*.",
- "Verfiy whether the card number was entered correctly, using the *Luhn algorithm*.",
- "Send 100 to *error* to make the LED turn on when the card number is incorrect.",
- "There will never be two card numbers entered during the same time unit.",
- "<PAGE>",
- "*The Luhn algorithm* works like this:",
- "Counting from the right, double every second digit of the number",
- "Take the sum of all digits. If you ended up with a value above 9 in the previous step, use the sum of the digits of that number",
- "If the sum is divisible by 10, the number passes the Luhn algorithm",
- "See the next page for an example.",
- "<PAGE>",
- "Take a number:",
- "|2|8|8|5|7|1|6|6|1|9|1|8|1|1|3",
- "Double every second digit, counting from the right:",
- "|2|*16*|8|*10*|7|*2*|6|*12*|1|*18*|1|*16*|1|*2*|3",
- "Add the digits together: 2+1+6+8+1+0+7+2+6+1+2+1+1+8+1+1+6+1+2+3=60",
- "The number passes because 60 is divisible by 10"
- }
- end
- -- The function get_board() allows you to specify an 18x7 "ASCII art" grid to customize
- -- the layout of the board and the placement of the input and output terminals. Generally
- -- speaking, inputs are placed on the left of boards, while outputs are placed on the right.
- --
- -- For empty space, use the '.' character.
- -- For buildable board space, use the '#' character.
- -- For an input or output terminal, use characters '0' through '9' no more than once each.
- -- For the bottom-left corner of the radio, use the 'R' character.
- -- For the bottom-left corners of dials, use characters 'A', 'B', and 'C'.
- --
- function get_board()
- return [[
- .0123##########...
- .....##########...
- .###############..
- .################4
- .###############..
- .##############...
- .##############...
- ]]
- end
- -- The function get_data() is called both to establish information about the puzzle (such as what
- -- the inputs and outputs are) and to generate the random test cases. Signal levels and XBus data
- -- should change from call to call, but information like the names and types of terminals should
- -- not change at all.
- --
- -- To create a standard input or output terminal, call create_terminal(). Valid terminal types are
- -- TYPE_SIMPLE, TYPE_XBUS, and TYPE_XBUS_NONBLOCKING. Valid terminal directions are DIR_INPUT and
- -- DIR_OUTPUT. Valid data for a simple I/O signal is an array of integers, 0 - 100 inclusive. Valid
- -- data for an XBus signal is an array of integer arrays, each with values -999 to 999 inclusive.
- --
- -- create_terminal(name, board_character, type, direction, data)
- --
- -- To create a radio (C2S-RF901), call create_radio(). You may only create one radio in each puzzle.
- -- Since radios are XBus-only, the only valid data for data_rx and data_tx are arrays of integer arrays,
- -- each with values -999 to 999 inclusive. You cannot customize the signal names for a radio.
- --
- -- By default the radio will be placed in the bottom-left corner of the screen. However, if you use an
- -- 'R' character in your board layout, the bottom-left corner of the radio will be placed there instead.
- --
- -- create_radio(data_rx, data_tx)
- --
- -- To create a dial (N4DL-1000), call create_dial(). You may create up to three dials in each puzzle.
- -- The names of dials should be kept short, as there is not much room to display them visually. A valid
- -- value is an integer between 0 and 99, inclusive.
- --
- -- By default dials will be placed in the bottom-left corner of the screen. However, if you use an
- -- 'A', 'B', or 'C' character in your board layout, the bottom-left corners of the first, second, and
- -- third dials will be placed there, respectively.
- --
- -- create_dial(name, value)
- --
- -- NOTE: To generate random values you should use math.random(). However, you SHOULD NOT seed
- -- the random number generator with a new seed value, as that is how the game ensures that
- -- the first test run is consistent for all users, and thus something that allows for the
- -- comparison of cycle scores.
- --
- -- NOTE: Fun fact! Arrays in Lua are implemented as tables (dictionaries) with integer keys that
- -- start at 1 by convention. Contrast this with nearly every other programming language, in
- -- which arrays start with an index of 0. Because of this, the 60 "time slices" that make
- -- up a test case are indexed from 1 to 60 inclusive.
- --
- function random_sequence(length)
- local result = {}
- for i = 1, length do
- result[i] = math.random(0, 9)
- end
- return result
- end
- function get_luhn_digit(seq)
- local checksum = 0
- for i = 1, #seq do
- local digit = seq[#seq - i + 1]
- if i % 2 == 1 then
- digit = digit * 2
- end
- if digit < 10 then
- checksum = checksum + digit
- else
- checksum = checksum + 1 + (digit - 10)
- end
- end
- return (10 - checksum % 10) % 10
- end
- function get_data()
- local inputs = {{}, {}, {}, {}}
- local output = {}
- local current_lane = 1
- local time = 1
- while time <= 60 do
- local seq = random_sequence(14)
- local correct_luhn = get_luhn_digit(seq)
- local last_digit
- if math.random(1, 5) == 1 then
- last_digit = math.random(0, 9)
- else
- last_digit = correct_luhn
- end
- seq[15] = last_digit
- inputs[current_lane][time] = seq
- if last_digit != correct_luhn then
- output[time] = 100
- end
- if current_lane < 4 then
- current_lane = current_lane + 1
- else
- current_lane = 1
- end
- time = time + 4
- end
- create_terminal("card#", "0", TYPE_XBUS, DIR_INPUT, inputs[1])
- create_terminal("card#", "1", TYPE_XBUS, DIR_INPUT, inputs[2])
- create_terminal("card#", "2", TYPE_XBUS, DIR_INPUT, inputs[3])
- create_terminal("card#", "3", TYPE_XBUS, DIR_INPUT, inputs[4])
- create_terminal("error", "4", TYPE_SIMPLE, DIR_OUTPUT, output)
- end
Advertisement
Add Comment
Please, Sign In to add comment