2021-12-16 21:11:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-17 15:05:05 +00:00
|
|
|
"AdventOfCode2021/shared"
|
2021-12-16 21:11:39 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-12-17 19:11:20 +00:00
|
|
|
content := returnContent("../input")
|
|
|
|
//content := returnContent("../testInput")
|
2021-12-17 15:05:05 +00:00
|
|
|
|
|
|
|
binary := shared.HexToBinary(content)
|
|
|
|
|
|
|
|
pointer := 0
|
2021-12-17 19:11:20 +00:00
|
|
|
|
|
|
|
answer := SeparatePackets(&binary, &pointer)
|
|
|
|
|
|
|
|
fmt.Println(answer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func SeparatePackets(binary *string, pointer *int) (packetValue int) {
|
2021-12-17 15:05:05 +00:00
|
|
|
version := 0
|
|
|
|
typeID := 0
|
|
|
|
lengthTypeID := "0"
|
|
|
|
length := 11
|
2021-12-17 19:11:20 +00:00
|
|
|
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
|
2021-12-17 15:05:05 +00:00
|
|
|
}
|
2021-12-17 19:11:20 +00:00
|
|
|
}
|
|
|
|
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)
|
2021-12-17 15:05:05 +00:00
|
|
|
} else {
|
2021-12-17 19:11:20 +00:00
|
|
|
//add the series of ... bit numbers
|
|
|
|
intialPointerVal := *pointer
|
|
|
|
for {
|
|
|
|
if *pointer-intialPointerVal < subLength {
|
|
|
|
subPacketValues = append(subPacketValues, SeparatePackets(binary, pointer))
|
|
|
|
} else {
|
|
|
|
break
|
2021-12-17 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-17 19:11:20 +00:00
|
|
|
packetValue = ComputePackets(typeID, subPacketValues)
|
2021-12-16 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:11:20 +00:00
|
|
|
}
|
|
|
|
return
|
2021-12-16 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 19:11:20 +00:00
|
|
|
func ComputePackets(typeID int, values []int) (answer int) {
|
2021-12-17 15:05:05 +00:00
|
|
|
answer = 0
|
2021-12-17 19:11:20 +00:00
|
|
|
|
2021-12-17 15:05:05 +00:00
|
|
|
switch typeID {
|
|
|
|
case 0:
|
|
|
|
//sum
|
|
|
|
for _, value := range values {
|
|
|
|
answer += value
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
//product
|
2021-12-17 19:11:20 +00:00
|
|
|
answer = 1
|
2021-12-17 15:05:05 +00:00
|
|
|
for _, value := range values {
|
|
|
|
answer = answer * value
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
//min
|
2021-12-17 19:11:20 +00:00
|
|
|
answer = math.MaxInt
|
2021-12-17 15:05:05 +00:00
|
|
|
for _, value := range values {
|
2021-12-17 19:11:20 +00:00
|
|
|
if value < answer {
|
|
|
|
answer = value
|
2021-12-17 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
//max
|
|
|
|
for _, value := range values {
|
|
|
|
if value > answer {
|
|
|
|
answer = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 5:
|
|
|
|
//greater than
|
|
|
|
if values[0] > values[1] {
|
|
|
|
answer = 1
|
|
|
|
}
|
|
|
|
case 6:
|
|
|
|
//less than
|
|
|
|
if values[0] < values[1] {
|
|
|
|
answer = 1
|
|
|
|
}
|
|
|
|
case 7:
|
|
|
|
//equal to
|
|
|
|
if values[0] == values[1] {
|
|
|
|
answer = 1
|
|
|
|
}
|
2021-12-16 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:05:05 +00:00
|
|
|
return
|
2021-12-16 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:05:05 +00:00
|
|
|
func returnContent(path string) *string {
|
2021-12-16 21:11:39 +00:00
|
|
|
//read file and return it as an array of integers
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
2021-12-17 15:05:05 +00:00
|
|
|
var content string
|
2021-12-16 21:11:39 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unlucky, the file didn't open")
|
|
|
|
return &content
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2021-12-17 15:05:05 +00:00
|
|
|
content = scanner.Text()
|
2021-12-16 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &content
|
|
|
|
}
|