length(ARGS) == 2 || error("JuliaMARS v0.1 - usage: julia juliamars.jl warrior1.red warrior2.red") const CORESIZE = 8000 const MAXPROCS = 8000 const MAXCYCLES = 80000 const MAXLENGTH = 100 const ROUNDS = 100 const FNUMBER = 7293 const OPCODES = Dict( "dat" => :dat, "nop" => :nop, "mov" => :mov, "add" => :add, "sub" => :sub, "mul" => :mul, "div" => :div, "mod" => :mod, "jmp" => :jmp, "jmz" => :jmz, "jmn" => :jmn, "djn" => :djn, "spl" => :spl, "seq" => :seq, "sne" => :sne, "slt" => :slt, "cmp" => :cmp) const MODS = Dict( "i" => :i, "f" => :f, "x" => :x, "a" => :a, "b" => :b, "ab" => :ab, "ba" => :ba) const OPCODE, MODIFIER, AMODE, AFIELD, BMODE, BFIELD = 1, 2, 3, 4, 5, 6 const FIELDS = Dict( :a => [(AFIELD, AFIELD)], :b => [(BFIELD, BFIELD)], :ab => [(AFIELD, BFIELD)], :ba => [(BFIELD, AFIELD)], :f => [(AFIELD, AFIELD), (BFIELD, BFIELD)], :i => [(AFIELD, AFIELD), (BFIELD, BFIELD)], :x => [(BFIELD, AFIELD), (AFIELD, BFIELD)]) const ADDRS = Dict( "#" => :immediate, "\$" => :direct, "*" => :aindirect, "@" => :bindirect, "{" => :apredec, "}" => :apostinc, "<" => :bpredec, ">" => :bpostinc) const OPS = Dict(:add => +, :sub => -, :mul => *, :div => div, :mod => mod) @inbounds function eval_operand(mode::Symbol, field::Int, core::Vector{Vector{Any}}, pc::Int) mode === :immediate && return pc, copy(core[pc + 1]) addr = (pc + field) % CORESIZE ins = core[addr + 1] mode === :direct && return addr, copy(ins) field = mode in (:bindirect, :bpredec, :bpostinc) ? BFIELD : AFIELD mode in (:bpredec, :apredec) && (ins[field] = mod(ins[field]::Int - 1, CORESIZE)) addr = (addr + ins[field]::Int) % CORESIZE indi = copy(core[addr + 1]) mode in (:bpostinc, :apostinc) && (ins[field] = mod(ins[field]::Int + 1, CORESIZE)) addr, indi end function load_warrior(filename::String) warrior = Vector{Any}[] start = 0 for line in eachline(filename) line = strip(lowercase(line)) (isempty(line) || startswith(line, r";|org|program|end")) && continue if startswith(line, "start") start = length(warrior) line = replace(line, r"^start\s+" => "") end m = match(r"(\w+)\.(\w+)\s*(.?)\s*(-?\d+)\s*,\s*(.?)\s*(-?\d+)", line) m === nothing && error("Invalid instruction: $line") opcode, modifier, amode, afield, bmode, bfield = m.captures ins = Any[get(OPCODES, opcode, nothing), get(MODS, modifier, nothing), get(ADDRS, amode, nothing), mod(parse(Int, afield), CORESIZE), get(ADDRS, bmode, nothing), mod(parse(Int, bfield), CORESIZE)] any(isnothing, ins) && error("Invalid instruction: $line") push!(warrior, ins) end warrior, start end function main() win, loss, tie = 0, 0, 0 prng = FNUMBER - MAXLENGTH redcode = [load_warrior(file) for file in ARGS] warriors = [x[1] for x in redcode] start = [x[2] for x in redcode] for r in 1:ROUNDS core = Vector{Any}[Any[:dat, :f, :direct, 0, :direct, 0] for _ in 1:CORESIZE] w2start = prng % (CORESIZE + 1 - 2 * MAXLENGTH) + MAXLENGTH procs = [Int[start[1]], Int[w2start + start[2]]] core[1:length(warriors[1])] .= copy.(warriors[1]) core[(w2start + 1):(w2start + length(warriors[2]))] .= copy.(warriors[2]) war = isodd(r) ? 1 : 2 @inbounds for _ in 1:(MAXCYCLES * 2) pc = popfirst!(procs[war]) ir = copy(core[pc + 1]) pa, ia = eval_operand(ir[AMODE]::Symbol, ir[AFIELD]::Int, core, pc) pb, ib = eval_operand(ir[BMODE]::Symbol, ir[BFIELD]::Int, core, pc) op = ir[OPCODE]::Symbol modf = ir[MODIFIER]::Symbol if op === :mov if modf === :i core[pb + 1] = copy(ia) else for (src, dst) in FIELDS[modf] core[pb + 1][dst] = ia[src]::Int end end push!(procs[war], (pc + 1) % CORESIZE) elseif op === :spl push!(procs[war], (pc + 1) % CORESIZE) length(procs[war]) < MAXPROCS && push!(procs[war], pa) elseif op in (:jmz, :jmn, :djn) if op === :djn for (src, dst) in FIELDS[modf] core[pb + 1][dst] = mod(core[pb + 1][dst]::Int - 1, CORESIZE) ib[dst] = mod(ib[dst]::Int - 1, CORESIZE) end end zero = all(((src, dst),) -> ib[dst]::Int == 0, FIELDS[modf]) push!(procs[war], xor(op === :jmz, zero) ? (pc + 1) % CORESIZE : pa) elseif op in (:add, :sub, :mul) for (src, dst) in FIELDS[modf] core[pb + 1][dst] = mod(OPS[op](ib[dst]::Int, ia[src]::Int), CORESIZE) end push!(procs[war], (pc + 1) % CORESIZE) elseif op in (:seq, :sne, :cmp) if modf === :i equal = ia == ib else equal = all(((src, dst),) -> ia[src]::Int == ib[dst]::Int, FIELDS[modf]) end push!(procs[war], mod(pc + (xor(op != :sne, equal) ? 1 : 2), CORESIZE)) elseif op === :slt less = all(((src, dst),) -> ia[src]::Int < ib[dst]::Int, FIELDS[modf]) push!(procs[war], (pc + (less ? 2 : 1)) % CORESIZE) elseif op === :jmp push!(procs[war], pa) elseif op === :nop push!(procs[war], (pc + 1) % CORESIZE) elseif op in (:mod, :div) nonzero = true for (src, dst) in FIELDS[modf] if ia[src]::Int == 0 nonzero = false else core[pb + 1][dst] = mod(OPS[op](ib[dst]::Int, ia[src]::Int), CORESIZE) end end nonzero && push!(procs[war], (pc + 1) % CORESIZE) end isempty(procs[war]) && break war = 3 - war end if isempty(procs[1]) loss += 1 elseif isempty(procs[2]) win += 1 else tie += 1 end prng = mod(16807 * (prng % 127773) - 2836 * div(prng, 127773), 2147483647) # Park-Miller MINSTD PRNG end println("$win $tie\n$loss $tie") end main()