1
0
Fork 0

update day2

rewrite all functions to methods
This commit is contained in:
Sebastian Mark 2024-12-02 18:19:40 +01:00
parent 3fbc954566
commit 39f1e7ae74
2 changed files with 59 additions and 60 deletions

View file

@ -6,15 +6,22 @@ import (
"sort" "sort"
) )
var reports [][]int type reports []report
type report struct {
levels []int
}
func main() { func main() {
reports = helper.ReadLinesToIntArrays() // generate reports
var reports reports
for _, val := range helper.ReadLinesToIntSlices() {
reports = append(reports, report{levels: val})
}
// part 1 // part 1
save_count := 0 save_count := 0
for _, r := range reports { for _, r := range reports {
if isReportSave(r) { if r.isSave() {
save_count++ save_count++
} }
} }
@ -23,79 +30,71 @@ func main() {
// part 2 // part 2
save_count = 0 save_count = 0
for _, r := range reports { for _, r := range reports {
if isReportSaveWithDamper(r) { if r.isSaveWithDamper() {
save_count++ save_count++
} }
} }
fmt.Printf("Save reports (with damper): %d\n", save_count) fmt.Printf("Save reports (with damper): %d\n", save_count)
} }
func isReportSaveWithDamper(report []int) bool { func (r report) isSave() bool {
if !r.isSorted() {
if isReportSave(report) {
return true
}
for i := range report {
// remove one item from report and try to validate
new_report := removeFromSlice(report, i)
if isReportSave(new_report) {
return true
}
}
return false
}
func removeFromSlice(slice []int, dropIndex int) []int {
var newslice []int
for i, v := range slice {
if i != dropIndex {
newslice = append(newslice, v)
}
}
return newslice
}
func isReportSave(report []int) bool {
if !isReportSorted(report) {
return false return false
} }
for i := range report { for i := 0; i < len(r.levels)-1; i++ {
if (i + 1) < len(report) { if !isValidLevelDifference(r.levels[i], r.levels[i+1]) {
if !isPairSave(report[i], report[i+1]) {
return false return false
} }
} }
}
return true return true
} }
func isReportSorted(slice []int) bool { func (r report) isSorted() bool {
ascSorted := sort.SliceIsSorted( ascSorted := sort.SliceIsSorted(
slice, r.levels,
func(i, j int) bool { return slice[i] < slice[j] }, func(i, j int) bool { return r.levels[i] < r.levels[j] },
) )
descSorted := sort.SliceIsSorted( descSorted := sort.SliceIsSorted(
slice, r.levels,
func(i, j int) bool { return slice[i] > slice[j] }, func(i, j int) bool { return r.levels[i] > r.levels[j] },
) )
return ascSorted || descSorted return ascSorted || descSorted
} }
func isPairSave(x int, y int) bool { func isValidLevelDifference(i int, j int) bool {
if x == y { if i == j {
return false return false
} }
diff := helper.Abs(x - y)
if diff > 3 { if helper.Abs(i-j) > 3 {
return false return false
} }
return true return true
} }
func (r report) isSaveWithDamper() bool {
if r.isSave() {
return true
}
for i := range r.levels {
// remove one item from report and try to validate
if r.removeLevelByIndex(i).isSave() {
return true
}
}
return false
}
func (r report) removeLevelByIndex(dropIndex int) report {
var newlevels []int
newlevels = append(newlevels, r.levels[:dropIndex]...)
newlevels = append(newlevels, r.levels[dropIndex+1:]...)
return report{levels: newlevels}
}

View file

@ -38,7 +38,7 @@ func ReadLinesTwoIntSlices() (list_a []int, list_b []int) {
return list_a, list_b return list_a, list_b
} }
func ReadLinesToIntArrays() (lines [][]int) { func ReadLinesToIntSlices() (lines [][]int) {
scanner := GetLines() scanner := GetLines()
for scanner.Scan() { for scanner.Scan() {
string_line := strings.Fields(scanner.Text()) string_line := strings.Fields(scanner.Text())