-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.go
More file actions
137 lines (116 loc) · 2.92 KB
/
day01.go
File metadata and controls
137 lines (116 loc) · 2.92 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
package main
import (
"AdventOfCode-go/advent2023/utils"
"fmt"
"strings"
)
// Part 1: look for the first and last digit (in that order) and use them to make a 2 digit number
// Sum al the 2 digit numbers together
// Edge case: if there's only 1 number in the string, use it for both digits
// Part 2: digits can be spelt out OR numerical
func findNumberAndText(puzzleLine string, forwards bool) int {
var startPos, stopPos, step int
var bestPos, bestValue int
digits := [...]string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
// First, look for text numbers and record the lowest (forwards=true)
// or highest (forwards=false) positions where they are found
if forwards {
bestPos = len(puzzleLine)
} else {
bestPos = -1
}
for textValue, textNum := range digits {
var foundPos int
if forwards {
foundPos = strings.Index(puzzleLine, textNum)
} else {
foundPos = strings.LastIndex(puzzleLine, textNum)
}
if foundPos != -1 {
if forwards && foundPos < bestPos {
bestPos = foundPos
bestValue = textValue
}
if !forwards && foundPos > bestPos {
bestPos = foundPos
bestValue = textValue
}
}
}
if forwards {
startPos = 0
stopPos = len(puzzleLine)
step = 1
} else {
startPos = len(puzzleLine) - 1
stopPos = -1
step = -1
}
for i := startPos; i != stopPos; i += step {
if puzzleLine[i] > '0' && puzzleLine[i] <= '9' {
if forwards {
if i < bestPos {
return int(puzzleLine[i] - '0')
} else {
return bestValue
}
} else {
if i > bestPos {
return int(puzzleLine[i] - '0')
} else {
return bestValue
}
}
}
}
return bestValue
}
func findNumber(puzzleLine string, forwards bool) int {
var startPos, stopPos, step int
if forwards {
startPos = 0
stopPos = len(puzzleLine)
step = 1
} else {
startPos = len(puzzleLine) - 1
stopPos = -1
step = -1
}
for i := startPos; i != stopPos; i += step {
if puzzleLine[i] > '0' && puzzleLine[i] <= '9' {
return int(puzzleLine[i] - '0')
}
}
return 0
}
func day01(filename string, part byte, debug bool) int {
var result int
puzzleInput, _ := utils.ReadFile(filename)
if part == 'a' {
for _, puzzleLine := range puzzleInput {
firstDigit := findNumber(puzzleLine, true)
secondDigit := findNumber(puzzleLine, false)
result += (firstDigit * 10) + secondDigit
}
return result
}
// Part b
for _, puzzleLine := range puzzleInput {
firstDigit := findNumberAndText(puzzleLine, true)
secondDigit := findNumberAndText(puzzleLine, false)
result += (firstDigit * 10) + secondDigit
if debug {
fmt.Println(puzzleLine, (firstDigit*10)+secondDigit)
}
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day01(filenamePtr, execPart, debug))
}
}