2021-12-10 23:00:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-16 21:11:39 +00:00
|
|
|
"AdventOfCode2021/shared"
|
2021-12-10 23:00:22 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
content := returnContent("../input")
|
2021-12-16 19:55:49 +00:00
|
|
|
//content := returnContent("../testInput")
|
2021-12-10 23:00:22 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
var bracketMap map[string]string = make(map[string]string)
|
|
|
|
var reverseBracketMap map[string]string = make(map[string]string)
|
|
|
|
var bracketCost map[string]int = make(map[string]int)
|
2021-12-10 23:00:22 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
bracketMap[")"] = "("
|
|
|
|
bracketMap["]"] = "["
|
|
|
|
bracketMap["}"] = "{"
|
|
|
|
bracketMap[">"] = "<"
|
2021-12-10 23:00:22 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
reverseBracketMap["("] = ")"
|
|
|
|
reverseBracketMap["["] = "]"
|
|
|
|
reverseBracketMap["{"] = "}"
|
|
|
|
reverseBracketMap["<"] = ">"
|
2021-12-10 23:00:22 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
bracketCost[")"] = 1
|
|
|
|
bracketCost["]"] = 2
|
|
|
|
bracketCost["}"] = 3
|
|
|
|
bracketCost[">"] = 4
|
2021-12-10 23:00:22 +00:00
|
|
|
|
|
|
|
//reduce list to required incomplete set
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
stackList := []shared.Stack{}
|
2021-12-10 23:00:22 +00:00
|
|
|
autoCompletes := []int{}
|
|
|
|
|
|
|
|
for _, row := range *content {
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
stack := shared.Stack{}
|
2021-12-10 23:00:22 +00:00
|
|
|
keep := true
|
|
|
|
|
|
|
|
for _, char := range row {
|
2021-12-16 21:11:39 +00:00
|
|
|
brack, found := bracketMap[string(char)]
|
2021-12-10 23:00:22 +00:00
|
|
|
|
|
|
|
if !found {
|
|
|
|
//If it is an opening bracket
|
2021-12-16 21:11:39 +00:00
|
|
|
stack.Push(string(char))
|
2021-12-10 23:00:22 +00:00
|
|
|
} else {
|
2021-12-16 21:11:39 +00:00
|
|
|
if brack == stack.Peek().Value {
|
2021-12-10 23:00:22 +00:00
|
|
|
stack.Pop()
|
|
|
|
} else {
|
|
|
|
keep = false
|
|
|
|
// *content = *removeElement(content, i)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if keep {
|
|
|
|
stackList = append(stackList, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
emptyStackNode := shared.Node{}
|
2021-12-10 23:00:22 +00:00
|
|
|
|
|
|
|
for _, stack := range stackList {
|
|
|
|
autocomplete := 0
|
|
|
|
for *(stack.Peek()) != emptyStackNode {
|
|
|
|
autocomplete *= 5
|
|
|
|
autocomplete += bracketCost[reverseBracketMap[stack.Pop()]]
|
|
|
|
}
|
|
|
|
autoCompletes = append(autoCompletes, autocomplete)
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
autoCompletes = shared.MergeSort(autoCompletes, 0, len(autoCompletes)-1)
|
2021-12-10 23:00:22 +00:00
|
|
|
|
|
|
|
mid := autoCompletes[len(autoCompletes)/2]
|
|
|
|
|
|
|
|
fmt.Println(mid)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnContent(path string) *[][]byte {
|
|
|
|
//read file and return it as an array of integers
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
|
|
|
var content [][]byte
|
|
|
|
|
|
|
|
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, []byte(scanner.Text()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &content
|
|
|
|
}
|