Day 13
This commit is contained in:
parent
dde902d2b4
commit
8e5dfcccd8
155
day13/day13.go
155
day13/day13.go
|
@ -3,31 +3,151 @@ package main
|
||||||
import (
|
import (
|
||||||
"AOC2022/helper"
|
"AOC2022/helper"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Packet struct {
|
||||||
|
value int
|
||||||
|
values []Packet
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//args := os.Args[1:]
|
//args := os.Args[1:]
|
||||||
lines := helper.ReadTextFile("day13/input")
|
lines := helper.ReadTextFile("day13/input")
|
||||||
result := 0
|
//part1(lines)
|
||||||
index := 1
|
decodePacket2 := parse("[[2]]")
|
||||||
|
decodePacket6 := parse("[[6]]")
|
||||||
|
packets := []Packet{}
|
||||||
for i := 0; i < len(lines); i += 3 {
|
for i := 0; i < len(lines); i += 3 {
|
||||||
fmt.Println(lines[i])
|
packets = append(packets, parse(lines[i]))
|
||||||
fmt.Println(lines[i+1])
|
packets = append(packets, parse(lines[i+1]))
|
||||||
fmt.Println(comparePair(lines[i], lines[i+1]))
|
}
|
||||||
fmt.Println()
|
packets = append(packets, decodePacket2)
|
||||||
if comparePair(lines[i], lines[i+1]) == 0 {
|
packets = append(packets, decodePacket6)
|
||||||
result += index
|
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)
|
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
|
// 0 Right Order
|
||||||
//-1 Left Value to high
|
//-1 Left Value too high
|
||||||
//-2 Right had no values left
|
//-2 Right had no values left
|
||||||
func comparePair(left string, right string) int {
|
func comparePair(left string, right string) int {
|
||||||
lefIndex := 0
|
lefIndex := 0
|
||||||
|
@ -92,7 +212,7 @@ func getNextElement(packet string, currentIndex int) (int, int) {
|
||||||
for index < len(packet) {
|
for index < len(packet) {
|
||||||
switch packet[index] {
|
switch packet[index] {
|
||||||
case ',':
|
case ',':
|
||||||
index++
|
return index, -4
|
||||||
break
|
break
|
||||||
case '[':
|
case '[':
|
||||||
return index, -2
|
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) {
|
func readElement(packet string, currentIndex int) (element, newIndex int) {
|
||||||
index := currentIndex
|
index := currentIndex
|
||||||
newIndex = 0
|
newIndex = 0
|
||||||
|
|
|
@ -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]
|
Loading…
Reference in New Issue
Block a user