77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func countJoltage(i int, rows *[][]int) int {
|
|
totalJoltage := 0
|
|
|
|
for _, rowNumbers := range *rows {
|
|
index := 0
|
|
thisJoltage := 0
|
|
|
|
for digitPlace := range i {
|
|
index = int(math.Max(float64(index), float64(digitPlace)))
|
|
|
|
for candidate := index; candidate <= len(rowNumbers)-i+digitPlace; candidate++ {
|
|
if rowNumbers[candidate] > rowNumbers[index] {
|
|
index = candidate
|
|
}
|
|
}
|
|
|
|
thisJoltage += rowNumbers[index] * int(math.Pow10(i-digitPlace-1))
|
|
|
|
index++
|
|
}
|
|
totalJoltage += thisJoltage
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
func parseInt(s string) int {
|
|
if ii, err := strconv.Atoi(s); err != nil {
|
|
panic(err)
|
|
} else {
|
|
return ii
|
|
}
|
|
}
|