-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_one.go
194 lines (166 loc) · 4.91 KB
/
day_one.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"fmt"
"sort"
"strconv"
"strings"
)
type Numbers struct {
// an easy way to link int with their english name: 1 = one
Integer int
Name string
}
// an array of Numbers
type Num []Numbers
type Placement struct {
// Placement is a struct that contains the beginning index of a number written out and the word itself
Index int
Word string
}
// an array of Placements
type Placements []Placement
// get the word, return it's int counterpart
func (r Num) ValueOf(symbol string) int {
for _, s := range r {
if s.Name == symbol {
return s.Integer
}
}
return 0
}
// go char by char and determine if its an int
func find_digits_in_each_line(split []string, placements []Placement) ([]int, []Placement) {
res := []int{}
// traverse forwards through list of string chars and stop upon first occurrence of an int
for index, i := range split {
convert_to_num, err := strconv.Atoi(i)
if err != nil {
continue
} else {
res = append(res, convert_to_num)
placement := Placement{}
placement.Index = index
back_to_string := strconv.Itoa(convert_to_num)
placement.Word = back_to_string
placements = append(placements, placement)
}
break
}
// traverse backwards thru list of string chars and stop upon last occurrence of an int
for index := range split {
reverse := len(split) - 1 - index
convert_to_num, err := strconv.Atoi(split[reverse])
if err != nil {
continue
} else {
res = append(res, convert_to_num)
placement := Placement{}
placement.Index = len(split) - 1 - index
back_to_string := strconv.Itoa(convert_to_num)
placement.Word = back_to_string
placements = append(placements, placement)
}
break
}
return res, placements
}
// part one wanted us to simply find the first and last occurrence of a digit in a string
// ex: txdszrn5eight3cqeight1brqr would be 51
func find_digits_in_lines() {
fileScanner := read_file("day_one_input.txt")
fileScanner.Split(bufio.ScanLines)
// unused in part one
var placements = []Placement{}
bigNumber := 0
for fileScanner.Scan() {
split := strings.Split(fileScanner.Text(), "")
result, _ := find_digits_in_each_line(split, placements)
added := ""
for _, number := range result {
s := strconv.Itoa(number)
added = added + s
}
back, _ := strconv.Atoi(added)
bigNumber = back + bigNumber
}
// part one result
fmt.Print(bigNumber)
}
// creates a Placement struct that attaches the index and word
func create_new_placement(text string, index int, name string) Placement {
placement := Placement{}
placement.Index = index
placement.Word = name
return placement
}
// part two wanted us to find ints OR words that spelled out numbers
// example: jvvslnkdk6qnfzjzvseight55eight the number derived would be 68
// from the first int 6 and the last occurrence of 'eight'
func find_anything_in_lines() {
fileScanner := read_file("day_one_input.txt")
fileScanner.Split(bufio.ScanLines)
// int to its respective word form
var allNums = Num{
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"},
{6, "six"},
{7, "seven"},
{8, "eight"},
{9, "nine"},
}
bigNum := 0
for fileScanner.Scan() {
var placements = Placements{}
text := fileScanner.Text()
for _, str := range allNums {
// if we find a number in word form
if strings.Contains(text, str.Name) {
// what is its starting index?
index := strings.Index(text, str.Name)
// if there are multiple same words in the line, we need to know each of their starting indices
if index != strings.LastIndex(text, str.Name) {
last_index := strings.LastIndex(text, str.Name)
// append first occurrence
placement1 := create_new_placement(text, index, str.Name)
placements = append(placements, placement1)
// append last occurrence, those are all that matter
placement2 := create_new_placement(text, last_index, str.Name)
placements = append(placements, placement2)
} else {
// if there is only one occurrence of a number in word form
placement := create_new_placement(text, index, str.Name)
placements = append(placements, placement)
}
}
}
split := strings.Split(fileScanner.Text(), "")
_, more_placements := find_digits_in_each_line(split, placements)
placements = append(placements, more_placements...)
// compare indices to see whose first in the string!
sort.Slice(placements, func(i, j int) bool {
return placements[i].Index < placements[j].Index
})
final := Placements{placements[0], placements[len(placements)-1]}
add := ""
for _, values := range final {
// deal with the string conversions
num, err := strconv.Atoi(values.Word)
if err != nil {
value := allNums.ValueOf(values.Word)
str_form := strconv.Itoa(value)
add = add + str_form
} else {
str_form := strconv.Itoa(num)
add = add + str_form
}
}
back_to_int, _ := strconv.Atoi(add)
bigNum = bigNum + back_to_int
}
// part two result
fmt.Print(bigNum)
}