advent2022

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

commit 6bb3a066ed5fa1cde0df46219fd86379777d2d0e
parent 771f050148a3cf7f967aadd12ffdf97f8e199782
Author: bsandro <email@bsandro.tech>
Date:   Tue, 13 Dec 2022 12:03:53 +0000

Day 13 part 2

Diffstat:
Mday13/main.go | 46+++++++++++++++++++++++++++++++++++++---------
Aday13/task.txt | 151++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 188 insertions(+), 9 deletions(-)

diff --git a/day13/main.go b/day13/main.go @@ -5,9 +5,21 @@ import ( "fmt" "log" "os" + "sort" "strings" ) +type Packets []any + +func (p Packets) Len() int { return len(p) } +func (p Packets) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} +func (p Packets) Less(i, j int) bool { + cmp, _ := Compare(p[i], p[j]) + return cmp +} + func main() { if len(os.Args) > 1 { day13(os.Args[1]) @@ -26,9 +38,9 @@ func day13(input_file string) { } pairs := strings.Split(string(input), "\n\n") sum := 0 + var packets Packets for i, p := range pairs { pair := strings.Split(p, "\n") - //fmt.Printf("pair: %v\n", pair) if len(pair) < 2 { log.Fatal("Invalid pair", pair) } @@ -39,26 +51,43 @@ func day13(input_file string) { if err = json.Unmarshal([]byte(pair[1]), &p2); err != nil { log.Fatal(err) } - //fmt.Printf("\n\n--- PAIR %d ---\n", i+1) res, _ := Compare(p1, p2) if res { - //fmt.Printf("Pair %d ok (%v)\n", i+1, cont) sum += i + 1 - } else { - //fmt.Printf("Pair %d failed (%v)\n", i+1, cont) + } + packets = append(packets, p1, p2) + } + m1, m2 := "[[2]]", "[[6]]" + var marker1, marker2 any + json.Unmarshal([]byte(m1), &marker1) + json.Unmarshal([]byte(m2), &marker2) + packets = append(packets, marker1, marker2) + + sort.Sort(packets) + + v1, v2 := 0, 0 + for i, p := range packets { + j, err := json.Marshal(p) + if err != nil { + log.Fatal(err) + } + if string(j) == m1 { + v1 = i + 1 + } + if string(j) == m2 { + v2 = i + 1 } } fmt.Println("Part 1:", sum) + fmt.Println("Part 2:", v1*v2) } // returns (result, continue) func Compare(p1, p2 any) (bool, bool) { - //fmt.Printf("Compare(%v,%v)\n", p1, p2) p1ar, p2ar := IsArray(p1), IsArray(p2) 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)) @@ -72,7 +101,6 @@ func Compare(p1, p2 any) (bool, bool) { } func CompareArrays(p1, p2 []any) (bool, bool) { - //fmt.Printf("CompareArrays(%v, %v)\n", p1, p2) for i := 0; i < len(p1); i++ { if i >= len(p2) { return false, false @@ -82,7 +110,6 @@ func CompareArrays(p1, p2 []any) (bool, bool) { return res, false } } - //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) } @@ -94,6 +121,7 @@ func IsArray(v any) bool { return true default: log.Fatal("unknown value", v) + //return true } return false } diff --git a/day13/task.txt b/day13/task.txt @@ -0,0 +1,151 @@ +--- Day 13: Distress Signal --- + +You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a distress signal. + +Your handheld device must still not be working properly; the packets from the distress signal got decoded out of order. You'll need to re-order the list of received packets (your puzzle input) to decode the message. + +Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify how many pairs of packets are in the right order. + +For example: + +[1,1,3,1,1] +[1,1,5,1,1] + +[[1],[2,3,4]] +[[1],4] + +[9] +[[8,7,6]] + +[[4,4],4,4] +[[4,4],4,4,4] + +[7,7,7,7] +[7,7,7] + +[] +[3] + +[[[]]] +[[]] + +[1,[2,[3,[4,[5,6,7]]]],8,9] +[1,[2,[3,[4,[5,6,0]]]],8,9] + +Packet data consists of lists and integers. Each list starts with [, ends with ], and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line. + +When comparing two values, the first value is called left and the second value is called right. Then: + + If both values are integers, the lower integer should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input. + If both values are lists, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input. + If exactly one value is an integer, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing [0,0,0] and 2, convert the right value to [2] (a list containing 2); the result is then found by instead comparing [0,0,0] and [2]. + +Using these rules, you can determine which of the pairs in the example are in the right order: + +== Pair 1 == +- Compare [1,1,3,1,1] vs [1,1,5,1,1] + - Compare 1 vs 1 + - Compare 1 vs 1 + - Compare 3 vs 5 + - Left side is smaller, so inputs are in the right order + +== Pair 2 == +- Compare [[1],[2,3,4]] vs [[1],4] + - Compare [1] vs [1] + - Compare 1 vs 1 + - Compare [2,3,4] vs 4 + - Mixed types; convert right to [4] and retry comparison + - Compare [2,3,4] vs [4] + - Compare 2 vs 4 + - Left side is smaller, so inputs are in the right order + +== Pair 3 == +- Compare [9] vs [[8,7,6]] + - Compare 9 vs [8,7,6] + - Mixed types; convert left to [9] and retry comparison + - Compare [9] vs [8,7,6] + - Compare 9 vs 8 + - Right side is smaller, so inputs are not in the right order + +== Pair 4 == +- Compare [[4,4],4,4] vs [[4,4],4,4,4] + - Compare [4,4] vs [4,4] + - Compare 4 vs 4 + - Compare 4 vs 4 + - Compare 4 vs 4 + - Compare 4 vs 4 + - Left side ran out of items, so inputs are in the right order + +== Pair 5 == +- Compare [7,7,7,7] vs [7,7,7] + - Compare 7 vs 7 + - Compare 7 vs 7 + - Compare 7 vs 7 + - Right side ran out of items, so inputs are not in the right order + +== Pair 6 == +- Compare [] vs [3] + - Left side ran out of items, so inputs are in the right order + +== Pair 7 == +- Compare [[[]]] vs [[]] + - Compare [[]] vs [] + - Right side ran out of items, so inputs are not in the right order + +== Pair 8 == +- Compare [1,[2,[3,[4,[5,6,7]]]],8,9] vs [1,[2,[3,[4,[5,6,0]]]],8,9] + - Compare 1 vs 1 + - Compare [2,[3,[4,[5,6,7]]]] vs [2,[3,[4,[5,6,0]]]] + - Compare 2 vs 2 + - Compare [3,[4,[5,6,7]]] vs [3,[4,[5,6,0]]] + - Compare 3 vs 3 + - Compare [4,[5,6,7]] vs [4,[5,6,0]] + - Compare 4 vs 4 + - Compare [5,6,7] vs [5,6,0] + - Compare 5 vs 5 + - Compare 6 vs 6 + - Compare 7 vs 0 + - Right side is smaller, so inputs are not in the right order + +What are the indices of the pairs that are already in the right order? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is 13. + +Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs? + +Your puzzle answer was 5717. +--- Part Two --- + +Now, you just need to put all of the packets in the right order. Disregard the blank lines in your list of received packets. + +The distress signal protocol also requires that you include two additional divider packets: + +[[2]] +[[6]] + +Using the same rules as before, organize all packets - the ones in your list of received packets as well as the two divider packets - into the correct order. + +For the example above, the result of putting the packets in the correct order is: + +[] +[[]] +[[[]]] +[1,1,3,1,1] +[1,1,5,1,1] +[[1],[2,3,4]] +[1,[2,[3,[4,[5,6,0]]]],8,9] +[1,[2,[3,[4,[5,6,7]]]],8,9] +[[1],4] +[[2]] +[3] +[[4,4],4,4] +[[4,4],4,4,4] +[[6]] +[7,7,7] +[7,7,7,7] +[[8,7,6]] +[9] + +Afterward, locate the divider packets. To find the decoder key for this distress signal, you need to determine the indices of the two divider packets and multiply them together. (The first packet is at index 1, the second packet is at index 2, and so on.) In this example, the divider packets are 10th and 14th, and so the decoder key is 140. + +Organize all of the packets into the correct order. What is the decoder key for the distress signal? + +Your puzzle answer was 25935.