Day 02. solutions

This commit is contained in:
Borna Rajković 2024-12-03 19:35:51 +01:00
parent eff1d70d1c
commit 46835c090c
6 changed files with 1135 additions and 0 deletions

View File

@ -6,3 +6,6 @@ import _ "embed"
//go:embed day_01/data.txt
var Day01Data string
//go:embed day_02/data.txt
var Day02Data string

1000
day_02/data.txt Normal file

File diff suppressed because it is too large Load Diff

6
day_02/example_data.txt Normal file
View File

@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

97
day_02/solution.go Normal file
View File

@ -0,0 +1,97 @@
package day_02
import (
"strconv"
"strings"
)
// task:https://adventofcode.com/2024/day/2
// short summary - find valid reports
// reports are valid if:
// 1. numbers are either all increasing or all decreasing
// 2. two adjacent numbers differ by at least one and at most three.
func SolveBasic(input string) int {
reports := strings.Split(input, "\n")
safeReports := 0
for _, report := range reports {
if isSafeReport(asInts(strings.Split(report, " "))) {
safeReports++
}
}
return safeReports
}
// task:https://adventofcode.com/2024/day/2#part2
// short summary - find valid reports
// same as above, reports are valid if:
// 1. numbers are either all increasing or all decreasing
// 2. two adjacent numbers differ by at least one and at most three.
// BUT ALSO
// 3. reports is valid if removing one number makes it safe
func SolveComplex(input string) int {
reports := strings.Split(input, "\n")
safeReports := 0
for _, report := range reports {
levels := asInts(strings.Split(report, " "))
if isSafeReport(levels) {
safeReports++
} else {
reducedLevels := make([]int, len(levels)-1)
for skipped := 0; skipped < len(levels); skipped++ {
pos := 0
for i := 0; i < len(levels); i++ {
if i == skipped {
continue
}
reducedLevels[pos] = levels[i]
pos++
}
if isSafeReport(reducedLevels) {
safeReports++
break
}
}
}
}
return safeReports
}
// helper methods
func isSafeReport(levels []int) bool {
if levels[0] == levels[1] {
return false
} else if levels[0] < levels[1] {
for i := 0; i < len(levels)-1; i++ {
diff := levels[i+1] - levels[i]
if diff < 1 || diff > 3 {
return false
}
}
} else if levels[0] > levels[1] {
for i := 0; i < len(levels)-1; i++ {
diff := levels[i] - levels[i+1]
if diff < 1 || diff > 3 {
return false
}
}
}
return true
}
func asInts(numbers []string) []int {
values := make([]int, 0, len(numbers))
for _, v := range numbers {
values = append(values, asInt(v))
}
return values
}
func asInt(v string) int {
value, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
return value
}

24
day_02/solution_test.go Normal file
View File

@ -0,0 +1,24 @@
package day_02_test
import (
"advent_of_code_2024/day_02"
_ "embed"
"testing"
)
//go:embed example_data.txt
var ExampleData string
func TestBasicSolutionExample(t *testing.T) {
result := day_02.SolveBasic(ExampleData)
if result != 2 {
t.Fatalf("Expected 2 received %d", result)
}
}
func TestComplexSolutionExample(t *testing.T) {
result := day_02.SolveComplex(ExampleData)
if result != 4 {
t.Fatalf("Expected 4 received %d", result)
}
}

View File

@ -2,6 +2,7 @@ package main
import (
"advent_of_code_2024/day_01"
"advent_of_code_2024/day_02"
_ "embed"
"fmt"
"os"
@ -15,6 +16,10 @@ func main() {
fmt.Printf("Day 01. Basic: %d\n", day_01.SolveBasic(Day01Data)) // 2113135
case "01-complex":
fmt.Printf("Day 01. Complex: %d\n", day_01.SolveComplex(Day01Data)) // 19097157
case "02-basic":
fmt.Printf("Day 02. Basic: %d\n", day_02.SolveBasic(Day02Data)) // 690
case "02-complex":
fmt.Printf("Day 02. Complex: %d\n", day_02.SolveComplex(Day02Data)) // 710
}
}