advent-of-code/day2/day2.go

61 lines
1.1 KiB
Go

package main
import (
"fmt"
)
// https://adventofcode.com/2024/day/2
// 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
var reports = [][]int{{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}}
func abs(num int) int {
if (num < 0) {
return -num
} else {
return num
}
}
// lvl == level
func compareLevels(report []int) (bool) {
var isReportSafe bool = false
var incrOrDecr bool = report[1] > report[0] // incr: true | decr: false
for i := 0; i < len(report)-1; i++ {
var lvlDifference int = abs(report[i]-report[i+1])
if lvlDifference > 3 || lvlDifference == 0 {
return false
} else if incrOrDecr != (report[i+1] > report[i]) {
return false
} else {
isReportSafe = true
continue
}
}
return isReportSafe
}
func main() {
var safeReportCount int
for _, report := range reports {
isReportSafe := compareLevels(report)
if isReportSafe {
safeReportCount++
}
}
fmt.Printf("List of reports: %v\n", reports)
fmt.Printf("Number of safe reports: %v\n", safeReportCount)
}