Befunge-98

john Howard

Winner

Interesting implementation, though mostly the winner because the others didn't want the BlizzCon ticket.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
-- init IP list with our first IP
local IPL = {
    {
        id = 1,
        IP = {0,0},
        delta = {1,0},
        SO = {0, 0},
        SS = {{}}
    }
}
local IPLi = 1 -- indicates the index of the current IP in IPL
local nextID = 2; -- so we can keep the IDs unique even if the highest ends and a new one is made
local FS = { [0] = {}} -- funge sapce
--init some registers
local Ax, Bx
local stringmode = false
local oldseed = os.time() --initialize random seed
local extents = {{0,0},{0,0}}

-- instruction queue
IQ = {
    [10] = function()
            io.write("\n")
        end,
    [32] = function()
            advance()
            local instruction = fetch()
            local code = decode(instruction)
            execute(code)
        end,
    [33] = function()
            Ax = pop()
            if Ax == 0 then
                Ax = 1
            else
                Ax = 0
            end
            push(Ax)
        end,
    [34] = function()
            stringmode = not stringmode
        end,
    [35] = function()
            advance()
        end,
    [36] = function()
            pop()
        end,
    [37] = function()
            Ax = pop()
            Bx = pop()
            if Ax ~= 0 then
                push(Bx % Ax)
            else
                push(0)
            end
        end,
    [38] = function()
            Ax = io.read(1)
            if string.match(Ax,"%d") then
                push(Ax)
            else
                execute(38)
            end
        end,
    [39] = function()
            advance()
            push(decode(fetch()))
        end,
    [42] = function()
            Ax = pop()
            Bx = pop()
            push(Bx * Ax)
        end,
    [43] = function()
            Ax = pop()
            Bx = pop()
            push(Bx + Ax)
        end,
    [44] = function()
            Ax = pop()
            if Ax > 0 then
                io.write(string.char(Ax))
            end
        end,
    [45] = function()
            Ax = pop()
            Bx = pop()
            push(Bx - Ax)
        end,
    [46] = function()
            Ax = pop()
            io.write(Ax)
            io.write(" ")
        end,
    [47] = function()
            Ax = pop()
            Bx = pop()
            if Ax ~= 0 then
                push(math.floor(Bx / Ax))
            else
                push(0)
            end
        end,
    [48] = function()
            push(0)
        end,
    [49] = function()
            push(1)
        end,
    [50] = function()
            push(2)
        end,
    [51] = function()
            push(3)
        end,
    [52] = function()
            push(4)
        end,
    [53] = function()
            push(5)
        end,
    [54] = function()
            push(6)
        end,
    [55] = function()
            push(7)
        end,
    [56] = function()
            push(8)
        end,
    [57] = function()
            push(9)
        end,
    [58] = function()
            Ax = pop()
            push(Ax)
            push(Ax)
        end,
    [59] = function()
            local code = ""
            while code ~= 59 do --find next ;
                advance()
                code = decode(fetch())
            end
            --advance past ; and execute command
            advance()
            execute(decode(fetch()))
        end,
    [60] = function()
            IPL[IPLi].delta = {-1,0}
        end,
    [61] = function()
            local cmd = ""
            Ax = pop()
            while Ax ~= 0 do
                cmd = cmd .. string.char(Ax)
                Ax = pop()
            end
            push(os.execute(cmd))
        end,
    [62] = function()
            IPL[IPLi].delta = {1, 0}
        end,
    [63] = function()
            math.randomseed(oldseed)
            oldseed = oldseed * math.random(1000000)
            local num = math.floor(math.random(1,4))
            local deltas = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
            IPL[IPLi].delta = deltas[num]
        end,
    [64] = function()
            table.remove(IPL,IPLi)
            IPLi = IPLi - 1
        end,
    [91] = function()
            local curdelta = deepcopy(IPL[IPLi].delta)
            IPL[IPLi].delta[1] = curdelta[2]
            IPL[IPLi].delta[2] = curdelta[1] * -1
        end,
    [92] = function()
            Ax = pop()
            Bx = pop()
            push(Ax)
            push(Bx)
        end,
    [93] = function()
            local curdelta = deepcopy(IPL[IPLi].delta)
            IPL[IPLi].delta[2] = curdelta[1]
            IPL[IPLi].delta[1] = curdelta[2] * -1
        end,
    [94] = function()
            IPL[IPLi].delta = {0, -1}
        end,
    [95] = function()
            Ax = pop()
            if Ax ~= 0 then
                IPL[IPLi].delta = {-1, 0}
            else
                IPL[IPLi].delta = {1, 0}
            end
        end,
    [96] = function()
            Ax = pop()
            Bx = pop()
            if Ax > Bx then
                push(0)
            else
                push(1)
            end
        end,
    [97] = function()
            push(10)
        end,
    [98] = function()
            push(11)
        end,
    [99] = function()
            push(12)
        end,
    [100] = function()
            push(13)
        end,
    [101] = function()
            push(14)
        end,
    [102] = function()
            push(15)
        end,
    [103] = function()
            Ax = pop()
            Bx = pop()
            local char = FS[IPL[IPLi].SO[1] + Ax][IPL[IPLi].SO[2] + Bx]
            push(string.byte(char))
        end,
    [106] = function()
            Ax = pop()
            for i = 1, Ax do
                advance()
            end
        end,
    [107] = function()
            local i = pop()
            advance()
            local code = decode(fetch())
            while i > 0 do
                if code == 32 or code == 59 then
                    execute(code)
                    code = decode(fetch())
                else
                    execute(code)
                end
                i = i - 1
            end
        end,
    [110] = function()
            while #IPL[IPLi].SS[1] > 0 do
                pop()
            end
        end,
    [112] = function()
            Ax = pop()
            Bx = pop()
            local char = string.char(pop())
            if FS[IPL[IPLi].SO[1] + Ax] == nil then
                FS[IPL[IPLi].SO[1] + Ax] = {}
            end
            FS[IPL[IPLi].SO[1] + Ax][IPL[IPLi].SO[2] + Bx] = char
        end,
    [113] = function()
            Ax = pop()
            os.exit(Ax)
        end,
    [114] = function()
            IPL[IPLi].delta[1] = IPL[IPLi].delta[1] * -1
            IPL[IPLi].delta[2] = IPL[IPLi].delta[2] * -1
        end,
    [115] = function()
            Ax = pop()
            advance()
            FS[IPL[IPLi].IP[1]][IPL[IPLi].IP[2]] = string.char(Ax)
        end,
    [116] = function()
            local IP = {}
            IP.id = nextID
            nextID = nextID + 1
            IP.delta = {IPL[IPLi].delta[1] * -1, IPL[IPLi].delta[2] * -1}
            IP.IP = {IPL[IPLi].IP[1] + IP.delta[1], IPL[IPLi].IP[2] + IP.delta[2]}
            IP.SO = {IPL[IPLi].SO[1], IPL[IPLi].SO[2]}
            IP.SS = deepcopy(IPL[IPLi].SS)
            table.insert(IPL,IPLi,IP)
            IPLi = IPLi + 1
        end,
    [118] = function()
            IPL[IPLi].delta = {0, 1}
        end,
    [119] = function()
            Ax = pop()
            Bx = pop()
            if Ax > Bx then
                execute(91)
            elseif Ax < Bx then
                execute(93)
            else
                execute(122)
            end
        end,
    [120] = function()
            Ax = pop()
            Bx = pop()
            IPL[IPLi].delta = {Bx, Ax}
        end,
    [121] = function()
            Ax = pop()
            if Ax < 1 then

            end
        end,
    [122] = function()
            --do nothing
        end,
    [124] = function()
            Ax = pop()
            if Ax == 0 then
                IPL[IPLi].delta = {0, 1}
            else
                IPL[IPLi].delta = {0, -1}
            end
        end;
    [126] = function()
            push(decode(io.read(1)))
        end;
}

