From 9c337c1a9aa2df421a0232e9b09834dd141e8683 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Fri, 10 Dec 2021 07:26:12 +0100 Subject: [PATCH] 10: part1+2 --- 10/input | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 10/solve.zig | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 10/testinput | 10 ++++++ 3 files changed, 187 insertions(+) create mode 100644 10/input create mode 100644 10/solve.zig create mode 100644 10/testinput diff --git a/10/input b/10/input new file mode 100644 index 0000000..6b75869 --- /dev/null +++ b/10/input @@ -0,0 +1,90 @@ +({[[{{({<<<<(<{}()><()[]>)[([]{}){<>[]}]><[{<>()}{<><>}]<(()[])([]<>)>>>){<([(())<()<>>][{{}{}}[(){ +({{<{<<(([({<[[]()]<()<>>>{(()())(<>())}}[{[(){}]}(<{}[]>[<>])])<[[[{}()](<>{})]]<<{{}()}[<>]>[({})[{}()]]>> +<[<{{<<{[{{([(())<{}{}>][{<><>}(<><>)])}}]((<(<{[]<>}>(<()()>[[]()]))>))}([(<<({[]()})>(({[][]}([]{})){(()() +({<{{{{<<{([{[<>()][<>()]}[((){})([]())]])[[<[()()]<()<>>>[{<>()}{(){}}]][[{<>{}}[{}[]]][([]())[{}() +[((<[[{<[<{[<[<>[]]<(){}>><(()[])(()())>]((<<>{}>[<><>]){[()<>]{{}<>}})}>]<<[<[<()()>](({}{})( +[{{([([[<([[<[()()][()<>]>][<<{}{}>[<>{}]>(((){}){[][]})]][<[<()()><{}()>](<<><>><[]{}>)><<<<>()><{}<>>>(([]( +<{[{(<{<[[{{{[[]{}]<{}<>>>{{{}()}<[]<>>}}([(()[])]{[{}<>][<>()]})}((<(<>{})<<>[]>>[[{}[]][{}{}]]) +[<[<(<{[<([({(<>())[{}()]}[[<><>]])]){<{(((){})[{}<>])<<<>[]>(()[])>}(<([]{})<<>[]>><[{}<>][{}()]>)><<({( +(([{[<[{{[<[{{{}{}}<[]{}>}[[{}{}]{(){}}]][(<{}()>([][])){<<><>>({}[])}]>[(<(<>{})<<>{}>><{(){}}[ +<<[{[{[(<(<({{[]{}}[<>]])[{{[]{}}{[]()}}(<<>{}>[<>{}])]>{{(<{}[]>[{}[]])<{[]{}}<[]<>>>}{<{{}[]}([]( +({<<<[[({(<{{<{}()><()<>>}[{[]{}}<[]<>>]}>[<({<>{}}[[]<>])(<(){}>{[]<>})>[[(()()}(<>[])][(<><>)<{} +[(<[{[[(<{({<[[]<>]><<()[]>[{}()]>}([{<>}<[]<>>]({{}()}<{}{}>])){({(()[])([][])}<<{}{}>({}{}) +{(<<<(([[((<(<()<>>{(){}}){{{}[]}{{}()}}><(({}){<>()})<((){})({}())>>))][[{[<[<>[]]{(){}}>]( +{[<((<[[[<<[[<{}<>>{[]()}]<[{}<>][[]{}]>]{<<{}>[()[]]>{[<><>][<>]}}>{{{{{}()}}[{[]<>}<<><>>]}}>]< +([(<[[<({{[([{[]()}{<>()}]{[()<>]<<>{}>})({{()()]([]<>)})]{<((<>())[[][]])[[()<>][<>[]]]>}}}{{[[(<<><>> +<({{({<([[({<{[][]}[{}[]]>}([(<>[])((){})]([[]<>]([]{}))))}({{<<[][]>[()()]>[<()[]>{<>{}}]}[{[{}{}] +<<<[<<(<(<{{[[()()]{[][]>]<<[]()><[]<>>>}[{(<>()){<><>}}[<()<>>[[]<>]]]}>{(<(<<>>[<><>])([ +[[{({[<{(([<<[<>()](<><>)>{({}<>)(()[])}>{(({}<>){{}<>})}]([<[()[]]>><[(<>[])[<>()]]>)))[{{(({ +[<[<[{{[{{(([{()[]}{<><>}]<[{}()]({}{})>)([{()<>}{[][]}]({<><>}([]<>))))<<{<{}{}]}<(<>())([] +([(<{<{([<[((<()<>>{[][]}))([<<><>><[]{}>]({()[]}(<>())))]([<[[]<>](())><[(){}]{()[]}>]{[[[] +[{[{({{[[(([[<{}()]{{}{}}]]){([([][])([]<>)]<[()<>](<>())>){[<(){}>[<>()]]{[{}<>][{}<>]}}})(<[<{()()}([]<>) +(({<[<[<{({[(<[]<>>{(){}})[<[][]>([]{})]]{<<<>{}>(()<>)>[[()[]]}}})}{<<<((()<>)[{}{}]){<<>{}>[<>()]}><< +<[{[<[([[{{<{(<><>)[{}[]]}<<<>{}><{}()>>>}}{<<(({}[])([]{})}{(<>{}){[]<>}}>((([]<>)<{}()>){{{}<>}})><<<< +[<[([[{({(<<<(<>())>(([]())[<>{}])><<<[]()>[()<>]><(<>){<>{}>>>><({[[]{}]<{}[]>}([<>{}][{}<> +{(<[<<(<[({(({[]()}<[]{}>)({[]<>}{<><>})){<(()[]){[][]}>([()]<<>()>)}}{{((<>())[()]){[{}<>]<<> +(<{{<(({{{<[[([]()>]<{<>()}[{}{}]>]>[([[()<>](<>())][{<>{}}<[][]>])<[<()[]>]({()[]}[[]()])>]}}}))>[[< +((({{<[{[((<(<[][]>([]())){[()<>][{}{}]}>){<{{<><>}<<><>>}<([]<>)>>{([{}()][()[]])}})<[<{{<>}[[][]]}[[()[]] +([<((<(<{<{{[<[]<>>{(){}}](<{}[]>(()[]))}}>}>)<{([{<<{()()}{()[]}>[{{}()}({}[])]><[[{}[]](<>())]{[()( +<<[<<(({({((([[][]]<[]{}>)<({}<>){()()}>)[<[{}<>][[]()]>{<[]()><<>>}])})}{(([[[<[][]><[]{}>](<{}[]>{ +<<[(<<(((<([[{()()}<(){}>]({()<>}[()<>])][{((){})(<><>)}{<<>[]>({}{})}]){(<{()[]}[<>()]>)<({<><>}){[ +{(<[{([<(<[(({<>()})<[{}<>]{()<>}>)(([<>()]{[][]}))]{[<{[]{}}>[(()<>)])(<({}{})><[{}<>]{<><>}>) +{((<{[<{{[[{(<<>[]>{{}[]})}{[({}<>)[<>[]]]}]]<[[[[()]{{}<>)]{({}()){[][]}}]<[{<><>}<[][]>]((<> +{(<({[<<<(<<{[{}{}]}([[]()]({}[]))>(({<>()}<[]>)<{{}()}{{}{}}>}>)(<{{[()[]]{()}}(<<>()>{[]<>})}>)>[([[<{< +(<<[<<[(([[{({()}[<>[]])}][<(<[][]>{(){}})>(<<{}{}>{{}[]}>[({}<>)<<>[]>])]]))]((({[{{<[]{}>{{}()}}[([][])({}< +(({{<({<{{(<<{<>{}}{()[]}><[(){}][()[]]>><[<<>[]>{{}}](<()[]>{[]{}})>)}<{<([()<>]{<>{}})[([ +(<<[<{<{{<(({({}())([]{})}<({}())>)[[({}{})[<>()]]]>{{<{{}{}}<{}()>>(({}{})[{}{}])}}>({({<< +[[{([{<<<{<({[()[]][{}()]})>[{[({}{})[()()]](<<>()><<>[]>)}<(<<><>><[]{}>)<(<><>)<[]()>>>]}>>>}])}({(<<[[ +(<{(<<({[[{({{{}{}}[<>()>}[[<><>]])}]<{(<{()<>}{[]()}>[([][])[{}[]]])<[[<>][(){}]]{<[][]>([]{})}>}[((<<><> +<(({[(<<[<[[{{()()}(<>())}(<[][]><<><>>)]][[(<[]>[<>[]])([[]{}]{<>()})]{{[[]{}](<>[])}({()()}([]()))}]>]({({ +<<([<(((<{{[(<<>()>(()[])){{[]<>}<[]{}>}]<{[<>()][(){}]}>}<<[<{}<>>]>{([<><>]{<><>}){{{}[] +(([{[[[<{[{[[[()()]<()>]<[{}{}][(){}]>]}]}{[<{{[()<>][()[]]}}[{{()()}}[[<>[]][{}()]]])<<({<>()}<{}()>)>({([ +{[(<{{({[{{[{([]>({}<>)}<<{}[]>>][[{(){}}[<><>]]{<(){}>({}())}]}}[{([({}()){()[]}]{({})<[]<>>})[[[[]<>]]([<> +(([{[({{<([[([[]{}]<()[]>)(<<>>[<>[]])]{[{()()}<{}{}>](<{}()>([]{})>}])<[<{{<>{}}}<[<>[]]>>{[{[] +{({<<({<<<<{(([]()))(<<>()>)}({<()<>>{{}{}}})>[{[(<>)]{<[][]>[{}()]}}]>({[[[{}[]]({}[])]]})>>{{ +[(<[<((<[(([((<><>)([]{})){([][])[()()]}]<{{{}{}}{(){}}}>)<{{({}{})<()>}({()()}[[]{}])}<[[<>()]{()<>}>({<>} +<{{[(({{<((<({[]{}}(()()))[[{}{}]{{}<>}]>[([<>[]>(()<>))]))[[([{<><>}<<>[]>][[()<>]([][])]){<((){}){{}[ +((<[({<<({{[<[(){}](<><>)>[{[][]}[{}[]]]]}<([(<><>){[]{}}])([[()()]<()[]>])>}(([[[[]()]{{}{}}]{[{}<>]{() +(({{[<<<[[{<<[<><>]<{}<>>>[<<>()>(<>{})]>}<{(<()()><{}()>)<([]())[<>{}]>}>]]<<{([(<>{})[<>()]]{{[]()}{()( +<[{{{<<<[{[{<[()[]]({}[])>}[{({}{}){<>[]}}{<<>[]>[{}[]]}]]<[<({}{})>(([]())<{}{}>)]{([{}[]]([]<>) +{<<<[[(([{{[[(()<>)<()()>]][[{{}[]}[()()]]]}<<{[{}()]<{}[]>}[{()[]}<[]<>>]>>}]))][{(<[(<[<<><>>(()())]<({}{ +(<<<{{(<{[[[[(()())<<>[]>]<[[][]]>]((<{}<>><[][]>)[([]())[()()]])]([(<{}>(()[]))<([]()){[]{}}>]))}>)}({ +({<(({{{<{{[{<()<>>[<>[]]}([[]()][()()])]{([()<>]({}())){{{}()>{[]<>}}}}}>}<[{[<[<{}()>[{}{}]]<<()()><()( +<[{<<<([{<[{{<[][]><[]{}>}{[{}]{{}}}}<<{()[]}([]{})>([{}[])(()()))>][<[{[][]}{(){}}]{[()[] +{<[{{[<([[[[<{()[]}[<>()]>{<<>[]>{{}()}}]<({[]{}}{()<>}){{()[]}(<>{})}>]({[<()[]]([]{})][<<>()>(<><>)]}<< +({{[(([<[<[[{(()[])}{(()[])[()()]}]{[(<><>){<>{}}]<<[]<>><<><>>>}]>]<[(([<<>[]>]))]<((<{{}{}}{(){}}> +({[[(({[[<(<({<>{}})[([]<>)([]{})]>[<([][])<{}{}>>(<()()>)]){<[[<>()]{[]{}}]({{}{}}([]()))>}>]]}<{<{ +[[{([<({[[[<<<[]{}>({}())>([{}<>][<><>])>{<(()<>)<<><>>>{[<>{}]{[]{}}}}]{{((()[]))[({}[])<[]{}>]}[({<>{} +([[(([<[<([(<(<>()){{}{}>><<<>[]>[{}<>]>)<(<[]()>[()[]])>])>(<{([{[][]}<<>()>]<[<>[]]>)(<<{ +([{({<<<<[[({[[]<>]({}<>)}[({}[])(<>{})])][<{((){}){[]()}}<[{}[]]<[]{}>>>(<<()[]><{}<>>]{[<><>][{}<>]} +<<<<{{({<{{[([<>()][[][]]){{[]{}}<{}()>}][(<[]{}>[{}<>]>[{<>[]}((){})]]}}(<<<{[]{}}{{}}><([]{})({}<>)>>({(( +[[<<{<{<(<{({{{}[]}}((<>)(<>[]))){{{[]()}<()()>}[{<>()}<[]{}>]}}{[{({}{})<{}[]>}({<>[]}{[]<>})]{([{} +{{{[{[([[{{[{<{}>({}<>)}[([][]){<><>}]]([{{}[]}])}[<[[[]()]]{[[]{}]}>{<{<>()}>}]}]{[{[[[<>{}]{[]}](( +(<[[<<<({[[{{[<>[]]<(){}>}}<({<><>}{<>{}}){((){})([]<>)}>](<[[{}[]][[]]][{{}<>}[{}{}}]>((([][])[()()]){<() +{([((<{{{[[<<{()()}<[])>[{<>()}<()()>]>]((<[{}<>]<{}()>>{{{}<>}(()())}){{<()[]>(<><>)}{[[]()](<>())}})]} +{{<<<<({[<{<([{}<>]([][]))<<{}[]>([]{})>><[{<>()}{[]()}](<(){}><[]()>)>}<[<<[][]>({}())>(<[] +{[<[[((<({<<[<{}<>><{}()>]><<<{}<>>{<>}>{{{}[]){()<>}}>>([[{[][]}(()[])](({}[]){{}{}})])}<<({(<> +[[[<([[([<[{[{()[]}{{}[]}][(()()){[]{}}]}{{{<><>}{[][]}}{<{}<>]({}())}}]>[[<[{()<>}<{}{}>]<{[]()}({}<>)>>{<< +({[{<[[[([[((({}<>)((){})){({}{}){{}()}})[[(()())<{}[]>]]>[[[<{}()>]<[[]]<()[]>>][<(()<>){[]()} +[[<[{[<<<<{{({[]<>}({}[]))[<<><>>([]{})]}[<{<><>}<[]{}>><([]())[()<>]>]}<([{<><>}<()()>]<[(){}]({})> +[{(<<{{((({[<{<>[]}>](<[[]]({}[])>(<{}{}>{()()}))}<([({}<>)<{}()>]({[][]}({}())))>)<{{((<><> +{{((<(<[[<<(<([]()){()<>}><[()()]{[]{}}>)[[({}{})({}{})]<<{}{}><[][]>>]>[(<(())[<>[]]>)<<[<>() +[{([({<<{<<[{<[]>({}{})}((<>[])<<><>>)][<([])><{[]<>}<[]{}>>]><{({<>()}<<>[]>)}>>(([[({}{})[()[]]] +<{([({{{{(([{<[]()>(<>())}{{{}{}}{{}()}}]<{<{}()>[()<>]}({()[]}{<><>})>)<((<<>()>{<>{}}))({([]<>)[{}{}]}<([] +<{<[(<<({<(([(<><>)([]())]){{<<>()>}(<{}{}>{<>[]})})[{[{()<>}[<>[]]]([()()]{{}})}{([<>()]<[]()>)<[<>[]]{{}() +<{<{[[({<[[([[{}<>][{}<>]][(<>[]){()[]}])<([()[]])[<()<>>[{}()]]>}{[{[{}[]]<[]<>>}[(<>[])<()()>]]([< +[<{(<{<(([{[<<()()><()[]>>{({}{})<[][]>}]<(<(){}>)>}[<<<[]()>>[({}<>)([]())>><<((){}){<><> +[[[(((<({[([[<<>()>]{(<><>)<[]<>>}]([<[]()>{()()}]{[{}()]{<>}}))[[([<><>](<>[]))({<>()})]]](<(([[] +<{({(([{<[(<{{[]()}}>({[{}{}]{[]{}}}[{()}(<>{})>))]<(([[()][[][]]])<<<{}<>><(){}>>>)({<(<>{})>(([]())({}<>)) +<([([[[<([(<(([]<>)){[[]()][()]}>{[{<>()}([]{})]{([]<>)({}[])>})]<[<({(){}}(()<>)){{{}()}<[]{}>}><[[<>{}] +([(<[({<<(<{{(()<>)<(){}>}{<{}{}>}}>[<<(<>())<{}[]>><[<>()][<><>]>><<([]{}){[]()}>>])<[<{<[]()>[[]{}]}{([])<( +(<<{[<[(<(<<[<(){}>[[]{}]][({}())<[]{}>]>>{[<(<>())<{}()>>][<{{}<>}<[][]>><[<>()](()())>]}]>{({[<<[]() +[([<<<([<<[{{<()()><{}()>}{<[]{}>}}]{[{[[]()]}]({<(){}>(()[])})}>(<{{<<>{}>{()()}}<(()[])[[]()]>}(<<(){}>{ +<(<([((<((({(((){})]{{{}}<{}>}}<<(()())({}())>((<>[])([]()))>)){{[<((){})<<><>>>]}})><<[<[({[]{}}(<><>)){(< +<{[<[[({<[<<{(<>[])[()[]]}{<<>>(()[])}><[{(){}}[<>()]][{[][]}(()<>)]>>{<<(<>[])[[]()]>{{<>()}<{} +<<({[({{[([{[<[]><<>[]>]}<{{{}[]][()<>]}{{()[]}[{}<>]}>])]}<<{([{(<>[]){{}<>}}]([[<>()][()<>]]))({(<{ +(<{{{[[([<[{{{[]<>}[()<>]}<<<>()>>}[((()[]){()[]})]]>{{([([][])])<([<><>]{()<>})(<<>{}><<>{}>)>}{<<<[] +[[([<([[<[<{({()<>}{[]{}}){[{}[]][<>[]]}}>]>]{{({(([{}[]]({}{})){[<>{}]{{}<>}})<((<>()))[{[]{}}]>}(((( +({[<<[[[{{{[<<[]>([]<>)>(([]<>)<[]{}>)][<<{}[]>({}{})>]}([[<{}{}>[()[]]]((()<>)[<>[]])]{([()<>]{[][]}){{<>< +{<[{({[<({({[{{}[]}[[]{}]]({{}{}}([]()))}[(<()<>>{[]{}}){<[]{}>[()]}])}<({<{[]()}({}[])>[(()){<>()}]} +[([{{(([[{<[{(()())[{}[]]}<<<>><{}[]>>]{[{[]()}[[]<>]](<{}{}>[()()})}>}[([(<{}<>><{}<>>)(<<>{}>([]{}))]({([]{ diff --git a/10/solve.zig b/10/solve.zig new file mode 100644 index 0000000..c07ae47 --- /dev/null +++ b/10/solve.zig @@ -0,0 +1,87 @@ +const std = @import("std"); + +const Pair = struct { + opening: u8, + closing: u8, + score: u32, +}; + +const pairs = [4]Pair{ + Pair{ + .opening = '(', + .closing = ')', + .score = 3, + }, + Pair{ + .opening = '[', + .closing = ']', + .score = 57, + }, + Pair{ + .opening = '{', + .closing = '}', + .score = 1197, + }, + Pair{ + .opening = '<', + .closing = '>', + .score = 25137, + }, +}; + +pub fn main() !void { + var alloc = std.heap.GeneralPurposeAllocator(.{}){}; + defer std.debug.assert(!alloc.deinit()); + var gpa = &alloc.allocator; + + const stdin = std.io.getStdIn().reader(); + + var res: u32 = 0; + var scores = std.ArrayList(u64).init(gpa); + defer scores.deinit(); + + var tmp: [256]u8 = undefined; + while (try stdin.readUntilDelimiterOrEof(&tmp, '\n')) |line| { + var stack = std.ArrayList(u8).init(gpa); + defer stack.deinit(); + var corrupt = false; + + for (line) |char| { + for (pairs) |pair| { + if (char == pair.opening) { + try stack.append(char); + } + if (char == pair.closing) { + var matching = stack.pop(); + if (matching != pair.opening) { + res += pair.score; + corrupt = true; + } + } + } + } + + if (!corrupt and stack.items.len > 0) { + var score: u64 = 0; + + std.mem.reverse(u8, stack.items); + + for (stack.items) |char| { + score *= 5; + for (pairs) |pair, i| { + if (char == pair.opening) { + score += (@intCast(u64, i) + 1); + } + } + } + + try scores.append(score); + } + } + + std.sort.sort(u64, scores.items, {}, comptime std.sort.asc(u64)); + var middleScore = scores.items[@divFloor(scores.items.len, 2)]; + + std.debug.print("{}\n", .{res}); + std.debug.print("{}\n", .{middleScore}); +} diff --git a/10/testinput b/10/testinput new file mode 100644 index 0000000..2f182d8 --- /dev/null +++ b/10/testinput @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] \ No newline at end of file