-
Notifications
You must be signed in to change notification settings - Fork 0
/
01p2.go
79 lines (66 loc) · 1.4 KB
/
01p2.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
package main
import (
"fmt"
"strconv"
"strings"
"aoc2023/utils"
)
func D01P2() {
lines := utils.ReadLines("inputs/01.txt")
calibrationTotal := 0
for _, line := range lines {
numbers := []rune{}
for i, char := range line {
// Check for digit
if char >= '0' && char <= '9' {
numbers = append(numbers, char)
continue
}
// Check for a string representation of a number
if i+3 <= len(line) {
numberString := line[i:]
if i < len(line)-5 {
numberString = line[i : i+5]
}
stringAsDigit := rune(checkStringForNumber(numberString)[0])
if stringAsDigit != '0' {
numbers = append(numbers, stringAsDigit)
}
}
}
combinedNumString := fmt.Sprintf(
"%s%s",
string(numbers[0]),
string(numbers[len(numbers)-1]),
)
intVal, err := strconv.Atoi(combinedNumString)
if err != nil {
fmt.Println(err)
}
calibrationTotal += intVal
}
fmt.Println("Sum of all calibration values:", calibrationTotal)
}
func checkStringForNumber(stringToCheck string) string {
numberStrings := []string{
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
}
// check if the second character is a digit
if stringToCheck[1] >= '0' && stringToCheck[1] <= '9' {
return "0"
}
for i, numberString := range numberStrings {
if strings.Contains(stringToCheck, numberString) {
return strconv.Itoa(i + 1)
}
}
return "0"
}