refactor attempt 1

This commit is contained in:
Neil Hanlon 2024-12-06 12:13:15 -05:00
parent 232a56d130
commit fadc27b223
Signed by: neil
GPG Key ID: 705BC21EC3C70F34

View File

@ -12,6 +12,7 @@ type Report struct {
levels []int
valid bool
direction string
flag bool
}
// Reports stored one per line with levels in each column.
@ -31,6 +32,7 @@ func main() {
number: idx,
valid: true,
direction: "ASC",
flag: false,
}
for _, field := range reportFields {
reportVal, err := strconv.Atoi(field)
@ -38,8 +40,6 @@ func main() {
break
}
report.valid = IsValidReport(&report, reportVal)
report.levels = append(report.levels, reportVal)
}
reports = append(reports, report)
@ -48,7 +48,7 @@ func main() {
var valid int
for _, report := range reports {
fmt.Printf("%v\n", report)
if report.valid {
if report.IsValid() {
valid++
}
}
@ -56,52 +56,54 @@ func main() {
fmt.Printf("Total: %v Valid %v\n", len(reports), valid)
}
func IsValidReport(report *Report, reportVal int) bool {
if !report.valid {
func (report *Report) IsValid() bool {
// 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()
// }
return report.valid
}
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 {
return false
}
valid := true
if len(report.levels) >= 1 {
fmt.Printf("===reportVal===: %v\n", reportVal)
fmt.Printf("levels: %v\n", report.levels)
last := report.levels[len(report.levels)-1]
// if len(report.levels) == 1 {
// last = 0
// }
fmt.Printf("last: %v\n", last)
// Determine direction
fmt.Printf("direction: %v\n", report.direction)
if len(report.levels) == 1 {
report.direction = DetermineDirection(report.levels[0], reportVal)
}
fmt.Printf("direction: %v\n", report.direction)
return true
}
fmt.Printf("valid: %v\n", valid)
func (report *Report) ValidDirection() bool {
for idx, val := range report.levels {
if prev := idx - 1; prev < 0 {
continue
}
last := report.levels[idx-1]
switch report.direction {
case "ASC":
if last > reportVal {
valid = false
if last > val {
return false
}
case "DESC":
if last < reportVal {
valid = false
if last < val {
return false
}
default:
panic("aaaaaa")
}
}
fmt.Printf("valid: %v\n", valid)
// fmt.Printf("abs(%v-%v)=%v\n", reportVal, last, Abs(reportVal-last))
// Check if our deltas are OK
delta := Abs(reportVal - last)
fmt.Printf("delta: %v\n", delta)
// fmt.Printf("abs(%v-%v)=%v\n", reportVal, last, Abs(reportVal-last))
if delta < 1 || delta > 3 {
valid = false
}
fmt.Printf("valid: %v\n", valid)
fmt.Printf("\n\n")
}
return valid
return true
}
func DetermineDirection(one, two int) string {