day 16b incomplete
This idea relies on the fact that the packets are sequenstial as opposed to being recursive. To make it work I need to make the whole packet processing process a function and pass in the subpackets in
This commit is contained in:
parent
0db2c0a79e
commit
dc6d332cf8
@ -1,95 +1,146 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"AdventOfCode2021/shared"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
content := returnContent("../input")
|
//content := returnContent("../input")
|
||||||
//content := returnContent("../testInput")
|
content := returnContent("../testInput")
|
||||||
fmt.Println(content)
|
|
||||||
|
|
||||||
startingString := (*content)[0]
|
binary := shared.HexToBinary(content)
|
||||||
pairs := make(map[string]string)
|
|
||||||
count := make(map[string]int)
|
|
||||||
|
|
||||||
//Create map of pair values
|
pointer := 0
|
||||||
for i := 2; i < len(*content); i++ {
|
version := 0
|
||||||
split := strings.Split((*content)[i], " -> ")
|
typeID := 0
|
||||||
pairs[split[0]] = split[1]
|
lengthTypeID := "0"
|
||||||
|
length := 11
|
||||||
|
values := []int{}
|
||||||
|
|
||||||
|
answer := 0
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
//Fill initial list
|
}
|
||||||
list := LinkedList{Head: &Node{Value: string(startingString[0]), Next: nil}}
|
|
||||||
current := list.Head
|
|
||||||
for i := 1; i < len(startingString); i++ {
|
|
||||||
node := &Node{Value: string(startingString[i]), Next: nil}
|
|
||||||
count[string(startingString[i])]++
|
|
||||||
current.Next = node
|
|
||||||
current = node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Run iterations on list
|
fmt.Println(answer)
|
||||||
iterations := 40
|
|
||||||
for i := 1; i <= iterations; i++ {
|
|
||||||
current = list.Head
|
|
||||||
|
|
||||||
for current.Next != nil {
|
}
|
||||||
value := pairs[current.Value+current.Next.Value]
|
|
||||||
list.InsertItem(current, current.Next, value)
|
|
||||||
count[value]++
|
|
||||||
current = current.Next.Next
|
|
||||||
}
|
|
||||||
fmt.Println(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
//determine min and max
|
func ComputePackets(version int, typeID int, values []int) (answer int) {
|
||||||
|
answer = 0
|
||||||
|
switch typeID {
|
||||||
|
case 0:
|
||||||
|
//sum
|
||||||
|
for _, value := range values {
|
||||||
|
answer += value
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
//product
|
||||||
|
for _, value := range values {
|
||||||
|
answer = answer * value
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
//min
|
||||||
min := math.MaxInt
|
min := math.MaxInt
|
||||||
max := 0
|
for _, value := range values {
|
||||||
for _, value := range count {
|
if value < min {
|
||||||
if value > max {
|
|
||||||
max = value
|
|
||||||
} else if value < min {
|
|
||||||
min = value
|
min = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case 3:
|
||||||
fmt.Println(max - min)
|
//max
|
||||||
|
for _, value := range values {
|
||||||
}
|
if value > answer {
|
||||||
|
answer = value
|
||||||
type LinkedList struct {
|
|
||||||
Head *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) {
|
|
||||||
first.Next = &Node{Value: value, Next: second}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *LinkedList) PrintList() {
|
|
||||||
list := []string{}
|
|
||||||
current := l.Head
|
|
||||||
for current != nil {
|
|
||||||
list = append(list, current.Value)
|
|
||||||
current = current.Next
|
|
||||||
}
|
}
|
||||||
fmt.Println(list)
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Node struct {
|
func returnContent(path string) *string {
|
||||||
Value string
|
|
||||||
Next *Node
|
|
||||||
}
|
|
||||||
|
|
||||||
func returnContent(path string) *[]string {
|
|
||||||
//read file and return it as an array of integers
|
//read file and return it as an array of integers
|
||||||
|
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
var content []string
|
var content string
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Unlucky, the file didn't open")
|
fmt.Println("Unlucky, the file didn't open")
|
||||||
@ -100,7 +151,7 @@ func returnContent(path string) *[]string {
|
|||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
content = append(content, scanner.Text())
|
content = scanner.Text()
|
||||||
}
|
}
|
||||||
|
|
||||||
return &content
|
return &content
|
||||||
|
@ -1 +1 @@
|
|||||||
620080001611562C8802118E34
|
EE00D40C823060
|
Loading…
Reference in New Issue
Block a user