Compare commits

..

No commits in common. "2dc1d494af111febcf7935b256da08d7f1d3fed0" and "aafe39aa85518e32f415c54444a1865088611e89" have entirely different histories.

7 changed files with 30 additions and 56 deletions

View File

@ -2,14 +2,14 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-11 pkg: github.com/onyx-and-iris/aoc2024/day-11
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.05353 ns/op BenchmarkSolve-12 1000000000 0.07126 ns/op
BenchmarkSolve-12 1000000000 0.05134 ns/op BenchmarkSolve-12 1000000000 0.07114 ns/op
BenchmarkSolve-12 1000000000 0.04984 ns/op BenchmarkSolve-12 1000000000 0.07102 ns/op
BenchmarkSolve-12 1000000000 0.05239 ns/op BenchmarkSolve-12 1000000000 0.07157 ns/op
BenchmarkSolve-12 1000000000 0.04956 ns/op BenchmarkSolve-12 1000000000 0.07201 ns/op
BenchmarkSolve-12 1000000000 0.04873 ns/op BenchmarkSolve-12 1000000000 0.07461 ns/op
BenchmarkSolve-12 1000000000 0.04943 ns/op BenchmarkSolve-12 1000000000 0.07305 ns/op
BenchmarkSolve-12 1000000000 0.05024 ns/op BenchmarkSolve-12 1000000000 0.07548 ns/op
BenchmarkSolve-12 1000000000 0.05187 ns/op BenchmarkSolve-12 1000000000 0.07503 ns/op
BenchmarkSolve-12 1000000000 0.04968 ns/op BenchmarkSolve-12 1000000000 0.07202 ns/op
ok github.com/onyx-and-iris/aoc2024/day-11 4.083s ok github.com/onyx-and-iris/aoc2024/day-11 6.636s

View File

@ -2,5 +2,5 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-11/internal/one pkg: github.com/onyx-and-iris/aoc2024/day-11/internal/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.03459 ns/op BenchmarkSolve-12 1000000000 0.03508 ns/op
ok github.com/onyx-and-iris/aoc2024/day-11/internal/one 0.244s ok github.com/onyx-and-iris/aoc2024/day-11/internal/one 0.244s

View File

@ -1,29 +0,0 @@
package one
import "fmt"
const (
flip = iota
split
multiply
)
type result struct {
kind int
left int
right int
}
func (r result) String() string {
var kindStr string
switch r.kind {
case flip:
kindStr = "flip"
case split:
kindStr = "split"
case multiply:
kindStr = "multiply"
}
return fmt.Sprintf("kind: %s left: %d right: %d", kindStr, r.left, r.right)
}

View File

@ -28,14 +28,11 @@ func Solve(buf []byte) (int, error) {
next := queue.New[int]() next := queue.New[int]()
for !old.IsEmpty() { for !old.IsEmpty() {
n := old.Dequeue() n := old.Dequeue()
r := applyRules(n) left, right, ok := applyRules(n)
switch r.kind { if ok {
case flip, multiply: next.Enqueue(left)
next.Enqueue(r.right)
case split:
next.Enqueue(r.left)
next.Enqueue(r.right)
} }
next.Enqueue(right)
} }
old = next old = next
@ -45,14 +42,14 @@ func Solve(buf []byte) (int, error) {
return old.Len(), nil return old.Len(), nil
} }
func applyRules(num int) result { func applyRules(num int) (int, int, bool) {
switch num := num; { switch num := num; {
case num == 0: case num == 0:
return result{flip, 0, 1} return 0, 1, false
case lenItoa(num)%2 == 0: case lenItoa(num)%2 == 0:
left, right := splitNumber(num) left, right := splitNumber(num)
return result{split, left, right} return left, right, true
default: default:
return result{multiply, 0, num * magic} return 0, num * magic, false
} }
} }

View File

@ -44,7 +44,10 @@ func mustConv(s string) int {
func splitNumber(n int) (int, int) { func splitNumber(n int) (int, int) {
factor := int(math.Pow10(lenItoa(n) / 2)) factor := int(math.Pow10(lenItoa(n) / 2))
return int(n / factor), n % factor right := n % factor
left := int(n / factor)
return left, right
} }
func lenItoa(n int) int { func lenItoa(n int) int {

View File

@ -2,5 +2,5 @@ goos: linux
goarch: amd64 goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-11/internal/two pkg: github.com/onyx-and-iris/aoc2024/day-11/internal/two
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.01475 ns/op BenchmarkSolve-12 1000000000 0.03833 ns/op
ok github.com/onyx-and-iris/aoc2024/day-11/internal/two 0.102s ok github.com/onyx-and-iris/aoc2024/day-11/internal/two 0.322s

View File

@ -44,7 +44,10 @@ func mustConv(s string) int {
func splitNumber(n int) (int, int) { func splitNumber(n int) (int, int) {
factor := int(math.Pow10(lenItoa(n) / 2)) factor := int(math.Pow10(lenItoa(n) / 2))
return int(n / factor), n % factor left := int(n / factor)
right := n % factor
return left, right
} }
func lenItoa(n int) int { func lenItoa(n int) int {