day 14 complete

This commit is contained in:
Luke Else 2021-12-15 19:44:02 +00:00
parent f25f5f56b7
commit 7d8334b251
4 changed files with 230 additions and 0 deletions

3
day14/day 14a/go.mod Normal file
View File

@ -0,0 +1,3 @@
module PWD
go 1.17

107
day14/day 14a/main.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
"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)
//Create map of pair values
for i := 2; i < len(*content); i++ {
split := strings.Split((*content)[i], " -> ")
pairs[split[0]] = split[1]
}
//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 := 10
for i := 1; i <= iterations; i++ {
current = list.Head
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()
}
//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)
}
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 {
//read file and return it as an array of integers
file, err := os.Open(path)
var content []string
if err != nil {
fmt.Println("Unlucky, the file didn't open")
return &content
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
content = append(content, scanner.Text())
}
return &content
}

18
day14/day 14a/testInput Normal file
View File

@ -0,0 +1,18 @@
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

102
day14/input Normal file
View File

@ -0,0 +1,102 @@
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