From d9f6c01f2b5db021dddc6f7e85fd3851c6c1fdd2 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Fri, 10 Dec 2021 13:42:11 +0100 Subject: [PATCH] day 8,9 & 10 --- day_10/code.py | 58 +++++++++++++ day_10/example.txt | 10 +++ day_10/input.txt | 107 +++++++++++++++++++++++ day_8/code.py | 205 +++++++++++++++++++++++++++++++++++++++++++++ day_8/example.txt | 10 +++ day_8/example1.txt | 9 ++ day_8/input.txt | 201 ++++++++++++++++++++++++++++++++++++++++++++ day_9/code.js | 115 +++++++++++++++++++++++++ day_9/example.txt | 5 ++ day_9/input.txt | 101 ++++++++++++++++++++++ 10 files changed, 821 insertions(+) create mode 100644 day_10/code.py create mode 100644 day_10/example.txt create mode 100644 day_10/input.txt create mode 100644 day_8/code.py create mode 100644 day_8/example.txt create mode 100644 day_8/example1.txt create mode 100644 day_8/input.txt create mode 100644 day_9/code.js create mode 100644 day_9/example.txt create mode 100644 day_9/input.txt diff --git a/day_10/code.py b/day_10/code.py new file mode 100644 index 0000000..c4e1ba6 --- /dev/null +++ b/day_10/code.py @@ -0,0 +1,58 @@ +import fileinput + +scores = [ + 3, + 57, + 1197, + 25137 +] + +opening_chars = ['(', '[', '{', '<'] +closing_chars = [')', ']', '}', '>'] + +lines = fileinput.input() + +score = [] +completions = [] +completion_points = [] + +for line in lines: + total = 0 + stack = [] + line = line.strip() + complete = True + for column, c in enumerate(line): + if c in opening_chars: + stack.append(c) + if c in closing_chars: + i = closing_chars.index(c) + if stack[-1] != opening_chars[i]: + expected_i = opening_chars.index(stack[-1]) + # print(line) + + # print(''.join(column*[' ']+['^'])) + # print(f'Expected {closing_chars[expected_i]} but found {c}') + # print('') + score.append(scores[i]) + complete = False + break + stack.pop() + if not complete: continue + # the line is just incomplete + completion = [] + while len(stack) > 0: + i = opening_chars.index(stack.pop()) + char = closing_chars[i] + total = total*5 + (i+1) + completion.append(char) + completion_points.append(total) + print(''.join(completion)) +completion_points.sort() + +print('part 1') +print(score) +print(sum(score)) + +print('part 2') +print(completion_points) +print(completion_points[len(completion_points)//2]) \ No newline at end of file diff --git a/day_10/example.txt b/day_10/example.txt new file mode 100644 index 0000000..2f182d8 --- /dev/null +++ b/day_10/example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] \ No newline at end of file diff --git a/day_10/input.txt b/day_10/input.txt new file mode 100644 index 0000000..897ec77 --- /dev/null +++ b/day_10/input.txt @@ -0,0 +1,107 @@ +{{[<(<<<([{(({<>()}<[]()>)<({}{})(()<>)>){[([]()]{[]()}][<()()>{()[]}]}}[[(<[][]><{}{}>)]<[{{}()} +{<{<{[[[{[{<([[]()](()<>))>([<[]()>])}<{[[<>[]]<{}()>](<{}()>([]()))}<[{[][]}[<><>]][<()()>]>> +<<{[[<([<(<<([(){}])>(<<[]{}>(<>{})>({[]<>}{{}<>}))>)>]{[<[({<<>()>[()())}{([]())[<>[]]})([<(){}>]<<[][]>{{ +{[[[<({{{[{{({<>[]}[()()])[<<>[]>(()<>)]}<{{()()}[()()]}({{}()})>}[{[(<>{}){<><>]]<(()()){{}}>}<{{<>()}[[] +[<{[{[<[{[[({(()<>)}<{[]{}}<()>])<([[]<>][[]<>])(<<>()>{(){}})>]{{(<()[]>[<>{}]){([]{})}}[{{{}{}}(<>{} +[<<<{<<[((({{[{}[]]}}[[<()[]>(<>[])]{({}{})(()[])}]){<{{()()}{{}[]}}<{[]()}>>{<[(){}]><<{}{}>< +{[({[[[{<({[((<>[])([]{})){{{}<>}{(){}}}][<[{}{}]<{}()>><{{}()}<<><>]>]})>([[<[<<><>>{{}()}]<[{} +{(<(<{{(<[(((([]{})<{}[]>))(([[]()]{()<>})(({}{}){<>[]})))]{[([{<>{}}{()<>}]<[{}()]<<><>>> +{<[[[(<[{(<({[{}()]{(){}}}{{()<>}(()[])})({[{}()]{[]}})>)({{[{()()}[[][]])((<><>)<<><>>)}{ +{<{(<[(<{([<[<[][]>[()[]]]<{<>()}([]())>>]([{<<>{}><{}{}>}[[[]{}]<[]{}>]]{({()[]}){({}())((){})}})){([ +<[(((<{{<[[{<(<><>)<<><>>>{{[]<>}<()>}}<(<[]()>{()<>}]{([]<>)[[]{}]}>]](<{{({}<>)[[]()]}[([])]}<[<{ +({[[(<{[({<{<[{}()]([]{})>(({}{})(()<>))}<(<{}[]>({}()))[({}<>)]>>}[[([[(){}]([]<>)][(<><>)({}{}}])<{[{}()]}[ +[{{[(({[{{({(<()[]>{[]<>})}{[({}())]{<()[]>[{}{}]}})<(([()<>]{()()}){([]{})<[]>}){<<()<>>[<><>]>{<<>{} +{{<{[(<(({<{{<(){}>(()<>)}}[<<{}()>{{}{}}>([<>()]{<><>}]]>}(({{<{}()>([]())}{({}())}})<<{{ +{{<<<[{{<[(({[<>{}][[]()]})({[<>()]<<>[]>}(<{}{}>([]<>))))[[((()())[{}{}])[<[]()>[()<>]]]{[({}{}) +(({[({{<([<[{{(){}}<{}[]>}]([({}<>){<>[]}]{({}{})(()())})><{<[{}{}]{[][]}>{[{}{}]{[][]}}}{<[()]<{}{}>> +[{[({<({<<({((<>()))})<{<[[]<>](<><>)>}[{<<>[]>{<>()}}([{}{}]{{}[]})]>>>})>})<[[<[{{(<({<><>}{<><>})<<(){}>>> +{(<<{(<{<[[{<<()[]>{()<>}>}{(<<>()>({}()))}]]>([<[<<{}>[<>{}]>({[]<>}(()[]))]<{{<>()}<{}{}>} +{{(<([[<[({<{{{}{}}(<><>)}<({}<>)<{}[]>>>}[[({()<>}{{}[]}){<{}[]>{{}[]}}]<[{{}()}([]())][({}{})[{ +(<(<<{<[{{((<{{}()}[()]>({{}()}[[][]]))({{()[]}[<>{}]}{<()[]><()<>>}))[{[{[]()}[{}()]]([{}<>])}{<<()[ +[{(<<{[([[({<[{}{}){<>{}}><[()<>]([]{})>}{<[()[]][<><>]>[{[]<>}[<>{}]]})]]{<<[<({}[])>(<<>{}>[[][]])]><([{{}{ +[{([([[<{[[[<{<>[]}{()()}>((()<>))][{[{}()][[]{}]}]][{{(()<>)([][])}{({}{})(<>())}}(([{}{}]){< +<[{{({[[(([{[[{}[]]<<>()>][({}{})]}]<[<([]<>)><{()()}[()[]]>]<{(<>())<(){}>}<[{}<>][{}<>]>>>))<(((<{< +{<{{[{[{<<<{{([][])[<>]}<{{}<>}(<>())>}[<[()<>](<>[])>[[[][]]{()<>}]]>>>}<[[[{[({}())((){})]{(()() +{<{[(<{(<((({<<><>><<><>>})[[{()()}[()()]](<{}()>((){}))]))[{<[({}{})[{}[]]]([[]()])>((({}<>)[[] +[{{(<[[[{{[((<[]()>[[]<>>)[<()[]>])]({<[[]{}][{}{}]>{[[]]{<>{}}}}(<{[]{}}<<>{}>><([]<>){[]{}}>))}{[<{{[]() +{<<<(({{{{(<{{{}[]}}><[{(){}}<[]()>><<()()>({}())>>)<(({<>{}}([][]))<<()[]><{}()>>){[{<><>}[[][]] +{<[({({<<[{{{{[]{}}(()[])}<{(){}}>}<(([][]){[]{}})>}]{[{{<<><>>[[]<>]}}<[([]<>)<{}()>]{{{}{}} +{({<<[(([{(<((()[]))<[{}]>>[<({}[])[{}<>]>[{()()}<[]()>]])<((<(){}>(<>[]))<<<>><{}()>>)[<({}{}){ +<<[<(((<{{{<<{<>{}}<{}()>][([]())[{}()]]>[[{()<>}<{}[]>]<(()()){<>{}}>]}}<{([{<>()}<<><>>]{{()()}} +[(<<<[(([{[<(<()()>)[{<><>}{(){}}]>{{([]())[<>{}]}{{<>[]>[{}()]}}]}]))]>[[[{<((<(<()<>>){{<>[]}<[] +[{(<[((({{([(([]{})<{}()>)<[[]{}]{{}()}>]<{{<>[]}{[]()}}({<>()}[{}{}])>)[{{<[][]><<>[]>}[<{}{}>]}<<[[][]][( +([<(<<[(({[[<<[]{}]>{[()[]](()<>)}][({{}[]}{{}{}}){{{}<>}}]]<{({<>}(()[]))[<()[]>{[]{}}]}[{< +<<[<{{(([([<[[[]{}]<[]<>>][[<><>]{()()}]>]((({{}{}}(<>{})){([][])[{}()]})))<<([(<>)<[]{}>][{{}()} +(<{<([<[{[([(<[]{}>{{}})(([][]){[]{}})]<<({})[()()]>(({}[])[{}<>])>)(<[{{}{}}{{}[]}](<[]{}>[()[]])><<{{}()} +[{({{(({[{[<<[[]()]<<><>>>({()()})><[{<>()}{()<>}]<<()()><{}<>>>>]{(({<>{}}([][]})({{}[]}<<>()>))<<(< +{(<(<{{(((<({((){})[()[]]}{<{}()>{{}()}})[(<{}[]>[{}{}])([()[]]<<>()>)]>{[<[[]<>]<[]>><{{}<>} +{[[<{(<<<[{(((<>[])<{}<>>)<{[]{}}{<>{}}>)}[[{[{}{}][{}<>]}[(<>[])({}())]](<[{}{}]{()<>}>)]]([(((<>{})({} +{(<(({[([([((({}[])[()]))]([((<>())[[][]])])){{({([]())([][])}(<()()>{()[]}))}}]<{<(([{}[]](<>{}))[<[] +({<[[<<(<[(<[((){})]{[(){}][(){}]}><(({}())[<>()>){{()()}}>)(<[<()[]>(<>{})](<[]{}><(){}>)>[({[] +[<<[{{[<[<[[{[(){}]{<>[]}}[(<><>){{}{}}]]{<((){})(()[])>[[[]{}]{{}}]}]{(<{<><>}<{}<>>>)([(()[]){(){}}][ +(<<<((<[<[[[[{{}()}{[]<>}]<{(){}}[[]<>}>][<[[]{}]{{}<>}>{<<><>>([]{})}]]]{{{<{<><>}[{}()]>} +{{<<{[{[<[{(({{}()})(((){})[<>{}]))[[<[]{}>[[]]]{[[]{}]{{}{}}}]})>[{([{(<>[])[<>]}]<<[[]{}][<><>]>{{{}()}({ +[{{{(<{{[<{((<<>{}>[(){}])[(<>[])<[]<>>]][(({}<>)<[][]>)({[][]}{<>{}})]}({{[<>[]]([]<>)}}{( +<[([{[<<<[([(<(){}>{<>[]}){[{}[]]{[]{}}}]{({<><>})[[<>{}][[]{}]]}){<[<<>[]>[<><>]]({[]{}}{<>[]})>[ +[(<(<[({<[{{({()<>})<[()<>}<(){}>>}(({[]<>}(()<>))([()()]{<>}))}<{{([]<>)<<>>}[[{}<>]<[]()>]}<<([ +[[[<([[[[<<<((()<>)<[]<>>)(<{}()>[<>()]))>{((({}()))(<<>()>)){{{<><>}}<<{}<>>>}}><{{({[][]}<{}{}>)[(<><> +<<[({(<<<[{([(<>{})[{}[]]](({}[])))[<(<>[])><<()[]>[{}{}]>]}{([<[][]>{()()}][<[][]><[]<>>])}]>>>)}[{((<<([{(( +{(({[{{<[(<<<<<>()><{}{}>><<{}<>>[{}<>]>>((<()<>>)(<[]{}>[[]{}]))>((((<>())[[][]]){{()()}([][])}){<[< +({{{[(({<[[<[([][])(()[])](<{}<>>{{}()))><<(<>[])<{}()>>(<(){}>[<>()])>]]([<(<{}{}><[]()>)[[<>[]](()[]) +(<<[{[(({[({{{()()}(()<>)}<<<>()><<>[]>>})<{([<><>]<[]()>){<{}()>{(){}}}}>>}[{(<{[{}<>][<>{}]}([()()]({}() +({<<<<([{(([(<<>>{[]<>})][<<[]()>({}{})>])({(<{}[]>){([]<>)([]{})}}<[{[]{}}{<>{}}]>)){<{<[[]<>]([]<>)> +([{[[<((((<[[{<><>}(<>())]<(()<>){[][]}>]<[([])[<><>]](<{}{}}([]()))>>){{{[{<>{}}([]<>)]{[(){} +[{([[(({<[[{{(()[])(<>{})}<[()<>]{{}[]}>}{[<<>>[[]<>]]<[[]<>][<>()]>}]]{{[{<[]{}>((){})}<<<><>>{{}()}>] +(<({(<<({([<[{(){}}[[]()]]>[[[[]{}](<>())]]]{{({(){}}(<>{}))[[<>()]<<>{}>]}({[<>()]}{{[]<>}{[][]}})}]}){({{<[ +{([(((<{<[<{<((){})[<><>]>({[]()}<<>{}>)}}((<(<>())>{<{}()>{{}[]}}))]<{(({[]<>}<[]()>))<{{()}[()[]] +([[((([<[[{<[(()())][(<>[])]>[[[[]<>]{(){}}][[(){}]{<>[]}]]}({[<{}<>>(()())]<({}<>){[]<>}>}[(<{ +{([<{{{[{<[<<[<>()][<>[]]><<(){}>>>]<<({{}[]})<(()[])>>[<<[]<>>[()[]]><([][])[<>[]]>]>><[[[(<><> +<<{[<[{[[{{<[(<><>)[<>()]](<<>[]>{<>()})>{<{<>[])(<><>)><([]<>){<>()}>}}({([{}{}](<>()))<{(){}}([] +({<([({{{<{(({[]()}{<>[]})<{{}<>}[<><>]>)(<[[]]<()()>>]}>}}}){[{{<{<[[()[]]<()<>>]{{{}[]}{[]<>}}>}[{{{ +([{[[{<([<([({{}<>}<()[]>)<{<><>]{{}{}}>])[[{[<>[]]{{}<>}}({{}[]}<<>()>)]]>([{({<>}{()()})({{}{}}([]<>))}][ +{{[[((([{{[<{<{}()>}[(<>{})<{}()>]>({((){})([]<>)}([[]<>]{{}()}))]{[{{[]()}(())}(<<><>><<><>> +<{({[((<[<{[({<>[]}{{}{}})(({}<>)<()[]>)]({{{}()}[<>[]]}[<[]{}>[{}()]])}([{[<>()]({}[])}(<[][]> +<{[[{<<[<<<{({{}[]}[<>{}])<(<>())((){})>}<[<(){}>(<>())]>>(([<[]<>>{<>()]](<[][]>{()<>}))<([[][]]([]<>)){{()( +[[<[{[(({<<((<<>{}>[{}[]])){([<>{}]){{(){}}<(){}>>}>({<[{}<>][{}{}]><(<>{})>}[<({}{})[[]{}]>[[()()][{}]]])>} +({{<[<<[([{<<{[]()}{{}<>}>(<(){}>(<>{}))>}]<{[{{<><>}<<>[]>}(<()()><[][]})]{<[{}<>]<<>[]>>}}({{{()}( +[([(({[[<({[({()[]})([[]]{()<>})]<<{[]<>}{<>[]}>>}){{({[()[]](()<>)}({[][]}))<<[<>()]<[]<>>>[{{}<>}{()[]} +(<[<[([[{[<[[{{}[]}<<>>]{[[][]]<{}{}>}]({<()()>(<>{})}[<<>><[]{}>])><{{({}<>)[<>[]]}{[[]<>](<>())} +{[[[((([<[([(([]())({}{}))<<()<>>[(){}]>](<[<><>]{<><>}>{{{}[]}{<>()})))](({(<<>[]>)}){<<< +{<(<[<{{<<{[(<()[]>{{}<>})(<()()><[]{}>)]([{()()}<<>()>](<()[]>)}}([(<[][]>)[{{}<>}<()[]>]])>{<({<[]()>{ +[[[<[({([[{({(<>[])<{}>})<<<[]{}>>{[[]{}]{[][]>}>}[{<[<>[]](()[])><[{}<>][[]()]>}]]((<{{[][]}{(){}}}(([][]) +(<{<[<{({<[[[{()<>}[[][]]](([]{})[{}<>])][[<[]{}>{(){}}][<<>{}>[{}()]]]]<[{{()}(<>[])}((()<>)[{}[]])] +{[{({(<{(<(<{[<>()][()[]]}[({}{})<<><>>]><<(<>[])([]{})>])>)}(<<<([[{}{}]({})])({[[][]]<[]{}>}{[<>[] +[[[(<[{([[(([({}{})(()[])][<<>()>{[]()}])<<{<>{}}([]{})><(<>[]}<()[]>>>)({[<[]>((){})]<[{}<>][[]<>]>}{<<[][] +(<[<[(<<[<<<({{}[]}({}[])){(()<>){{}{}}}>>>[{[[<{}()><{}()>]{(<><>)<{}{}>}]([{[]<>}{<>[]}])}({{([]()){<>{}}} +[([[<<[(<[[{[((){})({}())][({}{})[()[]]]}({{()[]}}{[[]<>](<>())})]{(<[{}<>]{()()}>({<><>}((){})))}](( +{<[{([{<<(<{{{[]{}}}[([]<>)(()<>)]}<<<<>>[()<>]>{([][])(<>())}>>)<({{[()[]]{<>{})}<<[]{}>(()<>)>} +[[<[{[<{{<<(<[{}<>]{<>{}}><{<>[]}[[]<>]>)>[{[({}[]){(){}}]({<>()}[<>{}])}]>{([([{}{}][<>[]])[[{}{}]]]<{[() +{<({<{([<(<(<[(){}][()[]]><{<>()}<<>{}>>)<[{[][]}]{([][])<(){}>}>>[({(<>{})<<>{}>}{{{}<>}{[]<>}}){< +(<<(([{<[(<{({[]()}[[]{}])}([<())(<>{})]<(<>{}){<>[]}>)><{[<()<>>]{<()()>{{}<>}}}{{({}<>){ +[[<[[[<({<[{[{(){}}<()()>]}([{{}{}}{()()}][([]())<<>>])])[({<<<>()><[]()>>{([]()){[]}}}){{{[{}{}]<{}> +<[(<[(<{(<{[(([]()))]<{[[][]]}{[<>[]]}>}<{<([]<>)>}{[((){})<[]()>][({}{}){{}{}}]}>>{({{<{}()>[[]{}]}[(( +({(<[[([<{[([[<><>]((){})]{[[]{}]{()}})<[<[][]><{}<>>]((()[]))>]}(((((()())[[]()])({<>()}))<{[ +<<({[<<[<{{[([{}[]]({}))[{{}()}{{}()}]]{[<{}{}>]<{[]}{()[]}>}}}><[{<((<>()))<{{}{}}>>(<(<>{ +[(((<{[{<{<<{[[][]]{[][]}}>>{<{[[]{}][<>[]]}{([]{})({}{})}>{({{}()}<()<>>){({})[{}[])}}}}>[(<{{{(){}}}<({}[] +{<<(<{({[{<{<([]())[{}()]>[[()()]<()>)}<({[]()}<[]<>>)<{<>()}(())>>>}(([[{<>()}<<>()>]]{({()<>} +[[({<<[([{([[[<><>]([][])]<([]{})[<>[]]>][<[[]<>]((){})>])<(({()()}{{}{}}))({{()[]}([][]))[[<><>]<[]{}>])> +(<{<{[(([{[{<([][])<<>()]>{({}[])<<>>}}{<{[]<>}<[]{}>>}]{(<[{}[]]({}())><{(){}}{{}<>}>)}}][[{ +<{[[<[{(((([{[<>()][<>[]]}]<<{()<>}>(({}<>))>)[[({[]()}<[][]>){({}<>){[][]}}]((<{}{}>{()()}))])[< +<<<<[<[[[(<[{(()<>){()()}}([{}{}]<[]{}>)]>[{<<<>{}>{()[]}>({()}[()[]])}((({}())([][])))])]<([([<[] +{((<({({{{<<[{<>{}}(()[])]{(<>{})([]{})}>>{{{({})[<>{}]}{<{}>}}{{<[][]>[(){}]}{{{}[]}<<>()>}} +<[{[(((({{{[<<(){}>(<><>)>{{{}[]}<[]()>)]<<([][])([][])>[[<>()][[]<>]]>}((([{}])<<{}[]><()<> +[{[{<{[{<(([<(<>[])(()<>)>([[]<>]([]()))]<({{}<>}<[][]>){<<>{}>{[]()}}>))>[([(<{[]()}{{}[]}><(()<>){[] +[{[[[(([[[(([<{}[]>][[<><>]{()<>}]){[<[][]>]({<><>}[[][]])})({{{{}<>}{(){}}}<<()>[{}()]>])]]{(<<(( +(((([[{{<{[{<(()())(()[])>(({}<>){{}[]})}[<({}[])[[]()]><{{}()){<>()}>]]{[{<()<>><()()>}]{{<( +{[<[(({({<[(<{<>[]}>[{{}[]}{()[]}])[<[()[]]([]())>{[[]()]{{}()}}]]{<{<<>[]>(<><>)}>{([[]<>]{()<>})<{<>}>}}>}( +[({[{((([({(([[]<>]([]{}))<({}{})>)})(({<([]<>)([][])>}({<[]{}>[<>{}]}[<<><>>([]())])))][<<<({(){}}([]())) +[{<(<<{<({<{[({}<>)<{}{}>]{<[]{}>{<>[]}}}{{{{}()}(<><>)}({<>{}})}>{{<{{}()}<<>{}>>({{}}(()[]))}(({<>{}}[ +{(({<<{[((<((<{}()><()<>>)[<()()><{}[]>])[[<[]()>(()<>)][<{}()>{()[]}]]><<{(<>{})[[]{}]}[<[]<>><{}{}>]><{<{} +{{[{(<{({(<(<([]{})([][])>[{<>}<{}()>])(({()[]}[()<>>){(()<>)<{}[]>})>[(<([]<>)([]())>(<{}<>>[()[ +<[(<<[{[{[[<([{}<>]<[]<>>){[{}[]][{}[]]}>{<([]{})}<{{}{}}<<>()>>}]]}[([([{[]{}}<(){}>]{[{}{}][{}{}]})<<({}{} +{[<<[{{<<[{{[[[]<>]{<>[]}]<{{}{}}(()())>}<((<>[])<<><>>)>}<{[<[]><{}<>>]<[{}<>}{()[]}>}<([( +[<([({<<[{{{(<{}<>>({}())){[<><>][<>[]]}}<{<[]<>>{[]()})[([]{})<<>[]>]>}{{{[[][]]<{}[]>}[<[]<>>(()<>) +[((([[{<{[{{((<>[])(()<>)){{[][]}{{}{}}}}({({}<>)({}())}((<>{})[{}{}]))}]<(<((<>[])<(){}>)> +<({{({[{{<{([{()[]}(<><>)](((){})(()[])))({[<>{}]<<>[]>}[<<>[]>])}>}<[{<(<{}<>>)<{<><>}<[]>>>(([{}()] +[{{<({((<([(<[<>{}](()[])><{[]()}<<>()>>)[<(<>)(<>())>)]({({()()}[()[]])}<[[[]<>]<{}<>>](({}[]){ + diff --git a/day_8/code.py b/day_8/code.py new file mode 100644 index 0000000..1fda726 --- /dev/null +++ b/day_8/code.py @@ -0,0 +1,205 @@ +import fileinput +import itertools + +# key: the digit, value: the number of segments +digit_segment_count = { + 0: 6, + 1: 2, # + 2: 5, + 3: 5, + 4: 4, # + 5: 5, + 6: 6, + 7: 3, # + 8: 7, # + 9: 6 +} + +unique_digit = { # easy digits: 1,4,7,8 + 2: 1, + 4: 4, + 3: 7, + 7: 8 +} + +actual_digit = 'abcdefg' + +actual_digit_mapping = { + 0: 'abcefg', + 1: 'cf', + 2: 'acdeg', + 3: 'acdfg', + 4: 'bcdf', + 5: 'abdfg', + 6: 'abdefg', + 7: 'acf', + 8: 'abcdefg', + 9: 'abcdfg' +} + +# ab: digit 1 +# dab: digit 7 +# eafb: digit 4 +# acedgfb: digit 8 + +def get_keys(d, value): + keys = [] + ls = filter(lambda i: i[1] == value, list(d.items())) + ls = map(lambda i: i[0], ls) + return list(ls) + +data = [] +ex =[] +#ex = ['acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf'] +for line in ex+list(fileinput.input()): + content = line.strip() + if content == '': continue + + def parse(x): + l = map(str, x.split(' ')) + l = filter(lambda x: x != '', l) + return list(l) + + ls = list(map(parse, content.split('|'))) + data.append((ls[0], ls[1])) + +def part_1(): + count = 0 + for _,output in data: + for x in output: + out = get_keys(digit_segment_count, len(x)) + if len(out) == 1: + count += 1 + return count + +def is_similar(a, b): + return set(list(a)) == set(list(b)) + +#print(part_1()) + +def find_by_length(ls, length): + return list(filter(lambda x: len(x) == length, ls))[0] + +def diff_str(a, b): + commons = [] + longest,shortest = a, b + if len(b) > len(a): longest, shortest = b, a + for e in longest: + if e in shortest: + commons.append(e) + all_d = list(set(a+b)) + diff = [] + for e in all_d: + if e not in commons: + diff.append(e) + return diff + +def remove_char_from_str(subject, char): + return ''.join(list(filter(lambda x: x != char, subject))) + +def remove_set(subject, char_set): + return ''.join(list(filter(lambda x: not (x in char_set), subject))) + +def remove_char_to_entry(entry, char): + new_entry = [] + for e in entry: + new_entry.append(e.replace(char, '')) + return new_entry + +# return true +def included_in_str(subject, char_set): + pass + + +def process_entry(entry): + str_3 = find_by_length(entry, 3) + str_2 = find_by_length(entry, 2) + a = diff_str(str_3, str_2)[0] + cf = str_2 + # on récupère le bd à partir du 4 + str_4 = find_by_length(entry, 4) + bd = remove_set(str_4, cf) + + # enlever a + entry = remove_char_to_entry(entry, a) + + # trouver g + # trouver le str_5 qui continent cf et bd + str_5 = list( + filter( + lambda x: included_in_str(x, cf+bd), + find_by_length(entry, 5) + )) + print(str_5) + + return entry + +def digit_with_mapping(mapping, digit): + assert(digit in actual_digit_mapping) + actual = actual_digit_mapping[digit] + + encoded = [mapping[actual_digit.index(a)] for i,a in enumerate(actual)] + return ''.join(encoded) + + +def process_entry_force(entry): + sets = [set(s) for s in entry] + poss = list(itertools.permutations('abcdefg')) + for p in poss: + # we do an hypothesis that poss is the mapping + mapping = ''.join(p) + ok = True + for d in range(10): + s = set(digit_with_mapping(mapping, d)) + if not (s in sets): + ok = False + break + if ok: + return mapping + +def compute_output(mapping, segments): + def match(w): + i = mapping.index(w) + return actual_digit[i] + a = list(map(match, segments)) + a = list(sorted(a)) + key = ''.join(a) + d = get_keys(actual_digit_mapping, key)[0] + return d + + +def part_2(): + res = 0 + for entry in data: + mapping = process_entry_force(entry[0]) + val = int( + ''.join([str(compute_output(mapping, d)) for d in entry[1]]) + ) + print(f'{mapping=} {val=}') + res += val + print(res) + + +print(part_2()) + +""" +{dab} = {acf} +{ab} = {cf} + +so d = a + + +using the "unique signal patterns" hypothesis will give: + +0,1,2,3,4,5,6,7,8,9 + +on enlève les digits faciles (1,4,7,8) + +1 = cf +4 = bcdf +7 = acf +8 = abcdefg + +il reste donc 0,2,3,5,6,9 + +""" \ No newline at end of file diff --git a/day_8/example.txt b/day_8/example.txt new file mode 100644 index 0000000..c9f629b --- /dev/null +++ b/day_8/example.txt @@ -0,0 +1,10 @@ +be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe +edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc +fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg +fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb +aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea +fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb +dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe +bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef +egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb +gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce diff --git a/day_8/example1.txt b/day_8/example1.txt new file mode 100644 index 0000000..a758270 --- /dev/null +++ b/day_8/example1.txt @@ -0,0 +1,9 @@ +edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc +fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg +fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb +aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea +fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb +dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe +bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef +egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb +gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce diff --git a/day_8/input.txt b/day_8/input.txt new file mode 100644 index 0000000..79bf091 --- /dev/null +++ b/day_8/input.txt @@ -0,0 +1,201 @@ +eb cbgfae cabdf fedab efb adgcef cbgaefd egdb dbgefa eafgd | dfbae be gdafe gcefab +bfcae acegfd dbfac daf bgfdc dfgaceb gfebdc da dbag cdgbaf | bgfdc dfcba bdegfc efadcg +afgdb dcge ed dfe geafcd aefdcb adgef gfcea gdbecfa agfceb | dfe ceabdf efbcag gefacd +ebad gbfed dfe caefdbg gdbcf fgcdae de fdgabe efbag gabecf | gacfed bgedf dfebgac befga +dabegfc gfeab gdefbc ecagdb bcfd fd ecdgb adgfec bfdge dgf | efbag egbdf ceagfd dfegb +bfadge dbgec fgbdce cafbgde gbe gcbafd cfbe be cgbdf aedcg | bcdeg egacd beg cgedb +aecfgd dabcegf gca dbgacf gc degbaf caged gefc adebc agfed | agc gc cg fdgaebc +beagfc ecf agdecbf cf acfd dgace deafgc efbgd gdfce gadbce | decfbga dbaceg bfgde gdecfa +cgbef dafeg fdc gbadfc cbed aedbcgf cd bfagce fcgde efgcdb | gefcd fgacbd cd bfegdc +bce efdcbg ebgfda cbfgae bdegc ce dfec dbefg cgadb edcfgab | cfaebgd bedgc cfed efcd +gedcfba cfbdg dgecf bafdcg efbgda cbed efd bcfged de afegc | bfcdg agefc fabdgec fegca +fa dgcef gfacd bdgac fcbegd fedcga adef fcgaebd afbgec acf | gefdac egfcbda egfdc gefdca +ebcgf dagbc cfedga cfgba afg agebfc befa dcfgaeb af edbgcf | cbgefd acdgb afdgce fabe +gfe fg fdageb bgfd cagfde abegf baecf daegb cabged cgfdeba | gfe gdafbce fg bgdf +bagdce acfbd faebcgd dae fecdbg ageb ae dbcae defacg gedcb | dea eabg bcdae aecdb +gceaf dagc eda febcagd gebdaf ecafdg deacf ad cdebf abgcef | fdaec fdebc dcfae ade +adebcfg ecd ecbag bgaecf cgdfb afecdb gbedc caedbg aegd ed | bedcag gabecf dcgfb fcabed +fbecda cabgf dg deag cdeagf gdf begcdfa acfed cfagd dcegfb | dg edcfa gefdbc gdea +gfbcad dafbge fdcbea dgbfa dbeag eb eba eadcg bgef edgfcab | agbed degafb eagdb caedg +aegbdc fecdgb eac cgefab fecag abcf ca dbgeafc gebcf fedga | fbgecad abfc cgefba agcef +dc fcda gadbec fabcgde gbcfda dcg befcg deabfg bgdcf fgdba | cdfa acfdebg fgadbe ecfbg +cdbagf gcf badgc fgdac fc cbgead fbcd defga ecagbf gdbafce | gfabdc bdcf gcdab fdcb +cfdabe faedc febcg acbd abf cdagfe ebdgcaf bdgfea ba beafc | fab bafdce dacbfe fab +eag cfedba aecgdfb deafgb agcdb gefc eg gdeafc ecadg cedfa | fedabc gbadc bacdg gecad +fgcae afbdec cad edfab cedfa cdfb bdceag bfdeag fdgecba dc | ecagf dc fdecab cdaef +fcegbd dagefb dbf cgfbd eafcdg dbagc cfdeg gecbafd fb efcb | bf efcgd gdcef gbfedc +dgbcfe dagef fcbadg fdceg efbc edgbfca deabgc ec cbdfg gec | feagd dbafgec fdgcb bdgaecf +fgbcd dagcef fb begfac cbdge bcf gacfd gafbdc gbfadce adfb | bfc bcdgf deagcf fgdbc +gafdbe gefba bf gbdaec faecg bcgfead bgdf efb acfbde gdeba | becagd bf bdfg fb +af agdcfbe aegfbc agdf dfbce acdbf fab edabgc cdabg gdbafc | dagf fa bdfce bfgdca +edfgba edbgf afb beadc gbcdfae dfga afgceb af edbaf gefbcd | efgcba ebagdf dbgfe fa +fecba df dbf dcefgba dgaf aefbd decbgf bcdeag begad deafbg | acbedg degabc fdebcg deabg +cgabedf abd daebgf gbde db bdaefc fecgad cagfb gabdf edfga | gabfc cgbaf beagcdf cafdge +efdgb fbe adcbfe edfbga cafgbde gefdca be eagb gbfdc geafd | eb eabg bega abfgde +cfadg bfeacg gae ecdfga dgcea fabdcg efgbacd aecbd ge fedg | adcgef fdgaec gdface gcfadb +gabd fcdbag cagfbe deafgc dcbaf ad bdefc dca dcbefag bcagf | facdb gceadf dagb bcafg +adc eabfcd ecbgad dfab bedfc edcfa ecafg fecbdg fdaebgc ad | cadebf egcaf gaecf febdc +gfdab cd agcbef cedf ecabdf ebacf febdgca dbcgea fbdca dcb | fabdc fcbdae acebf dc +bagcd gdabfc dcg fgecdb dc dfcbaeg cbage dfgba dcfa dfbega | dc egbfcad gfdba dgc +deb beacd cdbga gbdfce cegfbda abgced be dacfbg agbe cfeda | gfbcad deb ebd cgadeb +efbag gbe gbaefc adbefcg gb bacg efbacd edafg bgdfec abcfe | bacg gfead dfeag dbfgce +gdbfea dacgfb dacbfge fgceb cd afedb efcdba bcfed cdb ecad | gdfacb bdgfea ecgbf ecda +caedbf dcge abgdc gcfbae fcdbgae bcg abcedg bgdfa cg decba | acdgbe cg gbc ecfdabg +adfce bdefga acdgfeb fadeg gecdba gebf bfcagd afg ebdga fg | bdage fg edafbg fgadbe +gfbdec cabdf dca fgca gbcdf ca fbaed cagbdf fagbced eadgcb | ac badecg afbde bcdfeg +gdc agbed adbgc abgfc eabgfcd acfd dc gecfdb cfeagb gcafbd | fdac beadg gcafbd cdabg +cf ecgfadb ecabdg begcaf bfadc fabdgc dbacg cgdf bfade cfb | dafbc cadgb becfdag cbf +fda abgfe gcadbf cdae egfcad bfedcg gcfed ad dfega fgdeacb | cgdef efcbgda fbgdeca eacd +ef ebfcad dceag acgbdf fec bagfedc fbeg ecgdf ebdfcg bfgcd | efcgd bgef fdcgb gfcbd +gadbecf bdgcea fb bafedg bfcdag bdfc aefgc dabgc facgb baf | abf adfebg fba bdagc +afgceb dfcba cdeb gafdecb bcf edfcba bc aedfc fdbga fcaedg | fbedgca efcgda ebacfg deabcf +gefbcad cgfbd cagdf dfa fa gdaefb fabc cefdgb aedgc gbcdfa | deafbg caegd ebfadcg dfa +bfced gdabfc dbeac afgdce fegb dfgcaeb egdbcf fce fe dbfcg | bfdce cdegfa acdegf befdc +edb befcda fabcd ed adbce cfbaegd fbcgad cedf gcaeb agebdf | fegdab bed fgaecdb afdecb +fbea gebdac gbdea fbeagd gdfab fa dfa adfcegb aecfdg bgcdf | faebdg cfbdgea dbcgf adbgf +eac cgabef aebgf cgbde dfebcag efadbc aegbc daefbg facg ca | fcag eabfcg cae cgbde +ce fce gdefb geca dcfeg afdcg gfcdea ecabgdf gbfdac cedafb | gcefd fcdage gdacf gefcd +fbdcgea fabcg ebafg gfecab fge gebc bdfcag debaf eg fgdeca | acgfb bacfg cgbfa gfbea +acdeb gcedba befac bacdgef gfbec fbdeca afc fdceag fa dabf | baegdc gecfb dgfeca bafcdge +dfgbc deagfcb cfga dabgc fagbed cfbgda bfedcg ag abg adceb | gdfceba cgdab dbcea ga +ecafbg ge feg cadef decfabg begd geafd dfgacb gfdaeb gbafd | dacef ge gef faedg +gabecd ecdag edabc fdcbgae ebdafc bcdg gbadef dg cfage edg | cgbade cbgd cgdb edfbgca +beca bgead fbecgd fbagcde cb gbc egfdab dgfac dgbac eabcgd | gaebd dcafg dbgefca abec +bcfea ead cebda fbdcge dgabce gedbc fcdage da dcafebg badg | cbged aegfcd dbag agdb +cefagd gfbedc fagc acedb efc aefbdg afcde fc gafed febgdac | feabdg ceafgd eacfdg feagdc +dbegfa aefdcbg bafecd cd cfaged bfade febdc adcb dcf cfbge | cd badfe dbca bdgfae +dcfa eaf aedgfc aecgbfd efcag fa edabfg fegdbc cgedf agbec | ceagb cfbdeg gabedf fa +db eadgbf dagfc gbefdc egbcaf bdegafc debc fbd bcdgf fegcb | gfcedb ebdc eagbfd ecbd +cb cgbafde dcfegb cdfgae facb bcg degab gefacb caefg cgbea | degba bfgedc egacb egcfa +gbcfd dabgfe abecfgd beagcf cg gcad bgadf fdecb bafdgc gbc | abegdf dagfeb cdebf gcbdf +bfaedc deabgf aecbf fgcba ec bdefacg aedc fcbgde dbafe bce | gfcdbea fbdae bdcaef ec +bedafgc dgcbfa ebc afgcb cfade geba debfcg cgfbea ceabf eb | faedc ebga bega baeg +dfeag acegd cae bgdca gdefab ec cefd cadgefb eadfcg fabcge | faecgb gedaf eca aec +abgf cgebdfa dfcab adcfg cdbgaf bdcefg efcdga dfb bf abecd | gfeadc baecd gcdeaf bfegdc +cbgde egfadc dafec abdf cba ba afgecb bdeac aedbcf adecfbg | eadfc debcg abc ba +ecdfgb adecgf fcged cbef bdceagf bde adgbc cbgde gbfead eb | agdcef dfabge eb cedfag +dgb bg dgfaec efadg bedag afbg bagfdce bfcegd edabc agebfd | befdga dgbea dceab ebadg +cfbe agedb bcg acefg acegfdb bgafec aecbg gafced abgfcd cb | cgbadf adfgcb gfacbed cdeagf +gadfc bag acgdbf abfgce bfgda fedcag dfcegab gb fdeba gdbc | bcfgae febad bg bag +cbaef fgea gabfdc fac cegdba ecabg fdebc ceagfdb af gaefbc | gfcbad dbcage cbaef fca +bace bdcfe bfgedc afb cabdfe ab aegdf befad agecbfd dfcgab | ab cebgfd bace fdgbeac +agfb bgcad edgca becfda cabfgd gefdcb ba cabdgef bcdfg bad | cdabg daceg bda bgfdc +cbefad gcfbade bdcgf edcaf gad acfegd dfeagb ga gcea cdagf | gda ag agd ga +agc adgec ca cgdfe bfdcaeg cdfbge cedfga edagb acbgef facd | abgde bdfcaeg gdabe gca +egcda degcb fabcgde fgaebd be gfdeac ecab bde gfcdb edgbac | dbe afgbde ecab deb +defbga bceg cabgef cadgbef agefb dfgcba becaf dceaf bc cba | gaebf cafeb bc dfacgb +bedag bdfcea dfbacg efdg ebcga abedfcg dg aefbd gbdfae bdg | caebg degbcaf gbcfda efadb +eb bacdf bde bega gdfaec daecg dafcbge acdbge cdabe ecdfgb | agecd eb edb bde +gabcd dcafbe edacgb badce gdcefba fgabc dcbgfe dg dage dgb | dg ceafgdb dfgbec ceadbg +acedb geadcb cafd edf fdcegb dbfae dfcabe fd agfdbce agbfe | adfbe dcbaef dcaf eagfb +dgfb daebcf gaced dab ecgbfad eabfgc db gabedf fgeba agbed | bd bd acedfb fcdbega +daebf befdac gf bdecfag egfb gabefd adfgbc fgeda fgd cdage | agedc fg fdbeac defcab +bcgda egbdac bgf dgbface cagbdf afcb fb ecfdgb edgfa gadbf | fbcegd egcbfd adgfcb dgbaec +de ebcfa dec bacegdf afdbec ecgbaf fcaed cafgd bfde cedabg | cdeagb cfgabde fbed bgedfca +cdgbf cgedbf cdef baceg ef dfbacg bef egbcf dfeagb bafgcde | gbfce fbdcag fcabgd ef +fbac efbdga gebdc fc bfegc bgcfead dfcega baecfg afgeb cgf | adcgbef abcgef fcg bacf +eagdbf de bacfgd abfce dfgab dfabe cfegda bedg fde cebagdf | dfgace cfgdba degb bged +cefabg bcfgd decbg fdcgeab cdbae ceg bgefcd efgd eg cfgdab | bfgdc fbdcga gce adecb +ecgabd ebgd gdc fdaegc dbcaf fcegdab acgeb agdcb eabfgc gd | gdbafec efacbdg bgeafc dfacb +egbfac abg fgeca fgaedc dbfeg cbae acbfgd gefab ba bfcdgea | gab ab efbgca aecbfdg +eadgcfb ge aegbd cedagf dagfb feabcd abcged dbeca cegb egd | debca fadcbe baedg ge +cagfeb edabc gd gfdabe cdfg degcfab bgcfa dgbacf gbcda dgb | gd bfeacg afedgb dbaec +ba bfad febcg decafg fdeac fdgcabe bca abdefc gacbde bcefa | badgce cba eacbf bcafe +ac cedfa bdecgf dgfea fgbdac dfgabce abce edcfb caefbd fac | gafcbd ecfgbd fbgcde faegd +bf eafcd caebg fgcb dcfbaeg abf bfaegd cbeagd baefgc acefb | gdbcefa cbeag fab bfa +gcb ebcgd cfdbaeg dafcge cedfg dgfbce badec begf gb cbgdfa | fgaebdc gbdce gbfe afdcbg +gfbdc dac bdeagf daebcf gecdaf fcgad geca ca gafed fbcdgae | bafcde dgbfc adebcf gcadf +ecbgaf cadefb ec debc bgcdaef ace ecafd egdfba aedbf facgd | ec eca efcagb bfaed +aef fbad ebacg afceb af fedcbg bfced cgadfbe fcbade gaecdf | bgcdef abgce efdbcg aebcg +cafbde ba cabf agfed ecfbd gfebdac dbeagc abe cbgedf fdbae | abe gdaceb eba bea +cbdfg ebg edagf be caegbd cbdgfae fgebd cefb cebgdf cbdgfa | bedgca be fbdcge edabcfg +ebga cbdafg cafgebd eb aedgbf ecfgbd fcaed eafbd bed adgfb | caefd bfdga dcfea cdgabf +efgdbac ab dgcfb abg afedg faeb dbegfa dacegf bdagf dagcbe | fbcgd gacbdfe cdbgf ab +ga dbfca acge edfgab gab agfcb cabfge bfdcge dgfeacb efgcb | ag fbecg ag bfcgea +fdcgbe bad cfbga ceadfb bdeacg bdafc ebdcgaf bcefd da afde | gefdbc daef fade afcdb +dcagbf cbgef abg ab feagd beagcf cfbegd cbae fegab gdbeacf | gba ebgfa ab fecdgb +cdbge aefcdb febca feadgb eagdbfc ecbgf gafc gbf ebacfg fg | gf gefbad gf dbecafg +dgb efdgab becfga aebgc gd cgde cebagd bagcd cebdgaf afbdc | agcbd dg gbd gdec +dcafebg ebdga degc ecgabd bge dfcgab efabgc feadb gadbc ge | badcg acgedb adebf badge +aegbfdc fa fagebd dfeac fae eadcgb edgfc bcfa abefcd bdace | fbca ecdbfa agecdfb dcfae +gbdcea gcfa ecdafbg bagfe bgc ecfdb gecbf fgaedb abfgce cg | dbafge bgaefd edcbf faebg +cfdegab dbgcfa ebf fe fcaeb fecdba cbage abcfd gfbead cefd | fcbea ef fe bceaf +dfebga fde bgfd fd gdcbea cebafd dgabe fegad cdeagfb acfge | fgeca cdeabg gdceab fd +fgeda fcdegb gdbaef dbfcega fad acfeg fgabdc baed da dgfeb | gdefa afd acfge efgbd +daecbg ace abcgfd dcbegfa efcag dafe ae cbegf gfcda agedcf | aedbgc gacef gadfce egfcb +dfacb egdac daecgb ceb be begafc edgb cadebfg edcba egfcad | abcegd cfdbage acbdf aegcfbd +cdfae afgced dbgac fcebad gfdbea badcf fb adcfebg cbef bfd | bafgde begfad bgcda aebfcd +gace eabdgf gcedf gfaedc efbdc edg ge gacbfed dfcga cfgbda | acge eg dfceg gefbad +abdcf bdegfa bgedfca ce geafd dfeagc edc aedfc fegc gacdbe | dcfaeg ecd cde aegfbd +egdbcf agdfbc gacdebf dec gdcfb gebda ec dafebc cgfe dcegb | cfbeadg efgdbc gcebfd ecd +dafge acefg ecgb cfagdb ebfcagd cgfab ec ecf aedcfb acbefg | becg ec gecbaf adfgbec +bad dafgc cgebd efbdcg cbedgfa cgabd bfeagd ab dgecba cbea | edcfabg fadcg dba gfcda +dcefag bcagd ecfag gfacbde fgcaeb cfagb afb efbagd ecfb bf | afdecg ebagdf bcfe fb +aedgcbf dacefg bfgce dgfebc dbcfag fb cefgd gabce bfc fbed | acgdfb bfegc geabc edgfac +df aebdgcf gdcba fcbad cbdage bdcagf deagcf fdbg aefbc afd | cdfba df cebfa daf +edcgf bfeadg bfdea bg gfbed gfba eafcdbg dcfaeb bge degbca | begcda aebfd fbag fedgc +dbfgaec bcf edcb abfeg egfcb fgbdca gcafed bc bfcdge cdfeg | dgefcba cfgbde ecdgf cb +eafg gbdaf gcabd fecbda dgebcaf cdfebg adfgbe bgf gf bdfea | gcadb fage gaef afdceb +debagfc cbdfa gebc cfgaed gc aegfdb cdfebg ebgdf cfg bcdgf | eafbdcg dgfebca deafcg gadefc +adg abced fbega dg abegcf fgde cfegadb gdabe aegfbd gdbacf | abecd afgbce gdabfc dag +efacg fagb ebgdfca dfcabe gcdbea fa fbaceg cegfd geacb fca | edgfc fca fa cgeba +fbadcg caebdg egfbd fdgcb gdbca fbc egdbacf facd fc egcbaf | acdgb egfbca gbceda gfcbd +adfge bfea fb fagbdc bdf edfbag ecdbg fgbed dbgafce ecgadf | dbefg bdegf aegcdf fdeagc +ebgdc afecg dcfgeb gbf cfbd dbaecg bf ecbfg agfdecb dbgfea | afgbcde gefbc fb gcebdf +fga efdab fgdcea bagc fbgcea gfebc abdcegf cbgfde gfbea ga | dgceaf gfa ag cbefg +gead dfaebgc ag fgcead dacef fdecba adgcf fgcbea cga bgdfc | faced fgbcd bgfeac fagcbe +bdfaceg gafbcd cfae daebg gecdf edcag cdegfa cga ecbgdf ac | gdeca agc face ac +dbgcf eg badfe edgbcf gbe aefbgdc eabfcg dgfbac decg fgdeb | cfgbad abdfe eabdf egb +fcbgad gfcadbe eadb bcadf cfedba bce be fecgd cgabef efdcb | fgcabe ebad edba fdcbga +df bedgf bcgedf bcaegd dcgf eacbdf ebcdg dfb agedfcb egfab | dcgf fdb edbgc egdacfb +facdb eca fceagd cfeba dcbe fbcead ec afdbgc acdgbfe begfa | fdeagc ce aec ec +ecfbad gdabefc edb be bceg abdfg dacgef gbaed cedag cebdga | eb adgfb gcade egcb +agedbf cegbda abf eafgc fb debafc gaebf eadfbcg begda bgfd | gacfe aegdb gaecf abdeg +afbced ae efa cdebf ceabf eacgfbd gbafc caed gbefad bedgcf | dbcgfe fae acgbf cfbae +fbeda cfaebg agbdcf gbaecdf dag gfecda gfabc gd bcgd dafgb | dafbecg dfeba dfaeb gfdcba +dacfbe dcbfgae abd ba fdgeb cbfa egadcb edabf fdeca geacdf | gcbdaef dbefa bfac dabfe +cgeafb fdce gafdbc edgca dbacegf efcagd cge baedg gdafc ce | bcfdega ce ec cefd +db eacgdf bgd gcbad afdcebg fcgbed dfab aebgc dfbcga fcagd | agcdf dbg gafcbd fecagd +fdge cgd eadfcb gd gbfdc bcfga cbefadg cfbde deabcg gbcefd | dgfe acfebd fbcdg ecdagb +gcefd fbd db efgdb cbgfea bead cfdbga begfa bfgdeac fgdeba | abcgfd dbf fabge fdb +efgcd ac eafcbd befagc bdcafeg fegba acgb fca gaefc ebafdg | efcag bagc ca gebafd +gb bgd bcdag adecb edbfag cdeagbf cfedab acfdg eagdbc cgeb | dcagb acfgedb dbg dbg +gfdeac gfe fbea bacge bcgafe becgdfa bcgfe ef egcbad fbdcg | bfdgc gcfeba adgceb fge +bcd dc dgfcba eagcdb cbgafed dgbfe dcebg feacbg cdea acbge | gbefd dbgfac gbdafc adce +bd dbegf bgd geadbc fgecd afebg fbagcde bgcfde dbfc eacgdf | bd gdb db gcedf +afdcgb cbeaf bedaf dfbge dab cbdfeag da fdaceb cgebfa deca | egfdb abd fdabce gcfedab +bcdfaeg afbegc dcbae defb cdaeg dfacbe cbe eb fcgabd abcdf | ebc caedb caedg be +dgcfeb dbefg ecfgd bge bafecg fgadce eb cdbe degcfba gadbf | be acegdbf cdgfbe abgfd +ab gfdbae dagfe agdfb bad facgde gfbdc dacfgeb aebg dbfcae | gabe bfgdae caedfb bcgeadf +cfade agfce ebacdf fde cbdfga bcedfg decfbag dacbf adeb de | fbcda ecafd aefdcb faceg +gbefda fdaegcb cbdag eacbd cea cefagd dbface efbc dbeaf ce | edbfa faecdb ebcf gdfceab +fgbead gcdbe ba adfeg bgdae afeb cadgfb bad bcgefda fgedac | cdafbg feagd abdge gcfbade +cbfe daceb decfag dbefca agdeb bc acfed egfbacd bafdgc cdb | dbc dbfaecg decaf gfcdab +eafdgc fcdb gbdae cb cgbad feacgb cgadfeb cdfbga abc fgdca | bafcdg cadfge baegd bfcgda +fabged cfdbea badec abc efadb gacde fdbc cb fbagec eabcgfd | cdaeb cbdea bfaed aebcd +gbc adfebc ecdagfb ecbgda geca eabdc dbgce edbfg gc abdgcf | gc fegadbc agec gc +cdgfb cbedag dcgef gcefad ceg adgebf cdeagfb dfgea acfe ce | cdebga ec dcgef ce +ca gdfba gadcf dfecgb cfadeg eadc afbceg fac dafcgbe decgf | decgf edgfc bgdcafe aecgdf +abdcge cae bfecg efcdba bfeda gcadfeb ac faebc fgbaed fcad | aebfc ca ca aebcf +bcafg cafeb eb feadbg fcgade fcdbea dagecfb ecdb abe dfaec | fcegad cbfgeda bcdaegf fcadeb +becg gfbda gae egfcdab ge deacbf edcfag agcdbe gadeb bcaed | gdabe cegb dcbgae abdce +adfebc fdbcga gecab gfbdae dg cdfg dbg fcadb bacdg gedcabf | egbac dbfaceg gd dg +ebcgfd agdbe fabd efcbga cfbgaed dgb fdgeab efgba db acdge | db cgead bgacdfe cgbefd +dcfbga edbag ae abe fdgbe dgbca aced bfaceg gbacde adfcbge | gefdb cdea cdgba bfdge +dbgcef acef cbe cbfedga decab acefdb fdbcga ce gbaed dbfac | ce fecabd daebc ebcdgf +db dcbaf egcfbad fbdeca ebdc egbfad cbagf bfd ecfagd aefdc | dagfbe cdeb cfbead fabcd +fbagdc gafbc aecbd fbgeac fcabd fdgc afd fd gfbcdea gbeafd | fda eacdb dbegfa dgcf +dgfeab fabcgd fg fgb cagfb dcgf dcabf cdfaeb fdgcbea bcaeg | adfbc dbgfca bcdafg gfb +baegc bd daefgc fadgc gfdb cbd ebacfd cdbgaf dgcabef cgbad | daebfcg fcbaed bd cgfabd +cafbgde cdf fdgae beacd cf adfce cfeg agfcbd fdcaeg gdfaeb | ecdfgba efdca acdbe cdabe +adfceb bg gdbeafc bgafe fcdbeg fegbca gfdae cafeb cbag beg | egafd efdcba gfacbe bcga +fbgda cfgbda fgdeb ebag cedbaf edb eb gafecdb gcefd fgbead | eagb dgbfe decafb adfgb +gefcbd eafdg cdbagef aegfc dg gbfcae dgf aefdb dgac adgcef | dgca feagbc dg agefcd +dbfeca ebcaf cag gafceb gcabefd bfga ga adgcfe ecdgb aecgb | gdbcafe agc cga afbce +ecdbfa cebdgaf efdbga bagfc gacbed cd cdfe dbfca ebdaf dac | daebf ecfd dfec cfed +agcef bf afbced badecg edfgbc efgabdc fegbc cbged fbe bfdg | dgfb fbdeca dagceb gcbed +daefgb cbaeg cegd gea cfbgad bfcea eg fadecbg bedagc abdcg | aeg ge gefdab ge +gfbca bac aebcfd dbcfag gdeacf bc gdafc dgbc ebafg ecbgdfa | cba dgcb cbdg eabfdc +dbegc gacfbd cegfab gabefdc bcgfe ef bfagc feg fdgeab feac | abfdcg afec gdbfea fdgaceb +bfcea cd cde dcfbeag gabed fegdca fdcb abgecf ebdca fabcde | efbac bfeagc cabdfe cfgabe + diff --git a/day_9/code.js b/day_9/code.js new file mode 100644 index 0000000..c5c6676 --- /dev/null +++ b/day_9/code.js @@ -0,0 +1,115 @@ +const readline = require('readline') +const rl = readline.createInterface({ + input: process.stdin, + terminal: false +}) + +lines = [] +rl.on('line', (line) => { + let raw = line.replace('\n', '').replace(' ', '') + if (raw.length > 0) { + lines.push(raw) + } +}) +rl.on('close', () => { + const db = false + + let rows = lines.map(l => l.split('').map(nb => parseInt(nb))) + //console.log(rows) + + // first part: find low points + let lowPoints = [] + let lowPointsCoord = [] + for (let i = 0; i < rows.length; i++) { + for (let j = 0; j < rows[i].length; j++) { + let val = rows[i][j] + let toCmpWith = [[i-1, j], [i, j+1], [i+1, j], [i, j-1]] + let success = true + + for (var k = 0; k < toCmpWith.length; k++) { + let [ y,x ] = toCmpWith[k] + if ( + y < 0 || + y >= rows.length || + x < 0 || + x >= rows[i].length + ) { + // ignore, because these point don't exists + continue + } + // important: you have to use less or equal HERE! + if (rows[y][x] <= val) { + success = false + break + } + } + if (success) { + lowPoints.push(val) + lowPointsCoord.push([i, j]) + } + } + } + + + // second part: find bassins + let findBassin = (alreadySeen, toSee) => { + if (toSee.length == 0) { + return [] + } + const e = toSee[0] + const [y,x] = e + // check if the point exists + if ( + y < 0 || + y >= rows.length || + x < 0 || + x >= rows[y].length + ) { + return findBassin(alreadySeen, toSee.slice(1)) + } + // check if it's 9 + if (rows[y][x] == 9) { + return findBassin(alreadySeen, toSee.slice(1)) + } + // check if it's already seen + if (alreadySeen.filter(([_y, _x]) => _y == y && _x == x).length > 0) { + return findBassin(alreadySeen, toSee.slice(1)) + } + neighbours = [[y-1, x], [y, x+1], [y+1, x], [y, x-1]] + + res = findBassin([e, ...alreadySeen], [...neighbours, ...(toSee.slice(1))]) + + return [e, ...res] + } + + let bassins = lowPointsCoord.map(c => findBassin([], [c])) + + let bassinsSizes = bassins + .map(b => parseInt(b.length)) + .sort((a, b) => a - b) + .slice(-3) + + bassinsSizesProduct = bassinsSizes.reduce((a, p) => a*p) + let bassinsCoords = bassins.flat() + + // output mini map of bassins + if (db) { + for (var i = 0; i < rows.length; i++) { + rowOutput = '' + for (var j = 0; j < rows[i].length; j++) { + if (bassinsCoords.filter(([y, x]) => y == i && x == j).length > 0) { + rowOutput += '\033[1m' + rows[i][j] + '\033[0m' + } else { + rowOutput += rows[i][j] + } + } + console.log(rowOutput) + } + } + + let risks = lowPoints.map(height => 1+height) + let sum = risks.reduce((e, s) => e+s) + + console.log(`Sum of the risk level of low points: ${sum}`) + console.log(`Product of 3 largest bassins size: ${bassinsSizesProduct}`) +}) \ No newline at end of file diff --git a/day_9/example.txt b/day_9/example.txt new file mode 100644 index 0000000..610bad9 --- /dev/null +++ b/day_9/example.txt @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 \ No newline at end of file diff --git a/day_9/input.txt b/day_9/input.txt new file mode 100644 index 0000000..cfc9531 --- /dev/null +++ b/day_9/input.txt @@ -0,0 +1,101 @@ +2165679878998998656789210124569321954398786566799434678987678943987789543249654323496545436767898789 +1034598769987897545678921235698949895987676445978926799876567899876678992198966212987432325456789678 +2123987659866789756789654396987898789998543234567897989665476999865567889987654109976521012378994567 +3299878543455699967899876989876765678987632125678998976562345698754378978998543298765432123499765678 +4989865432324589878966989879985434569998545234569769987421976789653234567899955349896743276589876789 +9876954321013678989545698767894325678987654345678956798599897898732145678989876456987654387678998891 +8765985432124589995433989857895436799399795456789545459988789999546234589376987868998765678989219910 +9879876543435699896512976546789945892198989767998932399875678987657355893245698979899896789698909899 +2989988998747898789329865437897896789987678998967890989654589998778456789356789989754987896567998778 +1098799987656789678998764323456789899876567899456999878963678999976567895467997897643498923458987668 +2987632198789994589019973212367899998765456796347899767894567898989678976579656789732399212349876545 +9876544999894123479929894343478989899954369965456987656789678967999799987689545698941987301298787636 +8997659898943234567898765854589876789865238896769876547678989459898989998795434567899876312987654323 +7898898767898765978929976765678945699832126789898765634567892398767678999896523458998765453498765678 +6639986656789879899412987876789436798743235699999854323456789987654567896987212967899876589999876789 +5429875345678998789323498987894323987654345678998743212369899996543498935698309898965989679878987891 +6598765239889765678934679998921012398767656789987654323498998987654599324569498789654398798765498990 +7987654378997654349895789989632123459878767999998785454567897998976678912978987698975459899854349789 +8999876567898765456789999876543434569989979998999897565878966789988789439899999567896599989964235689 +9998987679999876567899889989656545678996599987896999776989345678999899998789878456797989876543145678 +9867898789899987898998778298767876789323459876745678989793216789899999867698765345989878985432014567 +8756789996798998919987652109878989895414599765634569898672101236789987756549866239876767897643623478 +5345678945987899901998943212989291954323989654523498789543234345678996543239654349765656789876534678 +3234569129896987899879894523694390965439878943412987678954345689989989921098765698754545699998645789 +4345878998765456798967779634569989876549969892109876589765476797899979899999876798643234589329756899 +5676989987654345987759568965678977989698756789219987678976568945988869787894987987632123679998767998 +8787899998763236796543467897789765398797645894398998989989679439876755656943299876543234567899878987 +9898999879878345987622356789899879239987434789997879997898789598965634345892123987658545678979989876 +7979998767989659876210145899978998949876325679886767896569898987654321234789014698987676789569898765 +6567989654399998765431234998767987898765434598775756795456987898943210125679195989998987893498789654 +4379878943239899876542347897654365699896556987654345689669876789994323234698989877899298912345678923 +5498767892198789997659456789653234789987769876543234568979765878985467345997876766789129101234589214 +6987656789297656989798967896543125898998998765432145678998654569876575459876985455678998212345678925 +9876543899989549876987898987654014567999349654321035567896543467987986578965432324569987654476899434 +3987912999878939885676799998954323699891298767632123459987932356798997989874321013478998785677898765 +2399899878969998764365698999895434988789349876543234568999891238999898999985433144567899996789999876 +1498789965458987653234567998789569875678956989854347678999789999998789999876543234878998987899899987 +9987678994367998542123456789678979964569897898767898789988678789989678987987654345689987698998769898 +8976569989239876521012345694589999873456789979878939899876545698976567896598765456899876569987656789 +7665409879398765432125467893478987654589894567989323998787434567895457897459876567989765432398545899 +6543219768999898743234567932367898765679923459891019987654323678912345978367998788978954321297656789 +7654998957889987654565678943456789876789012398742198798776519799101256965459899899567895432398767898 +8769897545678998767676789658967899989892123987653987679887698989912367896598789923456987563499898967 +9899776534567899879789898767898978999943434598779876545998987679894567897987679646567898654989989656 +8987654323479943989892999898999767899854545679897997634569876568789678999996598767678999769878978987 +7598765454567892399921099989998656989769696789976598521698965457668999998889459878989998998767867998 +6439976767878901998432989878987545678998989899998965430987674343456889987764367989299987899654358999 +5321987988989919876549876567965434569987978998999876549876543212345678976543234590129876789543269789 +4210198999797899989856998457896323459876767897989987856987652101234589987654125799298765789654345678 +5323239789656789998769996567895454598765452346678999879876543212345892399775234598987654578975458989 +6434345678945678999998987678987765997654301234569986989987654523456791498965345987694323459876767899 +7546456799536569999877898789899899876543212365678965191298765789579890987896496976543213678989878989 +7658567894423456985456789899764932989654323989789763239349878897689999876989989897954323489998989678 +8769989943212569876578891998943210998765434599897654498999989998799998765679876789765434567897896567 +9878999654301459988989990987894329879876565678999765597889993239899899654598765678986965698986543456 +4989678943212398599998789876789498765987776789789897986778932101956789543219876789497876789995432345 +3294567894343987456987698765679987654598887895678989765567893222345997654348989894398987899984321234 +2123456789469876369876569654993298765789998924569878654456794533469898765656799989299798999875210123 +1012687996598765298765498765894999876896549012698765433345679944598789876797899878987699998764323234 +2144567897998654349874329876789899987987932123987654321256587895987678989898998767896587899875434765 +3234588989899969498965214987896789998999893234598763210123456789876567899979987656789476567996999876 +6545679876789898987654323498965678999998789456987654343234587895985456789567898967894353459989789989 +7959798765865767898885554569654567899987678967898765454345689924594368893456999989943212398765678991 +9898987874343456789876768678965678999876599978979876565456798765985457894577894598959101499754567892 +6767896543212345678987979789876989987765487899568997876569899879876567895678923987998912989663458993 +5456789652101256789698989892989898976521256789467898987678932989987678976989219896787899876562545789 +4345789863218789896569992901299787895432349894345989998899543891298889987894398785676989765421234599 +1235698754523698999678901292987676789765458943234567899978954789989999998995987654565678954310123459 +2376789765434597898989992989876545678976567894345979998767895679876789879789898543454789765471266867 +5488999878545986797899989876743434567899878987659898789856789798765678965698765432123499876562345678 +6789578987659875456789878965432123498967989998769765678943656987654567893987654321012987989673456989 +7892459898767954348898767896551012599459898999879874389432349897543456892198985432129896591989567896 +8901598789898965467897657897662156789349767899989543278921298765432346789019876543298789432399998925 +9212987653919876568998745698787345690198956789998784067890399764321235678923997854987678943989899014 +4323496542109997679459956789876556789987546990987665156789987654310124789434598969876567899878789123 +5654987673998698789349897990987697896543239891297543245678998765421249896595999198765434568965678934 +6785698789876549893298789321298789989432129789987654376789879878432556987989891098764323487894567895 +7896789898765434932197698934989896878941098691098765989898765986543477899876789199765412345993456789 +8987894929876545693296567899878965767892986589129876789997654397676589998975678987654325896789567895 +9199943212987676789985456789769874656789875478998998897986543298787699997754567898765434789899678934 +1098956323498987899876567898956212345678976567896569976987672109898789876543456789878965678998989023 +3987895454569999923987679967932101234589989678975456954398943212989899998754587892989878789987892134 +9876789565678989219898989456893212345678998789986347895239654323578998929995678990194989899856789645 +8765678978789678999789494345789924459789689899997468954398765434567897939876789789293299976745678957 +7654589899896567987654323234567895678993594909876579765459898545678986898987897678989198765534567898 +6543456799965456796543210125678998789301983212987989876589987656789765767998934569879989754323568999 +7955677989656345987876521436789219893219976323499298987678998769897654459899123699769876545213467899 +9876789878943245698987434545678999999398765454932127898789569899998943298767234987656987432102456789 +9987898967892134589998545656989789998999986579843456999893457989999892129654349876549876543212345699 +8799987655789023478987678987894599987891098798767897899912345679898789239765467999732987654325456789 +7698765434568934567898789898923989876789199899899929999101234598767678949876878987621298995434567992 +9569854323477895678909898769019874385878987956998919898919345987856567959989989099560129876545679543 +5498765412346789789212999652198763234569876545987898767898959876543459898998992198432234987987899954 +4329876101258999899854998943989753123778985439876789858967899987654598776787893987543456798998969895 +5210987612567899998769887899876541014569896598765678969458978998996987654456789598654567899329457789 +4329896523456789999898796598765432123456789699854569993234567899987998962367995459785678965412345678 +7439765434567899899987687439876563234567899987653456889135698967899876543467892349876789879523456989 +6549876569678998789876543312997654345678939798542345678956789456965987754578943656987897987634567891 +9656987678989498642989932101239767456789329654321234589767892367894398967989764767898956798765678932 +8767898989992349543498756322349878569895498765642355678998943456789239878998765678999547999896799653 +