-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22.go
More file actions
159 lines (133 loc) · 4.22 KB
/
day22.go
File metadata and controls
159 lines (133 loc) · 4.22 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
package main
import (
"fmt"
"bufio"
"os"
"flag"
//"strings"
"runtime/pprof"
"log"
)
// Read the text file passed in by name into a array of strings
// Returns the array as the first return variable
func readLines(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func calcRiskLevel(fileName string, part string, debug bool) int {
var riskLevel = 0
var caveDepth, xTarget, yTarget int = 0, 0, 0
var maxXGrid, maxYGrid int = 0, 0
// Read in the file contents
fileContents, _ := readLines(fileName)
_, _ = fmt.Sscanf(fileContents[0], "depth: %d", &caveDepth)
_, _ = fmt.Sscanf(fileContents[1], "target: %d,%d", &xTarget, &yTarget,)
maxXGrid = xTarget + 5
maxYGrid = yTarget + 5
erosionLevel := make([][]int, maxXGrid)
for i := 0; i < maxXGrid; i++ {
erosionLevel[i] = make([]int, maxYGrid)
}
fmt.Printf("maxXGrid: %d maxYGrid: %d\n", maxXGrid, maxYGrid)
if part == "a" {
// Let's do part a
if debug {
fmt.Println("Depth:", caveDepth)
fmt.Println("X and Y:", xTarget, yTarget)
}
for y := 0; y < maxYGrid; y++ {
for x := 0; x < maxXGrid; x++ {
if debug {
fmt.Printf("x: %d y: %d\n", x, y)
}
if y == 0 {
// If the region's Y coordinate is 0, the geologic index is its X coordinate times 16807.
erosionLevel[x][y] = ((x * 16807) + caveDepth) % 20183
if debug {
fmt.Printf("y is zero and x: %d y: %d erosionLevel: %d\n", x, y, erosionLevel[x][y])
}
} else {
if (x == 0 && y == 0) || (x == xTarget && y == yTarget) {
// The region at 0,0 (the mouth of the cave) has a geologic index of 0.
// The region at the coordinates of the target has a geologic index of 0.
erosionLevel[x][y] = (0 + caveDepth) % 20183
if debug {
fmt.Printf("x and y are zero and x: %d y: %d erosionLevel: %d\n", x, y, erosionLevel[x][y])
}
} else {
if x == 0 {
// If the region's X coordinate is 0, the geologic index is its Y coordinate times 48271.
erosionLevel[x][y] = ((y * 48271) + caveDepth) % 20183
if debug {
fmt.Printf("x is zero and x: %d y: %d erosionLevel: %d\n", x, y, erosionLevel[x][y])
}
} else {
// The region's geologic index = multiplying the erosion levels of the regions at X-1,Y and X,Y-1.
//fmt.Println("erosionLevel x - 1:", erosionLevel[x-1][y])
//fmt.Println("erosionLevel y - 1:", erosionLevel[x][y-1])
erosionLevel[x][y] = ((erosionLevel[x-1][y] * erosionLevel[x][y-1]) + caveDepth) % 20183
if debug {
fmt.Printf("x is zero and x: %d y: %d erosionLevel: %d\n", x, y, erosionLevel[x][y])
}
}
}
}
}
}
// Print and Calulate the RiskLevel
for y := 0; y <= yTarget; y++ {
for x := 0; x <= xTarget; x++ {
switch erosionLevel[x][y] % 3 {
case 0: fmt.Printf(".")
riskLevel += 0
case 1: fmt.Printf("=")
riskLevel += 1
case 2: fmt.Printf("|")
riskLevel += 2
}
}
fmt.Printf("\n")
}
fmt.Printf("\n")
} else {
// part b is where it's at
}
return riskLevel
}
// Main routine
func main() {
var debug bool = false
fileNamePtr := flag.String("file", "input.txt", "A filename containing input strings")
execPartPtr := flag.String("part", "a", "Which part of day05 do you want to calc (a or b)")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
flag.BoolVar(&debug, "debug", false, "Turn debug on or not")
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()
}
switch *execPartPtr {
case "a":
fmt.Println("Part a - Total Risk Level:", calcRiskLevel(*fileNamePtr, "a", debug))
case "b":
fmt.Println("Part b - Not implemented yet")
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}