advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit 771f050148a3cf7f967aadd12ffdf97f8e199782
parent 976539c3e64f1669f59d3d094b0c95b86204113b
Author: bsandro <email@bsandro.tech>
Date:   Tue, 13 Dec 2022 10:47:19 +0000

Day 13 part 1 solved

Diffstat:
Mday13/main.go | 39+++++++++++++++++++--------------------
1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/day13/main.go b/day13/main.go @@ -42,10 +42,10 @@ func day13(input_file string) { //fmt.Printf("\n\n--- PAIR %d ---\n", i+1) res, _ := Compare(p1, p2) if res { - //fmt.Printf("Pair %d ok\n", i+1) + //fmt.Printf("Pair %d ok (%v)\n", i+1, cont) sum += i + 1 } else { - //fmt.Printf("Pair %d failed\n", i+1) + //fmt.Printf("Pair %d failed (%v)\n", i+1, cont) } } @@ -56,33 +56,34 @@ func day13(input_file string) { func Compare(p1, p2 any) (bool, bool) { //fmt.Printf("Compare(%v,%v)\n", p1, p2) p1ar, p2ar := IsArray(p1), IsArray(p2) - if p1ar && p2ar { + if !p1ar && !p2ar { + p1n, p2n := int(p1.(float64)), int(p2.(float64)) + //fmt.Printf("%d < %d = %v, continue: %v\n", p1n, p2n, p1n<p2n, p1n==p2n) + return p1n < p2n, p1n == p2n + } else if p1ar && p2ar { return CompareArrays(p1.([]any), p2.([]any)) - } else if p1ar != p2ar { - if !p1ar { - return CompareArrays([]any{p1}, p2.([]any)) - } else { - return CompareArrays(p1.([]any), []any{p2}) - } + } else if p1ar { + return CompareArrays(p1.([]any), []any{p2}) + } else if p2ar { + return CompareArrays([]any{p1}, p2.([]any)) } - p1n, p2n := int(p1.(float64)), int(p2.(float64)) - //fmt.Printf("%d <= %d = %v, continue: %v\n", p1n, p2n, p1n<=p2n, p1n==p2n) - return p1n <= p2n, p1n == p2n + log.Fatal("Invalid something") + return false, false } func CompareArrays(p1, p2 []any) (bool, bool) { //fmt.Printf("CompareArrays(%v, %v)\n", p1, p2) - mi := len(p1) - if len(p2) < mi { - mi = len(p2) - } - for i := 0; i < mi; i++ { + for i := 0; i < len(p1); i++ { + if i >= len(p2) { + return false, false + } res, cont := Compare(p1[i], p2[i]) if !cont { return res, false } } - return len(p1) < len(p2), true + //fmt.Printf("cmp len(p1) %d vs len(p2) %d = %v\n", len(p1), len(p2), len(p1)<len(p2)) + return len(p1) < len(p2), len(p1) >= len(p2) } func IsArray(v any) bool { @@ -96,5 +97,3 @@ func IsArray(v any) bool { } return false } - -// 5660 too low