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