2021-12-10 22:21:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-16 21:11:39 +00:00
|
|
|
"AdventOfCode2021/shared"
|
2021-12-10 22:21:30 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
content := returnContent("../input")
|
2021-12-16 19:55:49 +00:00
|
|
|
//content := returnContent("../testInput")
|
2021-12-10 22:21:30 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
var bracketMap map[string]string = make(map[string]string)
|
|
|
|
var bracketCost map[string]int = make(map[string]int)
|
2021-12-10 22:21:30 +00:00
|
|
|
|
|
|
|
answer := 0
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
bracketMap[")"] = "("
|
|
|
|
bracketMap["]"] = "["
|
|
|
|
bracketMap["}"] = "{"
|
|
|
|
bracketMap[">"] = "<"
|
2021-12-10 22:21:30 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
bracketCost[")"] = 3
|
|
|
|
bracketCost["]"] = 57
|
|
|
|
bracketCost["}"] = 1197
|
|
|
|
bracketCost[">"] = 25137
|
2021-12-10 22:21:30 +00:00
|
|
|
|
|
|
|
for _, row := range *content {
|
2021-12-16 21:11:39 +00:00
|
|
|
var stack shared.Stack = shared.Stack{}
|
2021-12-10 22:21:30 +00:00
|
|
|
for _, char := range row {
|
2021-12-16 21:11:39 +00:00
|
|
|
brack, found := bracketMap[string(char)]
|
2021-12-10 22:21:30 +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 22:21:30 +00:00
|
|
|
} else {
|
2021-12-16 21:11:39 +00:00
|
|
|
if brack == stack.Peek().Value {
|
2021-12-10 22:21:30 +00:00
|
|
|
stack.Pop()
|
|
|
|
} else {
|
2021-12-16 21:11:39 +00:00
|
|
|
answer += bracketCost[string(char)]
|
2021-12-10 22:21:30 +00:00
|
|
|
fmt.Println("Illegal,", char, "found", answer)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(answer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|