-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
72 lines (65 loc) · 1.11 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
package main
import (
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
res := 0
for _, line := range lines {
del := aoc.NewDelimiter(line, " ")
if isSafe(del.GetInts()) {
res++
}
}
return res
}
func isSafe(elems []int) bool {
prev := elems[0]
increase := true
if elems[1] == elems[0] {
return false
}
if elems[1] < elems[0] {
increase = false
}
for i := 1; i < len(elems); i++ {
cur := elems[i]
if increase && cur < prev {
return false
}
if !increase && cur > prev {
return false
}
diff := aoc.Abs(prev - cur)
if diff < 1 || diff > 3 {
return false
}
prev = cur
}
return true
}
func fs2(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
res := 0
for _, line := range lines {
del := aoc.NewDelimiter(line, " ")
elems := del.GetInts()
if isSafe2(elems) {
res++
}
}
return res
}
func isSafe2(elems []int) bool {
if isSafe(elems) {
return true
}
for i := 0; i < len(elems); i++ {
e := aoc.FilterSliceIndices(elems, []int{i})
if isSafe(e) {
return true
}
}
return false
}