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
|
||||
|
||||
import (
|
||||
"AdventOfCode2021/shared"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content := returnContent("../input")
|
||||
//content := returnContent("../testInput")
|
||||
fmt.Println(content)
|
||||
//content := returnContent("../input")
|
||||
content := returnContent("../testInput")
|
||||
|
||||
startingString := (*content)[0]
|
||||
pairs := make(map[string]string)
|
||||
count := make(map[string]int)
|
||||
binary := shared.HexToBinary(content)
|
||||
|
||||
//Create map of pair values
|
||||
for i := 2; i < len(*content); i++ {
|
||||
split := strings.Split((*content)[i], " -> ")
|
||||
pairs[split[0]] = split[1]
|
||||
pointer := 0
|
||||
version := 0
|
||||
typeID := 0
|
||||
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
|
||||
iterations := 40
|
||||
for i := 1; i <= iterations; i++ {
|
||||
current = list.Head
|
||||
fmt.Println(answer)
|
||||
|
||||
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
|
||||
max := 0
|
||||
for _, value := range count {
|
||||
if value > max {
|
||||
max = value
|
||||
} else if value < min {
|
||||
for _, value := range values {
|
||||
if value < min {
|
||||
min = value
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(max - min)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
type LinkedList struct {
|
||||
Head *Node
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Value string
|
||||
Next *Node
|
||||
}
|
||||
|
||||
func returnContent(path string) *[]string {
|
||||
func returnContent(path string) *string {
|
||||
//read file and return it as an array of integers
|
||||
|
||||
file, err := os.Open(path)
|
||||
var content []string
|
||||
var content string
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unlucky, the file didn't open")
|
||||
@ -100,7 +151,7 @@ func returnContent(path string) *[]string {
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
content = append(content, scanner.Text())
|
||||
content = scanner.Text()
|
||||
}
|
||||
|
||||
return &content
|
||||
|
@ -1 +1 @@
|
||||
620080001611562C8802118E34
|
||||
EE00D40C823060
|
Loading…
Reference in New Issue
Block a user