From 0db48968c17b73684d3d2618108e926615f528ed Mon Sep 17 00:00:00 2001 From: Benjo Date: Fri, 10 Dec 2021 09:06:37 +0100 Subject: [PATCH] day 10 --- 10/10.py | 47 +++++++++++++++++++++++++++ 10/input | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 10/testInput | 10 ++++++ 3 files changed, 147 insertions(+) create mode 100644 10/10.py create mode 100644 10/input create mode 100644 10/testInput diff --git a/10/10.py b/10/10.py new file mode 100644 index 0000000..9e3bca1 --- /dev/null +++ b/10/10.py @@ -0,0 +1,47 @@ +openList = "(", "[", "{", "<" +closeList = ")", "]", "}", ">" +scores1 = 3, 57, 1197, 25137 + + +def readLines(): + with open("input") as file: + return file.read().splitlines() + + +def solve1(lines): + counts = [0, 0, 0, 0] + for line in lines[:]: + stack = [] + for char in line: + if char in openList: + stack.append(char) + else: + last = stack.pop() + closingIndex = closeList.index(char) + valid = closingIndex == openList.index(last) + if not valid: + counts[closingIndex] += 1 + lines.remove(line) + break + return sum([s * c for s, c in zip(scores1, counts)]), lines + + +def solve2(lines): + scores = [] + for line in lines: + stack = [] + for char in line: + stack.append(char) if char in openList else stack.pop() + stack.reverse() + score = 0 + for val in stack: + score *= 5 + score += openList.index(val) + 1 + scores.append(score) + scores.sort() + return scores[len(scores) // 2] + + +task1Score, incompleteLines = solve1(readLines()) +print(task1Score) +print(solve2(incompleteLines)) diff --git a/10/input b/10/input new file mode 100644 index 0000000..ec21a09 --- /dev/null +++ b/10/input @@ -0,0 +1,90 @@ +{[[<[({{[<[[[[<>()]{[][]}][[<>[]]([])]]]{({[[]()]<()<>>}[[()()]<{}()>])<{<[]<>><{}{}>}[<()<>>{[]()}]>} +[[<{(<{(<<{[[[{}[]]({}<>)]([[]<>][()()])](({<>}[<><>]))}><{({(<>{})<<>{}>}[[{}{}]<()<>>])(((()<>)({}{})){ +(<[<[[[<(<{[{<[][]>({}())}({<>{}})]({{<>{}}{{}[]}}<[{}()](<>)>)}(<[[{}{}]({}{})]{{{}<>}}>{{{() +<([({(<[{{{<[<<>[]>[<>]]<{[]<>}<{}>>>}[<<{<>()}<<>{}>>[([]<>)<<>()>]>{(<[]{}>({}()))([[][]]({}()) +<[{{(((([[<[{{[]()}[[][]]}[<<>()>{<>}]][{[{}{}]<()()>}({()[]}({}()))]>[(<(<>{}]><[()<>]{()[]}>)]]<( +[<[([(({<{[<[(<>())<{}[]>]>([[()<>][()[]]]<(<><>)>)]}<{{<(<>{})[<>[]]><(()<>){<>[]}>}}[{(({}[])<[][]>){{[]{ +{<[[{{[{<({[[{{}[]}<{}[]>][<<>{}>{<>[]}]]}{[(({}{})<()))[({}())]](<{<><>}([])><[()[]][()[]]>)})[(((([]())({ +[{{({<((({({{<{}[]>}[{()[]}{{}<>}]}({{{}()}[{}<>]}{[<><>]}))([(({}{})(<>[]))]([[{}()](()())]))}[[[[[()<>] +<<[{{<[((({{[({}[])({}())][[(){}]<<>[]>]}[<{{}{}}{()<>}>[<{}{}><<>{}>]]}[<<([]<>)<{}{}]>[<()><<><>>]><<( +<<[[[(({{{[{{<<>>[{}<>]}}[{([]{})[(){}]}{{{}[]}{{}{}}}]]}{[[[{()}[<>{}]]]{<{()())><[[]]({}())>}]{[{{()}<{ +[<[[<[(((([<({<><>}{[]()})[(()<>)<<>())]>{{({}{})}(<<>>)}]([<<{}{}>>]<[[{}[]]{()()}]<[[]<>]{<>{}}>>)))) +<{([({((<{[((<()[]>(()<>))<{(){}}{(){}}>)(<(()<>)}{<()<>><()()>})]}><{({[<<>[]><[]()>]}<<(<><>)[()()]><<{ +[[{[(<<({([<{[(){}]((){})}<[()<>]{[]{}}>>(<([]<>)>[[()<>](<>())>)]{(([(){}][()<>])<[{}{}]{(){}}>){[[<>{}] +<<(<<[[[<[<{<{()()}>[(<>()){{}()}]}](({{<>()}<{}{}>}<[[]()][{}<>]>))][[({<{}[]>[()]})([<(){}>[{}]])][{[([ +{{(([<<(<<<[<{<>{}}]<<[][]>(<>())>][{<<>()>([]{})}[(()[])]]>>>)[([{{<<{}()>{[][]}>}<[{{}[]}]{(<>{})[<>()]} +<[{{([[<([<{([{}[]])(<{}><<>>)}{[<[][]>[()[]]]((()())[{}[]])}>{[[{[]{}}<<>()>][[<><>][<><>]]]}] +[[[{[{[[<[<[({{}{}}(<><>))]>]{<(({{}()}(()()))<<[]{}>{()()}>)<[{[][]}<()<>>]{[{}())<<>{}>}>> +{[<[([<<(<[<[[<>()]<{}<>>](<<>{}><[]{}>)>(<(<>())<{}()>>)]>)[{{<<<(){}><(){}>><<[]<>><()()]>>}}{(<< +[{<[{{<{{[<[<<[]()>[<><>]>{[{}<>]}][({<>()}([]()))]>]}<[({<<<><>><[][]>>({[]<>}{{}[]})>)[{{<[][]><() +{[[(({<<<[{<{{{}()}[[]()]}<({}{}){<>[]}>><<[<>[]]>>}{{[(<>{})({}())]((<><>)[[]{}])}}]((<[({}{}){[] +<({<<<<{(<{<{[<><>}{<>()}}([<>{}])>[<([]{})((){})><(<>[])({}[])>]}((<[<>[]]([]{})>((<>[])<{}() +<{[(([({{[({(({}())<(){}>)}[({<>[]}<{}[]>)<([]())<{}{}>>])<({[<>[]][()()]})[{{<>{}}([]{})}[[<>()]]]>](<({ +[{[{<<[<{{(<({[][]}<<>{}>)(<()()>)>[({<>()}[[]{}>)<{{}{}}[(){}]>])((<((){})[<><>]>{(()[])({}{})}){{<()<> +(<{[(<{{<<[[([()()][(){}])([(){}][()<>])]<({<>{}}{[]{}})<<{}<>>({}{})>>]{{{{[]{}}}({<>{}}({}<>))} +{[{{{{<((([([<<>()><{}{}>]({[]<>}<<>()>)){({{}<>}[()[]])<[<>[]]{{}()}>}]{[({()[]}[{}<>])<{{}{}}({}{ +{(([(<<[(((([{[][]}[()[]]][{{}{}}{{}<>}])<<[[][]](()[])>{(()[])({}{})}>))){<{(<{[][]}<<><>>>(( +<([{<([((<(({([][]){()<>}}<<()[]>{{}<>}>){{(<>())[[]<>]}({{}<>}[{}{}])})>{<{{[(){}]}}[(<()[]>({ +{([<(<<{[(<<[[{}[]]<()<>>]<[{}[]](<>[])>>(<{<>()}[{}<>]>([(){}][[][]]))>)<[<[<<>[]><()()>][{{}[]} +{[[(({[[{([<[[<><>](<>[])]<[[]()]([][])>>][<[(<>())[<><>]]>])}]{{{({{([]())(())}(<<>>[()[]))})}{[{[[[]<>] +<<<([(<{(<([{(<>{})[(){}]}({()()}<()()>)])>[<([<<>[]>{{}()}][<()[]>{()<>}])(({<>[]}<{}[]>)<( +[[{[{{[{{[[{<{[]{}}{()()}>(<<>[]>(<>{}))}><{(<[]{}>[{}<>])(<()<>>([][]))}<[((){}){[][]}]<{{}()}([]<>)>>>]}{ +[(<<[[[[({[<{({})}<<<>><[][]>>>{<{<><>}{<><>}>[[{}[]][()]]}]}){({[[(()<>)[()()]]]({(()())<(){}>}{([]{} +({{[<[<<([{[{<{}<>>({}[])}]({{[][]}}[{{}[]}(<><>)])}][{<[<()<>>({}<>)]({()}{{}})>({{[]{})}({[ +{(({[[<(({<[[((){}){<>[]}]][((<>())([]<>))<([]()]>]>{{<[()[]]{<>{}}>{<<>[]>({})}}{{[<>{}]}{{()<>}{<>()}} +([{<(<[({[(<(<()<>>{{}{}})<<[][]><[]{}>>>([{<>}{()<>}][<{}()>([]<>)]))<<{{<>{}}<[]<>>}{<<>()>({}[])}>[ +<<{(<{{[<{[{<<{}{}>([]())>{[{}[]]{(){}}}}{<[[]<>]>({[]<>}([]{}))}][{((<>[]){<>[]})}]}{({((<>()) +{((((({[[<[<[{[]{}}][[{}[]]<[][]>]><[<{}{}>{{}{}}]>}({[[()<>]({}[])][[[]]<()<>>]}({{<>()}[<>{}]}<<(){ +<<((<[<([(({{<<><>>{()[]}}{<(){}>[<>{}]}}[({()<>}){(<>{})<[]{}>}])<<{<{}{}>}[(()())[<>()]] +[<((([<[<({({((){})}<({}<>)>)[<{(){}}{()<>}><<{}()>[[][]]>]}[({<{}{}>)({[]()}{<>{}}))[<[()<>] +{[([{<((<(<{[{{}<>}{{}}]({<><>}{[]{}})}>({{([]<>)}}<<{[]<>}[<><>]>{(<><>){[]<>}>>))>))>}][([<[[{({({< +([[{{({<<[[<[(<>())(<>[])]>[(<{}()>)(({}()){{}[]})]]{<{[(){}]}>(<<<>[]>([][])><([]{})>)}]{{[[({}[] +{({{({[<<{((({[]()}){{[]{}}(()[])})<((<>()>[{}{}])([<>][[]()])>)(<<<{}()>{[]()}><<[][]>[()[]]>>[<(<>( +<<{([([[{<<<({()()}{[]()}){{{}<>}[{}<>]}><{{()()}(<><>)}[[(){}>[[][]]]>>{{([{}[]]({}()))[<[]()><<>[]>]}<{<{ +<(<({({<<[[<<[{}<>]<{}<>>>{{{}<>}{[][]}}>{{{{}[]}[<>()]}[(()())<<>()>]}]<(<{()()}[{}{}]>)<({()<>}{< +<[{(<[{(([[[[<<>[]><()()>}{([]<>)<[]{}>}]<[(<><>)<[]()>]>]<([(()[])<[]>]{({}<>)(()[])})>]{{{([[]{}] +<[(<{<[{{[{{<[{}<>][{}()]>[<{}[]><{}()>]}<<<[]<>><()<>>><[()<>]<()()>>>}[{([<>{}]{<>()})(({}{})<(){} +[[{{[{[({<<<{[{}{}]>{<<>>{<>()}}>>{<(<<>()>{<>()})<(()()){[]()}>>}>}<{<(<({}())[[]<>]><<{} +(<({<<([[<[({[<>[]](<><>)}{<<>()>[()()]})]({({{}{}}(()))((()())[[]()])})>{<[[[<>[]]<<>{}>]{[< +[[<<[([{{{([[(<><>)(<>[])]])}}}])]<<({<{({[<<>><<>>]([()<>](()[]))})((<<[]<>>{()()}>)<([{}[]]< +{({({[[([(<{<{()()}>(<()()>)}{<{<>{}}<(){}>>[(<><>)[{}[]]]}>[(<(()()){<>}><<[]{}><[][]>>)<[<{ +[[([<{<[([([[{(){}}{(){}}]<{[]()}[<><>]>]({{{}{}}<()<>>}))[(((<>[])({}()))([(){}]<[]<>>))({[( +<[{({([<<{{([[[]()]{()}]([<><>][{}{}]))}}>[[{<<([][])({}<>)>[<[]{}](<><>)]>[[{<>{}}<{}<>>]((<>[])[<>])] +(([{(({{{{{[<[<><>]{[][]}>]<<{{}{}}<{}<>>>{(<>())}>}(<{[(){}]{(){}}}[[[]<>]([]<>)]><((()())({ +([{[<[({((([[[{}{}]<[]>]<([]{})([][])>]))(<<[[(){}](<><>)]>>))(<([({<>{}><<><>>)[{<>{}}(()[])]]<{([]())}> +{[{((([[(<([{[<>()]{[]<>}}<<[][]>{[][]}>]([<<>[]><{}[]>]{([][])<[][]>}))[<{<<><>>}<<<>()>{ +({(<[[<{<[{<{[()]<{}<>>}>{[[[]{}]<<>()>]{{<>{}}([]<>]}}}((<{{}()}[[][]]>({<>}{(){}})){<{(){}}( +{[{[[(((<((({<()<>><<>()>}<[[]()]>){{[(){}}[<>{}]}<<{}<>>{{}()}>})<{<(<>())[<>[]]>[<{}()>[ +[[{[<{([(<<{({<>}<[]()>)[{(){}}[[]<>]]}>{{(<{}>{{}()})[([]<>)<<>()>]}}>)])}<<<{{([(<()[]>{[]})<<(){}>[[ +<(([[[{<[[<<([[]()]({}[])){{[]<>}{{}{}}}><<(()<>){[]{}}>[{[][]}]>>[<{{{}{}}}[[[]<>][()[]]]>[{<{}<>><<>[]>}{ +({{{((({{{({<{{}()}>(<()()>{<><>})}<{<(){}><{}<>>}[<<><>>([]{})]>)[((<<><>><{}{}>))]}}((<{{{() +{<[({{(<<<[<<(<>())<[]()>>({[]<>}(()<>))>]>>{([[[{<>[]}[{}]]<(()()){[][]}>]])}>{({{{[(()[])([]<>) +((<{((([(({{{[[][]]}{<<><>>([])}}[{[()()]{()<>}}>})(<<((()<>)<[]()>)[[(){}][[]<>]]>(<([]{}){{}()}>{ +[[{([(<(<{{<<([]){()<>}>[{[][]}[()[]]]>[<{()()}[<>]><(<>){{}{}}>]}((<[<>()]<{}<>>><<[]<>><<>()>>)[{{{}}} +[{{[<{([<(<(<[<>]<[]{}>>[[<>{}]{{}{}}])(([()]{<>()})[[()<>]([]{})])>((([<>()}(<>{})){({}()){<>[ +([{{[<{<[<([<<{}<>>{{}{}>>{{{}}<[]>}])>]><<[{{(<<>[]>)}({(()()){()()}}(([][])))}]<{[[<[]<>>[()<>]]{[[] +<[({<(<<<(([([()()])([<>()][[]<>])}<<{[]()}<()<>>>{(<>{})[{}[]]}>)({[[()<>](<><>)][[[]{}]<{}()>]}<<[[]()]{() +((({<<[<[<{{[<{}()>([]())]{[{}<>]<{}()>}}(<(<>[]){{}}>)}{{<<(){}>{{}[]}>[{()()}[<><>]]}<([{}( +([({[[[<([(<<{<>{}}{<>()}>[<{}{}>(()())]><<[<>[]]({}{})>{{{}()}([]{})}>)[{<[{}{}](()())>([{}()][()])}<[[()( +<<[([([<([<(([<>()]<()[]>)({()()}([]())))><[{<{}()><{}<>>}[<[][]>{<>()}]]{<{()[]}>{[(){}]{{}<>}} +<(((<{<([<({<({}{})>{[<>[]][<>[]]}}<<({}[])[<>())>{({}())<<>()>}>)>][[{{{{{}()}{[]()}}<((){}) +<<<[<[<<[<<<{[{}()][<>{}]}[([]())({})]>[{([]()){()}}({{}[]})]>>([({{()()}[<>{}]}[<[]<>><[]>])](({[<>{}]{() +{((([<[({{<{<(<>())[[]<>]>}([{[]()}{{}[]}]([[]<>]))>({[[()[]]<()()>]([()<>](()<>))}[[<[]{}>]([()<>][ +<<([[{[[[{([((()<>)[<>[]])][[{<>()}[(){}]]([[][]]{[]()})])<[{<[]<>>[()[]]}][[<()<>><()()>]( +[[<[[<{{{[([{<{}{}>{{}{}}}{{()[]}}])]}[[[(<<[]<>>>{{<>()}})([{(){}}(()[])]<{<>[]}[(){}]>)]{[({ +{[<[[{({((<[{<{}{}>[<><>]}((<><>)[<>{}])][<[()[]]<[]<>>>[[(){}>(()())]]>[({<()[]><()[]>}({(){}})) +[[(([([[({{<{[[]]([]())}([{}<>](()<>))]}(<<[{}[]]{{}[]}><<[]<>>{[]<>}>><[(<>{})<<>[]>][{{}<>}[[][]]]>)} +{<{{{{[{({({<<{}{}><()>>}[{<[]()>([]())}(<[]{}>[<>[]])]){{({[]()>(<>[]))[{<>()}<<>{}>]}{[<()()>[{}() +({[<<<{[<(<({[()[]]<{}()}}<{()<>}<()<>>>)[[<<>{}><[]()>]]><(<<<>[]><<>()>>{(()())})[<[{}()]((){})><((){})( +<(({(([({<[<{<()<>><<><>>}[(<>)[[]<>]]>((<<>[]>{()<>}))]><[[{[[]<>]([]())}{[()[]](()[])}][({{}<>}[()[]]) +[<[{{([({{[([(()<>){(){}}](({}())))]({<(())>}))(<({<<>()>{<>{}}}((<>())[[]()]))([<<>{}>(<> +{{<([<{[<{([{{(){}}[()[]]}([[]()]{<>})]({{()[]}<<><>>}(<{}<>>{[]()})))}{{(<{<>{})<[]()>>[({}{ +<[{(({{<[[[[[[()<>](<>{})]<([][]){(){}}>]{([[]()][[]()]){{<>()}<<><>>}}](<(((){})[(){}]){<<>{}>}>)]<<{<{<>< +<<[(<(<(<(<[{({}())(()())}<<{}[]>[(){}]]]{<<<>[]>{<>()}><[<>()]>}>[({{()()}}){[{[]{}}<[]<>>]{{ +[(([{[{<<((<[[<><>][()<>]]<<{}{}>[[][]]>>(<{[]()}[{}[]]>[([]())({}{})])))>>[{{[{{<()()><(){ +[({<<{<{(((<{[[]()]}{{()()}(()<>)}>[[(()<>)<<>}]([[]<>]{{}()})]))[[([{<><>}{[]()}])<[((){})[[]<>]]>][ +([(<<<[(({<<{(()<>)[{}()]}({()[]}{{}<>})>>{({({}())[[]]}{(()<>)<{}()>))}})({[({(<>{})[{}<>] +[{<{((([((<(<<<>[]}[{}<>]>{<{}<>>[[]()]})>[{{<{}{}>}[([]<>)[[]{}]]}{[<<><>>[[]{}]]}])(((<<[] +[[[{[[{[{([([<{}()>({}())]<(()[])>)<(<[]{}>{{}}){{[]()}{<>[]}}>]{[<{{}{}}{{}<>}>[[<>[]]<<>{}>] +{<{<{{([(({[{<<>[]>(<>())}(({}<>)<<>>)](<{{}{}}<{}{}>>[{()()}[<><>]])})[[<<({}{})[{}[]]>[<{}{}>({} +((<(([{[[<[<{({}{})<{}[]>}{[[]<>][()[]]}>[((<>{})(<><>)){<{}()>(<>{})}]]>(({({<>[]}[{}<>]]<[<> diff --git a/10/testInput b/10/testInput new file mode 100644 index 0000000..7312dfa --- /dev/null +++ b/10/testInput @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[] \ No newline at end of file