diff --git a/functions/src/day10.ts b/functions/src/day10.ts new file mode 100644 index 0000000..5f9881d --- /dev/null +++ b/functions/src/day10.ts @@ -0,0 +1,87 @@ +import Day from "./day"; +import Utils from "./utils"; + +const MISSING_CHARACTER_SCORES: {[key: string]: number} = { + ")": 1, + "]": 2, + "}": 3, + ">": 4, +} + +class Day10 implements Day { + + part1(input: string[]): number | string { + + return Utils.sum( + input.map(s => this.checkSyntax(s)) + .map(r => r.error === "corrupt" ? this.getCorruptCharacterScore(r.got) : 0) + ); + + } + + part2(input: string[]): number | string { + + const incompletenessScores = input + .map(s => this.checkSyntax(s)) + .filter(r => r.error === "incomplete") + .map(r => r.error === "incomplete" ? this.calculateIncompletenessScore(r.missing) : 0); + + incompletenessScores.sort((a, b) => a - b); + return incompletenessScores[Math.floor(incompletenessScores.length / 2)]; + } + + calculateIncompletenessScore(missing: string): number { + + let score = 0; + for (const missingElement of missing) { + score *= 5; + score += MISSING_CHARACTER_SCORES[missingElement]; + } + return score; + } + + getCorruptCharacterScore(char: string): number { + switch (char) { + case ")": return 3; + case "]": return 57; + case "}": return 1197; + case ">": return 25137; + default: throw Error(`Can't find points for character '${char}'`); + } + } + + checkSyntax(line: string): { error: "corrupt", expected: string, got: string } | { error: "incomplete", missing: string } | { error: "none" } { + + const stack = []; + for (const char of line) { + + switch (char) { + case "(": stack.push(")"); break; + case "[": stack.push("]"); break; + case "{": stack.push("}"); break; + case "<": stack.push(">"); break; + default: { + const expected = stack[stack.length - 1]; + if (expected === char) { + stack.pop(); + } else { + return {error: "corrupt", expected, got: char}; + } + } + } + } + if (stack.length === 0) { + return { error: "none" } + } else { + let missing = ""; + while(stack.length > 0) { + missing += stack.pop(); + } + return { error: "incomplete", missing } + } + + } + +} + +export default Day10; \ No newline at end of file diff --git a/functions/src/index.ts b/functions/src/index.ts index e4525c1..2470077 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -11,6 +11,7 @@ import Day6 from "./day6"; import Day7 from "./day7"; import Day8 from "./day8"; import Day9 from "./day9"; +import Day10 from "./day10"; // // Start writing Firebase Functions @@ -36,6 +37,7 @@ export const day = { 7: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day7(), request, response) }), 8: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day8(), request, response) }), 9: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day9(), request, response) }), + 10: functions.region("europe-west1").https.onRequest((request, response) => { processDay(new Day10(), request, response) }), } diff --git a/input/day/10-example.http b/input/day/10-example.http new file mode 100644 index 0000000..3f57275 --- /dev/null +++ b/input/day/10-example.http @@ -0,0 +1,15 @@ +POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-10 +Content-Type: text/plain + +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] + +### diff --git a/input/day/10.http b/input/day/10.http new file mode 100644 index 0000000..e2d2efc --- /dev/null +++ b/input/day/10.http @@ -0,0 +1,99 @@ +POST http://localhost:5001/advent-of-code-2021-911a8/europe-west1/day-10 +Content-Type: text/plain + +({[[{[(([({((<[]>{[][]})[{<>[]}{[]{}}])[<[<><>][[]{}]>{[{}()][{}]}]}(<[{<>{}}[()]]<[{}[]]>><[{()}({}())] +[[<([<<<((([<<()()>{{}()}>[[()<>]]]{<{<>[]}(()<>)>[<{}{}>[{}]]}){{({<>()}{[]})<[[][]]>}[{[ +[{{<{({<<({{{<[]><<>[]>}{{()()}(<>[])}}<[{(){}}<<>{}>]((<>[]){{}{}})>}([[{[]<>]][(<>[])(()<>)]])){[ +[<([({{{<{[[[{{}{}}]{<[]()>([]}}][({()()}){<()<>><(){}>}]]<<(<[][]>({}{}))([[]<>]<[]()>)>{({<>[]}{ +[{(({<({(<(({<[]{}>(<>())}[([]()){()<>}]){<<(){}>{()<>}>[{<>{}}{<>()}]})({[<<>()><{}[]>]})>)[{([[{(){ +(<(({[<{[{[{<[()<>](()[])>}[<[()<>]((){})>([<>[]][[]{}])]]([[[[][]](()[])]<(<><>)<[]<>>>][{(<>{})}<<[]{}>[{}( +<[[<[{[<[[<<([{}[]][{}<>])<<<>()>([]{})>><((())[(){}])({<>()}{[]()})}>({(([]{})<{}[]>){{(){}}[[][]]}} +<{(<<<<{{({{((()())<<>{}>)([<>()][[]()])}[[{[][]}]<<()<>>[()<>]>]})}<<[<<({}<>>[[]<>]><[()[]]{{}{}}>>]<< +<[[[{<({(<{[((()()))]<{<()>{<><>}}((<>{})[{}[]])>}>)>)<((<<[{([]<>)([]<>)}[[[]{}](()[])]]({{()[]}})>{ +{[{[<<[<[<{[{{{}[]}[[]()]}{[[]<>]([]<>)}]<((()<>){<>()}){<[]()>{[]()}}>})<[{<{[]{}}[<>]><([] +<(<{[[{((<({((<>())<()<>>)<(<><>)<(){}>>}]({<[{}{}]{()<>}><<()<>>(<>())>}[{[<>[]][<><>]}<({}()){()()}> +[(([<{<[({{[<[(){}](()[])>]<<<[]()>{{}[]}><{()}(<>())>>}[<[{{}()}[<>{}]]>(<[<>[]]<()[]>>{<()><{}<> +{<[<<[{[([[[{<{}<>>{()[]}}[<[]()>([]<>)]][<{[]{}}{[]<>}>]]{[([(){}]{()<>}>{[<>{}][<>()]}]{<[{} +[[<{<<{{{(<({[<>[]]}{<(){}>[<>[]]}){([[]{}])({[]{}}(<>[]))}><<<{[]()}{<>()}>{(<>)<()[]>}>[[({}<>)(( +<(<(({<<[(<[{([]()){[][]}}<(()[])>]><[[{()<>}[()[]]]({{}<>}{<>()})][(<[]()>[{}()])[([]{})[ +(<[{<<[{<[([([()[]][[]{}])]{{(()<>)}[[(){}](()<>)]})]>([(([([][])[<>[]]]<<[]<>>>)[{<()[]>(()[])}({< +({(([<<<<<((<{{}{}}<()<>>>[<[][]>[{}<>]])([(<>()){()[]}]<<<><>>[{}<>]>>){([[{}{}][[]()]]<<[][]><[] +(((<{({{([[<<<(){}><{}<>>>>[{{[]<>}(()[])}(<[][]>[()])]]]<(({{{}[]}{<>{}}}[{<>{}}[()()]])[<[{}<>](() +<(([<<{[[{{{{(()[])([]{})}}{<<{}{}>{{}<>}>[{{}{}}<()()>]}}[{[((){})((){}]]}{<(<>[])<{}[]>>[<[]<>>({}<>)] +{([{([{[{<([<<{}()>[(){}]>(<<>{}>[<>{}])])[({[[]()]<[]{}>}<{{}{}}>)<[{<><>}](<[]<>>[{}<>])>]>}]}])[{{{[ +[(<{(([{{{({([[][]][()[]])}<<<()[]><()>><(<>{})<<><>>>>)<{[[<><>]<{}[]>]}>}([(<{<><>}{{}[]}>[[<>()](<><>) +(<(((<{(<[({([<>[]]{{}{}}){[[]{}][()<>]}}{[{(){}}[()<>]]<<<>[]>([][])>})][<{<[[]<>]((){})>([<>()]<(){}>)}>]>) +[{{[(({(<<[({{{}{}}<[][]>}([<>][[]{}]))]<<((<>())<()[]>)<({}())<{}[]>>>>>>)}{<<[{[[(<>()){<><>}]<( +<[[[((<{[<{(<[[]()]>){[{()<>}[<>()]]<{<><>}[[]<>]>}}[<([{}{}])({[]<>}[[]])><({()[]}(<>{}))[< +<{<<[<[<(([<{<(){}>({}())}{<<>>[[][]]}>[(<{}()>{(){}})(<[]><{}{}>)]]<((((){}){<><>})<{{}[]} +[(<[<(<{({{<(<{}{}>{{}()})<{[][]}{{}[]}>>(<((){})(()[])>{(<>())})}})<<<[[([]<>)([]<>)]{(<> +(({<[<{{<{<[(({}())[[]{}])<{<>[]}(()<>)>]([(<>[])<[]()>]([[]()){{}()}))>}>[({<(([][])({}{}))<{{}[]}[<> +((({({{[[(<{<<[]()><[]{}>><(()[])[{}()]>}[{[<>()]([])}{{{}{}}<()()]}]>)<{<[[<>{}][[]()]]({<>()}{<><>})> +{[[[[((<<<{[{([][]){<>{}}}[<<>()>([]{})]](<((){})<{}()>><([]())[<><>}>)}{{[[(){}]]{<<>[]><[]{}>}}<({(){ +({[[[(({<({[{[<>[]][{}{}]}<<<>>{<>{}}>]<{[{}<>]{()[]}}>}<<([[]{}]({}{}])>{[[<>[]]({}<>)]([{}[]] +{{{({{[{({{<{{()[]}{{}[]}}{<{}{}>}>{(({}())[()<>])((<>{})([]))}}}]<<[(((()<>)<(){}>){[[][]]{{} +{({<{<[{({[{{<<>>[{}{}]}[[<>[]]<{}[]>]}[<[()()]{[]}><([]<>){<>[]}>]]})}[({<[<<{}<>>(()[])>]<({{}( +[{((<{<({[{<[(()()){<><>}](<{}{}>{[][]})>[{<{}()><[]()>}]}][(<({[]{}})>)<<[({}{}){[]()}]{((){}){()[ +{<<<{({[(<{<{<()<>>([][]]}<(<>[])[()[]]>>}>)]}[{[{[{[<{}[]>]({()[]}<()()>)}][[[[{}()]{<>{}}][[[ +{[{{{{[((<{[[([]<>){{}[]}][<[]{}>({}{})]][[{<>()}[()[]]]<<[]{}><<>{}>>]}{{{[[]<>]<{}()>}[[[]()]{<>() +(([<[[[{{((<(<(){}>[[]<>]){{<><>}([][])}>{[{{}[]}{()[]}]<<{}<>><[]>>}))}({({[[<><>]<[]{}>][{< +[(<({[<[<<(<{([]{})(<>())}<{{}[]}{[]{}}>><(([]<>)[(){}])>)[<{[[]{}]([]{})}[<[][]>(()<>)]>{<{<>()}(()<>)>(<( +[{<(<((<{[(<{<<>()>}<<<>[]><<><>>>><{<()[]><()()>}{[(){}]{[]{}}}>)<{([<>{}][()>)<{[]{}}([][])>}>]({{<({} +({(<[<<{<(((<[{}<>]<<>()>><([]<>)>)[[<<>>{{}<>}](<{}<>>)])[{<[{}]<{}{}>><<[]<>>{[][]}>}}){(({( +(([([{{<<<{({[<>[]]<<>>}({()<>}))({(<>{})(()[])))}((<([]<>){<>[]}>(<[][]>{()<>})){([()()]{(){}})((<>())<{ +[{{[<<<((<[([{()<>}<()[]>][(()<>)])({<()<>>({}<>)}{{()<>}{()()}})]>[(<([()[]]<(){}>){[{}<>]{{}{}}}>[([ +[(({<[[[<<[[{[()()][{}()]}[[()<>]([]<>)]][<<<><>>[[]<>]><[()<>]<<>()>>]]([{{()[]}{()()}}]{([()[]]({}))<<[] +<[<(<(({<([<<[[]()]<[]<>>>[{[]()}<(){}>]>{<[{}[]]{[]{}}>[<{}<>>]}][<(<{}<>>{()<>})[{<><>}{[]()}] +[{[<<{{{{{{{[{[]<>}{[]{}}]}[[(<>())([][])]<({}[]}[{}[]]>]}{{<<[]<>>[[]<>]>}[[([]{})({}{})]([ +({[({[[{{[{[[{<>[]}(()<>)][<()()><[][]>]]{{{<>[]}<<>[]>}<[<>{}]<[][]>>}}<(<{[]{}}[{}{})>([{}{}](<>{})))([({} +<[(<{({<([<[{<(){}><<>[]>}{[[][]]<[][]>}]{<([]{}){[]<>}>}><[[<<>{}><{}[]>]]{<<()<>>{<>()}>({{}<>}<<>[]>)} +<[({({[{[<<{<([]())({}())><[()[]](<>{})>}>{{[(<>{})({}{})]{[[][]]{()[]}}}<{[<>[]]{<>()}}[{<>()}([][] +((({([<[<{{[<<{}[])[{}{}]>({{}()}{()<>})]{<<{}()><<>()>>(((){}){[]<>})}}([([()()]{<>{}}){({}[])[{} +(<((<([{<(<<{<<>[]>[{}{}]}[{<>}([]<>)]>(<{[]{}}{{}()}><(<>[]){()<>}])>{({[()<>]{<>{}}}[[[]<>]{<><>}]){ +<({<[[([<<[(<[()[]]>)({(()())(<>{})})]<(<[{}()]{{}[]}>)>>{[[{<<>()>[<>()]}{([]){<>()}}][<<[][]><(){} +((({<[<[{<(([({}()][<>()]]))[<[[()]<{}<>>]({[]()}(<>[]))>]>(<{{<<><>><()()>}({[]()}([]()))}(<{{}()}((){})>({{ +[{<<{(<{{{{[{<()[]>(<>())}](({[]{}}{(){}})<({}())[[]<>]>)}{<{[[][]]>{[()()]<<>()>}>[(<{}[]>[()()] +({[((<{<<[(<[{(){}}([]<>)]<(<>{})(<>())>>{{(<><>)(<>())}[<()><{}{}>]}){{{{<>()}<()()>}{[[] +<[[[{([{((<<({[][]}<()[]>)>><[(<<>{}>((){}))]{[<[]{}>({}<>)]({()[]}(<>{}))}>)<({{([]()){{}{}}}})<({<<>{}>{( +<<({<<[[<((<<{{}()}([]()]><{()<>}<<>()>>>({<<>{}>({}<>)}{<[]<>>[<><>]})))<[[<<<><>><()()>>{<<>{}>}]([< +([<{{<(<{{(<{[[]()][{}<>]}[(<><>)]>)}{({[(<>[]){{}{}}]<{{}{}}[(){}]>})[{({(){}}(<>()>)<[[][]][{}< +<[<[([[<<[([{[()<>]<[]<>>}<{()}[()]>])]<{<[<<>[]>[[]()]]<([][]}{<>[]}>>[<<<>{}>>{<{}[]>({}())}]}<[<<[]<>>>< +<{<([[(([{<<[{{}()}[<>()]}>{<[<><>](<>{})><<<>[]>>}>(([{()()}(<>())]<[<>{}]>))}]{{{[([[]<> +[{<({<<[((<{<{{}<>}[[][]]>[[{}()]<()[]>]}>{{{{[][]}<[][]>}{({}<>)[(){}]}}(([[]]{()[]})[[()<>]{ +<([(<<(([{{<({<><>}{()}}{{<>()}[{}<>]}>[<({}[])(<><>)><{{}[]}((){})>]}<(<{<>{}}({}())>)>}[{( +[{<(<({[[({({<(){}>[[]{}]}({<><>}[[]{}])){[<<>[]><()>]<[{}()]({})>}}[[({{}[]}<{}[]>]]<{<<>[]>(<><>) +[({{<[[<({<{(({}<>)[()[]]){{<>{}}<()[]>}}{(<<>[]>{<>[]})({<>[]}<()[]>)}>><{(<[()][(){}]>){((<>[]){<><>} +({{[{[{<<{((({(){}}){[<>{}]}))<<<{{}[]}{<>[]}>((()[]))>{{<{}{}><()>}[{{}{}}<<><>>]}>}><[(<[((){}){<>() +<[{<([[<[[[{<[<>[]]({}<>)>[{<>[]}[{}{}]]}<{<<>{}><{}>}[{{}()}[[]{}]]>]([(((){})[()[]])[<(){}><() +({{<<<([[([[<(<>{})([][]>>]]([[(()[]){{}()}]][{<()()>[<>{}]}[{[]{}}((){})]]))(<<{<{}><[]>}(<<>()>({}<>))>[ +{<{<<[<(([[[<{[]{}}[{}{}]>]{<<()[]>[(){}]>}]]<({<<()[]>{<>[]}>}{<[{}{}]{<>{}}>{[<>()}}})<<< +(({[<[[{<<<({((){})}[{()}({}[])])>>>{{{({(()<>)[[]<>]}[<[]<>>[{}[]]]){[<()[]>][(<><>)]}}}<(((<[]() +<{[({{{<([{[{{{}<>}}]<<[[]]({}{})>>}[<({[][]}<{}[]>)<{{}[]}>>]])>(((((<{()()}<<>[]>><[(){}][<>{ +<<(([<<[[[[({<<><>>[{}]}[{{}[]}{[][]}])][((((){}))[<(){}>])[({()()}[[]()))]]]{{[((<>)[{}{}])({{}[] +<<({<{{(<(({<((){}){[]()}>}){{{[{}()]}{(())<[][]>}}<[[()]](<{}()>{{}{}})>}]><<[({{<>[]}[(){} +([{{<[(<[([<[{{}{}}{<>[]}][(<>[])]>](([[()<>]([]())]((()()){{}[]}))<([()<>]<<><>>){<<>{}>(<>{})}>))<{{{<()[ +{[[[[([{[{[{{<<>()><(){}>}({<>()}[{}[]])}([{(){}}<{}{}>]({<>[]}(<>[])))]}({[[[()<>]{[][]}]]}<{<{<>()} +[<(([<<((<(((<[]()>{[]{}})<{()<>}([]{})>))<(([(){}]<[]{}>)<<{}>({}())>)((<{}()>{()<>}){({}{})<(){}>})>>((<<(< +[([[({<([(<((({}()))[{{}<>}{()[]}]){{{{}()}(<>{})}<<{}()>{<>{}}>}>)<([[[{}](<>{})]<[[]()]<<>[]>>] +(({(<<[<{(([((()<>)([]()))])<{(([]<>){()<>})[{{}<>}[{}<>]]}>)({([{<><>}({}())])})}(<([([<>{}])<<< +<(([([{[[{{<[<()>](({}()){<>[]})>[[({}[])]{<()<>>{{}}}]}[<{{[]<>}[[]()]}>]}]](([<<{((){})(<>)}{[()[]][( +({(({<<[{{[{<<()[]>{[]()}>([<><>]<{}>)}<(<{}()>[[]()])[<[]}[()]]>]}}{[{(([()()][{}<>]))}<[([[][]][<>{ +{(([({<<[[{<(<[]<>>([]<>)][<<>()>[[]<>]]><{(())[(){}]}<(()[]){{}[]}>>}[<[(<><>)(<>[])]<{[]}>><[[()[]][[]]]((< +{{(<[[({(([[(<{}<>>(()())>{<(){}>{<>{}}}]{[{()[]}{[]{}}]}]<[[[<><>][[]<>]][{{}{}}]]<([{}{}]<<><>>)< +[<<{<(<[([[(<<()<>>([]{})>(<{}<>>{[]<>})){<[<>{}]<[][]>>(<()[]><<><>>)}]<([(<>()){<>{}}](< +([[{{([[([{[<<()<>>[()[]}>[[<>[]](<><>)]][{{{}()}{{}{}}}(<(){}>[{}()])]}]){{{{{((){})[{}<>]}<{{}<>}[[] +[[{(({{{<[(<{(<>())<[]<>>}[([][])]>)][<[({()()}(<>())){[{}<>]<[]()>}]}[[{{(){}}{()()}}]{[{{}{}}[{ +({<{<{[<<{({{{{}{}}(<>())}<{[]}<{}{}>>}(([[]{}]([]{}))))}>>]}<{{<({[<([]())>(({}<>)<{}<>>)]}<[<(()()}{{}()} +<<[[<[<[[{({<[<>[]][()()]>{<{}()>{<><>}}}[{([][])<()<>>}<{{}()}([][])>])[{[[{}]({}{})]{{[][]}([][])}} +[({[[[[[[({{{<()<>>}}[[(()())({}[])][{[]{}}[()()]]]}{{{{[]()}<[]>}[[<>[]]<<><>>]}<[<{}<>>(<>)]<{<>{} +{[[([([({{[{<{{}[]}{()[]}>}{({<>()})<<<>{}><{}{})>}][[<[{}{}][()<>]>{[[][]][<>[]]}]{[{()[]}[{}()]]({[]<> +{{[[<{({({[[<<<>{}><{}[]>><([][])(()<>)>]]}<([{[<>]<<>{}>}<[{}[]]([][])>][<({}{}>([]<>)>{[<>{}]}])[ +({{[{{(({{<({(<><>)<[]{}>}[[{}[]]<[]{}>])>(([{{}()}{[][]}]{{()()}<{}<>>}){<{{}[]}{{}{}}>{(()())}}) +{({<{[<<<{<<<{{}()}[[][]]>{<()>(<>())}>>({[[<>[]]<(){}>]<[[]{}]{<><>}>}<[(()[])<<>[]>][<[]()>]>)}({< +{<{([[<[<{{[[[{}<>]<(){}]][<()>(<>{})]]<<<()[]>[<>[]]>>}}{(<{{[]<>}<<>[]>}(<[]<>>)>([{()()}[[]<> +<<((<<<<{<<{({<>[]}({}<>))}(([<><>]{<>()}>([<>[]][(){}]))>><(({<()[]>}({[]<>}[<>[]]))<({(){}} +[{<({<<<{({[{[[]()]<<>>}{{[]()}[<><>]}]}{(({<><>}<[][]>){({}{})(<>{})})})}{<<({(()())<[]()]})([{<><>}<()< +(<((([(<<<<<{[{}{}][{}{}]}(<<><>>[[]<>])>{<{<>{}}<<>()>><{<>[]}[{}{}]>}>(((<<><>>({}())))([<<>[]>]<<{}()>([] +<[{([<<<{{[<<({}())[<>]>[{[]{}}[()]]>{{[<>{}]{<><>}}<({}[]){()<>}>}]}}[{{({{()()}(()[])}<<[ + +###