-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
139 lines (122 loc) · 2.25 KB
/
main.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
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
package main
import (
"bufio"
"io"
"strconv"
"strings"
)
func fs(input io.Reader, reg map[string]int) (int, error) {
scanner := bufio.NewScanner(input)
var instructions []Instruction
for scanner.Scan() {
s := scanner.Text()
idx := indexAll(s, " ")
if strings.Index(s, "cpy") != -1 {
s2 := s[idx[1]+1:]
s1 := s[idx[0]+1 : idx[1]]
i1, err := strconv.Atoi(s1)
if err != nil {
instructions = append(instructions, Cpy{
vr1: ValueReg{r: &s1},
r2: s2,
})
} else {
instructions = append(instructions, Cpy{
vr1: ValueReg{i: &i1},
r2: s2,
})
}
} else if strings.Index(s, "inc") != -1 {
instructions = append(instructions, Inc{s[4:]})
} else if strings.Index(s, "dec") != -1 {
instructions = append(instructions, Dec{s[4:]})
} else if strings.Index(s, "jnz") != -1 {
s2 := s[idx[1]+1:]
i2, err := strconv.Atoi(s2)
if err != nil {
panic(err)
}
s1 := s[idx[0]+1 : idx[1]]
i1, err := strconv.Atoi(s1)
if err != nil {
instructions = append(instructions, Jnz{
vr1: ValueReg{r: &s1},
jump: i2,
})
} else {
instructions = append(instructions, Jnz{
vr1: ValueReg{i: &i1},
jump: i2,
})
}
} else {
panic(s)
}
}
for i := 0; i < len(instructions); {
i += instructions[i].apply(reg)
}
return reg["a"], nil
}
type Instruction interface {
apply(reg map[string]int) int
}
type Cpy struct {
vr1 ValueReg
r2 string
}
func (c Cpy) apply(reg map[string]int) int {
if c.vr1.r != nil {
reg[c.r2] = reg[*c.vr1.r]
} else {
reg[c.r2] = *c.vr1.i
}
return 1
}
type Inc struct {
r string
}
func (i Inc) apply(reg map[string]int) int {
reg[i.r]++
return 1
}
type Dec struct {
r string
}
func (d Dec) apply(reg map[string]int) int {
reg[d.r]--
return 1
}
type Jnz struct {
vr1 ValueReg
jump int
}
func (j Jnz) apply(reg map[string]int) int {
v := 0
if j.vr1.r != nil {
v = reg[*j.vr1.r]
} else {
v = *j.vr1.i
}
if v != 0 {
return j.jump
}
return 1
}
type ValueReg struct {
r *string
i *int
}
func indexAll(s string, search string) []int {
i := 0
var res []int
for i < len(s) {
index := strings.Index(s[i:], search)
if index == -1 {
return res
}
res = append(res, index+i)
i += index + len(search)
}
return res
}