Skip to content

Instantly share code, notes, and snippets.

@GreenLightning
Created June 16, 2015 19:00
Show Gist options
  • Select an option

  • Save GreenLightning/0bac5bb63aa1e3a09896 to your computer and use it in GitHub Desktop.

Select an option

Save GreenLightning/0bac5bb63aa1e3a09896 to your computer and use it in GitHub Desktop.

Revisions

  1. GreenLightning created this gist Jun 16, 2015.
    113 changes: 113 additions & 0 deletions 19392494.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    -- For anyone who liked the PRIME DETECTOR puzzle included in some of the early access
    -- builds of TIS-100...

    -- INSTALLATION: Copy everything in this file. Open TIS-100. Go to the SPECIFICATION
    -- EDITOR. Click on IMPORT SPECIFICATION FROM CLIPBOARD. Done!

    -- The function get_name() should return a single string that is the name of the puzzle.
    --
    function get_name()
    return "PRIME DETECTOR"
    end

    -- The function get_description() should return an array of strings, where each string is
    -- a line of description for the puzzle. The text you return from get_description() will
    -- be automatically formatted and wrapped to fit inside the puzzle information box.
    --
    function get_description()
    return { "READ A VALUE FROM IN", "WRITE 1 TO OUT IF IT IS PRIME", "IF NOT TRUE, WRITE 0 INSTEAD" }
    end

    -- The function get_streams() should return an array of streams. Each stream is described
    -- by an array with exactly four values: a STREAM_* value, a name, a position, and an array
    -- of integer values between -999 and 999 inclusive.
    --
    -- STREAM_INPUT: An input stream containing up to 39 numerical values.
    -- STREAM_OUTPUT: An output stream containing up to 39 numerical values.
    -- STREAM_IMAGE: An image output stream, containing exactly 30*18 numerical values between 0
    -- and 4, representing the full set of "pixels" for the target image.
    --
    -- NOTE: Arrays in Lua are implemented as tables (dictionaries) with integer keys that start
    -- at 1 by convention. The sample code below creates an input array of 39 random values
    -- and an output array that doubles all of the input values.
    --
    -- 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 TIS-100 ensures that
    -- the first test run is consistent for all users, and thus something that allows for the
    -- comparison of cycle scores.
    --
    -- NOTE: Position values for streams should be between 0 and 3, which correspond to the far
    -- left and far right of the TIS-100 segment grid. Input streams will be automatically
    -- placed on the top, while output and image streams will be placed on the bottom.
    --
    function get_streams()
    local input = {}
    local output = {}
    for i = 1, 39 do
    -- This makes sure that about 50% of the numbers are prime.
    -- We use a known prime in only 40% of cases, because some of
    -- the random numbers are prime as well. That is why we need
    -- to check whether the input is prime separately as well.
    if math.random() < 0.4 then
    input[i] = primes[math.random(#primes)]
    else
    input[i] = math.random(100, 999)
    end
    if is_prime(input[i]) then
    output[i] = 1
    else
    output[i] = 0
    end
    end
    return {
    { STREAM_INPUT, "IN", 1, input },
    { STREAM_OUTPUT, "OUT", 2, output }
    }
    end

    -- The function get_layout() should return an array of exactly 12 TILE_* values, which
    -- describe the layout and type of tiles found in the puzzle.
    --
    -- TILE_COMPUTE: A basic execution node (node type T21).
    -- TILE_MEMORY: A stack memory node (node type T30).
    -- TILE_DAMAGED: A damaged execution node, which acts as an obstacle.
    --
    function get_layout()
    return {
    TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
    TILE_MEMORY, TILE_COMPUTE, TILE_COMPUTE, TILE_MEMORY,
    TILE_DAMAGED, TILE_COMPUTE, TILE_COMPUTE, TILE_COMPUTE,
    }
    end

    -- Returns true if number is one of the primes between 100 and 999.
    -- Otherwise returns false.
    --
    function is_prime(number)
    for _, prime in ipairs(primes) do
    if number == prime then
    return true
    end
    end
    return false
    end

    -- Primes between 100 and 999.
    --
    primes = {
    101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
    151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
    199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
    263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
    317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
    383, 389, 397, 401, 409, 419, 421, 431, 433, 439,
    443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
    503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
    577, 587, 593, 599, 601, 607, 613, 617, 619, 631,
    641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
    701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
    769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
    839, 853, 857, 859, 863, 877, 881, 883, 887, 907,
    911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
    983, 991, 997
    }