added day 03

This commit is contained in:
Simon Ziegler
2025-12-03 16:17:18 +01:00
parent 15935d3f23
commit 8dfe3e1f07
4 changed files with 284 additions and 0 deletions

76
03/main.go Normal file
View File

@@ -0,0 +1,76 @@
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
}
}