From 375f5b5919dabad2fa5c575d8c3183b05d58cc1f Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 10 Dec 2021 20:36:30 -0500 Subject: [PATCH 1/4] Adding Day 10 Part 1 --- inputs/day10-example.txt | 10 ++++ inputs/day10.txt | 102 +++++++++++++++++++++++++++++++++++++++ puzzles/day10.py | 94 ++++++++++++++++++++++++++++++++++++ tests/test_day10.py | 38 +++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 inputs/day10-example.txt create mode 100644 inputs/day10.txt create mode 100644 puzzles/day10.py create mode 100644 tests/test_day10.py diff --git a/inputs/day10-example.txt b/inputs/day10-example.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/inputs/day10-example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/inputs/day10.txt b/inputs/day10.txt new file mode 100644 index 0000000..ca048fc --- /dev/null +++ b/inputs/day10.txt @@ -0,0 +1,102 @@ +[<<{[{([([({<{<>{}}>[[[]{}][[]<>])}{[(())][[<>{}]{[]{}}]})](([{<<>{}>[()<>]}<<<>()>{{}<>}>]{[{{}()}[(){}]]}) +[({[[(<{[<[<{({}[])(<>{})}(([]<>)[[]()])>{[<{}()><{}{}>][<[]()>({}())]}]<(<(()<>){{}{}}>({()()}(()[])))>>[([ +(<([[<(<<{((<<<>{}>{{}{}}>({()<>}{()<>}))<[(<><>)<<>()>]{((){}){[]<>}}>)}({<[(()[]){()[]}]{(( +<[(<{[{{([([{{{}<>}}{{<>()}<<>>}]){[[<[]<>>(())]<<<>{}>(())>][<<{}{}>(<>())>{<{}{}><{}<>>}]}]<<({{() +{(((<{{{<<{({{()[]){()[]}}({{}}[<>[]])){[{()<>}<()()>]}}>{[[{<[]{}>{{}()}}<<()[]>[{}[]]>]{[{(){}}(()[])]<({ +<(<{<<{{([[({{<>()}[(){}>}[(<>)(()[])])([[<>()]<[]()>])]{(<(<>()){[][]}>[((){}){[]{}}])[[{[]<>}({} +((<[<(([[{[{{[{}()]{<>[]}}}]{[{<()()>[{}[]]}[<[]{}>]]{{{<><>}[()[]]}<{{}{}}[<>()]>}}}[{{<{[][]}({}())><{ +[([{({[({<[[<([][])<(){}>><<<><>>[[]()]>]<<({}{}))[<<>[]>]>]{[(([]<>){[]<>}){[{}{}](()())}]}>[<<{<[][ +({{{(<{({((<({<>()})[(<>[])<{}()>]>{{<[]()><{}[])}[{[]{}}({}{})]})){{{[<(){}><[]<>>][<{}()>{(){} +(<[<[<{[<{{[[([]{})]](([{}{}]({}[])){<()()>({}())})}[((<{}()>{[][]}))[[<()[]><()[]>>([[]<>]([]{}))]]}({[ +<{{[<[<<(<[[([[]<>]({}<>))][[({}<>){{}()}]{<(){}>(()[])}]]>{[([[[]{}][{}()]][({}{})((){})])({({} +([{{[{{[<<[([([]())<[][]>]({<>()}{[]()}))<(<<>{}>{()})>][[[{<>[]}[(){}]][[()[]]({}())]][(<[]{} +<[{<[<[<{[<([[{}()]]){<{{}<>}[{}<>]>([{}{}]<[][]>)}>][<([{(){}}<<><>>]<<[]{}>[()<>]>)><{[([]( +<([[([((<[{<<<()<>><[][]>>{({}[])({}{})}>({({}<>){[]}})}<(<{[]{}}{()<>}>([(){}][()<>]))[{[{}]<{}()>}<{{} +[{{[([{{{{(({({}<>)<[][]>})([{<>()]<<>{}>]{[()()]}))({{<(){}>{<>{}}}}([<()<>>[{}{}]]{{[]{}}(()[])}))}{(((<[] +<{{{([<[{((<<<[]{}>{()[]}>{(()[])[{}()]}>[[(<><>)][[(){}]{[]<>}]])({{((){})<[][]>}[<[]()>(<>{})]}<([{}< +<[(((<{(({[{[[[]()][[]<>]]<{<><>}[()()]>}][{[[{}{}]<{}()>]<([]()){<>[])>}[(<{}<>>({}()))(<<><>>((){} +([{{[[((<{([<<()[]>{[][]}>{[[]{}]<{}()>]]{<<()<>>>{({}<>)((){})}})<[{[<>()](<>[])}]{({()}({} +(({[{(({{{<{(({}{}){[]<>})<{[][]}>}<<{{}[]}<{}}>{{()[]}{<>[]}}>>}(<<[([]{})<<>()>]<(()<>)<{}<>>>>>[{{(()[]){{ +<[{<<<(([{{(<[[]()][{}[]]>[[{}()]<{}()>])<[[[]]{[][]}][(()[]}]>}}][{({<<<><>>>[<{}[]>({}{})]}<(([])<{}{}>)>) +{<(<[(({<[<{(({}){()<>}}}([[<>()](()[])])>((<[[]{}]{[]<>}>({[]<>}[[]<>])))]>}({{{[{{<>()}[(){ +[{<{{{[<[[<<<[<>]>[[[][]][()<>]]>{<{(){}}<()[]>>([()[]](<>))}>[{[{<>{}}[[][]]][[[][]][<>[]])}<({{}()}{(){}}){ +<<<<(([((<(([{<>()}<[]{}>]))({({{}[]}<(){}>)[(()())<{}<>>]}<{<[]()>{()[]}}([<><>]([]{}))>)>[<{<<()[]>[()<> +<{<{<((({[<{(<{}()><()[]>)<[{}()]<<>()]>}<{(<>())[[]<>]}[(<>[])[<>[]]]>>]<<<<([]{})(()())>{({}( +(<[(({[(<{(([({}{})(<><>)]<{[]}(())>)<([(){}])>)(<(((){})<<>])([{}{}][{}{}])><[<()()>[{}()]]{(( +<{<(<([<{[<{[{<>()}{<>[]}]}<<[[]()]({}<>)><[[][]]([]{})>>>[[[{<>{}}(()())]({()<>}<{}{}>)]] +{[<[(<((<{<[[<[]()>[[]()]]([{}()]<<>>)]([([][])[(){}]])><{({<>[]}[[]<>])<{<>()}<()()>>}((<{}()>)<(<>[]){[]() +<{<[{((<[{([[<<><>>(()<>)](([]()){[]()})]<{(<><>){(){}}}>)}<(([{()()}<<>[]>])(<<<>{}>(()())><<{}{}><{}[]>> +(<([{<<[<({({[<>{}](<>{})}((<>{})<{}{}>))[<[()](<>())>{[[]()]<()<>>}]})[{({(()[]){[]{}}}[[[]()] +{({<(<{{[<(<([<>[]][{}{}])[[{}<>]{[][]}]>[[((){})[<>()]]({[]<>}<{}()>)]){[([[][]]<[]{}>)[[{}()]]][[([][]){[ +({[<{({{(<{[{<{}[]>{<><>}}<<<>[]>([]())>](<(<><>)<[]{}>><{<>()}<()<>>>]}<{[([])<{}<>>][<[]<>> +[{<[<{<({{(((<{}[]>{[]{}}){<<>[]>[<>[]]}))<[(<<>[]>{[]{}})[<{}[]>{<>{}}]]>}{([{[<>{}]([][])}<{()()}{<><> +{[([{<([{{<[<{(){}}[()<>]><[[]][[]<>]>]([<()<>>({}<>)][<{}()>[<><>]])><<{<()()><<>()>}[[<>[] +<{({[{{([({(({[]{}}<{}{}>)({[][]}<[][]>)){{[()<>]<<><>>}{<{}{}>}}}{({[[]()](<>())}{[()[]]}>[<{()}{[] +<{<({([{{{((<{<>}<[]()>><[()()][[]<>]>)([{()<>}{<>[]}](<<>()>([]{})]))(<{<(){}>}([<>[]](()[]))>[ +[{[({({<(([({{{}<>}}[<[]<>]({}{})]){({()()}{{}[]})}]{[[[()<>]<<>[]>][([][])]]})<[<<([]<>)([]{})><{< +<[[[({{{([{({<()[]>(<><>)}<{<>}([]())>)([[{}[]]<<>()>]([<>()][[]<>]))}<{<(()[])>>>]<<<{{()()}[()<>]}{([] +[<[({<<<<<<[{{{}{}}{[]<>}}[<{}{}>(<>[])]]({<<>[]><{}()>})><<<{<>()}[()<>]>[<<>[]><<>{}>]>{<{[]<>}[() +([[<<(((<[(({({}{})<[]{}>})<<<{}[]>{()<>}><{[]()}>>)((<{<>()}[<>[]]>[<{}()><{}[]}]))]{<[({(){}} +[({<(<<[{<[[<(<><>)[{}]><([][])[{}<>]>]{([<>[]]({}()))[{[]<>}([]<>)]}]>{<(<[[][]]<[]()>>{(() +{({(([({{{(<({()()}<<>()>)[(<>[])[{}{}]]>[{[()[]]([][]>}[{(){}}<{}<>>]])[(<{[][]}(<>{})>{[[]<> +(<{([(((<{[<{[<><>]{()[]}}<<[]>>><{({}<>)(<>[])}{<{}[]>[[]{})}>]({<{{}<>}(()[])>{([][])[{} +<<(<[({{[<<({{[]()}})<(([][])([]()))>){([(<>{})][<{}<>>{{}[]}])[(({}())(<><>))<<[]><<>[]>>]}>({[{{<>}(<>[])}{ +[{[[<<<{(([([({}<>)[<>()]]<(<>())>){([<>][<>()]){{(){}}[<>{}]}}]([<{{}{}}<{}<>>><{<><>}>]<(<<>[]><{}[]>){[ +<<{<[([{{[[[{<<>()>({}())}<<(){}>[{}()]>]](([{[]{}}[<>()]][[[]()](()[])]){[[{}()]<[]>][(<><>}]})]}[< +<[(<<(({<{((([<><>]<(){}>)<{{}}({}())>)<[<()<>>[()[]]]({[]}{[]()})>)}><<[(((()<>))<(<><>)>)]([((()<>){{}{}} +{({({<<[(<{{<{(){}}<[][]>>[[<>()]({}<>)]}}><<(<{[]<>}([][])>[<{}<>>([]())]){(<()[]>([]()))}>[({[()<> +<([({{<{([<<[[{}()]][<()[]><<>{}>]>([<{}[]><()[]>]<{{}()}[[]{}]>)>{<(<(){}>)({{}{}}{{}<>})>{([(){}][[][]])[{ +<<<<(([<([[{{[{}{}]{<>[]}}[[{}{}]<[]{}>]}[({[]()}[[]<>])[[[]{}]<<>()>]]](<{<[][]}{<>()}}{{[][]}}>{ +{[[<[{<{<((<<([]())({}())><(<><>)<{}<>>>)<[{{}[]}][<{}<>>([]{})]>)<{[<{}[]>(<><>)]<<(){}><[]{}>>}{<<<>{}>> +<(<<((({{[({<<{}[]>{<>[]}>{({}{}){{}[]}}}{[[()()](<>{})]{{<><>}<<>[]>}}){<[{()()}<()[]>][[{}{ +[<([<<{<<[<<([()[]]({}[]))<{<>()}>>{[<()<>>[{}()]]<{()[]}{(){}>>}>]>><[([([<[]<>>{<>{}}]<([][]){<>{}}>) +[{{([{([(({<{({}[])(()())}{{{}<>}[()()]}>[[[<>[]]([][])]({<><>}<{}{}>)]}[[<{()<>}[<>()]>[<{}{}>[()()]]]{([( +<({[(([[({((<({}[])([]<>)>{<()[]>([]{})}){{([][]){[]{}}}[({}{})[{}]]})<{<{[][]}([]())><[[][]]{<><>}>}<< +[<<((<<[<{{{{<{}[]>[{}[]]}{{<>{}}{<>[]}}}[{(<>[])({}())}{<<>()>(())}]}{<{(()()){()<>}}><[([]())]<<()[]>[( +([[(<<[{[({(<<[]<>><{}>>({<>[]}([]())))[((<>[]){()()}){{[]<>}[[]()]}]}([({[]()}[[]{}])[<{}()>{[]<>} +{(<((<{([<<{{{()[]}<(){}>>[[[]()]<{}<>>]}<([[]<>]){(<>{})<()[]>}>>>{(({[<>()]([]())}))}]([<{({{ +[[[[<([{[[{<[{()}(<>[])]>(<[[]{}]<{}{}>>)}]<<((<(){}>{{}[]})[([]{})[[][]]]){{<(){}><[]()>}{(()<>)[[]<>]} +<([{<<[<{{([(<<>()>(<>[]))<[<>[]]>](<({}())<()()>>)>{{{[<>[]]{<>{}}}({(){}}{{}()})}}}({{([()[]][[][]])[ +<<({({{<(<(([<[]()>[{}<>]]<({}){()[]}>){[<[]{}>(()<>)]}){[{{<>{}}{()[]}}((<>[]))]{(<{}()>{(){}])[(<>{})< +(<<({(<<([(([<<><>>([][])]<[<>[]][()]>)<(<[]<>>[<>{}])>)(([({}()){[]()}]((()[])(<>()))){<<{}<>>{[]{}}>{[( +([[[{{[<{(({{(()<>){<>{}}}{[{}<>](<>[])}}{{<<><>>{{}[]}}([<><>]{<>()})}))([<([[]<>])>]({[<()[]>][ +<<(([([[{({([<<><>><[]{}>][(()())(()[])])<([{}()][{}[]])<((){}){[]()}>>}<{[{[]()}[[]()]]}>)[<((<[]{}>)((<>[]) +{<<{[[((<({<{<<>{}>}[(()<>)[{}[]]]>[([<>()]({}()))]}<<<<<>[]><<>>>>{(([]{})[()()])}>)((<((()[])(()<> +<{<{<{<[({(<[(<>[]){<>{}}]{(()[])([]{})}>)}<[({<<>()><{}()>})(([()<>}[[]{}])<{{}<>}>)]>)<(<{{{[]{}}<<>{}> +<[[{(<{(<([<<{{}{}}[[]<>]>><{{(){}}<()<>>}{<{}{}>{[]{}}}>])>{{<{([{}()][[][]])<([][])({}<>)>}>{ +({({<<[{[[{{[{{}()}{()[]}]{[{}[]](()())}}<[([][]){()[]}]<[[]()]((){})>>}]<[(<{[]{}>{{}<>}>{ +{{[<[((([<[([[()[]]{()())]<({}{}){[]{}}>)]>][<<({<(){}>({}{})}{<<>[]>[()]})<(<(){}>{[]()}){ +(({[((<(<({([(()()){<>[]}]){({{}<>}{{}[]})}})[({({{}<>}[{}()]){{[]()}<<>{}>}}<<<()()>[<>{}]>{<{}{}>}>)]>[[ +((<[[((<[(({[{<>()}(<>[])]}{[(()())[[][]>][<{}<>>{<><>}]})([{{<><>}({}<>)}]{(<[]<>>(()[])){<{}< +<[[{[<[<{<<<<([]{})[<><>]>(({}())(()<>))><([[]{}][[]()])<{[][]}[<>{}]>>>(({(()[]][[][]]}){{<<><>>{[][]}}} +[<[{{[{[<<{(<([][]){[]<>}><<[][]>([]<>)])<<<[]{}><<>[]>>([{}<>]<[][]>)>}>><{([({<><>}<()()>)<({ +{<{([<{(((<{<{[][]}{()()}>(((){}){()()})}>)[({<<<><>){<>()}>((()[]){()[]})}{({<>()}{{}()})({<>{} +[[[(<<{({[{[[<[]<>>[{}()]][[{}[]][{}()]]][[[()<>]([][])]<<()>([]<>)>]}[({[<>]{()<>}}(<<>()><(){}>))[<({}[ +<[{({<{[<[(<({{}[]}<<>()>)([{}<>]{[]{}})>)[(<{()}<()>>{[()<>][()[]]})<[[()()][{}<>]])]]<[[{(<>[]){<>()}} +<((<{[{{<{([{<()()>(()[])}{{[]<>}[[]{}]}](((<>())){{[][]}<{}[]>}))(<{((){})[<>{}]}[[<>()]<<>[]>]><{({}<>)({ +{<[[<(([<[[[{{[]()}{<>{}}}<<<>[]>>][(({}{}){<>[]})[(<>{}){()<>}]]]<{<((){})([][])>}<[((){})[[]{} +([[{[[<{<((<(<<>>)>){((<{}<>>{<>()})){([{}[]}{<>()})<<{}()><<><>>>}})<{{([(){}])<<{}[]>{[]<>}>}<<<()<>>[[ +<[([[[[[{((<{<(){}><<><>>}[{[]<>}[<><>]]>[({()[]}{<>[])){{{}<>}[[]{}]}])<[([[]<>][{}])([[]()][{}()])]<<{[ +[{[<[<(<[([[([<>[]]<(){}>){(<><>)<{}<>>}]<[[[]()][<><>]]{{[]<>}(()[])}>]){{<{{[]{}}}((<>()){[][]}) +{{([[({{(<<(<[{}[]]<<>[]>><{{}<>}{(){}}>)>>)}})((<{<{<[([][])][[<>{}]<()[]>])}<([[{}[]]{[]()}]{ +<({{{{{{<(<<[<()()>([]<>)](<[]()>[<>{}])>(<{<><>}(<>[])>)>[({(<>())<<>{}>})])><<<[{[[]()]<{ +{[([(<([[({<({{}[]}{{}()})<<{}<>>{<>{}}>>})([([{[][]){{}<>}]{{<>}[()[]]})((<(){}>[[]()])[[[][]]([]<>)])])]{ +{(<[<{[[{({<[{<>[]}<()<>>](<{}()><{}{}>)><<({}<>)<{}()>>([<><>][{}()])>})}<[{<([()[]])>}]>](<({(<(<>())(<>< +({<(({{{[[{<([[]<>])>(<{<>}>[[()()][()()]])}]]}<<[<[<<{}[]>((){})>{([]{})}]><<{<{}()>{[]()}}{{()()}< +<{<<(<[<{[<[{<{}{}>[<>{}]}[{[]<>}{<><>}]]{{<()<>>}[<{}()><{}()>]]>]}>]>)>>}<<[<(([[[<((<(){}>){([]{} +<<(<[[<[[[<<({<>{}}{()})><[([][]){[]<>}][[()[]]]>>]]{[{(<(<>{})({}{})>)}]({[({{}<>}([]<>))([()[] +<[[(({[<(<{([(()()){()[]}]<(()[]){<>()}>)<[<(){}><[]{}>]({[][]}<[]{}>)>}[<[(<>)<<>{}>)>({{<>{}}(() +([[({[<<{(<[<{{}{}}<{}{}>>{({}())(()<>)}]>[([{[]}(<>{})])])}>>]{[{[<{<(<<>{}><{}[]>)><({[]<>}<{}<> +{(<(<[<{[<{<({()[]}){({})}>}{[<[{}[]](<>())>{[{}<>](()[])}]}>]{{<[[[<><>]]](<(<>{})><{[]()}>)>}{[<{[{}{}](( +{{{(({({<[[[[[<>{}]<[]{}>][((){}){{}{}}]][[([]<>)<[]{}>]{({}())(<>[])}]][<((()<>)[()])><{[{}[]]{[][]}}{{ +{<[({{([({(([<{}[]>{()[]}]){<[[][]][<>{}]>[{<>[]}({})]})}{[{[{()()}{<>()}]}{[([]{})<<><>>]}]})<< +{<<<{{({{<([<({}{})<<>{}>><[[][]]({}<>)>])>([{([{}()])}([<(){}>{{}()}](<<>{}>(<>[])))](<([< +<{{{<({[[{{<<[(){}]<[]<>>>{{{}()}([]{})}>}<<[<<>{}><{}[]>]([(){}][[]{}])><[{{}{}}<{}<>>][<[][]> +((<(<<{<(<{[[{[][]}]{{()<>}<<>{}>}](<[[]<>]<{}()>>((()())[<>{}]))}[([(<>{})<()[]>]<[[][]]<[]{} +{[<{[[{<[((<<<<>[]>[[]{}]>({{}[]>)>)<{[[[]{}]<{}{}>]<([][])(()<>)>}>)](<[[<[<>()]({}[])>]]>({{<<[]{}>{[] +<[{{[(<<([[[[<<>()><{}<>>](((){}))]([<{}{}>[<>()]](<[]()>[[]()]))][{{{<>}{[]{}}}{{{}<>}({}<>)}}[{({}{}>{{}<>} +<[<{{([{[{[((([]())({}<>)){[[]()][{}{}]})]({{(()[])[()()]}})>(({[({}())[<>()]]{<<>{}>[<>{}]}})[[{[()<>]<<> +[{([<<[<[{{<<{<>[]}({}[])>>{<{{}{}}[<><>]>([[]<>]<{}[]>)}}{[([()[]](<>))<{{}[]}<(){}>]]}}[[<{[<>[]]{[]< +<[[([<{<{<[([[()[]]])<({[]()}({}()))[(<>{}){<>[]}]>]>(([<{{}()}(()[])>{{{}<>><<>[]>}][[({}<>)[<>[]]]{{ +[<((({((<<[[[{{}<>}{()<>}]]({(<>[])<()<>>}{(()()){{}{}}})]<<(({}<>))>{<<[]()>><[()()]<[]{}>>}>>>)(<<{<{<{ +(<(({(<(<<([<([]<>)[{}{}>>({{}()}[{}[]])]<({[]<>}[{}{}])<([][]){{}{}}>>)>{[(<<[][]><{}()>>{{<>{ diff --git a/puzzles/day10.py b/puzzles/day10.py new file mode 100644 index 0000000..b1ba859 --- /dev/null +++ b/puzzles/day10.py @@ -0,0 +1,94 @@ +""" +Advent of Code 2021 - Day 10 + +Run with: + python puzzles/day10.py inputs/day10.txt +""" + +import pathlib +import sys +from typing import Dict, List, Tuple + +from collections import Counter + + + +class Subsystem: + nav_data = None + open_chars = ['(', '[', '{', '<'] + close_chars = [')', ']', '}', '>'] + + def __init__(self, inputs): + self.nav_data = inputs + self.chunks, self.corrupted_chars = self.validate() + + def validate(self) -> Tuple[Dict, List[str]]: + chunks = {"corrupted": [], "incomplete": [], "complete": []} + corrupted_chars = [] + + for line in self.nav_data: + corrupted = False + + opens = [] + for char in line: + if char in self.open_chars: + opens.append(char) + continue + + open_char = self.open_chars[self.close_chars.index(char)] + if opens[-1] != open_char: + corrupted = True + corrupted_chars.append(char) + break + + del opens[-1] # Clear the open bracket + + if corrupted: + chunks["corrupted"].append(line) + elif len(opens) != 0: + chunks["incomplete"].append(line) + else: + chunks["complete"].append(line) + + return chunks, corrupted_chars + + def score_corrupted(self) -> int: + scores = {")": 3, "]": 57, "}": 1197, ">": 25137,} + return sum([scores[char] for char in self.corrupted_chars]) + + +def part1(inputs: List[int]) -> int: + s = Subsystem(inputs) + return s.score_corrupted() + + +def part2(inputs: List[int]) -> int: + return False + + +def parse(inputs: str) -> List[int]: + """Parse the input string""" + return inputs.split() + + +def solve(path: str) -> Tuple[int, int]: + """Solve the puzzle""" + puzzle_input = parse(pathlib.Path(path).read_text().strip()) + part1_result = part1(puzzle_input) + part2_result = part2(puzzle_input) + + return part1_result, part2_result + + +def main() -> None: + for path in sys.argv[1:]: + print(f"Input File: {path}") + + part1_result, part2_result = solve(path) + + print(f"Part 1 Result: {part1_result}") + print(f"Part 2 Result: {part2_result}") + + +if __name__ == "__main__": + main() diff --git a/tests/test_day10.py b/tests/test_day10.py new file mode 100644 index 0000000..39824ee --- /dev/null +++ b/tests/test_day10.py @@ -0,0 +1,38 @@ +import pathlib +import pytest +import sys + +ROOT_DIR = pathlib.Path(__file__).parent.parent +sys.path.append(f"{ROOT_DIR}/puzzles") +import day10 as aoc + +INPUTS_DIR = f"{ROOT_DIR}/inputs" + +@pytest.fixture +def example_data(): + input_path = f"{INPUTS_DIR}/day10-example.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +@pytest.fixture +def day10_data(): + input_path = f"{INPUTS_DIR}/day10.txt" + return aoc.parse(pathlib.Path(input_path).read_text().strip()) + + +def test_example1(example_data): + assert aoc.part1(example_data) == 26397 + + +@pytest.mark.skip(reason="Not implemented") +def test_example2(example_data): + assert aoc.part2(example_data) == ... + + +def test_part1(day10_data): + assert aoc.part1(day10_data) == 387363 + + +@pytest.mark.skip(reason="Not implemented") +def test_part2(day10_data): + assert aoc.part2(day10_data) == ... -- 2.30.1 From 2b9aa841932eee67ea08463fa9aff940c1865df5 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 10 Dec 2021 20:59:54 -0500 Subject: [PATCH 2/4] Adding Day 10 Part 2 --- puzzles/day10.py | 30 ++++++++++++++++++++++++------ tests/test_day10.py | 6 ++---- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/puzzles/day10.py b/puzzles/day10.py index b1ba859..7df9b1f 100644 --- a/puzzles/day10.py +++ b/puzzles/day10.py @@ -9,7 +9,7 @@ import pathlib import sys from typing import Dict, List, Tuple -from collections import Counter +from statistics import median @@ -18,13 +18,14 @@ class Subsystem: open_chars = ['(', '[', '{', '<'] close_chars = [')', ']', '}', '>'] - def __init__(self, inputs): + def __init__(self, inputs: List[str]) -> None: self.nav_data = inputs - self.chunks, self.corrupted_chars = self.validate() + self.validate() - def validate(self) -> Tuple[Dict, List[str]]: + def validate(self) -> None: chunks = {"corrupted": [], "incomplete": [], "complete": []} corrupted_chars = [] + incomplete_chunks = [] for line in self.nav_data: corrupted = False @@ -47,15 +48,31 @@ class Subsystem: chunks["corrupted"].append(line) elif len(opens) != 0: chunks["incomplete"].append(line) + incomplete_chunks.append(opens) else: chunks["complete"].append(line) - return chunks, corrupted_chars + self.chunks = chunks + self.corrupted_chars = corrupted_chars + self.incomplete_chunks = incomplete_chunks def score_corrupted(self) -> int: scores = {")": 3, "]": 57, "}": 1197, ">": 25137,} return sum([scores[char] for char in self.corrupted_chars]) + def score_incomplete(self) -> int: + scores = {")": 1, "]": 2, "}": 3, ">": 4,} + total_scores = [] + for chunk in self.incomplete_chunks: + total_score = 0 + for char in reversed(chunk): + close_char = self.close_chars[self.open_chars.index(char)] + total_score = total_score * 5 + scores[close_char] + total_scores.append(total_score) + + return median(total_scores) + + def part1(inputs: List[int]) -> int: s = Subsystem(inputs) @@ -63,7 +80,8 @@ def part1(inputs: List[int]) -> int: def part2(inputs: List[int]) -> int: - return False + s = Subsystem(inputs) + return s.score_incomplete() def parse(inputs: str) -> List[int]: diff --git a/tests/test_day10.py b/tests/test_day10.py index 39824ee..81acba1 100644 --- a/tests/test_day10.py +++ b/tests/test_day10.py @@ -24,15 +24,13 @@ def test_example1(example_data): assert aoc.part1(example_data) == 26397 -@pytest.mark.skip(reason="Not implemented") def test_example2(example_data): - assert aoc.part2(example_data) == ... + assert aoc.part2(example_data) == 288957 def test_part1(day10_data): assert aoc.part1(day10_data) == 387363 -@pytest.mark.skip(reason="Not implemented") def test_part2(day10_data): - assert aoc.part2(day10_data) == ... + assert aoc.part2(day10_data) == 4330777059 -- 2.30.1 From a33c9e57caab26108844343a3fc42b19d59a56ed Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 10 Dec 2021 21:01:09 -0500 Subject: [PATCH 3/4] Black formatting --- puzzles/day10.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/puzzles/day10.py b/puzzles/day10.py index 7df9b1f..85dc4af 100644 --- a/puzzles/day10.py +++ b/puzzles/day10.py @@ -12,11 +12,10 @@ from typing import Dict, List, Tuple from statistics import median - class Subsystem: nav_data = None - open_chars = ['(', '[', '{', '<'] - close_chars = [')', ']', '}', '>'] + open_chars = ["(", "[", "{", "<"] + close_chars = [")", "]", "}", ">"] def __init__(self, inputs: List[str]) -> None: self.nav_data = inputs @@ -42,7 +41,7 @@ class Subsystem: corrupted_chars.append(char) break - del opens[-1] # Clear the open bracket + del opens[-1] # Clear the open bracket if corrupted: chunks["corrupted"].append(line) @@ -73,7 +72,6 @@ class Subsystem: return median(total_scores) - def part1(inputs: List[int]) -> int: s = Subsystem(inputs) return s.score_corrupted() -- 2.30.1 From 5fef76f44c5072d26d3522a35428870454ff71c6 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 10 Dec 2021 21:01:20 -0500 Subject: [PATCH 4/4] Readding black to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 1cc18fc..3481db4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +black numpy pytest -- 2.30.1