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:
Luke Else 2021-12-17 15:05:05 +00:00
parent 0db2c0a79e
commit dc6d332cf8
2 changed files with 117 additions and 66 deletions

View File

@ -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{}
//Fill initial list answer := 0
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 for pointer < len(binary)-6 {
iterations := 40 //Get version from first 3 Bits
for i := 1; i <= iterations; i++ { current := ""
current = list.Head current = binary[pointer : pointer+3]
version += shared.BinaryToInteger(&current)
pointer += 3
for current.Next != nil { //determine packet type ID from next 2 bits
value := pairs[current.Value+current.Next.Value] current = ""
list.InsertItem(current, current.Next, value) current = binary[pointer : pointer+3]
count[value]++ typeID = shared.BinaryToInteger(&current)
current = current.Next.Next pointer += 3
}
fmt.Println(i) 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
}
//determine min and max
min := math.MaxInt
max := 0
for _, value := range count {
if value > max {
max = value
} else if value < min {
min = value
} }
} }
fmt.Println(max - min) fmt.Println(answer)
} }
type LinkedList struct { func ComputePackets(version int, typeID int, values []int) (answer int) {
Head *Node answer = 0
} switch typeID {
case 0:
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) { //sum
first.Next = &Node{Value: value, Next: second} for _, value := range values {
} answer += value
}
func (l *LinkedList) PrintList() { case 1:
list := []string{} //product
current := l.Head for _, value := range values {
for current != nil { answer = answer * value
list = append(list, current.Value) }
current = current.Next case 2:
//min
min := math.MaxInt
for _, value := range values {
if value < min {
min = value
}
}
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
}
} }
fmt.Println(list)
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

View File

@ -1 +1 @@
620080001611562C8802118E34 EE00D40C823060