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:
parent
dc6d332cf8
commit
b2f39abd93
@ -9,87 +9,95 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//content := returnContent("../input")
|
content := returnContent("../input")
|
||||||
content := returnContent("../testInput")
|
//content := returnContent("../testInput")
|
||||||
|
|
||||||
binary := shared.HexToBinary(content)
|
binary := shared.HexToBinary(content)
|
||||||
|
|
||||||
pointer := 0
|
pointer := 0
|
||||||
version := 0
|
|
||||||
typeID := 0
|
|
||||||
lengthTypeID := "0"
|
|
||||||
length := 11
|
|
||||||
values := []int{}
|
|
||||||
|
|
||||||
answer := 0
|
answer := SeparatePackets(&binary, &pointer)
|
||||||
|
|
||||||
for pointer < len(binary)-6 {
|
|
||||||
//Get version from first 3 Bits
|
|
||||||
current := ""
|
|
||||||
current = binary[pointer : pointer+3]
|
|
||||||
version += shared.BinaryToInteger(¤t)
|
|
||||||
pointer += 3
|
|
||||||
|
|
||||||
//determine packet type ID from next 2 bits
|
|
||||||
current = ""
|
|
||||||
current = binary[pointer : pointer+3]
|
|
||||||
typeID = shared.BinaryToInteger(¤t)
|
|
||||||
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' {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
answer = shared.BinaryToInteger(&value)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
//operator value
|
|
||||||
//find the lenth of the subpackets definition
|
|
||||||
lengthTypeID = string(binary[pointer])
|
|
||||||
|
|
||||||
if lengthTypeID == "0" {
|
|
||||||
length = 15
|
|
||||||
} //default = 11
|
|
||||||
pointer++
|
|
||||||
|
|
||||||
//subpacket length value
|
|
||||||
temp := binary[pointer : pointer+length]
|
|
||||||
length = shared.BinaryToInteger(&temp)
|
|
||||||
fmt.Println(length, lengthTypeID)
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
fmt.Println(values)
|
|
||||||
} else {
|
|
||||||
fmt.Println("here")
|
|
||||||
//add the series of ... bit numbers
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(answer)
|
fmt.Println(answer)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ComputePackets(version int, typeID int, values []int) (answer int) {
|
func SeparatePackets(binary *string, pointer *int) (packetValue int) {
|
||||||
|
version := 0
|
||||||
|
typeID := 0
|
||||||
|
lengthTypeID := "0"
|
||||||
|
length := 11
|
||||||
|
subPacketValues := []int{}
|
||||||
|
|
||||||
|
//Get version from first 3 Bits
|
||||||
|
current := ""
|
||||||
|
current = (*binary)[*pointer : *pointer+3]
|
||||||
|
version += shared.BinaryToInteger(¤t)
|
||||||
|
*pointer += 3
|
||||||
|
|
||||||
|
//determine packet type ID from next 2 bits
|
||||||
|
current = ""
|
||||||
|
current = (*binary)[*pointer : *pointer+3]
|
||||||
|
typeID = shared.BinaryToInteger(¤t)
|
||||||
|
*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' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
packetValue = shared.BinaryToInteger(&value)
|
||||||
|
return
|
||||||
|
|
||||||
|
} else {
|
||||||
|
//operator value
|
||||||
|
//find the lenth of the subpackets definition
|
||||||
|
lengthTypeID = string((*binary)[*pointer])
|
||||||
|
if lengthTypeID == "0" {
|
||||||
|
length = 15
|
||||||
|
} //default = 11
|
||||||
|
*pointer++
|
||||||
|
|
||||||
|
//subpacket length value
|
||||||
|
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 < subLength; i++ {
|
||||||
|
//packetString := (*binary)[*pointer : *pointer+11]
|
||||||
|
subPacketValues = append(subPacketValues, SeparatePackets(binary, pointer))
|
||||||
|
}
|
||||||
|
packetValue = ComputePackets(typeID, subPacketValues)
|
||||||
|
} else {
|
||||||
|
//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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ComputePackets(typeID int, values []int) (answer int) {
|
||||||
answer = 0
|
answer = 0
|
||||||
|
|
||||||
switch typeID {
|
switch typeID {
|
||||||
case 0:
|
case 0:
|
||||||
//sum
|
//sum
|
||||||
@ -98,15 +106,16 @@ func ComputePackets(version int, typeID int, values []int) (answer int) {
|
|||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
//product
|
//product
|
||||||
|
answer = 1
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
answer = answer * value
|
answer = answer * value
|
||||||
}
|
}
|
||||||
case 2:
|
case 2:
|
||||||
//min
|
//min
|
||||||
min := math.MaxInt
|
answer = math.MaxInt
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
if value < min {
|
if value < answer {
|
||||||
min = value
|
answer = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case 3:
|
case 3:
|
||||||
|
@ -1 +1 @@
|
|||||||
EE00D40C823060
|
880086C3E88112
|
Loading…
Reference in New Issue
Block a user