-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.go
More file actions
168 lines (142 loc) · 5.17 KB
/
day08.go
File metadata and controls
168 lines (142 loc) · 5.17 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
package main
import (
"fmt"
"bufio"
"os"
"flag"
"strconv"
"strings"
)
// 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 printStringArray(tempString []string) {
// Loop through the array and print each line
for i:= 0; i < len(tempString); i++ {
fmt.Println(tempString[i])
}
}
// func processNodes (part A)
//
// We're not going to build a clever structure here. Just walk the license file recursively
// and get the values we need. Bascially:
//
// Function has 3 parameters: list of strings, current position in list, current total
// Function returns 2 parameters: current position in list, current total
//
// - Call function with our current position in the list of strings and the current total
// - function reads from current position in the list of strings.
// - if there's a new node, call function with current position and current total
// - if no new node, use returned current position to read our meta data and add to current total
// - return current position in list, current total
func processNode(systemLicense []string, currentPosInList int, currentTotal int) (int, int) {
var numChildren int
var numMetaData int
numChildren, _ = strconv.Atoi(systemLicense[currentPosInList])
numMetaData, _ = strconv.Atoi(systemLicense[currentPosInList+1])
currentPosInList += 2
for numChildren > 0 {
currentPosInList, currentTotal = processNode (systemLicense, currentPosInList, currentTotal)
numChildren--
}
for numMetaData > 0 {
tempCurrentTotal, _ := strconv.Atoi(systemLicense[currentPosInList])
currentTotal += tempCurrentTotal
currentPosInList++
numMetaData--
}
return currentPosInList, currentTotal
}
// func processChild (part B)
//
// We're not going to build a clever structure here. Just walk the license file recursively
// and get the values we need. Bascially:
//
// Function has 3 parameters: list of strings, current position in list, current total
// Function returns 2 parameters: current position in list, current total
//
// - Call function with our current position in the list of strings and the current total
// - function reads from current position in the list of strings.
// - if a node has no children, it's value is the sum of its metas
// - if a node has children, the meta data is pointers to its children that make up its value
// - if meta is 0 or higher than nodes number of children, skip it
// - if a child is referenced multiple times, it counts multiple times towards the value
func processChild(systemLicense []string, currentPosInList int, currentTotal int) (int, int) {
var numChildren int
var tempNumChildren int
var tempNumMetaData int
var numMetaData int
var childValues[1000]int
numChildren, _ = strconv.Atoi(systemLicense[currentPosInList])
numMetaData, _ = strconv.Atoi(systemLicense[currentPosInList+1])
currentPosInList += 2
if numChildren == 0 {
for tempNumMetaData = numMetaData; tempNumMetaData > 0 ; {
tempCurrentTotal, _ := strconv.Atoi(systemLicense[currentPosInList])
currentTotal += tempCurrentTotal
currentPosInList++
tempNumMetaData--
}
return currentPosInList, currentTotal
}
for tempNumChildren = 1; tempNumChildren <= numChildren ; {
currentPosInList, childValues[tempNumChildren] = processChild (systemLicense, currentPosInList, childValues[tempNumChildren])
tempNumChildren++
}
for numMetaData > 0 {
tempMetaDataItem, _ := strconv.Atoi(systemLicense[currentPosInList])
if tempMetaDataItem == 0 || tempMetaDataItem > numChildren {
currentPosInList++
numMetaData--
} else {
currentTotal += childValues[tempMetaDataItem]
currentPosInList++
numMetaData--
}
}
return currentPosInList, currentTotal
}
// Handles everything needed to work out the system license file (day 08)
func processLicenseFile(fileName string, part string) int {
var systemLicenseString string
var currentPosInList int = 0
var currentTotal int = 0
// Read contents of file into a string array
fileContents, _ := readLines(fileName)
systemLicenseString = fileContents[0]
systemLicense := strings.Split(systemLicenseString, " ")
if part == "a" {
currentPosInList, currentTotal = processNode(systemLicense, currentPosInList, currentTotal)
return currentTotal
} else {
currentPosInList, currentTotal = processChild(systemLicense, currentPosInList, currentTotal)
return currentTotal
}
return 0
}
// Main routine
func main() {
fileNamePtr := flag.String("file", "input1.txt", "A filename containing input strings")
execPartPtr := flag.String("part", "a", "Which part of day08 do you want to calc (a or b)")
flag.Parse()
switch *execPartPtr {
case "a":
fmt.Println("Part a - License File checksum:", processLicenseFile(*fileNamePtr, "a"))
case "b":
fmt.Println("Part b - License File checksum:", processLicenseFile(*fileNamePtr, "b"))
default:
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
}
}