|
@@ -36,50 +36,49 @@ func readInput(file *os.File) [][]int {
|
|
return reports
|
|
return reports
|
|
}
|
|
}
|
|
|
|
|
|
-func check(a, b int, direction int) (bool, int) {
|
|
|
|
- delta := b - a
|
|
|
|
- if delta == 0 || delta < -3 || delta > 3 {
|
|
|
|
- return false, direction
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if direction == 0 {
|
|
|
|
- direction = delta
|
|
|
|
- } else if direction < 0 && delta > 0 || direction > 0 && delta < 0 {
|
|
|
|
- return false, direction
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true, direction
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func safe(report []int, skip bool) bool {
|
|
|
|
|
|
+func safe(report []int) (bool, int) {
|
|
var direction int
|
|
var direction int
|
|
- var status bool
|
|
|
|
edge := len(report)
|
|
edge := len(report)
|
|
|
|
|
|
for i := 1; i < edge; i++ {
|
|
for i := 1; i < edge; i++ {
|
|
- status, direction = check(report[i], report[i-1], direction)
|
|
|
|
- if !status {
|
|
|
|
- if skip && i+1 < edge {
|
|
|
|
- status, direction = check(report[i+1], report[i-1], direction)
|
|
|
|
- if status {
|
|
|
|
- i++
|
|
|
|
- skip = false
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ delta := report[i] - report[i-1]
|
|
|
|
+ if delta == 0 || delta < -3 || delta > 3 {
|
|
|
|
+ return false, i
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if direction == 0 {
|
|
|
|
+ direction = delta
|
|
|
|
+ } else if direction < 0 && delta > 0 || direction > 0 && delta < 0 {
|
|
|
|
+ return false, i
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- return false
|
|
|
|
|
|
+ return true, 0
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func part1(reports [][]int) int {
|
|
|
|
+ var result int
|
|
|
|
+ for _, report := range reports {
|
|
|
|
+ status, _ := safe(report)
|
|
|
|
+ if status {
|
|
|
|
+ result++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- return true
|
|
|
|
|
|
+ return result
|
|
}
|
|
}
|
|
|
|
|
|
-func part(reports [][]int, skip bool) int {
|
|
|
|
|
|
+func part2(reports [][]int) int {
|
|
var result int
|
|
var result int
|
|
for _, report := range reports {
|
|
for _, report := range reports {
|
|
- if safe(report, skip) {
|
|
|
|
|
|
+ status, failed := safe(report)
|
|
|
|
+ if status {
|
|
result++
|
|
result++
|
|
|
|
+ } else {
|
|
|
|
+ status, failed = safe(append(report[:failed], report[failed+1:]...))
|
|
|
|
+ if status {
|
|
|
|
+ result++
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -98,6 +97,6 @@ func main() {
|
|
}
|
|
}
|
|
|
|
|
|
reports := readInput(file)
|
|
reports := readInput(file)
|
|
- fmt.Println("Part1:", part(reports, false))
|
|
|
|
- fmt.Println("Part2:", part(reports, true))
|
|
|
|
|
|
+ fmt.Println("Part1:", part1(reports))
|
|
|
|
+ fmt.Println("Part2:", part2(reports))
|
|
}
|
|
}
|