98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
|
package day_02
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// task:https://adventofcode.com/2024/day/2
|
||
|
// short summary - find valid reports
|
||
|
// reports are valid if:
|
||
|
// 1. numbers are either all increasing or all decreasing
|
||
|
// 2. two adjacent numbers differ by at least one and at most three.
|
||
|
func SolveBasic(input string) int {
|
||
|
reports := strings.Split(input, "\n")
|
||
|
|
||
|
safeReports := 0
|
||
|
for _, report := range reports {
|
||
|
if isSafeReport(asInts(strings.Split(report, " "))) {
|
||
|
safeReports++
|
||
|
}
|
||
|
}
|
||
|
return safeReports
|
||
|
}
|
||
|
|
||
|
// task:https://adventofcode.com/2024/day/2#part2
|
||
|
// short summary - find valid reports
|
||
|
// same as above, reports are valid if:
|
||
|
// 1. numbers are either all increasing or all decreasing
|
||
|
// 2. two adjacent numbers differ by at least one and at most three.
|
||
|
// BUT ALSO
|
||
|
// 3. reports is valid if removing one number makes it safe
|
||
|
func SolveComplex(input string) int {
|
||
|
reports := strings.Split(input, "\n")
|
||
|
|
||
|
safeReports := 0
|
||
|
for _, report := range reports {
|
||
|
levels := asInts(strings.Split(report, " "))
|
||
|
if isSafeReport(levels) {
|
||
|
safeReports++
|
||
|
} else {
|
||
|
reducedLevels := make([]int, len(levels)-1)
|
||
|
for skipped := 0; skipped < len(levels); skipped++ {
|
||
|
pos := 0
|
||
|
for i := 0; i < len(levels); i++ {
|
||
|
if i == skipped {
|
||
|
continue
|
||
|
}
|
||
|
reducedLevels[pos] = levels[i]
|
||
|
pos++
|
||
|
}
|
||
|
if isSafeReport(reducedLevels) {
|
||
|
safeReports++
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return safeReports
|
||
|
}
|
||
|
|
||
|
// helper methods
|
||
|
func isSafeReport(levels []int) bool {
|
||
|
if levels[0] == levels[1] {
|
||
|
return false
|
||
|
} else if levels[0] < levels[1] {
|
||
|
for i := 0; i < len(levels)-1; i++ {
|
||
|
diff := levels[i+1] - levels[i]
|
||
|
if diff < 1 || diff > 3 {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
} else if levels[0] > levels[1] {
|
||
|
for i := 0; i < len(levels)-1; i++ {
|
||
|
diff := levels[i] - levels[i+1]
|
||
|
if diff < 1 || diff > 3 {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func asInts(numbers []string) []int {
|
||
|
values := make([]int, 0, len(numbers))
|
||
|
for _, v := range numbers {
|
||
|
values = append(values, asInt(v))
|
||
|
}
|
||
|
return values
|
||
|
}
|
||
|
|
||
|
func asInt(v string) int {
|
||
|
value, err := strconv.Atoi(v)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return value
|
||
|
}
|