-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathts.ts
58 lines (47 loc) · 1.6 KB
/
ts.ts
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
import readlineSync from 'readline-sync'
const readline = () => readlineSync.prompt({ encoding: 'utf-8', prompt: '' })
// ------ Everything above this line will get cut when running copy script
type Operation = 'VALUE' | 'ADD' | 'SUB' | 'MULT'
type Cell = { operation: Operation; arg1: string; arg2: string }
const n = parseInt(readline())
let cells: Cell[] = []
let cache: { [ref: string]: number } = {}
const refToInt = (cellData: string) => parseInt(cellData.replace('$', ''), 10)
const computeCell = (cell: Cell) => {
let arg1: number, arg2: number
if (cell.arg1.startsWith('$') && cell.arg1 in cache) arg1 = cache[cell.arg1]
else if (!cell.arg1.startsWith('$')) {
arg1 = parseInt(cell.arg1, 10)
cache[cell.arg1] = arg1
} else {
arg1 = computeCell(cells[refToInt(cell.arg1)])
cache[cell.arg1] = arg1
}
if (cell.arg2.startsWith('$') && cell.arg2 in cache) arg2 = cache[cell.arg2]
else if (cell.arg2.startsWith('_')) arg2 = -1
else if (!cell.arg2.startsWith('$')) {
arg2 = parseInt(cell.arg2, 10)
cache[cell.arg2] = arg2
} else {
arg2 = computeCell(cells[refToInt(cell.arg2)])
cache[cell.arg2] = arg2
}
switch (cell.operation) {
case 'VALUE':
return arg1
case 'ADD':
return arg1 + arg2
case 'SUB':
return arg1 - arg2
case 'MULT':
return arg1 * arg2
}
}
for (let i = 0; i < n; i++) {
const [operation, arg1, arg2] = readline().split(' ')
cells.push({ operation: operation as Operation, arg1, arg2 })
}
for (let i = 0; i < n; i++) {
const cellValue = computeCell(cells[i])
console.log(cellValue === -0 ? 0 : cellValue)
}