day 10a complete

first time using a stack, can recommend
This commit is contained in:
Luke Else 2021-12-10 22:21:30 +00:00
parent 10028f5d77
commit 0860573c1e
5 changed files with 225 additions and 0 deletions

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

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

71
day10/day 10a/main.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
content := returnContent("../input")
//content := returnContent("testInput")
var bracketMap map[byte]byte = make(map[byte]byte)
var bracketCost map[byte]int = make(map[byte]int)
answer := 0
bracketMap[')'] = '('
bracketMap[']'] = '['
bracketMap['}'] = '{'
bracketMap['>'] = '<'
bracketCost[')'] = 3
bracketCost[']'] = 57
bracketCost['}'] = 1197
bracketCost['>'] = 25137
for _, row := range *content {
var stack Stack = Stack{}
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 {
answer += bracketCost[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
}

39
day10/day 10a/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 10a/testInput Normal file
View File

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

102
day10/input Normal file
View File

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