-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.go
More file actions
82 lines (68 loc) · 1.83 KB
/
day08.go
File metadata and controls
82 lines (68 loc) · 1.83 KB
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
package main
import (
"fmt"
)
type bootCodeStruct struct {
operator string
opAmount int
}
func runSingleOp(singleOp bootCodeStruct, currentLine int, accumulator int, part byte, debug bool) (int, int) {
var nextLine int
switch singleOp.operator {
case "nop": // Does nothing. Ignore the opAmount.
nextLine = currentLine + 1
break
case "acc":
accumulator += singleOp.opAmount
nextLine = currentLine + 1
case "jmp":
nextLine = currentLine + singleOp.opAmount
default:
fmt.Println("Code is invalid")
break
}
return nextLine, accumulator
}
func runAllBootCode(filename string, part byte, debug bool) int {
var codeAlreadyRun [1000]bool
var bootCode [1000]bootCodeStruct
var accumulator int
puzzleInput, _ := readFile(filename)
// Process the boot code into a more usable form
for item, operatorLine := range puzzleInput {
fmt.Sscanf(operatorLine, "%s %d", &bootCode[item].operator, &bootCode[item].opAmount)
}
if debug {
// Print the boot code program
for i := 0; i < len(bootCode); i++ {
if bootCode[i].operator == "" {
break
}
fmt.Printf("%d Op: %s OpAmount: %d\n", i, bootCode[i].operator, bootCode[i].opAmount)
}
}
// Run the boot code program
var currentLine = 0
for true {
if codeAlreadyRun[currentLine] {
break
}
codeAlreadyRun[currentLine] = true
currentLine, accumulator = runSingleOp(bootCode[currentLine], currentLine, accumulator, part, debug)
}
return accumulator
}
// Main routine
func main() {
filenamePtr, execPart, debug, test := catchUserInput()
if test {
return
}
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else if execPart == 'a' {
fmt.Println("Accumulator:", runAllBootCode(filenamePtr, execPart, debug))
} else {
fmt.Println("Accumulator on working code:", runAllBootCodePartB(filenamePtr, execPart, debug))
}
}