-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 1.56 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
82
83
84
package main
import (
"fmt"
"go/build"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
)
func firstproblem(input string) int {
valid := 0
lines := strings.Split(input, "\n")
for _, line := range lines {
if line != "" {
count := 0
info := strings.Split(line, " ")
numbers := strings.Split(info[0], "-")
min, _ := strconv.Atoi(numbers[0])
max, _ := strconv.Atoi(numbers[1])
letter := info[1][:len(":")]
for _, char := range info[2] {
if fmt.Sprintf("%c", char) == letter {
count++
}
}
if count <= max && count >= min {
valid++
}
}
}
return valid
}
func secondproblem(input string) int {
valid := 0
lines := strings.Split(input, "\n")
for _, line := range lines {
if line != "" {
info := strings.Split(line, " ")
numbers := strings.Split(info[0], "-")
first, _ := strconv.Atoi(numbers[0])
second, _ := strconv.Atoi(numbers[1])
letter := info[1][:len(":")]
if (fmt.Sprintf("%c", info[2][first-1]) == letter) != (fmt.Sprintf("%c", info[2][second-1]) == letter) {
valid++
}
}
}
return valid
}
func main() {
path := filepath.Join(build.Default.GOPATH, "src", "github.com", "PFadel", "adventofcode", "2020", "2", "input")
b, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
payload := string(b)
start := time.Now()
r1 := firstproblem(payload)
elapsed1 := time.Since(start)
start = time.Now()
r2 := secondproblem(payload)
elapsed2 := time.Since(start)
fmt.Printf("%d, %f Seconds\n", r1, elapsed1.Seconds())
fmt.Printf("%d, %f Seconds\n", r2, elapsed2.Seconds())
}