day 16a complete

after taking up the iterative approach and sorting out all of the little bugs I was finally able to get the right value
This commit is contained in:
Luke Else 2021-12-17 19:11:20 +00:00
parent dc6d332cf8
commit b2f39abd93
2 changed files with 83 additions and 74 deletions

View File

@ -9,87 +9,95 @@ import (
)
func main() {
//content := returnContent("../input")
content := returnContent("../testInput")
content := returnContent("../input")
//content := returnContent("../testInput")
binary := shared.HexToBinary(content)
pointer := 0
answer := SeparatePackets(&binary, &pointer)
fmt.Println(answer)
}
func SeparatePackets(binary *string, pointer *int) (packetValue int) {
version := 0
typeID := 0
lengthTypeID := "0"
length := 11
values := []int{}
subPacketValues := []int{}
answer := 0
for pointer < len(binary)-6 {
//Get version from first 3 Bits
current := ""
current = binary[pointer : pointer+3]
current = (*binary)[*pointer : *pointer+3]
version += shared.BinaryToInteger(&current)
pointer += 3
*pointer += 3
//determine packet type ID from next 2 bits
current = ""
current = binary[pointer : pointer+3]
current = (*binary)[*pointer : *pointer+3]
typeID = shared.BinaryToInteger(&current)
pointer += 3
*pointer += 3
if typeID == 4 {
//literal value
value := ""
for {
//continue adding the 4 bit values while the packets continue
pointer++
value += binary[pointer : pointer+4]
pointer += 4
if binary[pointer-5] == '0' {
*pointer++
value += (*binary)[*pointer : *pointer+4]
*pointer += 4
if (*binary)[*pointer-5] == '0' {
break
}
}
answer = shared.BinaryToInteger(&value)
packetValue = shared.BinaryToInteger(&value)
return
} else {
//operator value
//find the lenth of the subpackets definition
lengthTypeID = string(binary[pointer])
lengthTypeID = string((*binary)[*pointer])
if lengthTypeID == "0" {
length = 15
} //default = 11
pointer++
*pointer++
//subpacket length value
temp := binary[pointer : pointer+length]
length = shared.BinaryToInteger(&temp)
fmt.Println(length, lengthTypeID)
pointer += length
temp := (*binary)[*pointer : *pointer+length]
subLength := shared.BinaryToInteger(&temp)
*pointer += length
//assert into values based on typeID (0 = total length, 1 = )
if lengthTypeID == "1" {
//add the series of 11 bit numbers
for i := 0; i < length; i++ {
packetString := binary[pointer : pointer+11]
values = append(values, shared.BinaryToInteger(&packetString))
pointer += 11
for i := 0; i < subLength; i++ {
//packetString := (*binary)[*pointer : *pointer+11]
subPacketValues = append(subPacketValues, SeparatePackets(binary, pointer))
}
fmt.Println(values)
packetValue = ComputePackets(typeID, subPacketValues)
} else {
fmt.Println("here")
//add the series of ... bit numbers
intialPointerVal := *pointer
for {
if *pointer-intialPointerVal < subLength {
subPacketValues = append(subPacketValues, SeparatePackets(binary, pointer))
} else {
break
}
}
packetValue = ComputePackets(typeID, subPacketValues)
}
}
}
fmt.Println(answer)
return
}
func ComputePackets(version int, typeID int, values []int) (answer int) {
func ComputePackets(typeID int, values []int) (answer int) {
answer = 0
switch typeID {
case 0:
//sum
@ -98,15 +106,16 @@ func ComputePackets(version int, typeID int, values []int) (answer int) {
}
case 1:
//product
answer = 1
for _, value := range values {
answer = answer * value
}
case 2:
//min
min := math.MaxInt
answer = math.MaxInt
for _, value := range values {
if value < min {
min = value
if value < answer {
answer = value
}
}
case 3:

View File

@ -1 +1 @@
EE00D40C823060
880086C3E88112