-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.go
47 lines (41 loc) · 813 Bytes
/
part2.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
package day02
import (
"strconv"
"strings"
"github.com/patrick22414/Advent-of-Code-2024/readinput"
)
func IsSafeDampened(s []int) bool {
if IsSafe(s) {
return true
}
// fmt.Printf("\n%v not safe\n", s)
for i := range s {
t := make([]int, i, len(s)-1)
copy(t, s[:i])
t = append(t, s[i+1:]...)
if IsSafe(t) {
// fmt.Printf("removing %v at %v makes it safe: %v\n", s[i], i, t)
return true
}
}
// fmt.Println("still not safe")
return false
}
func Part2() int {
safeReports := 0
for line := range readinput.ReadInput("./input.txt") {
ts := strings.Fields(line)
ns := make([]int, len(ts))
for i, token := range ts {
n, err := strconv.Atoi(token)
if err != nil {
panic(err)
}
ns[i] = n
}
if IsSafeDampened(ns) {
safeReports++
}
}
return safeReports
}