-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd5p1.go
51 lines (46 loc) · 983 Bytes
/
d5p1.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
// "slices"
"strconv"
"strings"
)
func main() {
scan := bufio.NewScanner(os.Stdin)
var input []string
rules := make(map[string][]string)
var mid_sum int
validExp := regexp.MustCompile(`[0-9]{2}\|[0-9]{2}`)
for scan.Scan() {
str := scan.Text()
if validExp.Match([]byte(str)) {
nums := strings.Split(str, "|")
rules[nums[1]] = append(rules[nums[1]], nums[0])
} else if scan.Text() == "" {
} else {
input = append(input, scan.Text())
}
}
for _, updates := range input {
safe := true
updates_slice := strings.Split(updates, ",")
for i := 0; i < len(updates_slice); i++ {
check_slice := updates_slice[i:]
for _, rule := range rules[updates_slice[i]] {
for _, page := range check_slice {
if rule == page {
safe = false
}
}
}
}
if safe {
mid_num, _ := strconv.Atoi(updates_slice[len(updates_slice)/2])
mid_sum += mid_num
}
}
fmt.Println(mid_sum)
}