-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.go
More file actions
175 lines (149 loc) · 5.23 KB
/
day11.go
File metadata and controls
175 lines (149 loc) · 5.23 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
172
173
174
175
package main
import (
"fmt"
"flag"
"strconv"
"math"
"runtime/pprof"
"log"
"os"
)
// Calc the power values over the entire map
func calcPowerOverMap(powerMap [][]int, puzzleInput int) {
var rackID, powerLevel int = 0, 0
for x := 1; x < len(powerMap); x++ {
for y := 1; y < len(powerMap[x]); y++ {
rackID = x + 10
powerLevel = ((rackID * y) + puzzleInput) * rackID
powerLevel = int(math.Mod(float64(powerLevel / 100),10))
powerLevel -= 5
powerMap[x][y] = powerLevel
}
}
}
// Checks the 3x3 grid starting at xCoord, yCoord and calcs the power of the grid
// Returns: the power level seen
func checkPower(powerMap [][]int, xCoord int, yCoord int, part byte) (int, int) {
var powerLevel int = 0
var bestPower, bestGridSize int = 0, 0
if part == 'a' {
for x := xCoord; x < xCoord + 3; x++ {
for y := yCoord; y < yCoord + 3; y++ {
powerLevel += powerMap[y][x]
}
}
// fmt.Printf("adding x: %d y: %d powerlevel: %d\n", xCoord, yCoord, powerLevel)
return powerLevel, 3
} else {
for t := 3; (xCoord + t) < 300 && (yCoord + t) < 300; t++ {
powerLevel = 0
for x := xCoord; x < xCoord + t; x++ {
// Slow code follows
// First line is 1 minute
// Second line 7-8 minutes
//
// Possible answer is to change the way the code works completely
// merge calcPowerOverMap into this function so we aren't passing a slice around
// then for each square in the grid, calc the power then add the powerLevel up
for y := yCoord; y < yCoord + t; y++ {
powerLevel = powerLevel + powerMap[y][x]
}
}
if powerLevel > bestPower {
bestPower = powerLevel
bestGridSize = t
}
}
return bestPower, bestGridSize
}
}
// Finds the 3x3 grid that has the highest power output in the overall large grid
// returns the x, y coords of the top left of the best 3x3 grid, then the best power level seen
func findBestPowerInMap(powerMap [][]int, part byte) (int, int, int) {
var testPower, bestXCoord, bestYCoord, bestPower, bestSizeOfgrid, sizeOfGrid int = 0, 0, 0, 0, 0, 0
if part == 'a' {
for x := 1; x < len(powerMap) - 2; x++ {
for y := 1; y < len(powerMap[x]) - 2; y++ {
testPower, sizeOfGrid = checkPower(powerMap, x, y, part)
if testPower > bestPower {
bestXCoord = y
bestYCoord = x
bestPower = testPower
bestSizeOfgrid = sizeOfGrid
}
}
}
return bestXCoord, bestYCoord, 3
} else {
for x := 1; x < len(powerMap) - 2; x++ {
for y := 1; y < len(powerMap[x]) - 2; y++ {
testPower, sizeOfGrid = checkPower(powerMap, x, y, part)
if testPower > bestPower {
bestXCoord = y
bestYCoord = x
bestPower = testPower
bestSizeOfgrid = sizeOfGrid
}
}
}
return bestXCoord, bestYCoord, bestSizeOfgrid
}
}
func printPowerMap(powerMap [][]int, grid int, xPrint int, yPrint int) {
fmt.Printf("%d \n", powerMap[xPrint][yPrint])
}
// Find the fuel cell's rack ID, which is its X coordinate plus 10.
// Begin with a power level of the rack ID times the Y coordinate.
// Increase the power level by the value of the grid serial number (your puzzle input).
// Set the power level to itself multiplied by the rack ID.
// Keep only the hundreds digit of the power level (so 12345 becomes 3; numbers with no hundreds digit become 0).
// Subtract 5 from the power level.
func powerCalc(puzzleInput int, gridSize int, part byte, xPrint int, yPrint int) (int, int, int) {
var bestXCoord, bestYCoord, sizeOfGrid int = 0, 0, 0
powerMap := make([][]int, gridSize)
for i := 1; i < len(powerMap); i++ {
powerMap[i] = make([]int, gridSize)
}
calcPowerOverMap(powerMap, puzzleInput)
bestXCoord, bestYCoord, sizeOfGrid = findBestPowerInMap(powerMap, part)
return bestXCoord, bestYCoord, sizeOfGrid
}
// Main routine
func main() {
var puzzleInput, gridSize int = 0, 0
var xcoord, ycoord int = 0, 0
var xPrint, yPrint int = 0, 0
var sizeOfGrid int = 0
puzzleInputPtr := flag.String("puzzle", "7165", "Puzzle input value")
gridSizePtr := flag.String("grid", "300", "Size of grid to calc power values for")
xPrintPtr := flag.String("x", "10", "xcoord to print")
yPrintPtr := flag.String("y", "10", "y coord to print")
execPartPtr := flag.String("part", "a", "Which part of day10 do you want to calc (a or b)")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
puzzleInput, _ = strconv.Atoi(*puzzleInputPtr)
gridSize, _ = strconv.Atoi(*gridSizePtr)
xPrint, _ = strconv.Atoi(*xPrintPtr)
yPrint, _ = strconv.Atoi(*yPrintPtr)
switch *execPartPtr {
case "a":
xcoord, ycoord, sizeOfGrid = powerCalc(puzzleInput, gridSize, 'a', xPrint, yPrint)
fmt.Println("Part a - Coords of the highest power 3x3:", xcoord, ycoord)
case "b":
xcoord, ycoord, sizeOfGrid = powerCalc(puzzleInput, gridSize, 'b', xPrint, yPrint)
fmt.Println("Part b - Coords of the highest power 3x3:", xcoord, ycoord, sizeOfGrid)
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}