Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Cube
- local Cube = {}
- function Cube.new()
- local faces = {}
- for i = 0, 5 do
- faces[i] = {}
- for n = 0, 8 do
- faces[i][n] = i
- end
- end
- return setmetatable({
- faces = faces
- }, {
- __index = Cube,
- __tostring = Cube.tostring
- })
- end
- function Cube:R(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 2, 8, 3 do
- t = f[0][i]
- f[0][i] = f[2][i]
- f[2][i] = f[5][10 - i]
- f[5][10 - i] = f[4][10 - i - 2]
- f[4][10 - i - 2] = t
- end
- -- Rotate face 3 CW
- local f = f[3]
- t = f[0]
- f[0] = f[6]
- f[6] = f[8]
- f[8] = f[2]
- f[2] = t
- t = f[1]
- f[1] = f[3]
- f[3] = f[7]
- f[7] = f[5]
- f[5] = t
- end
- end
- function Cube:L(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 0, 6, 3 do
- t = f[0][i]
- f[0][i] = f[4][6 - i + 2]
- f[4][6 - i + 2] = f[5][6 - i]
- f[5][6 - i] = f[2][i]
- f[2][i] = t
- end
- -- Rotate face 1 CW
- local f = f[1]
- t = f[0]
- f[0] = f[6]
- f[6] = f[8]
- f[8] = f[2]
- f[2] = t
- t = f[1]
- f[1] = f[3]
- f[3] = f[7]
- f[7] = f[5]
- f[5] = t
- end
- end
- function Cube:U(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 0, 2 do
- t = f[1][i]
- f[1][i] = f[2][i]
- f[2][i] = f[3][i]
- f[3][i] = f[4][i]
- f[4][i] = t
- end
- -- Rotate face 1 CW
- local f = f[0]
- t = f[0]
- f[0] = f[6]
- f[6] = f[8]
- f[8] = f[2]
- f[2] = t
- t = f[1]
- f[1] = f[3]
- f[3] = f[7]
- f[7] = f[5]
- f[5] = t
- end
- end
- function Cube:D(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 6, 8 do
- t = f[4][i]
- f[4][i] = f[3][i]
- f[3][i] = f[2][i]
- f[2][i] = f[1][i]
- f[1][i] = t
- end
- -- Rotate face 1 CW
- local f = f[5]
- t = f[0]
- f[0] = f[2]
- f[2] = f[8]
- f[8] = f[6]
- f[6] = t
- t = f[1]
- f[1] = f[5]
- f[5] = f[7]
- f[7] = f[3]
- f[3] = t
- end
- end
- function Cube:F(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 0, 2 do
- t = f[0][6 + i]
- f[0][6 + i] = f[1][6 - 3 * i + 2]
- f[1][6 - 3 * i + 2] = f[5][8 - i]
- f[5][8 - i] = f[3][3 * i]
- f[3][3 * i] = t
- end
- -- Rotate face 2 CW
- local f = f[2]
- t = f[0]
- f[0] = f[6]
- f[6] = f[8]
- f[8] = f[2]
- f[2] = t
- t = f[1]
- f[1] = f[3]
- f[3] = f[7]
- f[7] = f[5]
- f[5] = t
- end
- end
- function Cube:B(n)
- n = n % 4
- local t = 0
- local f = self.faces
- for _ = 1, n do
- for i = 0, 2 do
- t = f[0][i]
- f[0][i] = f[3][3 * i + 2]
- f[3][3 * i + 2] = f[5][2 - i]
- f[5][2 - i] = f[1][6 - 3 * i]
- f[1][6 - 3 * i] = t
- end
- -- Rotate face 4 CW
- local f = f[4]
- t = f[0]
- f[0] = f[6]
- f[6] = f[8]
- f[8] = f[2]
- f[2] = t
- t = f[1]
- f[1] = f[3]
- f[3] = f[7]
- f[7] = f[5]
- f[5] = t
- end
- end
- function Cube:value(n)
- if n < 0 or n > 5 or n % 1 ~= 0 then
- return 0
- end
- local sum = 0
- for i = 0, 8 do
- sum = sum + self.faces[n][i]
- end
- return sum
- end
- function Cube:tostring()
- local s = ""
- for y = 0, 2 do
- s = s .. " "
- for x = 0, 2 do
- s = s .. self.faces[0][y * 3 + x]
- end
- s = s .. "\n"
- end
- for y = 0, 2 do
- for i = 1, 4 do
- for x = 0, 2 do
- s = s .. self.faces[i][y * 3 + x]
- end
- end
- s = s .. "\n"
- end
- for y = 2, 0, -1 do
- s = s .. " "
- for x = 0, 2 do
- s = s .. self.faces[5][y * 3 + x]
- end
- s = s .. "\n"
- end
- return s:sub(1, #s - 1)
- end
- -- Interpreter
- local C = {}
- setmetatable(C, {__call = C.new})
- function C.new()
- return setmetatable({
- instance = true,
- cube = Cube.new(),
- notepad = 0,
- input = 0,
- command = nil
- }, {__index = C})
- end
- function C:exec(program)
- assert(self.instance, "Must be an instance, call new() first")
- assert(type(program) == "string", "program must be a string")
- self.program = program
- for i = 1, #self.program do
- local c = self.program:sub(i, i)
- local n = tonumber(c)
- if n then
- if not self.command then
- print(self.cube:tostring())
- return
- end
- if self.command then
- self:command(n)
- end
- else
- self.command = self.commands[c]
- end
- end
- end
- function C:value(n)
- if n % 1 ~= 0 then
- return 0
- end
- if n >= 0 and n <= 5 then
- return self.cube:value(n)
- elseif n == 6 then
- return self.notepad
- elseif n == 7 then
- return self.input
- else
- return 0
- end
- end
- C.commands = {
- ['R'] = function(self, n)
- self.cube:R(n)
- end,
- ['L'] = function(self, n)
- self.cube:L(n)
- end,
- ['U'] = function(self, n)
- self.cube:U(n)
- end,
- ['D'] = function(self, n)
- self.cube:D(n)
- end,
- ['F'] = function(self, n)
- self.cube:F(n)
- end,
- ['B'] = function(self, n)
- self.cube:B(n)
- end,
- ['&'] = function(self, n)
- self.program = ""
- end,
- ['+'] = function(self, n)
- self.notepad = self.notepad + self:value(n)
- end,
- ['-'] = function(self, n)
- self.notepad = self.notepad - self:value(n)
- end,
- ['*'] = function(self, n)
- self.notepad = self.notepad * self:value(n)
- end,
- ['/'] = function(self, n)
- self.notepad = math.floor(self.notepad / self:value(n))
- end,
- ['^'] = function(self, n)
- self.notepad = self.notepad ^ self:value(n)
- end,
- ['='] = function(self, n)
- self.notepad = (self.notepad == self:value(n)) and 1 or 0
- end,
- [':'] = function(self, n)
- self.notepad = self:value(n)
- end,
- ['@'] = function(self, n)
- --print(self:value(n) % 256)
- io.write(string.char(self:value(n) % 256))
- end,
- ['%'] = function(self, n)
- io.write(self:value(n))
- end,
- ['$'] = function(self, n)
- local inp = io.read("*n")
- self.input = inp or self.input
- end,
- ['~'] = function(self, n)
- local inp = io.read(1)
- self.input = inp and string.byte(inp) or -1
- end,
- ['#'] = function(self, n)
- print(self.cube:tostring())
- print()
- end
- }
- local cubically = C.new()
- cubically:exec(select(1, ...))
- print()
- print("============")
- print("Final state:")
- print(cubically.cube:tostring())
- print("Notepad: " .. cubically.notepad)
- print("Input: " .. cubically.input)
Advertisement
Add Comment
Please, Sign In to add comment