diff options
author | Julien Dessaux | 2021-12-10 21:41:37 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-10 21:41:37 +0100 |
commit | 5d7ee2e5028cb283735b6788fa046843ed1f0821 (patch) | |
tree | 72c1138f59c471199991675ea1b1db8387ff899e /2021/10 | |
parent | Added solutions for 9th day : minimums and basins in height map (diff) | |
download | advent-of-code-5d7ee2e5028cb283735b6788fa046843ed1f0821.tar.gz advent-of-code-5d7ee2e5028cb283735b6788fa046843ed1f0821.tar.bz2 advent-of-code-5d7ee2e5028cb283735b6788fa046843ed1f0821.zip |
Added solutions for 10th day : syntax scoring
Diffstat (limited to '2021/10')
-rw-r--r-- | 2021/10/example | 10 | ||||
-rw-r--r-- | 2021/10/first.go | 66 | ||||
-rw-r--r-- | 2021/10/input | 90 | ||||
-rw-r--r-- | 2021/10/second.go | 86 |
4 files changed, 252 insertions, 0 deletions
diff --git a/2021/10/example b/2021/10/example new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/2021/10/example @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/2021/10/first.go b/2021/10/first.go new file mode 100644 index 0000000..0c96fe8 --- /dev/null +++ b/2021/10/first.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +type stack []byte + +func (s *stack) push(b byte) { + *s = append(*s, b) +} + +func (s *stack) pop() *byte { + l := len(*s) + if l == 0 { + return nil + } else { + elt := (*s)[l-1] + *s = (*s)[:l-1] + return &elt + } +} + +func main() { + score := 0 + s := make(stack, 0) + + scanner := bufio.NewScanner(os.Stdin) +out: + for scanner.Scan() { + line := scanner.Text() + for i := 0; i < len(line); i++ { + c := line[i] + if c == '(' || c == '[' || c == '{' || c == '<' { + s.push(c) + continue + } + b := s.pop() + switch c { + case ')': + if *b != '(' { + score += 3 + continue out + } + case ']': + if *b != '[' { + score += 57 + continue out + } + case '}': + if *b != '{' { + score += 1197 + continue out + } + case '>': + if *b != '<' { + score += 25137 + continue out + } + } + } + } + fmt.Println(score) +} diff --git a/2021/10/input b/2021/10/input new file mode 100644 index 0000000..5bdcea7 --- /dev/null +++ b/2021/10/input @@ -0,0 +1,90 @@ +{<{<[{<[<([[(<<>[]>[<>()]){(()<>){<>{}}}]<{<{}<>){()<>}}[[()[]]{(){}}]>]{<([[]][{}<>]){<{}<>>{[]<>}}><[( +[<{(([{<[(<{[{()()}{()[]}]{[<>[]]}}>)[[{[<[][]>(()[])]<({}<>)<()<>>>}([({}{}){[]<>}])]<{{([][])[() +([<[{[{[{(<{<[[]][[][]]>[(<>[])<[]()>]]<({()<>}([]<>))>>)((([[{}{}]](<(){}><[]{}>))<<{{}[]}[<>()]><[[][]](()( +(<<<{{{[[<(((<[]()><{}{}>)([<>{}](<>{}}))([(()())[[]<>]]<{[]()}(<>[])>))[[[<{}[]>({}{})]]<{[<>()]((){})}<<[][ +{{[<{([[[([(<(<><>)>{([][]){()[]}})<{{{}()}<()<>>}>])]]<({<{(<()<>>([][]))<<(){}>]}{{<()() +<[{({[(<[<{{<(<>[])<[][]>]}}((((()<>)<<>[]>)[[<>{}]<{}>]){{(()[])}})>(<<(([]{})<<>[]>)[([][]) +((<[[(([{[[{((()())[{}{}])([(){}]<{}[]>)][[{{}{}}[[][]]][(()())(<>[])]]]{<[[{}[]]]<<()<>>[ +([(<<(<<[[<{{([]())[()<>]}<{<><>}[<>[]]>}{<<[][]>[[][]]><[[]<>}({}<>)>}>[[<([]{})>{({}{})[()[]]}][<{[] +<<[[<{[<<[<<[{<>[]}({}[])]<[[][]]<{}()>>>((<{}[]>{{}{}}>(<<>{}>([]{})))>(<(<[]()>(<>{}))<((){ +<{<[[<([[[[[<[{}{}](<><>)><{()<>}[{}()]>]{<<()()>{{}{}}>[[()<>][{}()]]}]{{({[]()}[[]<>])(<<>{}> +([{[[<(({<((({()()}({}{}))<<{}()><[]<>>>)([{{}<>}][<[][]>(<><>)]))>{{[[[{}[]]([]<>)][{()<>}]][([[]{}]{<>[]})] +(<(<{<{{({<({[{}[]]<(){}>}[<<>[])(<>{})])[[<{}<>>[[]<>]][([]{})<<>[]>]]>[{([{}<>]([]<>))<[<>() +(({[[[<{([[{<[{}()]<[]<>>>}]{<[<{}>{<><>}]>{{(()[])}<[<>{}]{<>[]})}}]{[[<[<>]{{}[]}><[()[]]{{} +{[[{{<({{<<<{[<>()]{{}()}}[([]<>){()<>}]>>(<{(<><>){[][]}}[{[]{}}([][])]>{[{()[]}{<>}}{{{}{}}< +(<<[{[[{<<{{{<[]()>}}<[{()()}(<><>)](<(){}>[{}[]])>}<{{[()[]]([]{})}}{{((){}){[]()}}[({}{})< +<<<{<{{([<<{[{<><>}((){})]{(<><>)(()<>)})<{[{}<>]{<>()}}{<()[]><(){}>}>>>{{<<<[]()>{()[]}>({ +{[(<[[{[(({<(<{}<>>{<>()}>({[]{}})>[[(()<>){<>[]}][[<><>]{<>{}}]]}))[[{{[<()[]>(()<>)]}}([([<>{ +<([({((<((({[<()<>>]<<()[]>(()[])>}{({()[]}[[]{}])})<[{[()<>](<>{})}<(<>{}){()[]}>]<((()())[[]()])>>))>< +{{([([(<((([[({}[])(<>{})][<()()>{<>()}]])[{{[()<>]{{}()}}<[()()][()[]]>}]){{{[{[]()}]}<[<<>( +<[[{((<{{{(<((<>{})[{}[]])(<()[]><()()>)>{[{<>()}[()[]]]{{[]<>}<<><>>}})}(<{<(<>)[()]>}>)}<{<[([<>{}][(){}]) +<{[<[{[<{[(<[[<>[]]<{}<>>]>[[<<>[]>{{}[]}]{({}[])[[]<>]}])]{{{<<{}{}>{<>()}>[{()<>}{(){}}]}{<[()()][(){}] +({<([<[<((<({({}[])<[][]>})[<[<>()]([]{})><(<><>){{}[]}>]>))[<[<([[][]]{{}[]})>](<(<<>()>)(<{}<>>)>)>]>]>] +{{{[{{<<{<[<(<<>[]>[{}{}])>[[{{}<>}<<>[]>][[[]<>]<[]{}>]]]{<({()<>}((){})){[[][]]<[]>}>[<(<>[])>([{ +{<({[{{{(<{<[<[]<>>]{{{}[]}<<><>>}>{[[<>[]]<{}[]>]}}(<(<()()><{}[]>)>{[[()()][()<>]]})><(({[()()]}[[[ +<<[{<{{<(<({[<()()>{()<>}]((()[]){[]{}})}{[(()[])[{}{})]{({}<>){<>()}}}){({<{}<>>[{}]}(<{}[]>))[{[[] +<[((<[{<<<(({[[]{}](<>)}{({}{})})(({{}{}})<{()()}>})<<([()[]]{<>()}){([]()){<><>}}>>>>{(<<{<{}()>{()<>}}( +{(({[[<{[{[(({{}<>}(()[]))[([]{}){()()}])](<<{{}()}{<>{}}><<{}[]><{}()>>><[[<>()][()]]({<> +[[({<(<[[({[{<{}[]>(()<>)}<{()()}>][[[{}<>](()[])]{(<><>){()<>}}]}[([{<><>}<<>()>]<{{}()}<<>{}>>)[{{( +[[<(([<{{{<{({<>{}}{<>{}})}<[<{}[]>(<>[])]<((){})>>>{[{<{}[]>{{}[]}}{<[][]>{{}<>}}]{(<[][]>{()))<<[]{}>{( +(<[{<<({<<{{(([]()){[]()})[({}{})]}({({}{})<{}{}>}(({}){{}<>}))}{<<<{}{}><{}()>><[{}()][[]]>>}>>[<{({({})[<> +{([(<([[<{(<({()[]>[<><>]){[[]<>](<><>)}>{{[()<>]({}{})}<{<>[]}[(){}]>})}{(({<[]()>{{}()}}{{<>()}(< +(<<[{([<[{{[[([]{})]<(()<>){<><>}>]<[{<>[]}<{}[]>]>}<<{([]<>)[{}<>]}(([]<>)<<>()>)>[<{<>{}} +<[([[<<<<<({<[()[]]>}[<({}<>)>])[[({{}()}(()<>))<<<><>>{[]{}}>]{{[<>()][[][]]}{<{}{}>({}())}}]>[{{{[ +[<{[(<[<<{<(<{<>()}>[{{}()}{{}{}}])>}>([<[<[[][]]>((()<>)<(){}>)]{(<()>)(<<>[]>({}()))}>([[<[]<>> +<[(([<{(<<<{{<[]<>><[]<>>}{<()[]>(<>())}}<{<<>()>{[]}}(<()<>>[[]{}])>>>[<{([()[]]<{}()>)<<{}{}>{{}[]}>}{((< +[{<[{[([(<({<<<>{}><<><>>>{(()()){()[]}}}({[<>[]]<<><>>}([()]({}<>)))){{{[[]{}]{{}<>}}[(<>{}){<>}]}[<{[][]} +<<<{({[(({[{({<><>}([][])){{<><>}[(){}]}}(([{}{}][{}[]]))]({<({}<>){{}()}>[[{}[]]{[]{}}]}}}))]<[[<(<{{() +[(([[(<({{({(<()()>(<>{}))}({<[]<>>{[]<>}}<[<><>]{[]<>}>)]<([[[]{}][[]{}]]{([][]){()<>}}){{[{} +<{(<[<{<{([{([()[]]<(){}>)([[]<>]({}()))}](<([[]<>]<<>{}>)[{[]{}}[<><>]]>))}>}>[{<({<<[(()() +[{({[({<[((({[<>()]<[][]>}{{<>}{(){}}})([(()<>)<<><>>][({}{})<<>[]]]))<<<{{}()}<[]<>>>(<()[]><[]()>)> +[[<{((<<(<<{(<<>[]>[(){}])(([]<>)([]{}))}<[[(){}]<[]<>>]{(<>())(<>)}>>>)>[{<[{{[<>{}]{[]()}}[ +<([{{{<([([([<(){}><()[]>])([[{}<>]]{[()<>>({}())})]<[<{()[]}{()()}><[[]()][<><>]>]>)]<{({((()() +<<{([<[[{<[<(({}<>)(()()))({{}[]>[<>()])>{[<{}>]<[{}{}]>}]([{{[]{}}[{}()]}{[()<>]{<>}}]{([<>{}][[]() +({[[[[(<<{{[{(<>[])[{}]}[<[]<>>[<><>]]]}}([<([<><>]{<>{}})[<[]{}>[<><>]]>{(<()<>>[{}])[([][])[{}{}]]}])>>( +<({<(<<[({[{{{{}[]}<<><>>}[([][])(()<>)]}]<<[[{}]{<>()}]>>}<<<{<<>[]>([]())}<({}())[<><>]>>{<{<>{} +(<({{<{[{[({[{[]<>}{()[]}]}<{{{}()}{[]<>}}<<{}<>>[{}{}]>>)[[({{}<>}[<>()])<[{}[]][()()]>]}][{{<[[]{}][[]()] +(<<[((<{<<{[[{<>{}}[<><>]]{{{}{}}{<>[]}}]<[{()()}([]())](({}()){()[]})>}>)<((<<[{}[]]>[{<>[]}([]{})]>)({({[]< +([({<<(({((<{[{}[]]<()<>>}{[{}<>][{}()]}>)<<(<{}[]>[[][]]){<()<>)<<>()>}><(<<><>><[]{}>)[<<>()>[< +[([<[[(((((({<(){}>[{}{}]}([[]()]{()[]})))<[(({}<>)[<>{}])(([][])([]{}))](<[()()]{<>()}><[()[]]{()( +({[({[{<<({<[(<>())<{}<>>]>[[(<><>)][{{}{}}[{}()]]]}<[([[]<>]([]<>))<[()<>]<()()>>]<{<{}[]>({}())}{<< +(<<{(<<({{<<<<(){}>([]())>{[()()][{}{}]}>[([[]<>]{{}}){{{}{}}}]>}}{(({{(<><>){<>()}}}[({[]()})[[[]]{[]{}}]] +((<[([((<[([{[<>()][()()]}{[{}<>][<>[]]}]<(([][])({}())){<()[]>([]())}>)[{{<<><>>([]{}}}[{<><>}<{}[] +<{(<<[<<<{[{<<[]()>[()<>]>(([]{})([]<>))]{<([][])<<>{}>>{(<>()){()}}}](<{[<>[]]}([()()]{<>{}})>(<[()[]][ +[<{{[[[<[[{((([]())<{}[]>){(<><>)})[{{{}}}[({}[])({}{})]}}{{(([]<>)<<><>>)<([][])({}<>)>}{<<()[]><()()>>[<{} +<<([(<<({<<(<(<>[]){()[]}>{[()<>)(<><>)})>{((<{}[]>(()[]))({[]<>}[{}{}]))<{<<><>>(<>())}<({}<>)[[]] +<(<<{[<<[[[{<[<>{}]{<><>}>[<<>[]>[(){}]}}{{<{}{}>}(<<>()><[]()>)}]]{{([[[]{}][[]()]][[<>()][{}[]]] +(<{<<{<{[{<{<[()()]<{}()]>{<<>()>({})}}>{[{<{}<>>}<<[]<>><[][]>>]<([{}{}][[]<>]){{[][]}{<>}}>}}] +{{[[([<[[{<[[<[]()>[()<>]]<({}[])[{}[]]>]>}<[((([][]){[]}){<{}{}>((){})})][([([][])({}[])](<{}<>><<>{}>))]> +[[{{({{[[[{<[{(){}}{[]<>}>[<{}[]>]>}]((<{<(){}><{}()>}[<<><>>({}{})]><<{{}{}}>(({}[]){()<>} +[<<([<<({[<({{()()}[[]()]}({{}<>}[<>()]))>([[[{}<>]([][])][({}()){[]()}]])]((([[(){}](()<>)](({}{}>)) +<[{<{(<<([({[({}<>)[[][]]]{{[][]}<{}{}>}})([[[[]{}](<>{})]([<><>](()()))]{{([])[()()]}({<>[]})])]<<(({[]( +[((<<[<[{<[{{[()[]]({}())}([[]{}]<()[]>)}[(<[]()>{()[]})<<<>()>[()()]>}]({{[{}{}][()[]]}{{<>()} +{{{{<[([([<({{[]()}<(){}>}{(()[])<<>()>})>{([({}[])<[]<>>][[[]()]])}]<{<((<>)[()<>])<(()<>) +[[<{<([[([<[{[<><>]{(){}}}{(<>()){{}}}]>[[{{<>[]}{[]}}]]][<{[<{}()>]<[{}()][<><>]>}(<{[]<> +((<[<{<<<{<<{[[]{}]}>{<(<><>)<<>()>>{(<><>)({}())})>{{<([]{})<[]()>>{[<>{}][()()]}}[<<<>{}>([]{})>[{{}}<()() +<<({(<{[[<((<{<>[]}(()())>>[<[[]<>][<>{}]>])[(<{{}{}}><<<>()>>)(([[]()]{[]<>}))]><<[{<[]{}><()[ +(({({({<(([[{({}[])([]())}]({<<>{}>[()<>]}{<[][]>({}[])})][{{[[]<>]}}{[<<>><{}{}]]}])<<[{{()[]} +<[(([<(<{[[([((){}){{}<>}]<[{}{}]{()<>}>)<<{<>[]}[(){}]><({}())([][])>>]<[{<()()>{<><>}}[(<>{}]([]())]][{ +{[<{([{<[{<[<<{}<>>[{}{}]>[[<><>](<>[])]]<{<(){}>([]<>)}{{()<>}([]())}>>{(<(<>)[()<>]>{[{}[]] +{(((((<<[[{(([(){}]<[]()>)[<<><>>])<<{()}{<>[]}><(<>[]){{}{}}>>}({<[{}<>]([]<>)>{{{}{}}<<>{}>}}{([ +<{{<<<(<{{<([<()()>([]<>)]<{()()}<()<>>>){<<[]{}><[][]>><{{}<>}>}>{([[<>]{[]()}][<{}><[][]>])[{((){})< +{{[{[{([{<{<[{()()}{()}]([<>{}][{}{}])><<[[]()]>({()<>}<[][]>)>}({((<>())({}()))[[{}()][<>[]]]}<{([]<>)<{}< +{([<[<(<[([<[{<><>}[{}{}]](<{}<>>[[]<>])>[{{[][]}[{}()]}(<{}[]><{}{}>)]])]<<<[<(<><>)<<>>>{[{}()]}]< +<<[(([[{{({(<[<>()][()<>]>[(()())((){})])(<((){})[()()]><[()<>]<{}[]>>)}{{<(()){{}<>}><<{}{}>{{}()}>} +{({([({{(<((<(()[]){()<>}>[[()<>]]))>{[<[({}()){{}<>}){<<>[]>{()<>}}><[[{}()][<><>]][[<>()] +[<([<{<<[[(<[{{}<>}{{}[]}]<(()[])<()()>>>[{[<><>][{}{}]}])>{[{<([]())[<>{}]>{<<><>>{(){}}}}{[(< +({{(<{((<({{([()[]](<>))({[][]}{()()})}{(<<>[]><()[]>){((){})[<><>]}}})((<{(<>{}}<(){}>}{{<>{}}{<><>}}>{( +[({[{{([({[{<{<>[]}[[]()]>}[(({}<>){<>[]})[{{}{}}<{}()>]]]}{[{[{<>()}](<<>()>(<>()))}][<(({} +<{([[(<[{[{(<<{}<>>(<>{})><{(){}}<{}<>>>)(({()()}([]())){<[]{}>({}<>)})}[{<[(){}][(){}]>{{<>()}<( +{[(<<<[<{[{({[<>()]{<>[]}})[<<<>()><{}())>([[]()]((){}))]}][[{<[<>[]]<<>()>>(({}()))}](<{<<>>{<>[] +{{{({{<[{([[(([]())<<><>>)(([]{})[[]()])]((<{}[]>{[]()}))]<{<{<>[]}(()())>({<>[]}[<><>])}(((()()){<>[]} +<[{[([{([[<(<([][]){{}[]}>([(){}][<>]))><({<()[]>[<>()]}[([]{})])>]({{[(<><>)(<>)]{([]())}}<<{<><>}<{}() +[[(<[[(<([([{[{}{}]{{}()}}(({}<>)<[]{}>)]{<(()())>})]({{(([]<>)[[]<>]){[<><>]{<><>}}}}[<{<<><>>[<>{ +([[(({(<({<{<[<>{}]([]{})>([{}<>]<{}{}>)}[{{[]<>}[[]{}]}]>}{([([[]{}][{}()])([<>]({}<>))])<([[{ +{[[<[({{<({[{({}<>){[]<>}}[({})([]{})]]{<{{}[]}{[]{}}>}}(((<{}>[[]<>]))<(<{}[]>([]<>))>))[[{ +<((<(([(<<[[<{()<>}<{}<>>>{<<><>>{[]<>}}]({{[]<>}[[]{}]}<(<><>)([]<>)>)]><{<[[[]{}]<[]{}>]>}<(<{<>[] +<[[<({[[<{<[<([][]){<>[]}><((){}){()[]}>]>}(<<<{(){}}[[]()]>({[]{}}(()<>))>><[({{}{}}[[]{}])(<<>()><<>[ +[<<[[[[({<{[[{()()}<()()>]{[[][]][[]<>]}][<<<>{}>[<>]>{{<><>}}]}<{[<<>{}>{{}()}]{<{}[]>})<[(())<{}{}>] +{{{<<({(([{{[(<>{})<<>[]>][[[]{}]{()[]}]}<<({}())>{{<>()}<{}<>>}>}]<[<[<()()>{{}()}]<([]<>)>>[([[]]<<>[]>){[ +([[[(<{(({({<[{}()]<[]{}>>[<<><>>(<><>)]}[{<<>>([]{})}([{}[]]{<><>])]){[({()()}((){}))[[[] diff --git a/2021/10/second.go b/2021/10/second.go new file mode 100644 index 0000000..69bd104 --- /dev/null +++ b/2021/10/second.go @@ -0,0 +1,86 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "sort" +) + +type stack []byte + +func (s *stack) clear() { + *s = make(stack, 0) +} + +func (s *stack) push(b byte) { + *s = append(*s, b) +} + +func (s *stack) pop() *byte { + l := len(*s) + if l == 0 { + return nil + } else { + elt := (*s)[l-1] + *s = (*s)[:l-1] + return &elt + } +} + +func main() { + scores := make([]int, 0) + s := make(stack, 0) + + scanner := bufio.NewScanner(os.Stdin) +out: + for scanner.Scan() { + line := scanner.Text() + s.clear() + for i := 0; i < len(line); i++ { + c := line[i] + if c == '(' || c == '[' || c == '{' || c == '<' { + s.push(c) + continue + } + b := s.pop() + switch c { + case ')': + if *b != '(' { + continue out + } + case ']': + if *b != '[' { + continue out + } + case '}': + if *b != '{' { + continue out + } + case '>': + if *b != '<' { + continue out + } + } + } + score := 0 + for c := s.pop(); c != nil; c = s.pop() { + score *= 5 + switch *c { + case '(': + score += 1 + case '[': + score += 2 + case '{': + score += 3 + case '<': + score += 4 + } + } + if score != 0 { + scores = append(scores, score) + } + } + sort.Ints(scores) + fmt.Println(scores[len(scores)/2]) +} |