-
Notifications
You must be signed in to change notification settings - Fork 27
/
10033.go
75 lines (70 loc) · 1.5 KB
/
10033.go
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
// UVa 10033 - Interpreter
package main
import (
"fmt"
"os"
)
func solve(lines []string) int {
registers := make([]int, 10)
ram := make([]int, 1000)
cnt := 0
pointer := 0
for {
line := lines[pointer]
jump := false
switch {
case line == "100":
cnt++
return cnt
case line[0] == '2':
registers[line[1]-'0'] = int(line[2] - '0')
case line[0] == '3':
registers[line[1]-'0'] += int(line[2] - '0')
case line[0] == '4':
registers[line[1]-'0'] *= int(line[2] - '0')
case line[0] == '5':
registers[line[1]-'0'] = registers[line[2]-'0']
case line[0] == '6':
registers[line[1]-'0'] += registers[line[2]-'0']
case line[0] == '7':
registers[line[1]-'0'] *= registers[line[2]-'0']
case line[0] == '8':
registers[line[1]-'0'] = ram[registers[line[2]-'0']]
case line[0] == '9':
ram[registers[line[2]-'0']] = registers[line[1]-'0']
case line[0] == '0' && registers[line[2]-'0'] != 0:
jump = true
pointer = registers[line[1]-'0']
}
registers[line[1]-'0'] %= 1000
cnt++
if !jump {
pointer++
}
}
}
func main() {
in, _ := os.Open("10033.in")
defer in.Close()
out, _ := os.Create("10033.out")
defer out.Close()
var kase int
var line string
first := true
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
if first {
fmt.Fscanln(in)
first = false
} else {
fmt.Fprintln(out)
}
var lines []string
for {
if _, err := fmt.Fscanf(in, "%s", &line); err != nil {
break
}
lines = append(lines, line)
}
fmt.Fprintln(out, solve(lines))
}
}