-
Notifications
You must be signed in to change notification settings - Fork 1
/
16.go
82 lines (72 loc) · 1.33 KB
/
16.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
82
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
)
func main() {
file, err := os.Open("input/16.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
sue := make(map[string]int)
sue["children"] = 3
sue["cats"] = 7
sue["samoyeds"] = 2
sue["pomeranians"] = 3
sue["akitas"] = 0
sue["vizslas"] = 0
sue["goldfish"] = 5
sue["trees"] = 3
sue["cars"] = 2
sue["perfumes"] = 1
scanner := bufio.NewScanner(file)
re := regexp.MustCompile(" ([a-z]+): ([0-9]+)")
sue1, sue2 := 0, 0
for i := 1; scanner.Scan(); i++ {
isSue, isRealSue := true, true
line := scanner.Text()
res := re.FindAllStringSubmatch(line, -1)
if res == nil {
log.Fatal("Regex failed on input", line)
}
for _, v := range res {
item := v[1]
qty, err := strconv.Atoi(v[2])
if err != nil {
log.Fatal("Failed to convert", v[2])
}
if sue[item] != qty {
isSue = false
}
if item == "cats" || item == "trees" {
if sue[item] > qty {
isRealSue = false
}
} else if item == "pomeranians" || item == "goldfish" {
if sue[item] < qty {
isRealSue = false
}
} else {
if sue[item] != qty {
isRealSue = false
}
}
}
if isSue {
sue1 = i
}
if isRealSue {
sue2 = i
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(sue1)
fmt.Println(sue2)
}