Jonathan D. Storm 2 months ago
parent
commit
b917809b2f
2 changed files with 86 additions and 0 deletions
  1. 26 0
      cmd/day_11/main.go
  2. 60 0
      cmd/day_11/main_test.go

+ 26 - 0
cmd/day_11/main.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"math"
+)
+
+func main() {
+}
+
+func step(start, next []uint64) []uint64 {
+	for _, stone := range start {
+		if stone == 0 {
+			next = append(next, 1)
+			continue
+		}
+		length := uint64(math.Log10(float64(stone))) + 1
+
+		if length%2 == 0 {
+			mod := uint64(math.Pow10(int(length / 2)))
+			next = append(next, stone/mod, stone%mod)
+			continue
+		}
+		next = append(next, stone*2024)
+	}
+	return next
+}

+ 60 - 0
cmd/day_11/main_test.go

@@ -0,0 +1,60 @@
+package main
+
+import (
+	"slices"
+	"testing"
+)
+
+func TestExamples(t *testing.T) {
+	cases := []struct {
+		start    []uint64
+		steps    int
+		expected []uint64
+	}{
+		{[]uint64{0, 1, 10, 99, 999}, 1,
+			[]uint64{1, 2024, 1, 0, 9, 9, 2021976},
+		},
+		{[]uint64{125, 17}, 6,
+			[]uint64{2097446912, 14168, 4048, 2, 0, 2, 4, 40, 48, 2024, 40, 48, 80, 96, 2, 8, 6, 7, 6, 0, 3, 2},
+		},
+	}
+	for _, c := range cases {
+		actual := make([]uint64, 0, 64)
+		previous := make([]uint64, len(c.start), 64)
+		copy(previous, c.start)
+
+		for i := 0; i < c.steps; i++ {
+			actual = actual[:0]
+			actual = step(previous, actual)
+			if len(actual) > len(previous) {
+				previous = make([]uint64, len(actual), 2*len(previous))
+			}
+			copy(previous, actual)
+		}
+
+		if !slices.Equal(actual, c.expected) {
+			t.Errorf("expected %v; got %v", c.expected, actual)
+		}
+	}
+}
+
+func TestExample2(t *testing.T) {
+	start := []uint64{125, 17}
+	expected := 55312
+
+	actual := make([]uint64, 0, 64)
+	previous := make([]uint64, len(start), 64)
+	copy(previous, start)
+
+	for i := 0; i < 25; i++ {
+		actual = actual[:0]
+		actual = step(previous, actual)
+		if len(actual) > len(previous) {
+			previous = make([]uint64, len(actual), 2*len(previous))
+		}
+		copy(previous, actual)
+	}
+	if len(actual) != expected {
+		t.Errorf("expected %d; got %d", expected, len(actual))
+	}
+}