function deepcopy(curTable)
    local duptable = {}
    for i, v in pairs(curTable) do
        if type(v) == "table" then
            duptable[i] = deepcopy(v)
        else
            duptable[i] = v
        end
    end
    return duptable
end

function push(v)
    table.insert(IPL[IPLi].SS[1],v)
end

function pop()
    if IPL[IPLi].SS[1][1] then
        return table.remove(IPL[IPLi].SS[1])
    else
        return 0
    end
end

function advance()
    if #IPL > 0 then
        IPL[IPLi].IP[1] = IPL[IPLi].IP[1] + IPL[IPLi].delta[2] --deltas are switched to change between the way we
        IPL[IPLi].IP[2] = IPL[IPLi].IP[2] + IPL[IPLi].delta[1] --think of coordinates and the way tables are indexed
        if IPL[IPLi].IP[1] > extents[2][1] then
            IPL[IPLi].IP[1] = extents[1][1]
        elseif IPL[IPLi].IP[1] < extents[1][1] then
            IPL[IPLi].IP[1] = extents[2][1]
        end
        if IPL[IPLi].IP[2] > extents[2][2] then
            IPL[IPLi].IP[2] = extents[1][2]
        elseif IPL[IPLi].IP[2] < extents[1][2] then
            IPL[IPLi].IP[2] = extents[2][2]
        end
    end
end

function fetch()
    return FS[IPL[IPLi].IP[1]][IPL[IPLi].IP[2]]
end

function decode(char)
    if char == nil then
        char = " "
    end
    return string.byte(char)
end

function execute(code)
    if IQ[code] then
        IQ[code]()
    else
        IQ[114]()
    end
end

local function run(funge)
    local i = 0
    local j = 0
    --load funge into fungespace
    for c = 1, #funge do
        local char = string.sub(funge,c,c)
        if (char ~= '\n') then
            table.insert(FS[i], j, char)
            if j > extents[2][2] then
                extents[2][2] = j
            end
            j = j + 1
        else
            i = i + 1
            table.insert(FS, i, {})
            if i > extents[2][1] then
                extents[2][1] = i
            end
            j = 0
        end
    end


    --start main loop
    while #IPL > 0 do  --while there is a running IP
        IPLi = 1  --start at first IP
        while IPLi <= #IPL do  --cycle through IP list

            --fetch
            local instruction = fetch()

            --decode
            local code = decode(instruction)

            --execute
            if (not stringmode) or code == 34 then
                execute(code)
            else
                push(code)
                --consolidate concurrent spaces
                if code == 32 then
                    while FS[IPL[IPLi].IP[1] + IPL[IPLi].delta[2]][IPL[IPLi].IP[2] + IPL[IPLi].delta[1]] == " " do
                        IPL[IPLi].IP[1] = IPL[IPLi].IP[1] + IPL[IPLi].delta[2]
                        IPL[IPLi].IP[2] = IPL[IPLi].IP[2] + IPL[IPLi].delta[1]
                    end
                end
            end

            --advance IP
            advance()

            --next IP
            IPLi = IPLi + 1
        end
    end
end

Facts

Date created
15 Jun 2009
Updated
15 Jun 2009

Contestant