day04
This commit is contained in:
68
2025/go/day03/day03.go
Normal file
68
2025/go/day03/day03.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package day03
|
||||
|
||||
import (
|
||||
"adventofcode2025/utils"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const NUM_OF_CELLS = 12
|
||||
|
||||
func Part1(input string) int {
|
||||
var jolts []int
|
||||
batteries := strings.Split(input, "\n")
|
||||
for _, battery := range batteries {
|
||||
maxJolt := 0
|
||||
cells := strings.Split(battery, "")
|
||||
for i := 0; i < len(cells)-1; i++ {
|
||||
for j := i + 1; j < len(cells); j++ {
|
||||
jolt := utils.MustAtoi(fmt.Sprintf("%s%s", cells[i], cells[j]))
|
||||
if jolt > maxJolt {
|
||||
maxJolt = jolt
|
||||
}
|
||||
}
|
||||
}
|
||||
jolts = append(jolts, maxJolt)
|
||||
}
|
||||
sum := 0
|
||||
for _, jolt := range jolts {
|
||||
sum += jolt
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
var jolts []string
|
||||
batteries := strings.Split(input, "\n")
|
||||
for _, battery := range batteries {
|
||||
start := 0
|
||||
end := len(battery) - NUM_OF_CELLS
|
||||
j := ""
|
||||
for i := 0; i < NUM_OF_CELLS; i++ {
|
||||
v, pos := find_first_max(battery[start : end+1])
|
||||
j = j + v
|
||||
start = start + pos + 1
|
||||
end = len(battery) - NUM_OF_CELLS + i + 1
|
||||
}
|
||||
jolts = append(jolts, j)
|
||||
}
|
||||
sum := 0
|
||||
for _, jolt := range jolts {
|
||||
fmt.Println(jolt)
|
||||
sum += utils.MustAtoi(jolt)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func find_first_max(s string) (string, int) {
|
||||
max := byte('0')
|
||||
pos := -1
|
||||
for i := 0; i < len(s); i++ {
|
||||
t1 := s[i]
|
||||
if t1 > max {
|
||||
max = s[i]
|
||||
pos = i
|
||||
}
|
||||
}
|
||||
return string(max), pos
|
||||
}
|
||||
Reference in New Issue
Block a user