-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.go
60 lines (52 loc) · 1.2 KB
/
8.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
package main
import (
"fmt"
"strconv"
"strings"
)
func main8() {
input := getBootcode()
//input := []string{"nop +0", "acc +1", "jmp +4", "acc +3", "jmp -3", "acc -99", "acc +1", "jmp -4", "acc +6"}
acc := 0
visits := map[int]int{}
nops := map[int]int{}
nopToIgnore := 0
hasLoop := true
replacedOp := false
for hasLoop == true {
acc = 0
for i := 0; i < len(input); i++ {
hasLoop = false
opline := input[i]
opsplit := strings.Split(opline, " ")
op := opsplit[0]
val, _ := strconv.Atoi(opsplit[1])
if replacedOp == false && op == "jmp" {
nops[i] = 0
if len(nops)-1 > nopToIgnore {
fmt.Println("replacing", op, val, "with nop")
op = "nop"
replacedOp = true
}
}
//fmt.Println(i, op, val, "nopToIgnore", nopToIgnore, "nops", len(nops))
visitsBefore := len(visits)
visits[i] = 0
if visitsBefore == len(visits) {
fmt.Println("part 1: loop detected. accumulator is", acc)
hasLoop = true
break
}
if op == "nop" {
} else if op == "acc" {
acc += val
} else if op == "jmp" {
i += val - 1
}
}
replacedOp = false
visits = map[int]int{}
nopToIgnore++
}
fmt.Println("?part 2: without loop, accumulator is", acc)
}