-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_console.go
148 lines (127 loc) · 3.55 KB
/
game_console.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
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
type instruction struct {
operation string
argument int
visited bool
}
func (i *instruction) String() string {
return fmt.Sprintf("%v %v", i.operation, i.argument)
}
func buildInstructions(input *[]string) []*instruction {
instructions := make([]*instruction, len(*input))
for i, line := range *input {
splitParts := strings.Split(line, " ")
convertedArgument, err := strconv.Atoi(splitParts[1])
check(err)
newInt := instruction{visited: false}
newInt.operation = splitParts[0]
newInt.argument = convertedArgument
instructions[i] = &newInt
}
return instructions
}
type cpu struct {
instructions []*instruction
accumulator int
currentIndex int
lastInstructionExecuted *instruction
halted bool
output string
input []string
debug bool
}
func buildCPU(input []string) *cpu {
console := cpu{}
console.accumulator = 0
console.currentIndex = 0
console.input = input
console.instructions = buildInstructions(&console.input)
console.halted = false
return &console
}
func (console *cpu) Next() {
console.currentIndex++
}
func (console *cpu) Tick() {
if console.currentIndex >= len(console.instructions) {
console.halted = true
console.output = fmt.Sprintf("OUT Program terminated successfully, Accumulator: %v", console.accumulator)
return
}
currentInstruction := console.instructions[console.currentIndex]
if currentInstruction.visited {
console.halted = true
console.output = fmt.Sprintf("ERR Infinite loop detected! Last Instruction: %v, Accumulator: %v", console.lastInstructionExecuted, console.accumulator)
return
}
console.lastInstructionExecuted = currentInstruction
switch currentInstruction.operation {
case "acc":
console.accumulator += currentInstruction.argument
currentInstruction.visited = true
case "jmp":
console.currentIndex += currentInstruction.argument
currentInstruction.visited = true
return
case "nop":
currentInstruction.visited = true
}
console.Next()
}
func (console *cpu) Boot() string {
for !console.halted {
if console.debug && console.currentIndex < len(console.instructions) {
log.Printf("%v %v", console.currentIndex, console.instructions[console.currentIndex])
}
console.Tick()
}
return console.output
}
func (console *cpu) Reset() {
console.accumulator = 0
console.currentIndex = 0
console.halted = false
console.output = ""
console.lastInstructionExecuted = nil
console.instructions = buildInstructions(&console.input)
}
func (console *cpu) CorrectErrors() string {
var nopIndexes, jmpIndexes []int
for i, inst := range console.instructions {
switch inst.operation {
case "nop":
nopIndexes = append(nopIndexes, i)
case "jmp":
jmpIndexes = append(jmpIndexes, i)
default:
continue
}
}
for _, nopIndex := range nopIndexes {
console.Reset()
oldInstruction := console.instructions[nopIndex]
newInstruction := instruction{operation: "jmp", argument: oldInstruction.argument, visited: false}
console.instructions[nopIndex] = &newInstruction
output := console.Boot()
if strings.Contains(output, "success") {
return output
}
}
for _, jmpIndex := range jmpIndexes {
console.Reset()
oldInstruction := console.instructions[jmpIndex]
newInstruction := instruction{operation: "nop", argument: oldInstruction.argument, visited: false}
console.instructions[jmpIndex] = &newInstruction
output := console.Boot()
if strings.Contains(output, "success") {
return output
}
}
return ""
}