AdventOfCode2021/day16/16a/main.go

73 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-12-16 21:11:39 +00:00
package main
import (
2021-12-17 13:23:44 +00:00
"AdventOfCode2021/shared"
2021-12-16 21:11:39 +00:00
"bufio"
"fmt"
"os"
)
func main() {
content := returnContent("../input")
//content := returnContent("../testInput")
2021-12-17 13:23:44 +00:00
binary := shared.HexToBinary(content)
pointer := 0
version := 0
for pointer < len(binary)-6 {
//Get version from first 3 Bits
current := ""
current = binary[pointer : pointer+3]
version += shared.BinaryToInteger(&current)
pointer += 3
//determine packet type ID from next 2 bits
current = ""
current = binary[pointer : pointer+3]
typeID := shared.BinaryToInteger(&current)
pointer += 3
if typeID == 4 {
//literal value
for binary[pointer] == '1' {
pointer += 5
}
pointer += 5
} else {
//operator value
if binary[pointer] == '1' {
pointer += 12
} else {
pointer += 16
}
2021-12-16 21:11:39 +00:00
}
}
2021-12-17 13:23:44 +00:00
fmt.Println(version)
2021-12-16 21:11:39 +00:00
}
2021-12-17 13:23:44 +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 13:23:44 +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 13:23:44 +00:00
content = scanner.Text()
2021-12-16 21:11:39 +00:00
}
return &content
}