-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecember_18.adb
278 lines (260 loc) · 11.6 KB
/
December_18.adb
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
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Maps; use Ada.Strings.Maps;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Assertions; use Ada.Assertions;
procedure December_18 is
Input_File : File_Type;
Text : Unbounded_String;
Start_At, First : Positive;
Last : Natural;
Op_Set : constant Character_Set := To_Set ("sndsetaddmulmodrcvjgz");
Reg_Range : constant Character_Range := ('a', 'z');
Reg_Set : constant Character_Set := To_Set (Reg_Range);
Int_Set : constant Character_set := To_Set ("-0123456789");
type Op_Codes is (snd, set, add, mul, mod_op, rcv, jgz);
-- mod is a reserved word
subtype Registers is Character range 'a' .. 'z';
type instructions is record
Op_Code : Op_Codes;
Dst_Imm : Boolean; -- selects between destination and immediate
Destination : Registers := 'z';
Dst_Num : Long_Long_Integer := 0;
Src_Imm : Boolean; -- selects between source and immediate
Source : Registers := 'z';
Src_Num : Long_Long_Integer := 0;
end record;
subtype Addresses is Long_Long_Integer range 0 .. 100;
Type Register_Files is array (Registers) of Long_Long_Integer;
type Program_Stores is array (Addresses) of Instructions;
Register_File : Register_Files := (others => 0);
Program_Store : Program_Stores;
Last_Instruction : Addresses := 0;
Last_Sound : Long_Long_Integer := 0; -- for compatibility with register file
procedure Process_file is
Load_At : Addresses := 0;
function Get_Op_Code return Op_Codes is
Op_String : String (1 ..3);
begin -- Get_Op_Code
Find_Token (Text, Op_Set, Start_At, Inside, First, Last);
Op_String := Slice (Text, First, Last);
Start_At := Last + 1;
if Op_String = "snd" then
return snd;
elsif Op_String = "set" then
return set;
elsif Op_String = "add" then
return add;
elsif Op_String = "mul" then
return mul;
elsif Op_String = "mod" then
return mod_op;
elsif Op_String = "rcv" then
return rcv;
elsif Op_String = "jgz" then
return jgz;
else
Put_Line ("Unknown Op Code: " & Op_String);
assert (False);
return snd; -- this is unnreachable code
end if;
end Get_Op_Code;
function Get_Register return Registers is
begin -- Get_Register
Find_Token (Text, Reg_Set, Start_At, Inside, First, Last);
Start_at := Last + 1;
return Element (Text, First);
end Get_Register;
begin -- Process_File
while not End_Of_File (Input_File) loop
Text := To_Unbounded_String (Get_Line (Input_File));
Put_Line (To_String (Text) & " -> ");
Flush;
Start_At := 1;
Program_Store (Load_At).Op_Code := Get_Op_Code;
Find_Token (Text, Int_Set, Start_At, Inside, First, Last);
Program_Store (Load_At).Dst_Imm := First = 5;
if Program_Store (Load_At).Dst_Imm then
Program_Store (Load_At).Dst_Num :=
Long_Long_Integer'Value (Slice (Text, First, Last));
Start_At := Last + 1;
else
Program_Store (Load_At).Destination := Get_Register;
end if;
case Program_Store (Load_At).Op_Code is
when snd | rcv =>
null;
when set | add | mul | mod_op | jgz =>
Find_Token (Text, Int_Set, Start_At, Inside, First, Last);
Program_Store (Load_At).Src_Imm := Last > 0;
if Program_Store (Load_At).Src_Imm then
Program_Store (Load_At).Src_Num :=
Long_Long_Integer'Value (Slice (Text, First, Last));
else
Program_Store (Load_At).Source := Get_Register;
end if;
end case;
Put_Line (Addresses'Image (Load_At) & ": "
& Op_Codes'Image (Program_Store (Load_At).Op_Code) & " " &
Boolean'Image (Program_Store (Load_At).Dst_Imm) & " " &
Program_Store (Load_At).Destination & " " &
Long_Long_Integer'Image (Program_Store (Load_At).Dst_Num) & " " &
Boolean'Image (Program_Store (Load_At).Src_Imm) & " " &
Program_Store (Load_At).Source & " " &
Long_Long_Integer'Image (Program_Store (Load_At).Src_Num));
Flush;
Last_Instruction := Load_At;
Load_At := Load_At + 1;
end loop; -- process one Instruction
end Process_File;
procedure Execute is
Instruction_Pointer : Long_Long_Integer := 0;
procedure Put_State is
begin -- Put_State
Put (Long_Long_Integer'Image (Instruction_Pointer) & ": " );
Put (Op_Codes'Image (Program_Store (Instruction_Pointer).Op_Code)
& " ");
if Program_Store (Instruction_Pointer).Dst_Imm then
Put (Long_Long_Integer'Image (Program_Store (Instruction_Pointer).Dst_Num)
& " ");
else
Put (Program_Store (Instruction_Pointer).Destination & " ");
end if;
if Program_Store (Instruction_Pointer).Src_Imm then
Put_Line (Long_Long_Integer'Image (Program_Store (Instruction_Pointer).
Src_Num));
else
Put_Line (Program_Store (Instruction_Pointer).Source & "");
end if;
for I in Registers loop
Put (" (" & I & ")" & Long_Long_Integer'Image (Register_File (I)));
end loop;
New_Line;
end Put_State;
begin -- Execute
while (Instruction_Pointer >= 0) and
(Instruction_Pointer <= Last_Instruction) loop
Put_State;
case Program_Store (Instruction_Pointer).Op_Code is
when snd =>
if Program_Store (Instruction_Pointer).Dst_Imm then
Last_Sound := Program_Store (Instruction_Pointer).Dst_Num;
else
Last_Sound := Register_File
(Program_Store (Instruction_Pointer).Destination);
end if;
Instruction_Pointer := Instruction_Pointer + 1;
when set =>
if Program_Store (Instruction_Pointer).Src_Imm then
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Program_Store (Instruction_Pointer).Src_Num;
else
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Source);
end if; -- Src_Imm
Instruction_Pointer := Instruction_Pointer + 1;
when add =>
if Program_Store (Instruction_Pointer).Src_Imm then
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) +
Program_Store (Instruction_Pointer).Src_Num;
else
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) +
Register_File
(Program_Store (Instruction_Pointer).Source);
end if;
Instruction_Pointer := Instruction_Pointer + 1;
when mul =>
if Program_Store (Instruction_Pointer).Src_Imm then
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) *
Program_Store (Instruction_Pointer).Src_Num;
else
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) *
Register_File
(Program_Store (Instruction_Pointer).Source);
end if;
Instruction_Pointer := Instruction_Pointer + 1;
when mod_op =>
if Program_Store (Instruction_Pointer).Src_Imm then
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) mod
Program_Store (Instruction_Pointer).Src_Num;
else
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Register_File
(Program_Store (Instruction_Pointer).Destination) mod
Register_File
(Program_Store (Instruction_Pointer).Source);
end if;
Instruction_Pointer := Instruction_Pointer + 1;
when rcv =>
if Program_Store (Instruction_Pointer).Dst_Imm then
if Program_Store (Instruction_Pointer).Dst_num /= 0 then
exit;
end if;
else
if Register_File
(Program_Store (Instruction_Pointer).Destination) /= 0 then
Register_File
(Program_Store (Instruction_Pointer).Destination) :=
Last_Sound;
exit;
end if;
end if;
Instruction_Pointer := Instruction_Pointer + 1;
when jgz =>
if Program_Store (Instruction_Pointer).Dst_Imm then
if Program_Store (Instruction_Pointer).Dst_Num > 0 then
if Program_Store (Instruction_Pointer).Src_Imm then
Instruction_Pointer := Instruction_Pointer +
Program_Store (Instruction_Pointer).Src_Num;
else
Instruction_Pointer := Instruction_Pointer +
Register_File
(Program_Store (Instruction_Pointer).Source);
end if; -- Src_Imm
else
Instruction_Pointer := Instruction_Pointer + 1;
end if; -- test value is an immediate
else
if Register_File
(Program_Store (Instruction_Pointer).Destination) > 0 then
if Program_Store (Instruction_Pointer).Src_Imm then
Instruction_Pointer := Instruction_Pointer +
Program_Store (Instruction_Pointer).Src_Num;
else
Instruction_Pointer := Instruction_Pointer +
Register_File
(Program_Store (Instruction_Pointer).Source);
end if; -- Src_Imm
else
Instruction_Pointer := Instruction_Pointer + 1;
end if; -- register test value
end if;
end case; -- Op_Code
end loop; -- while termination consitions not met
end Execute;
begin -- December_18
Open (Input_File, In_File, "20171218.txt");
Process_File;
Close (Input_File);
Execute;
Put_Line ("Last Sound:" & Long_Long_Integer'Image (Last_Sound));
end December_18;