-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (59 loc) · 966 Bytes
/
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
package main
import (
"fmt"
"slices"
"strings"
"github.com/devkvlt/aoc"
)
var reports [][]int
func init() {
lines := aoc.Lines("input")
reports = make([][]int, len(lines))
for i, line := range lines {
fields := strings.Fields(line)
report := make([]int, len(fields))
for j, f := range fields {
report[j] = aoc.Atoi(f)
}
reports[i] = report
}
}
func safe(report []int) bool {
d0 := report[1] - report[0]
for i := 1; i < len(report); i++ {
d := report[i] - report[i-1]
if d == 0 || aoc.Abs(d) > 3 || d*d0 < 0 {
return false
}
}
return true
}
func part1() {
sum := 0
for _, r := range reports {
if safe(r) {
sum++
}
}
fmt.Println(sum)
}
func part2() {
sum := 0
for _, r := range reports {
if safe(r) {
sum++
continue
}
for i := 0; i < len(r); i++ {
if safe(slices.Delete(slices.Clone(r), i, i+1)) {
sum++
break
}
}
}
fmt.Println(sum)
}
func main() {
part1() // 591
part2() // 621
}