mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
add result struct to package one
remove unnecessary assignments in util.splitNumber
This commit is contained in:
parent
cf22aff066
commit
2dc1d494af
29
day-11/internal/one/result.go
Normal file
29
day-11/internal/one/result.go
Normal file
@ -0,0 +1,29 @@
|
||||
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)
|
||||
}
|
@ -28,11 +28,14 @@ func Solve(buf []byte) (int, error) {
|
||||
next := queue.New[int]()
|
||||
for !old.IsEmpty() {
|
||||
n := old.Dequeue()
|
||||
left, right, ok := applyRules(n)
|
||||
if ok {
|
||||
next.Enqueue(left)
|
||||
r := applyRules(n)
|
||||
switch r.kind {
|
||||
case flip, multiply:
|
||||
next.Enqueue(r.right)
|
||||
case split:
|
||||
next.Enqueue(r.left)
|
||||
next.Enqueue(r.right)
|
||||
}
|
||||
next.Enqueue(right)
|
||||
}
|
||||
|
||||
old = next
|
||||
@ -42,14 +45,14 @@ func Solve(buf []byte) (int, error) {
|
||||
return old.Len(), nil
|
||||
}
|
||||
|
||||
func applyRules(num int) (int, int, bool) {
|
||||
func applyRules(num int) result {
|
||||
switch num := num; {
|
||||
case num == 0:
|
||||
return 0, 1, false
|
||||
return result{flip, 0, 1}
|
||||
case lenItoa(num)%2 == 0:
|
||||
left, right := splitNumber(num)
|
||||
return left, right, true
|
||||
return result{split, left, right}
|
||||
default:
|
||||
return 0, num * magic, false
|
||||
return result{multiply, 0, num * magic}
|
||||
}
|
||||
}
|
||||
|
@ -44,10 +44,7 @@ func mustConv(s string) int {
|
||||
func splitNumber(n int) (int, int) {
|
||||
factor := int(math.Pow10(lenItoa(n) / 2))
|
||||
|
||||
right := n % factor
|
||||
left := int(n / factor)
|
||||
|
||||
return left, right
|
||||
return int(n / factor), n % factor
|
||||
}
|
||||
|
||||
func lenItoa(n int) int {
|
||||
|
@ -44,10 +44,7 @@ func mustConv(s string) int {
|
||||
func splitNumber(n int) (int, int) {
|
||||
factor := int(math.Pow10(lenItoa(n) / 2))
|
||||
|
||||
left := int(n / factor)
|
||||
right := n % factor
|
||||
|
||||
return left, right
|
||||
return int(n / factor), n % factor
|
||||
}
|
||||
|
||||
func lenItoa(n int) int {
|
||||
|
Loading…
Reference in New Issue
Block a user