-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayTwentyThree.go
114 lines (81 loc) · 2.4 KB
/
DayTwentyThree.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
package adventofcode2015
import (
"fmt"
"strings"
"strconv"
)
func getRegisterValue(registers map[string]int, register string) int {
register = strings.Trim(register, ",")
if value,ok := registers[register]; ok {
return value
} else {
parsedValue,_ := strconv.Atoi(register)
return parsedValue
}
}
func processInstructions(registers map[string]int, instructions []string) {
fmt.Println("Going to process", len(instructions),"instructions")
programCounter := 0
for ; programCounter >=0 && programCounter < len(instructions); {
incValue := 1
instructionParts := strings.Split(instructions[programCounter]," ")
opCode := instructionParts[0]
switch (opCode) {
case "inc":
register := instructionParts[1]
registers[register] = getRegisterValue(registers,register) + 1
case "jio":
register := instructionParts[1]
if getRegisterValue(registers,register) == 1 {
incValue = getRegisterValue(registers,instructionParts[2])
}
case "jie":
register := instructionParts[1]
if getRegisterValue(registers,register) % 2 == 0 {
incValue = getRegisterValue(registers,instructionParts[2])
}
case "tpl":
register := instructionParts[1]
registers[register] = getRegisterValue(registers,register) * 3
case "hlf":
register := instructionParts[1]
registers[register] = getRegisterValue(registers,register) / 2
case "jmp":
value, _ := strconv.Atoi(instructionParts[1])
incValue = value
default:
fmt.Println(opCode)
}
programCounter += incValue
}
}
func DayTwentyThreeExample() {
fmt.Println("Day 23 - Example")
input := "inc a\njio a, +2\ntpl a\ninc a"
instructions := strings.Split(input, "\n")
registers := make(map[string]int)
registers["a"] = 0
registers["b"] = 0
processInstructions(registers,instructions)
fmt.Println(registers)
}
func DayTwentyThreePartOne() {
fmt.Println("Day 23 - Part One")
input := ReadFile("day23-input.txt")
instructions := strings.Split(input, "\n")
registers := make(map[string]int)
registers["a"] = 0
registers["b"] = 0
processInstructions(registers,instructions)
fmt.Println(registers)
}
func DayTwentyThreePartTwo() {
fmt.Println("Day 23 - Part Two")
input := ReadFile("day23-input.txt")
instructions := strings.Split(input, "\n")
registers := make(map[string]int)
registers["a"] = 1
registers["b"] = 0
processInstructions(registers,instructions)
fmt.Println(registers)
}