-
Notifications
You must be signed in to change notification settings - Fork 0
/
83-MicroAssembly.js
46 lines (39 loc) · 989 Bytes
/
83-MicroAssembly.js
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
// meidum - https://www.codingame.com/training/medium/micro-assembly
const [a, b, c, d] = readline().split(' ').map(Number)
const register = new Map()
register.set('a', a)
register.set('b', b)
register.set('c', c)
register.set('d', d)
const n = parseInt(readline())
const instructions = []
for (let i = 0; i < n; i++) {
instructions.push(readline().split(' '))
}
for (let i = 0; i < instructions.length; i++) {
const s = instructions[i]
const i2 = register.has(s[2]) ? register.get(s[2]) : parseInt(s[2])
const i3 =
s.length === 4
? register.has(s[3])
? register.get(s[3])
: parseInt(s[3])
: 0
switch (s[0]) {
case 'MOV':
register.set(s[1], i2)
break
case 'ADD':
register.set(s[1], i2 + i3)
break
case 'SUB':
register.set(s[1], i2 - i3)
break
case 'JNE':
if (i2 !== i3) {
i = parseInt(s[1]) - 1
}
break
}
}
console.log(Array.from(register.values()).join(' '))