-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintcode.jl
142 lines (117 loc) · 3.78 KB
/
intcode.jl
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
rndict = Dict(1 => 3, 2 => 3,
3 => 1, 4 => 1,
5 => 2, 6 => 2,
7 => 3, 8 => 3,
9 => 1,
99 => 0)
struct OpCode
instr::Int
mode::Array{Int, 1}
N::Int
end
function OpCode(c::Int)
digs = digits(c)
oc = digs[1] + (length(digs) > 1 ? 10*digs[2] : 0)
N = rndict[oc]
mode = zeros(Int, N)
mode[1:length(digs[3:end])] = digs[3:end]
return OpCode(oc, mode, N)
end
function intcode_computer(program::Array{Int,1}, input::Channel{Int}, output::Channel{Union{Int, Symbol}}; pc::Int=1)
Np = length(program)
Np_new = 10*Np
for j=1:(Np_new-Np)
push!(program, 0)
end
jump_flag = false
rel_base = 0
function load(addr::Int, mode::Int)::Int
#println("Load: $(addr) -- $(mode)")
if mode == 0
return program[addr+1]
elseif mode == 1
return addr
elseif mode == 2
return program[addr + rel_base + 1]
else
error("Unknown mode $(mode).")
end
end
function store(data::Int, addr::Int, mode::Int)
#println("Store: $(data) --> $(addr) -- $(mode)")
if mode == 0
program[addr+1] = data
elseif mode == 1
error("Invalid mode 1 for store instruction.")
elseif mode == 2
program[addr + rel_base + 1] = data
else
error("Unknown mode $(mode).")
end
end
function load_input(dst::Int, mode::Int)
#println("Waiting for next input: store to $(dst) -- $(mode)")
inp = take!(input)
store(inp, dst, mode)
end
function store_output(data::Int)
#println("Tried to store output $(data)")
put!(output, data)
end
function jtrue(x::Int, y::Int)
if x != 0
pc = y+1
jump_flag = true
end
end
function jfalse(x::Int, y::Int)
if x == 0
pc = y+1
jump_flag = true
end
end
lt(x::Int, y::Int)::Int = Int(x < y)
eq(x::Int, y::Int)::Int = Int(x == y)
while program[pc] != 99
jump_flag = false
oc = OpCode(program[pc])
#println(oc)
if oc.instr == 1
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
store(x+y, program[pc+3], oc.mode[3])
elseif oc.instr == 2
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
store(x*y, program[pc+3], oc.mode[3])
elseif oc.instr == 3
load_input(program[pc+1], oc.mode[1])
elseif oc.instr == 4
x = load(program[pc+1], oc.mode[1])
store_output(x)
elseif oc.instr == 5
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
jtrue(x, y)
elseif oc.instr == 6
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
jfalse(x, y)
elseif oc.instr == 7
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
store(lt(x,y), program[pc+3], oc.mode[3])
elseif oc.instr == 8
x = load(program[pc+1], oc.mode[1])
y = load(program[pc+2], oc.mode[2])
store(eq(x,y), program[pc+3], oc.mode[3])
elseif oc.instr == 9
rel_base += load(program[pc+1], oc.mode[1])
else
@error "Something went wrong! Got code $(oc.instr)."
end
pc += (jump_flag ? 0 : oc.N+1)
end
println("Computer finished!")
put!(output, :done);
end