-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (126 loc) · 2.82 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
140
141
142
143
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
type instruction struct {
command string
modifier int
visited bool
}
type program struct {
acc int
lines []instruction
current int
}
func (p *program) step() {
p.lines[p.current].visited = true
switch p.lines[p.current].command {
case "nop":
p.current++
case "jmp":
p.current += p.lines[p.current].modifier
case "acc":
p.acc += p.lines[p.current].modifier
p.current++
}
}
func (p *program) reset() {
p.acc = 0
p.current = 0
for i := 0; i < len(p.lines); i++ {
p.lines[i].visited = false
}
}
func (p *program) print() {
fmt.Println("acc", p.acc)
fmt.Println("cur", p.current)
for i := 0; i < len(p.lines); i++ {
fmt.Println(i, p.lines[i].command, p.lines[i].modifier)
}
fmt.Println("-----")
}
const notTerminated = 0
const infiniteLoop = 1
const normalTermination = 2
func (p *program) isTerminated() int {
if p.current >= len(p.lines) {
return normalTermination
}
if p.lines[p.current].visited == true {
return infiniteLoop
}
return notTerminated
}
func main() {
dataAsString := load("puzzle_input.txt")
//dataAsString := load("sample_input.txt")
fmt.Println("step1 -->", step1(dataAsString))
fmt.Println("step2 -->", step2(dataAsString))
}
func step2(dataAsString string) int {
prog := readInputIntoProgram(dataAsString)
swapLine := -1
for lineNumber, instruction := range prog.lines {
prog = readInputIntoProgram(dataAsString)
if lineNumber > swapLine && instruction.command == "nop" || instruction.command == "jmp" {
if instruction.command == "jmp" {
swapLine = lineNumber
prog.lines[swapLine].command = "nop"
} else if instruction.command == "nop" {
swapLine = lineNumber
prog.lines[swapLine].command = "nop"
}
for prog.isTerminated() == notTerminated {
prog.step()
}
if prog.isTerminated() == normalTermination {
return prog.acc
}
}
}
return -1
}
func step1(dataAsString string) int {
prog := readInputIntoProgram(dataAsString)
for prog.isTerminated() != infiniteLoop {
prog.step()
}
return prog.acc
}
func readInputIntoProgram(dataAsString string) program {
prog := program{acc: 0, lines: []instruction{}, current: 0}
for _, line := range strings.Split(dataAsString, "\n") {
parts := strings.Split(line, " ")
inst := instruction{command: parts[0], modifier: atoi(parts[1]), visited: false}
prog.lines = append(prog.lines, inst)
}
return prog
}
// ----
func exist(array []string, item string) bool {
for _, element := range array {
if element == item {
return true
}
}
return false
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}