-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
let data = require("fs").readFileSync("input.txt", { encoding: "utf-8" }).trim(); | ||
let lines = data.trim().split(/\n/); | ||
|
||
const openers = "([{<"; | ||
const closers = ")]}>"; | ||
const cScores = { ")": 3, "]": 57, "}": 1197, ">": 25137 }; | ||
let corrupted = 0; | ||
for (const line of lines) { | ||
let stack = []; | ||
for (const char of line) { | ||
const op = openers.indexOf(char); | ||
if (op !== -1) stack.push(closers[op]); | ||
else { | ||
if (stack.pop() !== char) { | ||
corrupted += cScores[char]; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
console.log(corrupted); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
let data = require("fs").readFileSync("input.txt", { encoding: "utf-8" }).trim(); | ||
let lines = data.trim().split(/\n/); | ||
|
||
const openers = "([{<"; | ||
const closers = ")]}>"; | ||
const iScores = { ")": 1, "]": 2, "}": 3, ">": 4 }; | ||
let incomplete = []; | ||
for (const line of lines) { | ||
let stack = []; | ||
let corrupted = false; | ||
for (const char of line) { | ||
const op = openers.indexOf(char); | ||
if (op !== -1) stack.push(closers[op]); | ||
else if (stack.pop() !== char) { | ||
corrupted = true; | ||
break; | ||
} | ||
} | ||
if (!corrupted && stack.length > 0) { | ||
let i = 0; | ||
for (const c of stack.reverse()) i = i * 5 + iScores[c]; | ||
incomplete.push(i); | ||
} | ||
} | ||
|
||
console.log(incomplete.sort((a, b) => a - b)[Math.floor(incomplete.length / 2)]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
((<(<{(<([<<{<[]<>><()>}<([]{})[<><>]>>({<{}<>><()()>})>{[({{}()}{<>()})]}]}>)[(({{<((<><>)([][]) | ||
{{[({{{[[<<{<([]{})({}())>}>([({()<>}{<><>})]{<(<>[])[()()]>{(<><>)(<>)}})><(<[[<>{}]<{}<>>]{<[ | ||
([{<[[((([([(({}))[<[]()>{[][]}]][<{{}{}}({}{}]><(<>{})([][])>])[<((()<>))>[<<<>{}>({}{})>( | ||
({<{<[<<((<({<[]>{{}}}{<<>[]>(<>)}){<{<>()}{[]{}}>[[<>[]][[]{}]]}>[<<[<>()]>>(<<{}()><<>()>>[{{}{}}{()[ | ||
{(<[{[<{[<{{{[{}{}]{[]{}}}[<()()><[]<>>]}[[<()()>{[]<>}}{{{}<>}(<>())}]}{<[<[]()>([][])]({[]{}} | ||
[<<({{({[[({{<[]()><<>[]>}{<(){}>{[]{}}}}[({<>()}({}[])){[{}{}]<()[]>}])[{<(<>[])>}<([()<>])((())(()[] | ||
<<<({<[[[({([<()<>>([]{})](<[]()>([]{})))(<[<>[]][[]{}]>([()()]<[]>)]})]{{{<[[()[]](<>())]{[<>{}]{< | ||
{(<{{{<[{{<<{[[]()][{}<>]}>(({[]}[()<>])([()<>][()<>]))>[{([()][[]<>])}]}}<([([[[][]]{[]<>}]{([][])({}())})] | ||
[[([<{[(<(<[[([]<>)]{[<>()](<>{})}]{{[{}{}]{{}{}}}}><<[([]{}){<>{}}]{[[]{}]{[]<>}}>(({(){}}){{()[] | ||
[<((<<[{<[(<[<()()>]<<()<>>([]<>)]>({{()[]}{()[]}}[{{}()}<<>[]>]))([([[]<>]<(){}>)[<{}{}>[[ | ||
[[{{<{({<[([((()[]){{}<>})[<<><>>{{}<>}]](<<{}[]>[[][]]>{{()}<[][]>}))<<(({}<>)([][]))>(<([] | ||
<[({<(<[(<(([{{}[]}]<[<>()]<()<>>>)({{<>()}{{}[]}}{({})({}[])})){[[{<><>}[{}[])][[[]()][()[]]]][{([]())[()()] | ||
<(<[{<[{{(<{{[[]]{<>[]}}}(({{}<>}<<>[]>)[<<><>>{()[]}])><([(<>())]<[()[]]{(){}}>)(<[<>[]][<>()]>({[]<>}[ | ||
<([[({[((({(<<()[]>{[][]}>(<()[]>(<>()))){{(<>()){{}{}}}[<{}{}]({}<>)]}}{{<([]){[]{}}>}[{{{}[ | ||
<[{[<(([{[<<((()()){<>()})((<>{}))>(([()[]]([]())){{()[]}[[]<>]})>](([<[[]<>]({}<>)>{(<>){{}()}}]))}( | ||
<([{[({{(<[[{<<><>>{[]()}}{<{}[]><()[]>}]<(<{}[]>{{}[]})>]>[{<<{[][]}<()()>>>[<(()[])<()[]>>[[()[ | ||
{<[{[{([[[(({{[][]}{[]}}<[()[]]<<>{}>>)<<(<>()){{}{}}><<[]<>>({}())>>)(((<{}[]>[()[]]){([]())((){})}) | ||
[{<[<([{([{(<{[]<>}>(<()[]>{{}{}})){<[[]<>]{()()}>{[[]](()[])}}}<{(({}{})<[]<>>){[()<>]}}>]<([({{}<>}({}[]))] | ||
{[[{({[({{[[({()()}<<>()>)[[()()]{{}[]}]][([[]<>][()()])<([]())([][]}>]]}})[((([(({}{}))<({}<>)<{}[ | ||
{<[{([[<{{<<({<>{}}<<><>>)<{<>}([][])>>[{([]())}<{{}[]}>]>}}<<{(({(){}}))}><([(<()[]>((){}))({()<>})] | ||
<(({(<{([((<<<()()>{{}{}}>{[(){}]([][])}><({(){}}[[]()])<<{}>>>)<<(<[][]]([]()))<{[]{}}(<> | ||
<<<[<([<([(((<{}{}>){<()[]>{[]<>}})({(<>())[{}{}]}))([(<()[]>{<>[]})<[(){}][()()]>])])>]<(<({{(<[]{}){()< | ||
({<(<{{(<([(<(()())<{}[]>>[[{}<>][<>]])]{([{[]{}}[[][]]]([<>()]{()})){<([]())[[]<>]>}}){{[{(()<>){ | ||
<{{{[<<(<<{([<()[]>([]())]<{[]{}}<{}<>>>)[[<[]{}>]{[{}<>]{<>{}}}]}<[<[[][]][[][]]>]({<(){}><{}()>}({ | ||
<[[<(([([(<{[({}{}){()[]}]{[{}[]]([]<>)}}[<(())[()]]]>([([()[]][()()])]([(()<>)(<><>)](([]())[{}{}]))))])]))( | ||
([<[<{[{[([((([])]({<>[]}{{}[]}))(<{[]{}}{{}()}><{()[]}[{}{}]>)])<[{[(()[])<()<>>]<<{}<>>(()<>)>}{({<> | ||
([{{{[<[({(({([][])[()<>]}{{[][]}[()[]]})({{[][]}<[]{}>}{{{}[]}<{}[])})){<<<<>{}>({}{})>{{ | ||
{<{[([{<[({<{{{}{}}}{{[]}{()[]}}>}{(([()[]][{}[]])]})]<<(<[{()<>}([][])][{<>()}]>(({[][]}{<>[]})[[[][]]])) | ||
[([<(<[{<({<({[]()}<<>{}>)<<()[]>[[]()]>>}[{<[()[]]{{}[]}>}<[[()[]]((){})]<({}{})[[][]]>>])[[[{(()())({}())}( | ||
{{<([([[{<<[<[[][]){<><>}>]>[<[[{}<>]<()[]>]{{<><>}[{}<>]}>[[<{}[]>{[]{}}](<<>{}>)]]><<{{({}<>)}} | ||
{{<[<<<[<(([((()[]){<>[]})]{((()[]))<[[][]]([][])>})[({(<>)[()()]}([()<>]{()[]}))])((<[{<>[]}]>))>]>(((( | ||
[<{(((<[{(<[<<<><>><{}()>>]><{{(()[])(()[])}}{[[<>()]<<>[]>]{{<>()}[()[]]]}>)((<[<[]{}><{}[]>]{{[]()}{ | ||
{[<([{(<{([<[{{}[]}<[]()>][<()<>>{<>()}]>(([{}()]([][]))({[]{}}([]())>)])((({[[]{}]<{}[]>})<[<<>()>{[]{}}][{( | ||
(((((((<[<[[[(<>[]){<>}][[{}()][(){}]]](<[{}<>]{(){}}>[<<>{}><(){}>])]>]{{({{[{}<>][<><>]}}( | ||
<<(<<<({<[{{[(()<>)[()<>]]}}{<[[<><>][{}]](<<>[]>{{}<>})}<<<{}[]>[{}]>{<[]<>><<>{}>}>}]{<<<<<>[]>[()()]>[ | ||
{{[<[{{[<<{[[<<>{}>[{}()]]][(<<><>>{{}<>}){([]<>)[()<>]}]}>{{[(([]()){<><>})<<[]()>{()[]}>]}{([({}[])[{}()]] | ||
({([{{{(((<[(({})[[]{}])(<{}[]>{{}[]})][<{()[]}(<>[])>[[{}[]]<{}[]>]]>([{{[]<>}{<>{}}}<<{}[ | ||
<{<{{{((<<[<<[<>[]]>{[<>]<[][]>}>[<[{}{}][()()]><([]{})((){})>]]>><{{[{<{}{}>[()<>]}(((){})[<>{}])] | ||
{[((<<<[{<[({{[]()}([])}({{}<>}((){})))<<<[]<>>[()<>]>>]>}]({<<{<[[]<>](<><>)>{{[]<>}[()<>]}}>><{{<<{ | ||
[({[[<(([[[(<([][])<<>{}>>((()())[[]<>]))]<{{<[]()>}[{<>}{{}[]}]}>]])[<<[<(<[]>)[[[]<>]<<>[]>]>{({[]()}<[]>)< | ||
[{[{({[{({[{{({}[]){()<>)}<[<>{}]{{}()}>}[{[()()][{}{}]}[[<>{}]<[]()>]]]{<{<[]<>>[(){}]}((<>{})[ | ||
<{<[[<<{([{[<[()[]]<<><>>>({()()}(<>()))]{<({}<>)[()<>]><[{}{}]{[][]}>}}{<<{<>()}[[]<>]>{<()[]>[() | ||
{({<[[(([(<{[<<>[]>]}>[{{{{}()}{{}[]}}({<>{}}([]<>))}{{[[]{}][[][]]}}]){<<[[()()][(){}]][<{}[]>[()[]]]]{[[< | ||
[{<({([<[{{[({()})<({}<>)<{}()>>]}}{[[[{()[]}<<>[]>](<[]()>{[]<>}}][[(<>{}){<>{}}]{{[][]}[ | ||
[<{([(([([(([<{}<>>]([[]<>]<[]>))(({<><>}<{}[]>){[<><>][<>{}]}))])][<<{[<[(){}](<>[]]>]}[[{[[]()]}(([] | ||
<({({{{({<{[[{[]()}[()[]]]](([{}{}](<>())){{<>()}{{}[]}})}[(<[{}{}]<<>{}>>({[][]}))<<{{}[]}(()<>)>> | ||
(<[<<[{({[<<[({}[])[()[]]]<[()[]]<<>{}>>>>][[((<<>[]><()>)(<[]()><<><>>))]]}([[{<<<><>>(())>{{()()}}}{ | ||
{<(({[{([((<[[()][<>]]>[<(<>[]){()<>}>{[<>{}]{<>{}}}]){<([(){}][<>()])<({}<>)[[]{}]>>{(<[]<>><{ | ||
[[({([<<[{<({({}<>)({}())}){{{<>{}}(<><>)}<{{}{}}(()<>)>}}<<<[(){}]<<>>>((<>[])[[]<>])><<{[]<>}[()()]>{{(){} | ||
{({<([<([(({[({}())[[]()]]<[<>]{{}()}>}{[[{}<>]{<>[]}]<{{}[]}{{}{}}>}))({<([()[]>[[]]){<<>( | ||
[[{[[{{[{{<(<<[]<>><<><>>>[{[]}({})])<(([]()){()[]})>><[[<(){}>(<>())]][{{<>()}[[]()]}{[<>()]}]>}<{{(<<>{}> | ||
[{<[{(<({{{<{<(){}>(()<>)}>({[[]()][{}[]]}[[{}[]][()<>]])}{<<[(){}]{{}{}}>[[()<>]<<>()>]><<<[][]]([]<>) | ||
<<<({<{[<{<[<(<>{}){{}()}><[<>{}]>]>[(<{[]{}}({}())>)[(([]()){()()})<<(){}>{<>[]}>]]}[{({{[]{} | ||
{[((({[<{(({[([]<>)(<>)](<[]{}><(){}>)>[<{{}<>}([]())><((){}){()[]}>])({((()())(<><>))({()[]}{{}<>})}{<{ | ||
<([(({(<{<<[[<<>{}><[][]>]<[[]<>](<><>)>]{{((){}){<>{}}}[<{}()>([])]}>>}{{<<<(<><>){{}<>}>{{{}[]}}>[{ | ||
{{<{(((({<{{[({}{})][<<>()>[()[]]]}}{[{<[]()>(<>{})}[[{}<>](()<>)]]<[[()[]]]([[]()]<<><>>)>}]})<(<<([<[]{ | ||
(([((<[{(({[[[<>[]]{[]{}}](([]<>))]{{<[]{}][<>[]]}{<{}{}>({}())}}}<(({[]()}{[]{}}){(<>[])}){[<(){} | ||
{<(({{([{{{<((<><>)({}()))>([<<>()>({}())]{{<>[]}[<>{}]})>}}(({[<<{}<>><[]<>>>[<<>{}>{<>[]}]]({(()<>){<>[] | ||
<<<{{(((({[((<{}[]>[()<>])((<>[]))){[{[]{}}]{((){})([]())}}]}){([<<[{}()]<[]()>>[[<><>][{} | ||
[[{({[({[<<{[<()()><<>{}>]{[{}{}][[]()]}}><[{{<>[]}<()[]>}]>>]}<(({{{[<>()]<[][]>}[{[]<>}[[]{ | ||
<{({({{{{[{(([[][]](<>()})<[<>{}]{<>()}>)<(({}())(()<>))>}]}[((<(<{}{}>)({<>{}}{<>()})>{(([][]))<(()[ | ||
((<(([[<{{<[{<(){}>}{<()()><<><>)}][{{<>}}]>{([(()[])(<>[])]{({}{})(()<>)})<[({}()){{}[]}]>}}({ | ||
<{((([{([(<([[<>[]]([])](([]())[()()])){{{[]()}[<>()]}}>)<{{<<<>{}><()()>>)}(({({}[])<()()>} | ||
<({[[({[[({<[<[][]>]<[[]<>]([]{})>>{[{{}{}}((){})]}}<[<<[]{}><{}()>>][<({}[]]{[]{}}>{(<>){[] | ||
<<[<{{[<[<[[[{{}[]}[[]()]]]<({{}<>}(()<>))<[()[]]<[][]>>>](([[<>{}]([]())][<()<>>[{}{}]])<<<[]{}>{{}{}}>>) | ||
{([{{<([([<[{({}())(<><>)}[({}}<<>()>]]{<<<><>><[][]>>{<()><{}<>>}}>(<{[(){}]<<><>>}[([][])([]())]>(<<( | ||
(([[[({{{{{[<<{}()>><<{}()>>]{<{(){}}{<><>}>[([]<>)<()[]>]}}{(<{[]{}}[[]<>]>)[([[]<>][[]<>])([() | ||
{[<<[{[[({[{{{[]}}<[{}[]]({}())>}([((){}){()<>}]{<<>{}>{<><>}})]{[<{()[]}{()<>}]{(<>[])<{} | ||
<({<[([[({<<{{{}{}}([]{})}[[{}]{<>}]>{{({}[])<()<>>}<[<>{}][[]{}]>}>[<({{}<>})<<{}[]>({}())>>]})]]<<[<[{{[() | ||
[(<{<{(({[{{<{()()}<[]{}>>{([]())[{}[]]}}{<({}{}){[][]}>{(<><>)[{}{}]}}}[([({}<>)<{}{}>]<[ | ||
<[{(<{<{({[{((<>[]){[]})({{}<>})}]}[{[<{<>[]}<{}()>><[[]<>][[]<>]>]<[<[]()>([]<>)}(([][])(<>< | ||
[{[({([<<[[<<<{}<>><[]{}>>]][<{<<>()>((){})}<[{}()]<<>{}>>>{(<(){}>({}()))(<()()>{()})}]]>[({[([[]{}]{()[]}) | ||
{[({<[(<{(<[{<()()><()()>}(<{}()>{<>[]})]>([(<{}{}>(()<>)){[<>[]][{}()]})[{(()<>)({}[])}<(<>{})(<>( | ||
<[(({{([[(((<([]{}){()<>}>{[()<>]{[]<>}}){[<[]()><[]>][({}[])[<>()]]})[(<(<><>)(()[])>{{[]()}{[]{}}})[{<<> | ||
[({<({({(<{{{<(){}>[<><>]}(<[]{}>(()[]))}[[{{}[]}[[]()]]<[{}[]][()[]]>]}[<(<()[]>{{}})[[[]<>]]> | ||
<<[(<{<((<[<<<(){}><(){}>>({{}<>}([]()))><{{(){}}{()}}<<<>()><{}()>>>](<<(()())[[]]>(<[]()>([]<>))><(< | ||
{(({([[<{[{<(([][]))>{(((){})[[]<>])[(<>{}){{}[]}]}>[<({[]{}})[(()()){<>()}]>]][{[(((){})<()<>>){<<><>>}]<<<[ | ||
({[{{<[(<[({<<[]<>>{[]}>(<[]()){()()})}){{{{()()}(<>())}<[()<>](()())>}(({[]<>}([]<>)){[{}[]]})}][{{{[[]<>]{ | ||
(<([[{([{<{{<{()<>}[()()]>[([]())[()<>]]}}<([{(){}}{<>{}}]((<><>))){{{(){}}{{}<>}}}>>}({[(<{[]{}}[()<>]>{[[ | ||
{{(({[{[{[[{[{<>{}}]<[()[]]<{}<>>>}{<(()[])([]{})>({{}{}}<(){}>)}]{<(<<>()>(()[])){<{}()>< | ||
({<((<<<{{({[[<>]<[][]>]((()[])[<>()])}<([<><>]<<>()>)(([][]))>){(<<{}[]>[(){}]><([])({}()}>){{<[]<>> | ||
{[<[([(<<(<{[[[]{}]<[][]>][{<>}(()<>)]}<[{()[]}{[]<>}]>>[{[({}[])([]{})]((<>){<><>})}((([]<>}[<>[]]))])({( | ||
([{[[[{(((((<(()[])<()<>>>{<[]()>[{}<>]})(({()()}({}<>))<[[][]]<<>{}>>))<<({<>()}([]()))<<[]<>>[<>[]]>>[<{( | ||
[{{<({[<<{[<<(()())[[]]>{{[]<>}[[]<>]}>{[<<><>}]}](<{[<>[]][{}[]]}<{[]{}}[[]{}]>>)}><<({{(()())([ | ||
({{{[(([<(((<{<>}(<>())>)[((<>)<[]{}>)[([]())[()[]]]})<(((<>[])[{}]){{[][]}[()()]})<[(<>{})({}()) | ||
<[({[<[[{((({[[]<>][<>{}]})(<[{}[]]{[][]}><{()()}{<>{}}>)){(<({}())[<>()]><{[]()}{{}()}>)})}]( | ||
<{(([<[{<<<{<<[]{}>{<>()}>}({<<>[]><[][]>}(<()<>>))>>{([(([]{}){[]{}})((()[])[[][]])][{<()>}<[(){ | ||
{(<[({{(<{{<[({}{})<<>()>](([]<>)[{}])>[([{}{}]<()<>>)[[{}<>](()[])]]}[(({[]<>}){{{}[]}[[]{}]}){[<{}<>>([]<> | ||
{(<[{<[<<<{([<[]()>[<>[]]]{{<>{}}([]())})}<[[({}[]>([]())]]<([<>{}]{[]<>})>>>>[[<<<{<>{}}<()()>>([{}])>{<(( | ||
[{<{(([[[{<[(((){})<{}[]>)<{<>{}}>]<<<()<>>{[]{}}><<{}()>>>>}]]<<<[[(<()[]>{[]{}})][<{(){}}[<>{}]><<{}> |