diff --git a/day-11/internal/one/result.go b/day-11/internal/one/result.go new file mode 100644 index 0000000..0e5ef54 --- /dev/null +++ b/day-11/internal/one/result.go @@ -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) +} diff --git a/day-11/internal/one/solve.go b/day-11/internal/one/solve.go index 5549d23..77d1f2e 100644 --- a/day-11/internal/one/solve.go +++ b/day-11/internal/one/solve.go @@ -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} } } diff --git a/day-11/internal/one/util.go b/day-11/internal/one/util.go index a24a2b6..a5c930a 100644 --- a/day-11/internal/one/util.go +++ b/day-11/internal/one/util.go @@ -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 { diff --git a/day-11/internal/two/util.go b/day-11/internal/two/util.go index 6d9c0cf..fe41770 100644 --- a/day-11/internal/two/util.go +++ b/day-11/internal/two/util.go @@ -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 {