-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (63 loc) · 1.63 KB
/
main.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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
var numberWords = []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
type stream struct {
source string
}
// done returns whether there's anything left in the source string
func (s *stream) done() bool {
return len(s.source) == 0
}
// peek retrieves the current character without consuming it
func (s *stream) peek() int {
return int(s.source[0])
}
// consume consumes the current character
func (s *stream) consume() {
s.source = s.source[1:]
}
// match consumes an entire string if it matches the source string, but does nothing otherwise
func (s *stream) match(val string) bool {
if len(val) <= len(s.source) && s.source[:len(val)] == val {
s.source = s.source[len(val)-1:] // -1 so that 'eightwo' only consumes 'eigh' and allows matching 'two'
return true
}
return false
}
func isDigit(val int) (int, bool) {
if val < '0' || val > '9' {
return -1, false
}
return val - '0', true
}
func parse(line string) int {
var nums []int
s := stream{source: line}
outer:
for !s.done() {
char := s.peek()
if digit, ok := isDigit(char); ok {
nums = append(nums, digit)
s.consume()
continue
}
for i, word := range numberWords {
if s.match(word) {
nums = append(nums, i+1)
continue outer
}
}
s.consume()
}
return nums[0]*10 + nums[len(nums)-1]
}
func main() {
solver := lib.Solver[[]int, int]{
ParseF: lib.ParseLine(parse),
SolveF: lib.SumSlice[int],
}
solver.Expect("two1nine\neightwothree\nabcone2threexyz\nxtwone3four\n4nineeightseven2\nzoneight234\n7pqrstsixteen\n", 281)
solver.Verify(56324)
}