diff --git a/day13/day13.go b/day13/day13.go index d3b88ab..30092ee 100644 --- a/day13/day13.go +++ b/day13/day13.go @@ -3,31 +3,151 @@ package main import ( "AOC2022/helper" "fmt" + "reflect" + "sort" "strconv" ) +type Packet struct { + value int + values []Packet +} + func main() { //args := os.Args[1:] lines := helper.ReadTextFile("day13/input") - result := 0 - index := 1 + //part1(lines) + decodePacket2 := parse("[[2]]") + decodePacket6 := parse("[[6]]") + packets := []Packet{} for i := 0; i < len(lines); i += 3 { - fmt.Println(lines[i]) - fmt.Println(lines[i+1]) - fmt.Println(comparePair(lines[i], lines[i+1])) - fmt.Println() - if comparePair(lines[i], lines[i+1]) == 0 { - result += index + packets = append(packets, parse(lines[i])) + packets = append(packets, parse(lines[i+1])) + } + packets = append(packets, decodePacket2) + packets = append(packets, decodePacket6) + sort.Slice(packets, func(i, j int) bool { + result := compare(packets[i], packets[j]) + if result == 0 { + return true + } + return false + }) + result := 1 + for i, packet := range packets { + if reflect.DeepEqual(packet, decodePacket2) { + result *= i + 1 + fmt.Println(2) + fmt.Println(i + 1) + } + if reflect.DeepEqual(packet, decodePacket6) { + result *= i + 1 + fmt.Println(6) + fmt.Println(i + 1) } - - index++ } fmt.Println(result) } +func part1(lines []string) { + index := 1 + result := 0 + for i := 0; i < len(lines); i += 3 { + left := parse(lines[i][1 : len(lines[i])-1]) + right := parse(lines[i+1][1 : len(lines[i+1])-1]) + if compare(left, right) == 0 { + fmt.Println(index) + result += index + } + index++ + } + fmt.Println(result) +} + +// 2 same +// 1 left higher +// 0 left lower +func compare(left, right Packet) int { + if left.value != -1 && right.value != -1 { + if left.value < right.value { + return 0 + } + if left.value > right.value { + return 1 + } + if left.value == right.value { + return 2 + } + } + if left.value == -1 && right.value == -1 { + for i := 0; i < len(left.values); i++ { + if !(i < len(right.values)) { + return 1 + } + result := compare(left.values[i], right.values[i]) + if result == 0 { + return 0 + } + + if result == 1 { + return 1 + } + } + if len(right.values) > len(left.values) { + return 0 + } + return 2 + } + if left.value != -1 { + result := compare(Packet{value: -1, values: []Packet{{value: left.value, values: []Packet{}}}}, right) + if result == 0 { + return 0 + } + + if result == 1 { + return 1 + } + + } + if right.value != -1 { + result := compare(left, Packet{value: -1, values: []Packet{{value: right.value, values: []Packet{}}}}) + + if result == 0 { + return 0 + } + + if result == 1 { + return 1 + } + } + return 2 +} + +func parse(input string) Packet { + packet := Packet{-1, []Packet{}} + for i := 0; i < len(input); { + switch input[i] { + case '[': + endOfCurrentArray := getEndofCurrentArray(input[1:], i) + currentArray := input[i+1 : endOfCurrentArray] + packet.values = append(packet.values, parse(currentArray)) + i = endOfCurrentArray + 1 + case ',': + i++ + default: + endOfCurrentValue := getEndofCurrentValue(input, i) + currentValue := helper.RemoveError(strconv.Atoi(input[i:endOfCurrentValue])) + packet.values = append(packet.values, Packet{value: currentValue, values: []Packet{}}) + i = endOfCurrentValue + 1 + } + } + + return packet +} + // 0 Right Order -//-1 Left Value to high +//-1 Left Value too high //-2 Right had no values left func comparePair(left string, right string) int { lefIndex := 0 @@ -92,7 +212,7 @@ func getNextElement(packet string, currentIndex int) (int, int) { for index < len(packet) { switch packet[index] { case ',': - index++ + return index, -4 break case '[': return index, -2 @@ -130,6 +250,17 @@ func getEndofCurrentArray(packet string, currentIndex int) int { } } +func getEndofCurrentValue(packet string, currentIndex int) int { + index := currentIndex + for index < len(packet) { + if packet[index] == ',' { + return index + } + index++ + } + return index +} + func readElement(packet string, currentIndex int) (element, newIndex int) { index := currentIndex newIndex = 0 diff --git a/day13/testinputPart2 b/day13/testinputPart2 new file mode 100644 index 0000000..d188a15 --- /dev/null +++ b/day13/testinputPart2 @@ -0,0 +1,18 @@ +[] +[[]] +[[[]]] +[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] \ No newline at end of file