attempt 1 at day 2 part 2

This commit is contained in:
Neil Hanlon 2024-12-06 12:30:33 -05:00
parent fadc27b223
commit 8bc9fc8517
Signed by: neil
GPG Key ID: 705BC21EC3C70F34

View File

@ -12,7 +12,8 @@ type Report struct {
levels []int
valid bool
direction string
flag bool
flag int
dampened bool
}
// Reports stored one per line with levels in each column.
@ -32,7 +33,8 @@ func main() {
number: idx,
valid: true,
direction: "ASC",
flag: false,
flag: -1,
dampened: false,
}
for _, field := range reportFields {
reportVal, err := strconv.Atoi(field)
@ -47,29 +49,39 @@ func main() {
var valid int
for _, report := range reports {
fmt.Printf("%v\n", report)
if report.IsValid() {
valid++
}
fmt.Printf("%v\n", report)
}
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
}
func (report *Report) IsValid() bool {
if report.flag > 0 {
report.dampened = true
}
// Determine direction
if len(report.levels) >= 1 {
report.direction = DetermineDirection(report.levels[0], report.levels[1])
report.valid = report.ValidDirection() && report.ValidDeltas()
}
// if !report.valid && !report.flag {
// report.flag = true
// return report.IsValid()
// }
if !report.valid && report.flag > 0 && !report.dampened {
fmt.Printf("DAMPING:%v\n", report.levels)
report.levels = remove(report.levels, report.flag)
fmt.Printf("DAMPING:%v\n", report.levels)
return report.IsValid()
}
return report.valid
}
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
func (report *Report) ValidDeltas() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
@ -78,6 +90,7 @@ func (report *Report) ValidDeltas() bool {
last := report.levels[idx-1]
delta := Abs(val - last)
if delta < 1 || delta > 3 {
report.flag = idx
return false
}
}
@ -93,10 +106,12 @@ func (report *Report) ValidDirection() bool {
switch report.direction {
case "ASC":
if last > val {
report.flag = idx
return false
}
case "DESC":
if last < val {
report.flag = idx
return false
}
default: