aoc2024/02/main.go

156 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-12-06 16:40:07 +00:00
package main
import (
"fmt"
"os"
2024-12-06 18:59:37 +00:00
"slices"
2024-12-06 16:40:07 +00:00
"strconv"
"strings"
)
type Report struct {
number int
levels []int
valid bool
direction string
2024-12-06 17:30:33 +00:00
flag int
dampened bool
2024-12-06 16:40:07 +00:00
}
// Reports stored one per line with levels in each column.
// In safe reports, the levels are either all increasing or all decreasing AND
// Any two adjacent levels differ by at least one and at most three.
func main() {
input, err := os.ReadFile("input")
if err != nil {
fmt.Println(err)
}
var reports []Report
lines := strings.Split(string(input), "\n")
for idx, val := range lines[0 : len(lines)-1] {
reportFields := strings.Fields(val)
report := Report{
number: idx,
valid: true,
direction: "ASC",
2024-12-06 17:30:33 +00:00
flag: -1,
dampened: false,
2024-12-06 16:40:07 +00:00
}
for _, field := range reportFields {
reportVal, err := strconv.Atoi(field)
if err != nil {
break
}
report.levels = append(report.levels, reportVal)
}
reports = append(reports, report)
}
var valid int
for _, report := range reports {
2024-12-06 17:13:15 +00:00
if report.IsValid() {
2024-12-06 16:40:07 +00:00
valid++
}
2024-12-06 17:30:33 +00:00
fmt.Printf("%v\n", report)
2024-12-06 16:40:07 +00:00
}
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
}
2024-12-06 17:13:15 +00:00
func (report *Report) IsValid() bool {
2024-12-06 17:30:33 +00:00
if report.flag > 0 {
report.dampened = true
}
2024-12-06 17:13:15 +00:00
// Determine direction
2024-12-06 16:40:07 +00:00
if len(report.levels) >= 1 {
2024-12-06 17:13:15 +00:00
report.direction = DetermineDirection(report.levels[0], report.levels[1])
report.valid = report.ValidDirection() && report.ValidDeltas()
}
2024-12-06 16:40:07 +00:00
2024-12-06 17:30:33 +00:00
if !report.valid && report.flag > 0 && !report.dampened {
2024-12-06 18:59:37 +00:00
// First, try and just remove the flagged problem
orig := make([]int, len(report.levels))
for idx := range report.levels {
copy(orig, report.levels)
orig := slices.Delete(orig, idx, idx+1)
dampened := Report{
number: report.number,
valid: report.valid,
direction: report.direction,
flag: report.flag,
dampened: report.dampened,
levels: orig,
}
if report.valid = dampened.IsValid(); report.valid {
break
}
}
2024-12-06 17:30:33 +00:00
}
2024-12-06 17:13:15 +00:00
return report.valid
}
2024-12-06 17:30:33 +00:00
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
2024-12-06 17:13:15 +00:00
func (report *Report) ValidDeltas() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
continue
}
last := report.levels[idx-1]
delta := Abs(val - last)
if delta < 1 || delta > 3 {
2024-12-06 17:30:33 +00:00
report.flag = idx
2024-12-06 17:13:15 +00:00
return false
2024-12-06 16:40:07 +00:00
}
2024-12-06 17:13:15 +00:00
}
return true
}
2024-12-06 16:40:07 +00:00
2024-12-06 17:13:15 +00:00
func (report *Report) ValidDirection() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
continue
}
2024-12-06 18:59:37 +00:00
if val == 0 {
break
}
2024-12-06 17:13:15 +00:00
last := report.levels[idx-1]
2024-12-06 16:40:07 +00:00
switch report.direction {
case "ASC":
2024-12-06 17:13:15 +00:00
if last > val {
2024-12-06 17:30:33 +00:00
report.flag = idx
2024-12-06 17:13:15 +00:00
return false
2024-12-06 16:40:07 +00:00
}
case "DESC":
2024-12-06 17:13:15 +00:00
if last < val {
2024-12-06 17:30:33 +00:00
report.flag = idx
2024-12-06 17:13:15 +00:00
return false
2024-12-06 16:40:07 +00:00
}
2024-12-06 17:13:15 +00:00
default:
panic("aaaaaa")
2024-12-06 16:40:07 +00:00
}
}
2024-12-06 17:13:15 +00:00
return true
2024-12-06 16:40:07 +00:00
}
func DetermineDirection(one, two int) string {
diff := one - two
if diff < 0 {
return "ASC"
}
return "DESC"
}
func Abs(input int) int {
if input < 0 {
return -input
}
return input
}