From 39f1e7ae748642d2b9a9d543a92b2463906331e9 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 2 Dec 2024 18:19:40 +0100 Subject: [PATCH] update day2 rewrite all functions to methods --- 2024/02/main.go | 117 +++++++++++++++++++++---------------------- 2024/helper/input.go | 2 +- 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/2024/02/main.go b/2024/02/main.go index e8fe919..ae2a1f8 100644 --- a/2024/02/main.go +++ b/2024/02/main.go @@ -6,15 +6,22 @@ import ( "sort" ) -var reports [][]int +type reports []report +type report struct { + levels []int +} func main() { - reports = helper.ReadLinesToIntArrays() + // generate reports + var reports reports + for _, val := range helper.ReadLinesToIntSlices() { + reports = append(reports, report{levels: val}) + } // part 1 save_count := 0 for _, r := range reports { - if isReportSave(r) { + if r.isSave() { save_count++ } } @@ -23,23 +30,60 @@ func main() { // part 2 save_count = 0 for _, r := range reports { - if isReportSaveWithDamper(r) { + if r.isSaveWithDamper() { 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 } - for i := range report { + for i := range r.levels { // remove one item from report and try to validate - new_report := removeFromSlice(report, i) - if isReportSave(new_report) { + if r.removeLevelByIndex(i).isSave() { return true } } @@ -47,55 +91,10 @@ func isReportSaveWithDamper(report []int) bool { return false } -func removeFromSlice(slice []int, dropIndex int) []int { - var newslice []int - for i, v := range slice { - if i != dropIndex { - newslice = append(newslice, v) - } - } +func (r report) removeLevelByIndex(dropIndex int) report { + var newlevels []int + newlevels = append(newlevels, r.levels[:dropIndex]...) + newlevels = append(newlevels, r.levels[dropIndex+1:]...) - return newslice -} - -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 + return report{levels: newlevels} } diff --git a/2024/helper/input.go b/2024/helper/input.go index 6e00835..fe70669 100644 --- a/2024/helper/input.go +++ b/2024/helper/input.go @@ -38,7 +38,7 @@ func ReadLinesTwoIntSlices() (list_a []int, list_b []int) { return list_a, list_b } -func ReadLinesToIntArrays() (lines [][]int) { +func ReadLinesToIntSlices() (lines [][]int) { scanner := GetLines() for scanner.Scan() { string_line := strings.Fields(scanner.Text())