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,23 +30,60 @@ 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() {
return false
}
if isReportSave(report) { for i := 0; i < len(r.levels)-1; i++ {
if !isValidLevelDifference(r.levels[i], r.levels[i+1]) {
return false
}
}
return true
}
func (r report) isSorted() bool {
ascSorted := sort.SliceIsSorted(
r.levels,
func(i, j int) bool { return r.levels[i] < r.levels[j] },
)
descSorted := sort.SliceIsSorted(
r.levels,
func(i, j int) bool { return r.levels[i] > r.levels[j] },
)
return ascSorted || descSorted
}
func isValidLevelDifference(i int, j int) bool {
if i == j {
return false
}
if helper.Abs(i-j) > 3 {
return false
}
return true
}
func (r report) isSaveWithDamper() bool {
if r.isSave() {
return true return true
} }
for i := range report { for i := range r.levels {
// remove one item from report and try to validate // remove one item from report and try to validate
new_report := removeFromSlice(report, i) if r.removeLevelByIndex(i).isSave() {
if isReportSave(new_report) {
return true return true
} }
} }
@ -47,55 +91,10 @@ func isReportSaveWithDamper(report []int) bool {
return false return false
} }
func removeFromSlice(slice []int, dropIndex int) []int { func (r report) removeLevelByIndex(dropIndex int) report {
var newslice []int var newlevels []int
for i, v := range slice { newlevels = append(newlevels, r.levels[:dropIndex]...)
if i != dropIndex { newlevels = append(newlevels, r.levels[dropIndex+1:]...)
newslice = append(newslice, v)
}
}
return newslice return report{levels: newlevels}
}
func isReportSave(report []int) bool {
if !isReportSorted(report) {
return false
}
for i := range report {
if (i + 1) < len(report) {
if !isPairSave(report[i], report[i+1]) {
return false
}
}
}
return true
}
func isReportSorted(slice []int) bool {
ascSorted := sort.SliceIsSorted(
slice,
func(i, j int) bool { return slice[i] < slice[j] },
)
descSorted := sort.SliceIsSorted(
slice,
func(i, j int) bool { return slice[i] > slice[j] },
)
return ascSorted || descSorted
}
func isPairSave(x int, y int) bool {
if x == y {
return false
}
diff := helper.Abs(x - y)
if diff > 3 {
return false
}
return true
} }

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())