-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (62 loc) · 1.14 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
package main
import (
"bufio"
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
sum := 0
for scanner.Scan() {
pol := toPolicy(scanner.Text())
count := 0
for i := 0; i < len(pol.s); i++ {
r := rune(pol.s[i])
if r == pol.letter {
count++
}
}
if count >= pol.from && count <= pol.to {
sum++
}
}
return sum
}
type Policy struct {
from int
to int
letter rune
s string
}
func toPolicy(s string) Policy {
del := lib.NewDelimiter(s, " ")
first := del.GetString(0)
id := strings.Index(first, "-")
return Policy{
from: lib.StringToInt(first[:id]),
to: lib.StringToInt(first[id+1:]),
letter: rune(s[del.Ind[0]+1]),
s: del.GetString(2),
}
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
sum := 0
for scanner.Scan() {
pol := toPolicy(scanner.Text())
pol.from--
pol.to--
count := 0
if pol.from < len(pol.s) && rune(pol.s[pol.from]) == pol.letter {
count++
}
if pol.to < len(pol.s) && rune(pol.s[pol.to]) == pol.letter {
count++
}
if count == 1 {
sum++
}
}
return sum
}