Day02
This commit is contained in:
119
2025/gareth/day02/day02.go
Normal file
119
2025/gareth/day02/day02.go
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package day02
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Very messy and not my finest work but not too bad for 6am
|
||||||
|
|
||||||
|
func parseInput(input string) [][2]int {
|
||||||
|
var ranges [][2]int
|
||||||
|
lines := strings.Split(strings.TrimSpace(input), ",")
|
||||||
|
for _, part := range lines {
|
||||||
|
if part == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ab := strings.Split(part, "-")
|
||||||
|
if len(ab) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
a, _ := strconv.Atoi(ab[0])
|
||||||
|
b, _ := strconv.Atoi(ab[1])
|
||||||
|
ranges = append(ranges, [2]int{a, b})
|
||||||
|
}
|
||||||
|
return ranges
|
||||||
|
}
|
||||||
|
|
||||||
|
func divisors(n int) []int {
|
||||||
|
out := make([]int, 0)
|
||||||
|
for i := 1; i <= n; i++ {
|
||||||
|
if n%i == 0 {
|
||||||
|
out = append(out, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
ranges := parseInput(input)
|
||||||
|
invalidSum := 0
|
||||||
|
|
||||||
|
for _, r := range ranges {
|
||||||
|
low, high := r[0], r[1]
|
||||||
|
|
||||||
|
minLen := len(strconv.Itoa(low))
|
||||||
|
maxLen := len(strconv.Itoa(high))
|
||||||
|
|
||||||
|
for digits := minLen; digits <= maxLen; digits++ {
|
||||||
|
if digits%2 != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
half := digits / 2
|
||||||
|
start := int(math.Pow(10, float64(half-1)))
|
||||||
|
end := int(math.Pow(10, float64(half)))
|
||||||
|
|
||||||
|
for first := start; first < end; first++ {
|
||||||
|
s := strconv.Itoa(first)
|
||||||
|
selected, _ := strconv.Atoi(s + s)
|
||||||
|
|
||||||
|
if selected < low {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if selected > high {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidSum += selected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return invalidSum
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
ranges := parseInput(input)
|
||||||
|
total := 0
|
||||||
|
|
||||||
|
for _, r := range ranges {
|
||||||
|
low, high := r[0], r[1]
|
||||||
|
seen := make(map[int]bool)
|
||||||
|
|
||||||
|
minLen := len(strconv.Itoa(low))
|
||||||
|
maxLen := len(strconv.Itoa(high))
|
||||||
|
|
||||||
|
for d := minLen; d <= maxLen; d++ {
|
||||||
|
for _, L := range divisors(d) {
|
||||||
|
k := d / L
|
||||||
|
if k < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
start := int(math.Pow(10, float64(L-1)))
|
||||||
|
end := int(math.Pow(10, float64(L)))
|
||||||
|
|
||||||
|
for base := start; base < end; base++ {
|
||||||
|
s := strings.Repeat(strconv.Itoa(base), k)
|
||||||
|
selected, _ := strconv.Atoi(s)
|
||||||
|
|
||||||
|
if selected < low {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if selected > high {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !seen[selected] {
|
||||||
|
seen[selected] = true
|
||||||
|
total += selected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total
|
||||||
|
}
|
||||||
17
2025/gareth/day02/day02_test.go
Normal file
17
2025/gareth/day02/day02_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package day02
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPart1(t *testing.T) {
|
||||||
|
r := Part1(`11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`)
|
||||||
|
assert.Equal(t, 1227775554, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPart2(t *testing.T) {
|
||||||
|
r := Part2(`11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`)
|
||||||
|
assert.Equal(t, 4174379265, r)
|
||||||
|
}
|
||||||
1
2025/gareth/day02/input.txt
Normal file
1
2025/gareth/day02/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
78847-119454,636-933,7143759788-7143793713,9960235-10043487,44480-68595,23468-43311,89-123,785189-1014654,3829443354-3829647366,647009-692765,2-20,30-42,120909-197026,5477469-5677783,9191900808-9191943802,1045643-1169377,46347154-46441299,2349460-2379599,719196-779497,483556-641804,265244-450847,210541-230207,195-275,75702340-75883143,58-84,2152-3237,3367-5895,1552-2029,9575-13844,6048-8966,419388311-419470147,936-1409,9292901468-9292987321
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"aoc/2025/day03"
|
"aoc/2025/day02"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
data, _ := os.ReadFile("day03/input.txt")
|
data, _ := os.ReadFile("day02/input.txt")
|
||||||
fmt.Printf("part 1: %d\n", day03.Part1(string(data)))
|
fmt.Printf("part 1: %d\n", day02.Part1(string(data)))
|
||||||
fmt.Printf("part 2: %d\n", day03.Part2(string(data)))
|
fmt.Printf("part 2: %d\n", day02.Part2(string(data)))
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
fmt.Printf("Execution time: %s\n", elapsed)
|
fmt.Printf("Execution time: %s\n", elapsed)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user