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() { 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
answer := SeparatePackets(&binary, &pointer)
fmt.Println(answer)
}
func SeparatePackets(binary *string, pointer *int) (packetValue int) {
version := 0 version := 0
typeID := 0 typeID := 0
lengthTypeID := "0" lengthTypeID := "0"
length := 11 length := 11
values := []int{} subPacketValues := []int{}
answer := 0
for pointer < len(binary)-6 {
//Get version from first 3 Bits //Get version from first 3 Bits
current := "" current := ""
current = binary[pointer : pointer+3] current = (*binary)[*pointer : *pointer+3]
version += shared.BinaryToInteger(&current) version += shared.BinaryToInteger(&current)
pointer += 3 *pointer += 3
//determine packet type ID from next 2 bits //determine packet type ID from next 2 bits
current = "" current = ""
current = binary[pointer : pointer+3] current = (*binary)[*pointer : *pointer+3]
typeID = shared.BinaryToInteger(&current) typeID = shared.BinaryToInteger(&current)
pointer += 3 *pointer += 3
if typeID == 4 { if typeID == 4 {
//literal value //literal value
value := "" value := ""
for { for {
//continue adding the 4 bit values while the packets continue //continue adding the 4 bit values while the packets continue
pointer++ *pointer++
value += binary[pointer : pointer+4] value += (*binary)[*pointer : *pointer+4]
pointer += 4 *pointer += 4
if binary[pointer-5] == '0' { if (*binary)[*pointer-5] == '0' {
break break
} }
} }
answer = shared.BinaryToInteger(&value) packetValue = shared.BinaryToInteger(&value)
return
} else { } else {
//operator value //operator value
//find the lenth of the subpackets definition //find the lenth of the subpackets definition
lengthTypeID = string(binary[pointer]) lengthTypeID = string((*binary)[*pointer])
if lengthTypeID == "0" { if lengthTypeID == "0" {
length = 15 length = 15
} //default = 11 } //default = 11
pointer++ *pointer++
//subpacket length value //subpacket length value
temp := binary[pointer : pointer+length] temp := (*binary)[*pointer : *pointer+length]
length = shared.BinaryToInteger(&temp) subLength := shared.BinaryToInteger(&temp)
fmt.Println(length, lengthTypeID) *pointer += length
pointer += length
//assert into values based on typeID (0 = total length, 1 = ) //assert into values based on typeID (0 = total length, 1 = )
if lengthTypeID == "1" { if lengthTypeID == "1" {
//add the series of 11 bit numbers //add the series of 11 bit numbers
for i := 0; i < length; i++ { for i := 0; i < subLength; i++ {
packetString := binary[pointer : pointer+11] //packetString := (*binary)[*pointer : *pointer+11]
values = append(values, shared.BinaryToInteger(&packetString)) subPacketValues = append(subPacketValues, SeparatePackets(binary, pointer))
pointer += 11
} }
fmt.Println(values) packetValue = ComputePackets(typeID, subPacketValues)
} else { } else {
fmt.Println("here")
//add the series of ... bit numbers //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
} }
fmt.Println(answer) func ComputePackets(typeID int, values []int) (answer int) {
}
func ComputePackets(version int, 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:

View File

@ -1 +1 @@
EE00D40C823060 880086C3E88112