2024 day 2 part 2
This commit is contained in:
parent
0d7fadda2c
commit
c26376f49d
@ -6,12 +6,13 @@ import (
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
// "slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file, err := os.Open("./input_test.txt")
|
||||
file, err := os.Open("./input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %s\n", err)
|
||||
}
|
||||
@ -19,10 +20,9 @@ func main() {
|
||||
s := bufio.NewScanner(file)
|
||||
|
||||
result1 := 0
|
||||
line := 0
|
||||
result2 := 0
|
||||
|
||||
for s.Scan() {
|
||||
line++
|
||||
t := s.Text()
|
||||
strs := strings.Split(t, " ")
|
||||
var ints = []int{}
|
||||
@ -35,49 +35,79 @@ func main() {
|
||||
ints = append(ints, j)
|
||||
}
|
||||
|
||||
var prev int
|
||||
var current int
|
||||
var isIncreasing bool
|
||||
isSafe := true
|
||||
fcount := 0
|
||||
safe := checkSafe(ints)
|
||||
|
||||
for idx, num := range ints {
|
||||
current = num
|
||||
if idx == 0 {
|
||||
prev = num
|
||||
continue
|
||||
}
|
||||
if prev == current {
|
||||
isSafe = false
|
||||
fcount++
|
||||
break
|
||||
}
|
||||
if idx == 1 {
|
||||
isIncreasing = (prev < current)
|
||||
}
|
||||
diff := int(math.Abs(float64(prev) - float64(current)))
|
||||
|
||||
if diff < 1 || diff > 3 {
|
||||
isSafe = false
|
||||
}
|
||||
if isIncreasing && (prev > current) {
|
||||
isSafe = false
|
||||
}
|
||||
if !isIncreasing && (prev < current) {
|
||||
isSafe = false
|
||||
}
|
||||
|
||||
fmt.Printf("line: %d\tidx: %d\tprev: %d\tcurr: %d\tincr: %t\tdiff: %d\tfcount: %d\n", idx, line, prev, current, isIncreasing, diff, fcount)
|
||||
prev = num
|
||||
}
|
||||
|
||||
if isSafe {
|
||||
if safe {
|
||||
result1++
|
||||
}
|
||||
if fcount <= 1 {
|
||||
result2++
|
||||
} else {
|
||||
for idxRemove := 0; idxRemove < len(ints); idxRemove++ {
|
||||
var new_ints []int
|
||||
|
||||
for idx, i := range ints {
|
||||
if idx == idxRemove { continue }
|
||||
new_ints = append(new_ints, i)
|
||||
}
|
||||
|
||||
safe := checkSafe(new_ints)
|
||||
|
||||
if safe {
|
||||
result2++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Result1: ", result1)
|
||||
fmt.Println("Result2: ", result2)
|
||||
}
|
||||
|
||||
func checkSafe(nums []int) bool {
|
||||
var prev int
|
||||
var current int
|
||||
var isIncreasing bool
|
||||
isSafe := true
|
||||
firstFailedIdx := -1
|
||||
|
||||
for idx, num := range nums {
|
||||
current = num
|
||||
if idx == 0 {
|
||||
prev = num
|
||||
continue
|
||||
}
|
||||
if prev == current {
|
||||
if firstFailedIdx == -1 {
|
||||
firstFailedIdx = idx
|
||||
}
|
||||
isSafe = false
|
||||
break
|
||||
}
|
||||
if idx == 1 {
|
||||
isIncreasing = (prev < current)
|
||||
}
|
||||
diff := int(math.Abs(float64(prev) - float64(current)))
|
||||
|
||||
if diff < 1 || diff > 3 {
|
||||
if firstFailedIdx == -1 {
|
||||
firstFailedIdx = idx
|
||||
}
|
||||
isSafe = false
|
||||
}
|
||||
if isIncreasing && (prev > current) {
|
||||
if firstFailedIdx == -1 {
|
||||
firstFailedIdx = idx
|
||||
}
|
||||
isSafe = false
|
||||
}
|
||||
if !isIncreasing && (prev < current) {
|
||||
if firstFailedIdx == -1 {
|
||||
firstFailedIdx = idx
|
||||
}
|
||||
isSafe = false
|
||||
}
|
||||
|
||||
prev = num
|
||||
}
|
||||
return isSafe
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user