AdventOfCode2021/day10/10a/main.go

73 lines
1.3 KiB
Go
Raw Normal View History

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