Преглед на файлове

Let's keep it simple, still not solved

Piotr Czajkowski преди 2 седмици
родител
ревизия
8f56cd33ac
променени са 1 файла, в които са добавени 32 реда и са изтрити 33 реда
  1. 32 33
      02/code.go

+ 32 - 33
02/code.go

@@ -36,50 +36,49 @@ func readInput(file *os.File) [][]int {
 	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 status bool
 	edge := len(report)
 
 	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
 	for _, report := range reports {
-		if safe(report, skip) {
+		status, failed := safe(report)
+		if status {
 			result++
+		} else {
+			status, failed = safe(append(report[:failed], report[failed+1:]...))
+			if status {
+				result++
+			}
 		}
 	}
 
@@ -98,6 +97,6 @@ func main() {
 	}
 
 	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))
 }