2025 Day 2 Complete!
This commit is contained in:
67
2025/day02/main.go
Normal file
67
2025/day02/main.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToString()
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func part1(inp string) {
|
||||
var res int
|
||||
ranges := getRanges(inp)
|
||||
for _, rng := range ranges {
|
||||
for wrk := rng.beg; wrk <= rng.end; wrk++ {
|
||||
sWrk := h.Itoa(wrk)
|
||||
hlf := len(sWrk) / 2
|
||||
if sWrk[:hlf] == sWrk[hlf:] {
|
||||
res += wrk
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func part2(inp string) {
|
||||
var res int
|
||||
ranges := getRanges(inp)
|
||||
for _, rng := range ranges {
|
||||
for wrk := rng.beg; wrk <= rng.end; wrk++ {
|
||||
sWrk := h.Itoa(wrk)
|
||||
cnt := len(sWrk)
|
||||
for i := 0; i < cnt/2; i++ {
|
||||
ch := i + 1
|
||||
if cnt%ch != 0 { // Must be divisible
|
||||
continue
|
||||
}
|
||||
if strings.Repeat(sWrk[:ch], cnt/ch) == sWrk {
|
||||
res += wrk
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
beg, end int
|
||||
}
|
||||
|
||||
func getRanges(inp string) []Range {
|
||||
ret := []Range{}
|
||||
segs := strings.Split(inp, ",")
|
||||
for i := range segs {
|
||||
pts := strings.Split(segs[i], "-")
|
||||
ret = append(ret, Range{beg: h.Atoi(pts[0]), end: h.Atoi(pts[1])})
|
||||
}
|
||||
return ret
|
||||
}
|
||||
Reference in New Issue
Block a user