From c54a2e344009c7f9d4cbf57264dc3eb26a3d5dc3 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 4 Dec 2024 22:45:05 +0200 Subject: [PATCH] solve day 2 --- day1.go | 10 +++--- day2.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 10 ++++-- 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 day2.go diff --git a/day1.go b/day1.go index 4e55331..c1b756c 100644 --- a/day1.go +++ b/day1.go @@ -12,7 +12,7 @@ type Day1Input struct { column2 []int64 } -func day1_parse(lines []string) (*Day1Input, error) { +func Day1Parse(lines []string) (*Day1Input, error) { column1 := make([]int64, len(lines)) column2 := make([]int64, len(lines)) @@ -42,8 +42,8 @@ func day1_parse(lines []string) (*Day1Input, error) { return &parsed, nil } -func day1_part1(lines []string) (error) { - input, err := day1_parse(lines) +func Day1Part1(lines []string) (error) { + input, err := Day1Parse(lines) if err != nil { return err } @@ -70,8 +70,8 @@ func day1_part1(lines []string) (error) { return nil } -func day1_part2(lines []string) (error) { - input, err := day1_parse(lines) +func Day1Part2(lines []string) (error) { + input, err := Day1Parse(lines) if err != nil { return err } diff --git a/day2.go b/day2.go new file mode 100644 index 0000000..e7334c7 --- /dev/null +++ b/day2.go @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +type Day2Input [][]int64 + +func Day2Parse(lines []string) (Day2Input, error) { + rows := make([][]int64, len(lines)) + for i, line := range lines { + levelsCount := strings.Count(line, " ") + 1 + rows[i] = make([]int64, levelsCount) + + for j, level := range strings.Split(line, " ") { + number, err := strconv.ParseInt(level, 10, 0) + if err != nil { + return nil, err + } + + rows[i][j] = number + } + } + + return rows, nil +} + +func absInt(value int64) int64 { + if value < 0 { + return -value + } else { + return value + } +} + +func Day2IsReportSafe(report []int64) bool { + increasing := false + decreasing := false + + for i := range len(report)-1 { + if report[i+1] >= report[i] { + increasing = true + } + if report[i+1] <= report[i] { + decreasing = true + } + if absInt(report[i+1] - report[i]) >= 4 { + return false + } + + if increasing && decreasing { + return false + } + } + + return !(increasing && decreasing) && (increasing || decreasing) +} + +func Day2Part1(lines []string) error { + input, err := Day2Parse(lines) + if err != nil { + return err + } + + answer := 0 + for _, row := range input { + if Day2IsReportSafe(row) { + answer += 1 + } + } + + fmt.Println(answer) + + return nil +} + +func DeleteAtIndex(slice []int64, index int) []int64 { + result := make([]int64, len(slice)) + copy(result, slice) + return append(result[:index], result[index+1:]...) +} + +func Day2Part2(lines []string) error { + input, err := Day2Parse(lines) + if err != nil { + return err + } + + answer := 0 + for _, row := range input { + if Day2IsReportSafe(row) { + answer += 1 + continue + } + + for i := range len(row) { + if Day2IsReportSafe(DeleteAtIndex(row, i)) { + answer += 1 + break + } + } + } + + fmt.Println(answer) + + return nil +} diff --git a/main.go b/main.go index 8a3e6e6..4f7ec9d 100644 --- a/main.go +++ b/main.go @@ -37,9 +37,15 @@ func main() { if day == "1" { if part == "1" { - day1_part1(lines) + Day1Part1(lines) } else if part == "2" { - day1_part2(lines) + Day1Part2(lines) + } + } else if day == "2" { + if part == "1" { + Day2Part1(lines) + } else if part == "2" { + Day2Part2(lines) } } }