-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01.go
159 lines (146 loc) · 3.33 KB
/
Day01.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
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
var wordToNum = map[string]int{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
}
var td1 = "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet"
var td2 = `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`
func main() {
data := readData("../input.txt")
lines := strings.Split(string(data), "\n")
// use td1 or td2 for testing
// lines := strings.Split(td1, "\n")
// lines = strings.Split(td2, "\n")
part1Answer := part1(lines)
fmt.Println("Part 1 answer:", part1Answer)
part2Answer := part2(lines)
fmt.Println("Part 2 answer:", part2Answer)
}
func readData(path string) string {
data, err := os.ReadFile(path)
if err != nil {
fmt.Println("An error occurred.")
fmt.Println(err)
return ""
}
return string(data)
}
func part1(input []string) int {
total := 0
for _, line := range input {
digits := []rune{}
for _, ch := range line {
if ch >= '0' && ch <= '9' {
digits = append(digits, ch)
}
}
if len(digits) > 0 {
firstDigit := digits[0]
lastDigit := digits[len(digits)-1]
// println("firstDigit:", string(firstDigit))
// println("lastDigit:", string(lastDigit))
num, _ := strconv.Atoi(string([]rune{firstDigit, lastDigit}))
total += num
}
}
return total
}
func checkDigits(input string) string {
if val, ok := wordToNum[input]; ok {
return strconv.Itoa(val)
}
return input
}
// challenge no postive lookbehind in golang regex
// https://stackoverflow.com/questions/2973436/regex-look-behind-without-obvious-maximum-length-in-golang
func part2(lines []string) int {
total := 0
for _, line := range lines {
digits := []string{}
i := 0
for i < len(line) {
found := false
for _, digit := range append(keys(wordToNum), "0", "1", "2", "3", "4", "5", "6", "7", "8", "9") {
if strings.HasPrefix(line[i:], digit) {
if _, ok := wordToNum[digit]; ok {
digits = append(digits, checkDigits(digit))
} else {
digits = append(digits, digit)
}
i += len(digit)
found = true
break
}
}
if !found {
i++
}
}
if len(digits) > 0 {
println("line:", line)
firstDigit := digits[0]
lastDigit := digits[len(digits)-1]
println("firstDigit:", firstDigit)
println("lastDigit:", lastDigit)
num, _ := strconv.Atoi(firstDigit + lastDigit)
total += num
}
}
return total
}
func keys(m map[string]int) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func part22(lines []string) int {
total := 0
splitter := strings.Join(append(keys(wordToNum), "\\d"), "|")
re := regexp.MustCompile(splitter)
for _, line := range lines {
for word, num := range wordToNum {
line = strings.Replace(line, word, strconv.Itoa(num), -1)
}
println("line:", line)
// matches := re.FindAllString(line, -1)
digits := []string{}
// for _, match := range matches {
// digits = append(digits, match)
// }
digits = append(digits, re.FindAllString(line, -1)...)
if len(digits) > 0 {
firstDigit := digits[0]
lastDigit := digits[len(digits)-1]
if len(digits) == 1 {
num, _ := strconv.Atoi(firstDigit)
total += num
} else {
num, _ := strconv.Atoi(firstDigit + lastDigit)
total += num
}
}
}
return total
}