day 16a complete

This commit is contained in:
Luke Else 2021-12-17 13:23:44 +00:00
parent 6111f86a04
commit 0db2c0a79e
5 changed files with 78 additions and 200 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"math"
"os" "os"
) )
@ -13,7 +13,7 @@ func main() {
gamma, epsilon := findGammaAndEpsilon(content) gamma, epsilon := findGammaAndEpsilon(content)
fmt.Println(binaryToInteger(gamma) * binaryToInteger(epsilon)) fmt.Println(shared.BinaryToInteger(&gamma) * shared.BinaryToInteger(&epsilon))
} }
@ -40,17 +40,6 @@ func findGammaAndEpsilon(content *[]string) (gamma string, epsilon string) {
return return
} }
func binaryToInteger(input string) (value int) {
n := 0
for i := len(input) - 1; i >= 0; i-- {
if input[i] == '1' {
value += (int(math.Pow(float64(2), float64(n))))
}
n++
}
return
}
func returnContent(path string) *[]string { 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

View File

@ -1,95 +1,60 @@
package main package main
import ( import (
"AdventOfCode2021/shared"
"bufio" "bufio"
"fmt" "fmt"
"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], " -> ")
pairs[split[0]] = split[1]
}
//Fill initial list for pointer < len(binary)-6 {
list := LinkedList{Head: &Node{Value: string(startingString[0]), Next: nil}} //Get version from first 3 Bits
current := list.Head current := ""
for i := 1; i < len(startingString); i++ { current = binary[pointer : pointer+3]
node := &Node{Value: string(startingString[i]), Next: nil} version += shared.BinaryToInteger(&current)
count[string(startingString[i])]++ pointer += 3
current.Next = node
current = node
}
//Run iterations on list //determine packet type ID from next 2 bits
iterations := 10 current = ""
for i := 1; i <= iterations; i++ { current = binary[pointer : pointer+3]
current = list.Head typeID := shared.BinaryToInteger(&current)
pointer += 3
for current.Next != nil { if typeID == 4 {
value := pairs[current.Value+current.Next.Value] //literal value
list.InsertItem(current, current.Next, value) for binary[pointer] == '1' {
count[value]++ pointer += 5
current = current.Next.Next }
} pointer += 5
list.PrintList()
}
//determine min and max } else {
min := math.MaxInt //operator value
max := 0 if binary[pointer] == '1' {
for _, value := range count { pointer += 12
if value > max { } else {
max = value pointer += 16
} else if value < min { }
min = value
} }
} }
fmt.Println(max - min) fmt.Println(version)
} }
type LinkedList struct { func returnContent(path string) *string {
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)
}
type Node struct {
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 +65,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,102 +1 @@
SHHBNFBCKNHCNOSHHVFF 820D4A801EE00720190CA005201682A00498014C04BBB01186C040A200EC66006900C44802BA280104021B30070A4016980044C800B84B5F13BFF007081800FE97FDF830401BF4A6E239A009CCE22E53DC9429C170013A8C01E87D102399803F1120B4632004261045183F303E4017DE002F3292CB04DE86E6E7E54100366A5490698023400ABCC59E262CFD31DDD1E8C0228D938872A472E471FC80082950220096E55EF0012882529182D180293139E3AC9A00A080391563B4121007223C4A8B3279B2AA80450DE4B72A9248864EAB1802940095CDE0FA4DAA5E76C4E30EBE18021401B88002170BA0A43000043E27462829318F83B00593225F10267FAEDD2E56B0323005E55EE6830C013B00464592458E52D1DF3F97720110258DAC0161007A084228B0200DC568FB14D40129F33968891005FBC00E7CAEDD25B12E692A7409003B392EA3497716ED2CFF39FC42B8E593CC015B00525754B7DFA67699296DD018802839E35956397449D66997F2013C3803760004262C4288B40008747E8E114672564E5002256F6CC3D7726006125A6593A671A48043DC00A4A6A5B9EAC1F352DCF560A9385BEED29A8311802B37BE635F54F004A5C1A5C1C40279FDD7B7BC4126ED8A4A368994B530833D7A439AA1E9009D4200C4178FF0880010E8431F62C880370F63E44B9D1E200ADAC01091029FC7CB26BD25710052384097004677679159C02D9C9465C7B92CFACD91227F7CD678D12C2A402C24BF37E9DE15A36E8026200F4668AF170401A8BD05A242009692BFC708A4BDCFCC8A4AC3931EAEBB3D314C35900477A0094F36CF354EE0CCC01B985A932D993D87E2017CE5AB6A84C96C265FA750BA4E6A52521C300467033401595D8BCC2818029C00AA4A4FBE6F8CB31CAE7D1CDDAE2E9006FD600AC9ED666A6293FAFF699FC168001FE9DC5BE3B2A6B3EED060
CK -> N
VP -> B
CF -> S
FO -> V
VC -> S
BV -> V
NP -> P
SN -> C
KN -> V
NF -> P
SB -> C
PC -> B
OB -> V
NS -> O
FH -> S
NK -> S
HO -> V
NV -> O
FV -> O
FB -> S
PS -> S
FN -> K
HS -> O
CB -> K
HV -> P
NH -> C
BO -> B
FF -> N
PO -> F
BB -> N
PN -> C
BP -> C
HN -> K
CO -> P
BF -> H
BC -> S
CV -> B
VV -> F
FS -> B
BN -> P
VK -> S
PV -> V
PP -> B
PH -> N
SS -> O
SK -> S
NC -> P
ON -> F
NB -> N
CC -> N
SF -> H
PF -> H
OV -> O
KH -> C
CP -> V
PK -> O
KC -> K
KK -> C
KF -> B
HP -> C
FK -> H
BH -> K
VN -> H
OO -> S
SC -> K
SP -> B
KO -> V
KV -> F
HK -> N
FP -> N
NN -> B
VS -> O
HC -> K
BK -> N
KS -> K
VB -> O
OH -> F
KB -> F
KP -> H
HB -> N
NO -> N
OF -> O
BS -> H
VO -> H
SH -> O
SV -> K
HF -> C
CS -> F
FC -> N
VH -> H
OP -> K
OK -> H
PB -> K
HH -> S
OC -> V
VF -> B
CH -> K
CN -> C
SO -> P
OS -> O

View File

@ -1,18 +1 @@
NNCB 620080001611562C8802118E34
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

42
shared/Binary.go Normal file
View File

@ -0,0 +1,42 @@
package shared
import "math"
func BinaryToInteger(binaryString *string) (value int) {
n := 0
for i := len(*binaryString) - 1; i >= 0; i-- {
if (*binaryString)[i] == '1' {
value += (int(math.Pow(float64(2), float64(n))))
}
n++
}
return
}
func HexToBinary(hexString *string) (binary string) {
hexMap := make(map[string]string)
hexMap["0"] = "0000"
hexMap["1"] = "0001"
hexMap["2"] = "0010"
hexMap["3"] = "0011"
hexMap["4"] = "0100"
hexMap["5"] = "0101"
hexMap["6"] = "0110"
hexMap["7"] = "0111"
hexMap["8"] = "1000"
hexMap["9"] = "1001"
hexMap["A"] = "1010"
hexMap["B"] = "1011"
hexMap["C"] = "1100"
hexMap["D"] = "1101"
hexMap["E"] = "1110"
hexMap["F"] = "1111"
for _, hex := range *hexString {
binary = binary + hexMap[string(hex)]
}
return
}