-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.go
58 lines (47 loc) · 1.22 KB
/
02.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
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
)
const (
today = 2
)
func main() {
// Read todays file
dat, err := ioutil.ReadFile(fmt.Sprintf("%02d.in", today))
if err != nil {
panic(err)
}
// Split lines, and parse as int
lines := strings.Split(string(dat), "\n")
var result1, result2 int
for _, line := range lines {
elements := strings.Split(line, " ")
// parse rule
var ruleRange []int = mapToInt(strings.Split(elements[0], "-"))
var ruleChar string = elements[1][0:1]
// search for occurance of ruleChar
matches := regexp.MustCompile(ruleChar).FindAllStringIndex(elements[2], -1)
// compare with Part 1 rule
if len(matches) >= ruleRange[0] && len(matches) <= ruleRange[1] {
result1++
}
// compare with Part 2 rule
var matchesRule bool
for _, match := range matches {
// As FindAllString() is providing start and end of the match,
// we could compare to match[1] instead of match[0]+1 as we are looking for a single char anyway
if match[0]+1 == ruleRange[0] || match[0]+1 == ruleRange[1] {
matchesRule = !matchesRule
}
}
// Exactly one appearance
if matchesRule {
result2++
}
}
fmt.Printf("Part 1: %d\n", result1)
fmt.Printf("Part 2: %d\n", result2)
}