Skip to content

Commit

Permalink
day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
constb committed Dec 10, 2021
1 parent aae21bd commit 76f433e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 10/index.js
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);
26 changes: 26 additions & 0 deletions 10/index2.js
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)]);
90 changes: 90 additions & 0 deletions 10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
((<(<{(<([<<{<[]<>><()>}<([]{})[<><>]>>({<{}<>><()()>})>{[({{}()}{<>()})]}]}>)[(({{<((<><>)([][])
{{[({{{[[<<{<([]{})({}())>}>([({()<>}{<><>})]{<(<>[])[()()]>{(<><>)(<>)}})><(<[[<>{}]<{}<>>]{<[
([{<[[((([([(({}))[<[]()>{[][]}]][<{{}{}}({}{}]><(<>{})([][])>])[<((()<>))>[<<<>{}>({}{})>(
({<{<[<<((<({<[]>{{}}}{<<>[]>(<>)}){<{<>()}{[]{}}>[[<>[]][[]{}]]}>[<<[<>()]>>(<<{}()><<>()>>[{{}{}}{()[
{(<[{[<{[<{{{[{}{}]{[]{}}}[<()()><[]<>>]}[[<()()>{[]<>}}{{{}<>}(<>())}]}{<[<[]()>([][])]({[]{}}
[<<({{({[[({{<[]()><<>[]>}{<(){}>{[]{}}}}[({<>()}({}[])){[{}{}]<()[]>}])[{<(<>[])>}<([()<>])((())(()[]
<<<({<[[[({([<()<>>([]{})](<[]()>([]{})))(<[<>[]][[]{}]>([()()]<[]>)]})]{{{<[[()[]](<>())]{[<>{}]{<
{(<{{{<[{{<<{[[]()][{}<>]}>(({[]}[()<>])([()<>][()<>]))>[{([()][[]<>])}]}}<([([[[][]]{[]<>}]{([][])({}())})]
[[([<{[(<(<[[([]<>)]{[<>()](<>{})}]{{[{}{}]{{}{}}}}><<[([]{}){<>{}}]{[[]{}]{[]<>}}>(({(){}}){{()[]
[<((<<[{<[(<[<()()>]<<()<>>([]<>)]>({{()[]}{()[]}}[{{}()}<<>[]>]))([([[]<>]<(){}>)[<{}{}>[[
[[{{<{({<[([((()[]){{}<>})[<<><>>{{}<>}]](<<{}[]>[[][]]>{{()}<[][]>}))<<(({}<>)([][]))>(<([]
<[({<(<[(<(([{{}[]}]<[<>()]<()<>>>)({{<>()}{{}[]}}{({})({}[])})){[[{<><>}[{}[])][[[]()][()[]]]][{([]())[()()]
<(<[{<[{{(<{{[[]]{<>[]}}}(({{}<>}<<>[]>)[<<><>>{()[]}])><([(<>())]<[()[]]{(){}}>)(<[<>[]][<>()]>({[]<>}[
<([[({[((({(<<()[]>{[][]}>(<()[]>(<>()))){{(<>()){{}{}}}[<{}{}]({}<>)]}}{{<([]){[]{}}>}[{{{}[
<[{[<(([{[<<((()()){<>()})((<>{}))>(([()[]]([]())){{()[]}[[]<>]})>](([<[[]<>]({}<>)>{(<>){{}()}}]))}(
<([{[({{(<[[{<<><>>{[]()}}{<{}[]><()[]>}]<(<{}[]>{{}[]})>]>[{<<{[][]}<()()>>>[<(()[])<()[]>>[[()[
{<[{[{([[[(({{[][]}{[]}}<[()[]]<<>{}>>)<<(<>()){{}{}}><<[]<>>({}())>>)(((<{}[]>[()[]]){([]())((){})})
[{<[<([{([{(<{[]<>}>(<()[]>{{}{}})){<[[]<>]{()()}>{[[]](()[])}}}<{(({}{})<[]<>>){[()<>]}}>]<([({{}<>}({}[]))]
{[[{({[({{[[({()()}<<>()>)[[()()]{{}[]}]][([[]<>][()()])<([]())([][]}>]]}})[((([(({}{}))<({}<>)<{}[
{<[{([[<{{<<({<>{}}<<><>>)<{<>}([][])>>[{([]())}<{{}[]}>]>}}<<{(({(){}}))}><([(<()[]>((){}))({()<>})]
<(({(<{([((<<<()()>{{}{}}>{[(){}]([][])}><({(){}}[[]()])<<{}>>>)<<(<[][]]([]()))<{[]{}}(<>
<<<[<([<([(((<{}{}>){<()[]>{[]<>}})({(<>())[{}{}]}))([(<()[]>{<>[]})<[(){}][()()]>])])>]<(<({{(<[]{}){()<
({<(<{{(<([(<(()())<{}[]>>[[{}<>][<>]])]{([{[]{}}[[][]]]([<>()]{()})){<([]())[[]<>]>}}){{[{(()<>){
<{{{[<<(<<{([<()[]>([]())]<{[]{}}<{}<>>>)[[<[]{}>]{[{}<>]{<>{}}}]}<[<[[][]][[][]]>]({<(){}><{}()>}({
<[[<(([([(<{[({}{}){()[]}]{[{}[]]([]<>)}}[<(())[()]]]>([([()[]][()()])]([(()<>)(<><>)](([]())[{}{}]))))])]))(
([<[<{[{[([((([])]({<>[]}{{}[]}))(<{[]{}}{{}()}><{()[]}[{}{}]>)])<[{[(()[])<()<>>]<<{}<>>(()<>)>}{({<>
([{{{[<[({(({([][])[()<>]}{{[][]}[()[]]})({{[][]}<[]{}>}{{{}[]}<{}[])})){<<<<>{}>({}{})>{{
{<{[([{<[({<{{{}{}}}{{[]}{()[]}}>}{(([()[]][{}[]])]})]<<(<[{()<>}([][])][{<>()}]>(({[][]}{<>[]})[[[][]]]))
[([<(<[{<({<({[]()}<<>{}>)<<()[]>[[]()]>>}[{<[()[]]{{}[]}>}<[[()[]]((){})]<({}{})[[][]]>>])[[[{(()())({}())}(
{{<([([[{<<[<[[][]){<><>}>]>[<[[{}<>]<()[]>]{{<><>}[{}<>]}>[[<{}[]>{[]{}}](<<>{}>)]]><<{{({}<>)}}
{{<[<<<[<(([((()[]){<>[]})]{((()[]))<[[][]]([][])>})[({(<>)[()()]}([()<>]{()[]}))])((<[{<>[]}]>))>]>((((
[<{(((<[{(<[<<<><>><{}()>>]><{{(()[])(()[])}}{[[<>()]<<>[]>]{{<>()}[()[]]]}>)((<[<[]{}><{}[]>]{{[]()}{
{[<([{(<{([<[{{}[]}<[]()>][<()<>>{<>()}]>(([{}()]([][]))({[]{}}([]())>)])((({[[]{}]<{}[]>})<[<<>()>{[]{}}][{(
(((((((<[<[[[(<>[]){<>}][[{}()][(){}]]](<[{}<>]{(){}}>[<<>{}><(){}>])]>]{{({{[{}<>][<><>]}}(
<<(<<<({<[{{[(()<>)[()<>]]}}{<[[<><>][{}]](<<>[]>{{}<>})}<<<{}[]>[{}]>{<[]<>><<>{}>}>}]{<<<<<>[]>[()()]>[
{{[<[{{[<<{[[<<>{}>[{}()]]][(<<><>>{{}<>}){([]<>)[()<>]}]}>{{[(([]()){<><>})<<[]()>{()[]}>]}{([({}[])[{}()]]
({([{{{(((<[(({})[[]{}])(<{}[]>{{}[]})][<{()[]}(<>[])>[[{}[]]<{}[]>]]>([{{[]<>}{<>{}}}<<{}[
<{<{{{((<<[<<[<>[]]>{[<>]<[][]>}>[<[{}{}][()()]><([]{})((){})>]]>><{{[{<{}{}>[()<>]}(((){})[<>{}])]
{[((<<<[{<[({{[]()}([])}({{}<>}((){})))<<<[]<>>[()<>]>>]>}]({<<{<[[]<>](<><>)>{{[]<>}[()<>]}}>><{{<<{
[({[[<(([[[(<([][])<<>{}>>((()())[[]<>]))]<{{<[]()>}[{<>}{{}[]}]}>]])[<<[<(<[]>)[[[]<>]<<>[]>]>{({[]()}<[]>)<
[{[{({[{({[{{({}[]){()<>)}<[<>{}]{{}()}>}[{[()()][{}{}]}[[<>{}]<[]()>]]]{<{<[]<>>[(){}]}((<>{})[
<{<[[<<{([{[<[()[]]<<><>>>({()()}(<>()))]{<({}<>)[()<>]><[{}{}]{[][]}>}}{<<{<>()}[[]<>]>{<()[]>[()
{({<[[(([(<{[<<>[]>]}>[{{{{}()}{{}[]}}({<>{}}([]<>))}{{[[]{}][[][]]}}]){<<[[()()][(){}]][<{}[]>[()[]]]]{[[<
[{<({([<[{{[({()})<({}<>)<{}()>>]}}{[[[{()[]}<<>[]>](<[]()>{[]<>}}][[(<>{}){<>{}}]{{[][]}[
[<{([(([([(([<{}<>>]([[]<>]<[]>))(({<><>}<{}[]>){[<><>][<>{}]}))])][<<{[<[(){}](<>[]]>]}[[{[[]()]}(([]
<({({{{({<{[[{[]()}[()[]]]](([{}{}](<>())){{<>()}{{}[]}})}[(<[{}{}]<<>{}>>({[][]}))<<{{}[]}(()<>)>>
(<[<<[{({[<<[({}[])[()[]]]<[()[]]<<>{}>>>>][[((<<>[]><()>)(<[]()><<><>>))]]}([[{<<<><>>(())>{{()()}}}{
{<(({[{([((<[[()][<>]]>[<(<>[]){()<>}>{[<>{}]{<>{}}}]){<([(){}][<>()])<({}<>)[[]{}]>>{(<[]<>><{
[[({([<<[{<({({}<>)({}())}){{{<>{}}(<><>)}<{{}{}}(()<>)>}}<<<[(){}]<<>>>((<>[])[[]<>])><<{[]<>}[()()]>{{(){}
{({<([<([(({[({}())[[]()]]<[<>]{{}()}>}{[[{}<>]{<>[]}]<{{}[]}{{}{}}>}))({<([()[]>[[]]){<<>(
[[{[[{{[{{<(<<[]<>><<><>>>[{[]}({})])<(([]()){()[]})>><[[<(){}>(<>())]][{{<>()}[[]()]}{[<>()]}]>}<{{(<<>{}>
[{<[{(<({{{<{<(){}>(()<>)}>({[[]()][{}[]]}[[{}[]][()<>]])}{<<[(){}]{{}{}}>[[()<>]<<>()>]><<<[][]]([]<>)
<<<({<{[<{<[<(<>{}){{}()}><[<>{}]>]>[(<{[]{}}({}())>)[(([]()){()()})<<(){}>{<>[]}>]]}[{({{[]{}
{[((({[<{(({[([]<>)(<>)](<[]{}><(){}>)>[<{{}<>}([]())><((){}){()[]}>])({((()())(<><>))({()[]}{{}<>})}{<{
<([(({(<{<<[[<<>{}><[][]>]<[[]<>](<><>)>]{{((){}){<>{}}}[<{}()>([])]}>>}{{<<<(<><>){{}<>}>{{{}[]}}>[{
{{<{(((({<{{[({}{})][<<>()>[()[]]]}}{[{<[]()>(<>{})}[[{}<>](()<>)]]<[[()[]]]([[]()]<<><>>)>}]})<(<<([<[]{
(([((<[{(({[[[<>[]]{[]{}}](([]<>))]{{<[]{}][<>[]]}{<{}{}>({}())}}}<(({[]()}{[]{}}){(<>[])}){[<(){}
{<(({{([{{{<((<><>)({}()))>([<<>()>({}())]{{<>[]}[<>{}]})>}}(({[<<{}<>><[]<>>>[<<>{}>{<>[]}]]({(()<>){<>[]
<<<{{(((({[((<{}[]>[()<>])((<>[]))){[{[]{}}]{((){})([]())}}]}){([<<[{}()]<[]()>>[[<><>][{}
[[{({[({[<<{[<()()><<>{}>]{[{}{}][[]()]}}><[{{<>[]}<()[]>}]>>]}<(({{{[<>()]<[][]>}[{[]<>}[[]{
<{({({{{{[{(([[][]](<>()})<[<>{}]{<>()}>)<(({}())(()<>))>}]}[((<(<{}{}>)({<>{}}{<>()})>{(([][]))<(()[
((<(([[<{{<[{<(){}>}{<()()><<><>)}][{{<>}}]>{([(()[])(<>[])]{({}{})(()<>)})<[({}()){{}[]}]>}}({
<{((([{([(<([[<>[]]([])](([]())[()()])){{{[]()}[<>()]}}>)<{{<<<>{}><()()>>)}(({({}[])<()()>}
<({[[({[[({<[<[][]>]<[[]<>]([]{})>>{[{{}{}}((){})]}}<[<<[]{}><{}()>>][<({}[]]{[]{}}>{(<>){[]
<<[<{{[<[<[[[{{}[]}[[]()]]]<({{}<>}(()<>))<[()[]]<[][]>>>](([[<>{}]([]())][<()<>>[{}{}]])<<<[]{}>{{}{}}>>)
{([{{<([([<[{({}())(<><>)}[({}}<<>()>]]{<<<><>><[][]>>{<()><{}<>>}}>(<{[(){}]<<><>>}[([][])([]())]>(<<(
(([[[({{{{{[<<{}()>><<{}()>>]{<{(){}}{<><>}>[([]<>)<()[]>]}}{(<{[]{}}[[]<>]>)[([[]<>][[]<>])([()
{[<<[{[[({[{{{[]}}<[{}[]]({}())>}([((){}){()<>}]{<<>{}>{<><>}})]{[<{()[]}{()<>}]{(<>[])<{}
<({<[([[({<<{{{}{}}([]{})}[[{}]{<>}]>{{({}[])<()<>>}<[<>{}][[]{}]>}>[<({{}<>})<<{}[]>({}())>>]})]]<<[<[{{[()
[(<{<{(({[{{<{()()}<[]{}>>{([]())[{}[]]}}{<({}{}){[][]}>{(<><>)[{}{}]}}}[([({}<>)<{}{}>]<[
<[{(<{<{({[{((<>[]){[]})({{}<>})}]}[{[<{<>[]}<{}()>><[[]<>][[]<>]>]<[<[]()>([]<>)}(([][])(<><
[{[({([<<[[<<<{}<>><[]{}>>]][<{<<>()>((){})}<[{}()]<<>{}>>>{(<(){}>({}()))(<()()>{()})}]]>[({[([[]{}]{()[]})
{[({<[(<{(<[{<()()><()()>}(<{}()>{<>[]})]>([(<{}{}>(()<>)){[<>[]][{}()]})[{(()<>)({}[])}<(<>{})(<>(
<[(({{([[(((<([]{}){()<>}>{[()<>]{[]<>}}){[<[]()><[]>][({}[])[<>()]]})[(<(<><>)(()[])>{{[]()}{[]{}}})[{<<>
[({<({({(<{{{<(){}>[<><>]}(<[]{}>(()[]))}[[{{}[]}[[]()]]<[{}[]][()[]]>]}[<(<()[]>{{}})[[[]<>]]>
<<[(<{<((<[<<<(){}><(){}>>({{}<>}([]()))><{{(){}}{()}}<<<>()><{}()>>>](<<(()())[[]]>(<[]()>([]<>))><(<
{(({([[<{[{<(([][]))>{(((){})[[]<>])[(<>{}){{}[]}]}>[<({[]{}})[(()()){<>()}]>]][{[(((){})<()<>>){<<><>>}]<<<[
({[{{<[(<[({<<[]<>>{[]}>(<[]()){()()})}){{{{()()}(<>())}<[()<>](()())>}(({[]<>}([]<>)){[{}[]]})}][{{{[[]<>]{
(<([[{([{<{{<{()<>}[()()]>[([]())[()<>]]}}<([{(){}}{<>{}}]((<><>))){{{(){}}{{}<>}}}>>}({[(<{[]{}}[()<>]>{[[
{{(({[{[{[[{[{<>{}}]<[()[]]<{}<>>>}{<(()[])([]{})>({{}{}}<(){}>)}]{<(<<>()>(()[])){<{}()><
({<((<<<{{({[[<>]<[][]>]((()[])[<>()])}<([<><>]<<>()>)(([][]))>){(<<{}[]>[(){}]><([])({}()}>){{<[]<>>
{[<[([(<<(<{[[[]{}]<[][]>][{<>}(()<>)]}<[{()[]}{[]<>}]>>[{[({}[])([]{})]((<>){<><>})}((([]<>}[<>[]]))])({(
([{[[[{(((((<(()[])<()<>>>{<[]()>[{}<>]})(({()()}({}<>))<[[][]]<<>{}>>))<<({<>()}([]()))<<[]<>>[<>[]]>>[<{(
[{{<({[<<{[<<(()())[[]]>{{[]<>}[[]<>]}>{[<<><>}]}](<{[<>[]][{}[]]}<{[]{}}[[]{}]>>)}><<({{(()())([
({{{[(([<(((<{<>}(<>())>)[((<>)<[]{}>)[([]())[()[]]]})<(((<>[])[{}]){{[][]}[()()]})<[(<>{})({}())
<[({[<[[{((({[[]<>][<>{}]})(<[{}[]]{[][]}><{()()}{<>{}}>)){(<({}())[<>()]><{[]()}{{}()}>)})}](
<{(([<[{<<<{<<[]{}>{<>()}>}({<<>[]><[][]>}(<()<>>))>>{([(([]{}){[]{}})((()[])[[][]])][{<()>}<[(){
{(<[({{(<{{<[({}{})<<>()>](([]<>)[{}])>[([{}{}]<()<>>)[[{}<>](()[])]]}[(({[]<>}){{{}[]}[[]{}]}){[<{}<>>([]<>
{(<[{<[<<<{([<[]()>[<>[]]]{{<>{}}([]())})}<[[({}[]>([]())]]<([<>{}]{[]<>})>>>>[[<<<{<>{}}<()()>>([{}])>{<((
[{<{(([[[{<[(((){})<{}[]>)<{<>{}}>]<<<()<>>{[]{}}><<{}()>>>>}]]<<<[[(<()[]>{[]{}})][<{(){}}[<>{}]><<{}>

0 comments on commit 76f433e

Please sign in to comment.