-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (62 loc) · 1.51 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
package main
import (
"fmt"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
getTotalSafeReportCount(input)
}
func isReportSafe(reportNum []int) bool {
flagIncrease, flagDecrease := false, false
for i := 1; i < len(reportNum); i++ {
diff := reportNum[i] - reportNum[i-1]
if diff > 0 {
flagIncrease = true
} else if diff < 0 {
flagDecrease = true
} else {
return false
}
if flagDecrease && flagIncrease {
return false
}
if diff > 3 || diff < -3 {
return false
}
}
return true
}
func checkReportSafetyWithDeletion(reportNum []int) bool {
for i := 0; i < len(reportNum); i++ {
isSafe := isReportSafeWithDeletion(reportNum, i)
if isSafe {
return true
}
}
return false
}
func isReportSafeWithDeletion(report []int, deleteIndex int) bool {
copyReport := make([]int, len(report))
copy(copyReport, report)
if deleteIndex == len(copyReport)-1 {
copyReport = copyReport[:deleteIndex]
} else {
copyReport = append(copyReport[:deleteIndex], copyReport[deleteIndex+1:]...)
}
return isReportSafe(copyReport)
}
func getTotalSafeReportCount(reports []string) int {
var count int
var countWithDeletion int
for _, report := range reports {
reportNum := aoc.FetchSliceOfIntsInString(report)
if isReportSafe(reportNum) {
count++
} else if checkReportSafetyWithDeletion(reportNum) {
countWithDeletion++
}
}
fmt.Printf("answer for part 1: %d\nanswer for part 2: %d\n", count, count+countWithDeletion)
return count
}