day 16a complete
This commit is contained in:
parent
6111f86a04
commit
0db2c0a79e
@ -1,9 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AdventOfCode2021/shared"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
)
|
||||
|
||||
@ -13,7 +13,7 @@ func main() {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
//read file and return it as an array of integers
|
||||
|
||||
|
@ -1,95 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AdventOfCode2021/shared"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content := returnContent("../input")
|
||||
//content := returnContent("../testInput")
|
||||
fmt.Println(content)
|
||||
|
||||
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
|
||||
|
||||
//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
|
||||
}
|
||||
for pointer < len(binary)-6 {
|
||||
//Get version from first 3 Bits
|
||||
current := ""
|
||||
current = binary[pointer : pointer+3]
|
||||
version += shared.BinaryToInteger(¤t)
|
||||
pointer += 3
|
||||
|
||||
//Run iterations on list
|
||||
iterations := 10
|
||||
for i := 1; i <= iterations; i++ {
|
||||
current = list.Head
|
||||
//determine packet type ID from next 2 bits
|
||||
current = ""
|
||||
current = binary[pointer : pointer+3]
|
||||
typeID := shared.BinaryToInteger(¤t)
|
||||
pointer += 3
|
||||
|
||||
for current.Next != nil {
|
||||
value := pairs[current.Value+current.Next.Value]
|
||||
list.InsertItem(current, current.Next, value)
|
||||
count[value]++
|
||||
current = current.Next.Next
|
||||
}
|
||||
list.PrintList()
|
||||
}
|
||||
if typeID == 4 {
|
||||
//literal value
|
||||
for binary[pointer] == '1' {
|
||||
pointer += 5
|
||||
}
|
||||
pointer += 5
|
||||
|
||||
//determine min and max
|
||||
min := math.MaxInt
|
||||
max := 0
|
||||
for _, value := range count {
|
||||
if value > max {
|
||||
max = value
|
||||
} else if value < min {
|
||||
min = value
|
||||
} else {
|
||||
//operator value
|
||||
if binary[pointer] == '1' {
|
||||
pointer += 12
|
||||
} else {
|
||||
pointer += 16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(max - min)
|
||||
fmt.Println(version)
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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 +65,7 @@ func returnContent(path string) *[]string {
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
content = append(content, scanner.Text())
|
||||
content = scanner.Text()
|
||||
}
|
||||
|
||||
return &content
|
||||
|
103
day16/input
103
day16/input
@ -1,102 +1 @@
|
||||
SHHBNFBCKNHCNOSHHVFF
|
||||
|
||||
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
|
||||
820D4A801EE00720190CA005201682A00498014C04BBB01186C040A200EC66006900C44802BA280104021B30070A4016980044C800B84B5F13BFF007081800FE97FDF830401BF4A6E239A009CCE22E53DC9429C170013A8C01E87D102399803F1120B4632004261045183F303E4017DE002F3292CB04DE86E6E7E54100366A5490698023400ABCC59E262CFD31DDD1E8C0228D938872A472E471FC80082950220096E55EF0012882529182D180293139E3AC9A00A080391563B4121007223C4A8B3279B2AA80450DE4B72A9248864EAB1802940095CDE0FA4DAA5E76C4E30EBE18021401B88002170BA0A43000043E27462829318F83B00593225F10267FAEDD2E56B0323005E55EE6830C013B00464592458E52D1DF3F97720110258DAC0161007A084228B0200DC568FB14D40129F33968891005FBC00E7CAEDD25B12E692A7409003B392EA3497716ED2CFF39FC42B8E593CC015B00525754B7DFA67699296DD018802839E35956397449D66997F2013C3803760004262C4288B40008747E8E114672564E5002256F6CC3D7726006125A6593A671A48043DC00A4A6A5B9EAC1F352DCF560A9385BEED29A8311802B37BE635F54F004A5C1A5C1C40279FDD7B7BC4126ED8A4A368994B530833D7A439AA1E9009D4200C4178FF0880010E8431F62C880370F63E44B9D1E200ADAC01091029FC7CB26BD25710052384097004677679159C02D9C9465C7B92CFACD91227F7CD678D12C2A402C24BF37E9DE15A36E8026200F4668AF170401A8BD05A242009692BFC708A4BDCFCC8A4AC3931EAEBB3D314C35900477A0094F36CF354EE0CCC01B985A932D993D87E2017CE5AB6A84C96C265FA750BA4E6A52521C300467033401595D8BCC2818029C00AA4A4FBE6F8CB31CAE7D1CDDAE2E9006FD600AC9ED666A6293FAFF699FC168001FE9DC5BE3B2A6B3EED060
|
@ -1,18 +1 @@
|
||||
NNCB
|
||||
|
||||
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
|
||||
620080001611562C8802118E34
|
42
shared/Binary.go
Normal file
42
shared/Binary.go
Normal 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
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user