-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06.go
More file actions
171 lines (143 loc) · 3.82 KB
/
day06.go
File metadata and controls
171 lines (143 loc) · 3.82 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"AdventOfCode-go/advent2017/utils"
"fmt"
"strconv"
"strings"
)
func newMapKey(valueList []int) string {
var mapKey string
for _, value := range valueList {
if len(mapKey) == 0 {
mapKey = fmt.Sprintf("%d", value)
} else {
mapKey = mapKey + "-" + fmt.Sprintf("%d", value)
}
}
return mapKey
}
func debugMemoryLoopCount(filename string, part byte, debug bool) int {
seenPatternBefore := make(map[string]bool)
puzzleInput, _ := utils.ReadFile(filename)
puzzleInputSplit := strings.Fields(puzzleInput[0])
memoryBanks := make([]int, len(puzzleInputSplit))
for i := 0; i < len(puzzleInputSplit); i++ {
memoryBanks[i], _ = strconv.Atoi(puzzleInputSplit[i])
}
if debug {
fmt.Println(memoryBanks)
}
var maxPos, maxBlocks, currentPos int
var partbLoopCount int
var partbCountNow bool = false
var firstKeyRepeat string
for counter := 1; ; counter++ {
maxPos = 0
maxBlocks = 0
// Find the memory bank with the most blocks
for i := 0; i < len(memoryBanks); i++ {
if memoryBanks[i] > maxBlocks {
maxPos = i
maxBlocks = memoryBanks[i]
}
}
// Distribute the blocks across the memory banks, starting at the next bank
// following the (former) largest
memoryBanks[maxPos] = 0
currentPos = maxPos + 1
for blocks := maxBlocks; blocks > 0; blocks-- {
// If we're at the end of the memoryBanks loop back to the beginning
if currentPos >= len(memoryBanks) {
currentPos = 0
}
memoryBanks[currentPos]++
currentPos++
}
mapKey := newMapKey(memoryBanks)
var ok bool = false
if partbCountNow {
if mapKey == firstKeyRepeat {
_, ok = seenPatternBefore[mapKey]
}
} else {
_, ok = seenPatternBefore[mapKey]
}
if ok {
// If part b and this is the first time we've repeated the loop, continue
if !partbCountNow {
// We haven't been counting yet. As we've seen the first repeat it's time to count
firstKeyRepeat = mapKey
partbCountNow = true
partbLoopCount = 0
} else {
return partbLoopCount + 1
}
} else {
seenPatternBefore[mapKey] = true
if partbCountNow {
partbLoopCount++
}
}
}
}
func debugMemory(filename string, part byte, debug bool) int {
seenPatternBefore := make(map[string]bool)
puzzleInput, _ := utils.ReadFile(filename)
puzzleInputSplit := strings.Fields(puzzleInput[0])
memoryBanks := make([]int, len(puzzleInputSplit))
for i := 0; i < len(puzzleInputSplit); i++ {
memoryBanks[i], _ = strconv.Atoi(puzzleInputSplit[i])
}
if debug {
fmt.Println(memoryBanks)
}
var maxPos, maxBlocks, currentPos int
for counter := 1; ; counter++ {
maxPos = 0
maxBlocks = 0
// Find the memory bank with the most blocks
for i := 0; i < len(memoryBanks); i++ {
if memoryBanks[i] > maxBlocks {
maxPos = i
maxBlocks = memoryBanks[i]
}
}
if debug {
fmt.Printf("MaxPos: %d MaxBlocks: %d\n", maxPos, maxBlocks)
}
// Distribute the blocks across the memory banks, starting at the next bank
// following the (former) largest
memoryBanks[maxPos] = 0
currentPos = maxPos + 1
for blocks := maxBlocks; blocks > 0; blocks-- {
// If we're at the end of the memoryBanks loop back to the beginning
if currentPos >= len(memoryBanks) {
currentPos = 0
}
memoryBanks[currentPos]++
currentPos++
}
mapKey := newMapKey(memoryBanks)
_, ok := seenPatternBefore[mapKey]
if ok {
// Seen pattern before so exit
return counter
} else {
seenPatternBefore[mapKey] = true
}
}
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
switch execPart {
case 'a':
fmt.Printf("Result is: %d\n", debugMemory(filenamePtr, execPart, debug))
case 'b':
fmt.Printf("Result is: %d\n", debugMemoryLoopCount(filenamePtr, execPart, debug))
case 'z':
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}
}