64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func countJoltage(i int, rows *[][]int) int {
|
|
totalJoltage := 0
|
|
|
|
for _, rowNumbers := range *rows {
|
|
index := 0
|
|
|
|
for digitPlace := range i {
|
|
for candidate := index; candidate <= len(rowNumbers)-i+digitPlace; candidate++ {
|
|
if rowNumbers[candidate] > rowNumbers[index] {
|
|
index = candidate
|
|
}
|
|
}
|
|
|
|
totalJoltage += rowNumbers[index] * int(math.Pow10(i-digitPlace-1))
|
|
|
|
index++
|
|
}
|
|
}
|
|
|
|
return totalJoltage
|
|
}
|
|
|
|
func main() {
|
|
rows := strings.Split(loadData(false), "\n")
|
|
|
|
numbers := make([][]int, len(rows))
|
|
|
|
for ii, rowString := range rows {
|
|
row := strings.TrimSpace(rowString)
|
|
|
|
numbers[ii] = make([]int, len(row))
|
|
|
|
for numberIndex, cc := range row {
|
|
numbers[ii][numberIndex] = int(cc - '0')
|
|
}
|
|
}
|
|
|
|
fmt.Println(countJoltage(2, &numbers))
|
|
fmt.Println(countJoltage(12, &numbers))
|
|
}
|
|
|
|
func loadData(test bool) string {
|
|
fileName := "input"
|
|
|
|
if test {
|
|
fileName = "test"
|
|
}
|
|
|
|
if cont, err := os.ReadFile(fileName); err != nil {
|
|
panic(err)
|
|
} else {
|
|
return strings.TrimSpace(string(cont))
|
|
}
|
|
}
|