generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
63 lines (55 loc) · 830 Bytes
/
solution.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
package day01p2
import (
"io"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
matchmap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
}
sum := 0
for _, ln := range lines {
first := -1
last := -1
minpos := len(ln) + 1
maxpos := -1
for k, v := range matchmap {
p := strings.Index(ln, k)
if p == -1 {
// substring not found
continue
}
if p < minpos {
first = v
minpos = p
}
p = strings.LastIndex(ln, k)
if p > maxpos {
last = v
maxpos = p
}
}
sum += 10*first + last
}
return sum
}