day 10b complete

This commit is contained in:
Luke Else 2021-12-10 23:00:22 +00:00
parent 0860573c1e
commit b3be061915
5 changed files with 204 additions and 0 deletions

3
day10/day 10b/go.mod Normal file
View File

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

113
day10/day 10b/main.go Normal file
View File

@ -0,0 +1,113 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
content := returnContent("../input")
//content := returnContent("testInput")
var bracketMap map[byte]byte = make(map[byte]byte)
var reverseBracketMap map[byte]byte = make(map[byte]byte)
var bracketCost map[byte]int = make(map[byte]int)
bracketMap[')'] = '('
bracketMap[']'] = '['
bracketMap['}'] = '{'
bracketMap['>'] = '<'
reverseBracketMap['('] = ')'
reverseBracketMap['['] = ']'
reverseBracketMap['{'] = '}'
reverseBracketMap['<'] = '>'
bracketCost[')'] = 1
bracketCost[']'] = 2
bracketCost['}'] = 3
bracketCost['>'] = 4
//reduce list to required incomplete set
stackList := []Stack{}
autoCompletes := []int{}
for _, row := range *content {
stack := Stack{}
keep := true
for _, char := range row {
brack, found := bracketMap[char]
if !found {
//If it is an opening bracket
stack.Push(char)
} else {
if brack == stack.Peek().char {
stack.Pop()
} else {
keep = false
// *content = *removeElement(content, i)
break
}
}
}
if keep {
stackList = append(stackList, stack)
}
}
emptyStackNode := StackNode{}
for _, stack := range stackList {
autocomplete := 0
for *(stack.Peek()) != emptyStackNode {
autocomplete *= 5
autocomplete += bracketCost[reverseBracketMap[stack.Pop()]]
}
autoCompletes = append(autoCompletes, autocomplete)
}
autoCompletes = mergeSort(autoCompletes, 0, len(autoCompletes)-1)
mid := autoCompletes[len(autoCompletes)/2]
fmt.Println(mid)
}
// func removeElement(slice *[][]byte, i int) (new *[][]byte) {
// new = &[][]byte{}
// for j, row := range *slice {
// if i != j {
// *new = append(*new, row)
// }
// }
// return
// }
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
}

39
day10/day 10b/sort.go Normal file
View File

@ -0,0 +1,39 @@
package main
func mergeSort(nums []int, start int, end int) []int {
if start == end {
return []int{nums[start]}
}
var mid int = ((end - start) / 2) + start
//Assign values back into Left and right
left := mergeSort(nums, start, mid)
right := mergeSort(nums, mid+1, end)
var combined []int
//Pointers for new array
leftPointer, rightPointer := 0, 0
for leftPointer <= len(left)-1 || rightPointer <= len(right)-1 {
if leftPointer == len(left) {
addValue(&combined, right[rightPointer], &rightPointer)
} else if rightPointer == len(right) {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
if left[leftPointer] <= right[rightPointer] {
addValue(&combined, left[leftPointer], &leftPointer)
} else {
addValue(&combined, right[rightPointer], &rightPointer)
}
}
}
return combined
}
func addValue(nums *[]int, value int, pointer *int) {
*nums = append(*nums, value)
*pointer++
}

39
day10/day 10b/stack.go Normal file
View File

@ -0,0 +1,39 @@
package main
type Stack struct {
head *StackNode
}
func (s *Stack) Push(item byte) {
if s.head == nil {
s.head = &StackNode{
char: item,
next: nil,
}
} else {
new := StackNode{
char: item,
next: s.head,
}
s.head = &new
}
}
func (s *Stack) Pop() byte {
node := s.Peek()
s.head = node.next
return node.char
}
func (s *Stack) Peek() (node *StackNode) {
node = s.head
if node == nil {
node = &StackNode{}
}
return
}
type StackNode struct {
char byte
next *StackNode
}

10
day10/day 10b/testInput Normal file
View File

@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]