From aa88c703f772ffb6412412d38c2de58dfce8f020 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 10 Dec 2021 11:33:19 +0100 Subject: Day 10 --- day10/__init__.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/example.txt | 10 ++++++ day10/input.txt | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 day10/__init__.py create mode 100644 day10/example.txt create mode 100644 day10/input.txt diff --git a/day10/__init__.py b/day10/__init__.py new file mode 100644 index 0000000..b41ce55 --- /dev/null +++ b/day10/__init__.py @@ -0,0 +1,91 @@ +from collections import Counter +from functools import reduce +from math import floor +from typing import List, Iterator + +from aoc import BaseAssignment + + +class IncompleteLine(Exception): + def __init__(self, missing: List[str]): + self.missing = missing + +class CorruptedLine(Exception): + def __init__(self, got: str, expected: str, *args, **kwargs): + self.excpected = expected + self.got = got + +pairs = { + '(': ')', + '[': ']', + '{': '}', + '<': '>', +} + + +class Assignment(BaseAssignment): + @classmethod + def parse_line(cls, line: str): + stack = [] + for character in line: + if character in pairs: + stack.append(character) + elif character == pairs[stack[-1]]: + stack.pop() + else: + raise CorruptedLine(character, pairs[stack[-1]]) + + if len(stack) > 0: + raise IncompleteLine(list(reversed([pairs[c] for c in stack]))) + + +class AssignmentOne(Assignment): + example_result = 26397 + + penalties = { + ')': 3, + ']': 57, + '}': 1197, + '>': 25137, + } + + def run(self, input: Iterator[str]) -> int: + invalid_characters = [] + for line in input: + try: + self.parse_line(line) + except CorruptedLine as c: + invalid_characters.append(c.got) + except IncompleteLine: + continue + + counter = Counter(invalid_characters) + + return sum(counter[k] * v for k, v in self.penalties.items()) + + +class AssignmentTwo(Assignment): + example_result = 288957 + + penalties = { + ')': 1, + ']': 2, + '}': 3, + '>': 4, + } + + def run(self, input: Iterator[str]): + scores = [] + for line in input: + try: + self.parse_line(line) + except CorruptedLine: + continue + except IncompleteLine as c: + scores.append(reduce( + lambda out, item: (out * 5) + self.penalties[item], + c.missing, + 0 + )) + + return sorted(scores)[floor(len(scores) / 2)] diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/day10/example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..c029f1e --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,90 @@ +({{[<{[{[[<([[<><>][{}()]]<([]{})[<>()]>)><<<([]())>>{([()<>][{}{}])<[<>()]{()()}>}>]<(<[[[]<>]({}{})]{{{}<>} +[(<<<[[[{({(([{}{}]<()<>>)[<()<>>[[]<>]])}([{{<>()}}]))}<[{{[<[]()>][[<>()][[][]]]}{{(()()){{}{})}} +<(<(<<{<(({(<[()()]<<>[]>>((()[])(<><>)))}))[{<{<[(){}]>}><[<{{}[]}{[][]}>(<{}{}>)]([(<><>)(<>{})]{{(){}}[<>( +[{<[<{[[[<{<(<[]<>><<><>>)<{{}()}>>{[<()>{[]<>}]}}>[(<{[{}()]<<>()>}{(<>[])({}<>)}>)]]}{({< +[<[(<<<([[[<(<<>{}><()>)(<[]<>>([][]))>(<{{}<>}{{}[]}>)]](<(<({}){<>()}>{<{}<>>})[{[{}<>][< +[([(<([[[([([<[]()>[()()]]{{<>{}}([][])})<{<[][]>{()[])}[{<>{}}<<>()>]>][<[<{}<>>[[]{}]](<() +{({{<{[[(<[[{[()()][()[]]}<{<>[]}<{}()>>]({<<>>}(({}{})(<>)))]<([[()[]]<(){}>]{{{}<>}([]<>)})([{[][]}{<> +<(<{{([[<<({[<(){}>[()<>]]{[{}<>]<(){}>}}(<[[]{}]<[][]>>))<<[{{}()}][{<>[]}[{}<>]]>[([<>{}]{[]{}}) +[<{<((([{<(<(<{}>[{}()])>)<{[{<>()}[{}[]]][{[]}<{}()}]}((<{}>{[]<>})({()()}<[]{}>))>>}]))) +<{(<{((([((({({}<>)([]<>)}({()<>}[{}<>])){(<[][]><()[]>)<[{}[]]<[][]>>})[[{{<>{}}(<><>)}{[()()][()[]]}]])[[{ +{<[<[[[(({(<<{[][]}(<>{})>{<[]()><()()>}>(({[]<>}{()<>}){[()()]}))})<({{<(<>{})>[(<>[]){() +([[[{{[<([<<({[]{}}{(){}})(<[]<>><[]<>>)>{{{[]}{[]{}}}}><[[[<><>]{{}[]}]]<<[{}{}][(){}]>{<()[]>[{}[]]}>>] +{[[[<<({([<<(((){})<{}()>)[({}[])<()[]>]><<[[]{}]<<>()>>{(<>())}>](<<{[]{}}[<>()]>>[{({}<>)[()[]]} +[<{{<({<<[(<<<{}<>>{[]{}}><((){})((){})>>(<[[]<>][(){}]>[<{}()>({}<>)]])]<[<{[[]()]({}[])}>(< +<{(({{(({({<[<{}<>><[][]>]<{(){}}>>}[(<[[]{}](()())>[[()()][{}[]]])])<[(<[<><>](()[])><{{}{} +[[(({(({([[<{<{}<>>{(){}}}{(<>[])<[]()>}>][[[<[]()>[()]][<<>{}><{}()>]]([([][])])]]<({[[<>[]][{}<>]]<{ +<<[{{{<<[<{<<[<><>]{[]}>>{<([]<>)<<>[]>>[(<>{})(()())]}}{<[{{}<>}(<>[])]>[[{{}{}}<{}<>>](([ +((({{[[<[([[<{()<>}({}{})><<<>[]><()<>>>]{{<[]{}>[{}()]}(<[]<>><()[]>)}][{{<[][]>}<[()[]]<[]()>>}])]>]]}([{{[ +[([{({<<[{[[{{()()}}[[<>()]([]())]]{{(()<>)[{}()]}{[(){}][<>()]}}]}]>(([{[{[[]{}]{[]<>]}]{{<[]< +<(<(<({{<<([<{[]{}}[{}<>]>{<<><>>{{}{}}}])({(<<><>>((){}))({[]<>}<{}<>>)})>{{[<<()<>><[]()>><((){})<{}<>>>]< +([{<{{[{([{[<[(){}]{(){}}><{[]()}[{}{}]>]({<<>{}>({}[])})}[{[{[]()}<(){}>]({[]{}}{()()})}( +(<[([[(<([([<[[]()]>][<[()<>]([][]}>[(()[])<[]()>]])<{[<[][]>(()[])](([]()))}{[<{}<>>[[]()]]((()<>) +[([((([<<{[({{{}()}(<>())}([()<>]<{}()>))<<(()[])[()<>]>{<()>}>]}{{((({}{})[{}<>])[{[]()}([]{})])<<{<>[]}( +<([{(<<[(<({<<{}{}>{{}[]})<<<>[]><[]<>>>}(<([]<>)<(){}>>[[{}{}]<[]{}>]))>{([<<{}{}>>({[]()})])})]( +[(<[((<{[(((([[]]<<>[]>){<{}<>><()()>}))<[[{{}()}<<>()>]]>)]<<(<{(<>{})}{({}<>){{}()}}>([{()<>}{{}() +<{(<<[{<<[({<<{}{}><()[]>>}){[([{}{}]([]{}])(((){})[[]<>])]<<<()<>><<><>>>([{}()]<[][]>)>}]>>(<(<({({} +(([{{<{{(<{[{{<>[]}}({{}[]}<(){}>)]{(([]())[()<>])<[[]][[]()]>}}{<({(){}})<[{}{}]>>[((()()))]} +[(({[<{([{[[{[[]()]<[]{}>}]{{<{}[]>{<>{}}}{(()<>)<()<>>}}][<<<{}()>(()())>>]}(([{{<>[]}[(){}]}[([]<>)< +[<(<{{(<[(<<({()<>}<()()>)([<><>]<(){}>)>>)]>{<<[([[(){})<<>[]>])({<()>(()[])})][[({{}}(<>())){ +(((<<[<(([<[<[()()][{}()]>[([]())[[]()]]][<<<><>>{<><>}>{<<>[]>[<>()]}]>}{<{[[()[]]<()()>]{[[][]][[] +{{(<<{(<[<({{<<>()>{{}[]}}([[]{}]<[][]>)}<(({}()){()()})>)>({<[[<><>]{[]}]{(<>())([]{})}>[[<<>[]>({} +<<<[[[<[<{[[<({}())[[]()]><{(){}}{{}}>]][[[[{}[]]<(){}>]]<[{()()}<[]{}]]<(<>())[<><>]>>]}<[[({<>}<{}{}>)< +(<(<[<<{<[({([[]{}]{[]}){[(){}](<>{})}}{[[[]][[]()]]})<[<[<>()]{{}()}>][((()[]){(){}})[[() +<[{{{({(([{[{<{}<>>}<[{}<>]>]}]<([{{(){}}{[][]}}[<<>()><{}<>>]][{<{}[]>{()()}}])[({[{}<>][[][]]}{[{ +<[<{[([<<[[<[<()[]>[<>{}]]{([][]){[]<>}}>({{()<>}[()<>]}[{<>()}{<>{}}])]<<{({}())(<>)}[[()()][[]{}]]>>]>><( +{[{<([(<<[[<[[<>{}}{{}{}}][{{}{}}[{}{}]]>][{([[][]](()[]))(([][])(()<>))}{[<<>{}>{[]}]{((){})(<>)}}]]{[<<< +(({<<[<[<(<[<[<>{}]([]{})>(<[]<>><()>)][<[<>()]{()[]}>]>)[[[({{}{}){<><>})[(<>()){{}<>}]]]<<[<()[]>([][])]<<[ +<[({<<<<[{<<([{}[]][[]<>])[({}())<()<>>]>{<{{}<>}{[]}>[([]())({}()))}>}]{[<<<(<>())(()<>)>>{[<<><>>({}{})][ +<<((<{<{[{<<{<()<>>{<>{}}}>>}{<{{<<>>(()())}<{[]()}[()<>]>}{[<()>{<><>}]}>([<{[]<>}[{}<>]>{[[]<>]<< +([<[<[<<{{{{{(()<>)[<>()]}[({}{})[{}{}]]}(((()()))<{<><>}>)}<[<[<>()]{<><>}>[{<><>}{()}]]( +[([[((<({{([[([]{}){{}<>}]([[]()]<<>[]>)][[<()()>{{}}]<((){})[<>()]>])<<{<[][]>}[[[]<>]<{}<>>]>{{({}[])( +<(<<[<{{[([([(<>()}{()()}][{{}{}}[<>]]){<{<><>}<[]<>>>{{<><>}[[]()]}}]<<[([]<>)<()[]>]><[<[][]>]<[{}{}]{{}[]} +[({(({(({{([{[{}()]<()<>>}(([][])({}))])({(<{}()>)}{{([]<>)(<>{})}})}({<[<{}<>>{[][]}]({<>[]}<{ +[([[<[[{{<(<{<()<>><<>()]}<<[]{}>([]())>><<[[]<>]{{}<>}>>)><([<({}())>((<>{})[(){}])][<<[]()>( +[[(((<{({<<(<({}[])({}[])>[{[]{}}{<>{}}])[<<[][]>{<><>}>([[]()](<>[]))]>({<<<><>><[]()>>[([][])[[]{}]]})>}< +{[<(<<[{{{{<<{{}{}}{()<>}>(<[]()>({}<>))>}({<{{}()}{()()}>(<[]{}>({}<>))}([[<>()]({}())}(( +{[{([{<([({{{[{}<>){{}}}(<<>{}>{<><>})}[<<()()><<>[]>>]})(<<{([]<>)<{}()>}>>)])(<{<((<()[]>{{}{}})){({[]<>}( +<[[<(<{{{{{((<()<>>(<>{}))([{}{}]<<>[]>))<{<[]>(<>{})}>}}[(((([]<>){<>[]}))(({<>()}({}[]))))]}([{[<{[] +{<({[{[{<{{{[[<><>]{{}<>})<<[]()>([])>}[{{<>[]}{()<>}}]}<<{<{}<>>({}<>)}[<{}()>[[]<>]]>[<<<>{}>[(){} +<({({{([<[[[<{<>{}}([])>](<{[]}<[]<>>>)]]>)<[[<([<{}[]>[()()]]<(<>{})<{}[]>>){{({}<>)[[]<>]}<{{}{}}{()< +<{<{({<<{{{(<{[]{}}(()())>({<>[]}(<>[])))<{([][]){{}()}}(<{}[]>)>}{[<<()<>><{}[]>>{{[]{}}[[]() +{[<({<<{[(<{([{}[]]<[]<>>)<<{}[]>[()()]>}<<{()[]}{<>{}}>[[{}()]([]<>)]>>({[([]{})]<<[]{}><[][]>>}))][{[[<{[] +[<<{<({<(<{{[{()()}<[][]>]{{<><>}({}{})}}}<<(<()[]>{[][]})[[{}()]]><[<<>[]>(()<>)][(<>{})([]())]>>>[[<({ +{{((([[<[<[[[{()<>}(<><>)](<{}()>({}()))][{<{}()>[{}[]]}{<{}[]>[{}<>]}]]({{[<>[]]<<><>>}([<>]{[]})}([({ +<<([[{[{[<(({[{}{}]<()<>}}[(<>{})[(){}]])([[{}{}]]{[()<>]<()<>>})){{[([][])<()[]>]}}>]((<(<({ +(<({[{[{(<<<<([]<>)<[]()>><{()[]}{{}()}>>{{[[]()](<><>)}({<><>}{<>{}})}>{([((){})(<>())]{<<><>>({}()}})([[{} +<<<{(([(((({{<()())}<((){})[<>{}]>})(<[{<>{}}<{}()>]([{}[]][{}()])>[{{[]{}}{[]<>}}{{<><>}({}<>)}]) +(<{{[{{<([[[[([]()){(){}}][<[]()>{<><>}]]{[([])(<><>)]}]])((<[[({}{})[[]{}]]<({}){()}>][[( +{(((({([<[<<<[()()](<>{})>[{{}[]}<<>{}>]>>([{<{}()>[[]()]}{({}())<{}()>}])]{({<{[]{}}<[][]>><[[] +(<{[[<{{[((((<(){}><{}[]>)<{()()}<{}()>>)<<[(){}]>>}){(({{[]()}}<<<>[]>([]<>)>){{[<>{}]{[]() +<<{{{<<[{<[<[{[]{}}[{}<>]]<([]{})[<>{}]>>({[()()]}(([][])[<>()]>)][{<<<><>>(()<>)>[{<>()}<[]{ +[[<[[[<([((({{{}<>}[()[]]}{([]<>)<{}()>})[(<<>()>{<><>})]))<[({(()()){{}<>}}(({}())<[]{}>))({<<>{}>[[]{}]}( +{[[({[(<<{<((<{}()>({}<>))<<{}()><{}[]>>)>(<[<()()>(<>)]{([][])[(){}]}){([{}()]({}()))[[() +(<({[{<{<({{{({}<>)}{[()<>]<{}{}>}}{<<{}{}>([]<>)>(<{}<>>{<>{}})}}{[{<(){}>><[[]()](<>())> +[<{(({[{[<(<({[]{}}[<>])[{()()}(()<>)]>{{[[]{}]}<({}[])[{}{}]>})[{[[()]{<>{}}][<()[]>[[]()]] +(<[{{<(<({{{({[][]}{()()}){([]())<[]{}>}}}{(<<{}()>{{}{}}>[(<>[])])}]{[<[<(){}><{}<>>]<(<>[])(()[])>>[[{() +<<[<{[(<{({(((<><>))[[{}{}}(()())])<{<()[]>}>})<[{<<{}<>>(()())>[<<>[]>[{}()]]}{{<{}()>[<>{} +<({((<([{{<[(<{}[]><<>()>)(({}()){[]{}})]{{<()[]>[()<>]}<{()[]}{[]<>}>}><<{((){})[{}<>]}{(()[] +<{{[[<{<{<{{{[[]<>]}{{{}{}}<[]()>}}[<{()<>}(()())><<{}<>><()[]>>]}<([[<>()]([]{})]{{()}{<>[]}}){<<[]<>>>(( +<<({<{{[<{(((<(){}>{[]<>}){[(){}]})<({[]})[<()<>>{{}<>}]>)}><[({{<[][]>}}<{{[][]}<[]{}>}>>([(<{}<>>(()( +{<([{((({<((<<[]{}><{}<>>>[{{}()}[[][]]]))[<{[{}[]][<>[]]}>[<<{}()>{{}<>}>((<><>))]]>}(((<([[][]){[]( +[[<{(<(([<[{(<{}[]>[{}<>])<<{}<>><<>()>>]<{[()<>]{{}{}}}{<<><>>[[]{}]}>]<(<{()()}<<>[]>><({ +([{<(<<<([[{[[[]()]({}[])]({[]{}}(<>[]))}<{[[]{}]({}())}>]{([([][])[{}<>]]([<>()]<()[]>))}](<({({}[])((){})} +[<[{{[[[[[<({({}<>){{}{}}}{{[]<>}{<>}}){((<>())<[]<>>)[<()>[{}[]]]}>[{[[{}<>]]{{()<>}<()()>}>[[{[]< +(<{{{<([([([[([]())<[]{}>]<<()><[]()>>])])(<{[<(<>())<{}>>]<{[{}<>]<{}[]>}(([]<>)(<>()))>}(({<<>{}>[[] +{[(<[<{<<[{<<(()<>)<(){}>>[[<>[]](<>())]>{{[[]()][<>[]]}({{}[]}<<><>>)}}<<(([]<>)[()()])[[()<>][ +(<<[(([<([<<[[()[]]([]<>)]{(<>())([][])}>>{((<{}[]>{<>{}})[<<>()>])}]{([{{()()}<<>()]}{[[][]][(){}]}] +{{<[<(((<(<{{([]())<{}()>}<<<>[]>{{}<>}>}<<<[]{}>>(<[]{}>({}<>))>><<{[(){}][[]()]}{{<>[]}([] +[{{<[[([{[({[({}<>){{}()}]([{}<>]({}()))}(({()<>}<[][]>)({{}{}}{<>[]})))([([()[]][[]{}])]<<([ +([<(({<<(<([<([]()){(){}}>[([]{}){()[]})]{{(<>())(<>[])}<[<>[]][{}[]]>})><[[{(()<>){{}()}}{ +<((<<[{[[[[{[[<>{}][[]()]]{{[][]}<[][]>}>]]{({{[()[]]<{}[]>}<[<><>]<[]<>>>}<<<()<>>>[{()()}]>)(<<{<>()}>([{} +([(([{([((<{<([]<>)<{}<>>>(<{}{}>)}{{[{}]{<><>}}[[{}<>]([][]}]}>){<<<[{}()][{}{}]>>>[[{<{}{}>[{}[]]}]<[ +{[[(<([([{([<[()[]]<(){}>>[(<><>)[()()]]])}<{[[(<>[])([]{}>]{[[]<>]<{}[]>}]}[(((()<>)<()[]> +{{<(({([<<<<{([]()){()[]}}({()()})><(<()[]>[<>[]]}({<>{}}{[][]})>>{({<()[]>[{}<>]}((()[])(()<>)))}>>[( +{([[(({{{[[{<[{}()]<<>()>>[[()<>]{[]()}]}<[<{}()>{{}()}]{[[]<>](<>())}>]{({<(){}>(<>{})}{<[]>[[]()]}){<( +(<{<[(<[[<[{[[(){}][()()]]<[()<>]((){})>}[(<(){}>{<>{}>)]]{({{()}}{[{}<>]<<><>>})(({<>()}<{}()>)[{()}<< +[([([<{<((([[<[][]>{[]()}][<()<>><{}()>]]({[{}()]}[<()()>{[]<>}]))<[(<()()><()[]>){{[]<>}[()()]}] +{([<({[<((({(<{}<>>[{}()])<[<>{}]>})){{<<<[]()><[]<>>>{{<>()}[<>()]}>{[<()[]>[(){}]]})})<({ +<<<[[[((<{[{[([])[()<>]]((<>{}}(()()))}<{<[][]><[]()>}{<(){}>{{}<>}}>]<([<<>>]<([][])({}{})> +<[[({[<([{<<<(()[])>{{(){}}<[]<>>)>>[{{{()<>}[{}()]}<{()[]}[()<>]>}<<{()()}([]())>[(<>()){<>{}}]>]}])[((((<{ -- cgit v1.2.3