From: Brendan Hansen Date: Fri, 10 Dec 2021 14:38:33 +0000 (-0600) Subject: added day 10 of aoc-2021 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f35d72d1a45b51b368c9cb98f183bd46d797ce88;p=onyx.git added day 10 of aoc-2021 --- diff --git a/core/hash.onyx b/core/hash.onyx index 19d7959e..d6d89deb 100644 --- a/core/hash.onyx +++ b/core/hash.onyx @@ -2,6 +2,7 @@ package core.hash to_u32 :: #match { (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, + (key: i8) -> u32 { return ~~ key; }, (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; }, (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); }, (key: str) -> u32 { diff --git a/tests/aoc-2021/day10 b/tests/aoc-2021/day10 new file mode 100644 index 00000000..909031c4 --- /dev/null +++ b/tests/aoc-2021/day10 @@ -0,0 +1,3 @@ +Part 1: 339477 +[ 39987, 40412, 211418, 2961184, 18263624, 32699791, 34608922, 77999081, 143979073, 192930339, 219835591, 229509097, 481071808, 610107088, 621817986, 715998574, 726037067, 867149788, 940233121, 944880336, 1151348456, 1952645587, 2342374308, 2368462459, 3049320156, 3159526836, 3338592468, 3529238356, 3981995374, 4048632297, 4472552966, 5195055344, 5223271481, 5263640441, 5358005998, 5543505166, 5837871587, 5932417192, 6072890531, 9641557042, 10084664733, 17746755972, 20591383546, 20698042322, 21616583974, 23920462366, 24143557159, 26737415431, 30269373313 ] +Part 2: 3049320156 diff --git a/tests/aoc-2021/day10.onyx b/tests/aoc-2021/day10.onyx new file mode 100644 index 00000000..7e0e5450 --- /dev/null +++ b/tests/aoc-2021/day10.onyx @@ -0,0 +1,73 @@ +#load "core/std" + +use package core + +main :: (args) => { + for file: os.with_file("./tests/aoc-2021/input/day10.txt") { + reader := io.reader_make(file); + + bracket_map: Map(u8, u8); + bracket_map[#char "("] = #char ")"; + bracket_map[#char "["] = #char "]"; + bracket_map[#char "{"] = #char "}"; + bracket_map[#char "<"] = #char ">"; + + score_map: Map(u8, u32); + score_map[#char ")"] = 3; + score_map[#char "]"] = 57; + score_map[#char "}"] = 1197; + score_map[#char ">"] = 25137; + + corrupted_score := 0; + completion_scores: [..] u64; + while !io.reader_empty(^reader) { + line := io.read_line(^reader, consume_newline=false, inplace=true); + io.skip_whitespace(^reader); + + char_stack: [..] u8; + defer array.free(^char_stack); + for ch: line { + switch ch { + case #char "(", #char "[", #char "<", #char "{" { + char_stack << bracket_map[ch]; + } + + case #char ")", #char "]", #char ">", #char "}" { + x := array.pop(^char_stack); + if x != ch { + // printf("Expected '{}', found '{}' instead.\n", x, ch); + corrupted_score += score_map[ch]; + continue continue; + } + } + } + } + + assert(char_stack.count != 0, "Invalid input."); + + complete_score: u64 = 0; + while char_stack.count != 0 { + complete_score *= 5; + switch array.pop(^char_stack) { + case #char ")" do complete_score += 1; + case #char "]" do complete_score += 2; + case #char "}" do complete_score += 3; + case #char ">" do complete_score += 4; + } + } + + completion_scores << complete_score; + } + + printf("Part 1: {}\n", corrupted_score); + + array.quicksort(completion_scores, (x, y) => { + if x == y do return 0; + if x < y do return -1; + return 1; + }); + + println(completion_scores); + printf("Part 2: {}\n", completion_scores[completion_scores.count / 2]); + } +} \ No newline at end of file diff --git a/tests/aoc-2021/input/day10.txt b/tests/aoc-2021/input/day10.txt new file mode 100644 index 00000000..7074b6ec --- /dev/null +++ b/tests/aoc-2021/input/day10.txt @@ -0,0 +1,98 @@ +[[{[<{{<<{<[<{<>[]}([][])>{{<>()}<()>}](({{}<>}[[]<>])<([]<>){[]()}>)>[<{{<>}<[]()>}[{[][]}<{}{}>]><{< +{[<[({([[(<<(([]{}))<[{}()]>>>)]])<([<{[{({}{})[[]()]}[(()[])<{}()]]]{{[[][]]<<>[]>}}}{([([ +{<{<{(({[[([{<<><>>([]())}[{()[]}({}())]}<{{{}}{[]<>}}({<>{}})>){<(([]())([]<>))(<<>[]><[]<>>)>({[[] +{[([<{<([{{(({()<>}({}<>)))(<{<>{}}](<<>[]><()()>))}{[{{[]()}(<>[])}{{[][]}{<>{}}}]}}{[{{{{}[]}<<>()>}}<{([] +[({[{[(<<<[((<{}<>><{}{}>)({[]<>}((){}))){{[{}<>]{{}<>}}})[<[<<>>[<>{}]]{{<>[]}}>]>>[[([<{<><>} +<[{[{[[<{[[{<<[]()>[{}<>]>}]<([{{}}({}{})])>]{<[[<<><>><()[]>]([[]<>]{[]<>})]{(([][]){()[]}) +((<(<(<[(<([(<{}<>>(()()))](<{{}[]}{()[]}>))<<<(<>[]){()()}>([<>{}]{{}})>({{[][]}(()())}(< +{{([(<{<([([[[<><>][()<>]]{(()[])}])]({<[<<>>[()<>]]({()[]}[{}()])>]))>({<<[<{[]()}<<>[]>>{<[] +([<{{((<({<[<({}<>)[[]()]>]([(<>)<(){}>](([][]){{}{}}))>{(({<>[]}<()()>){(()[])})[{{[][]}((){})}[([]<>)({}{}) +<<<{<{<([<[((<()<>]<[]()>)[(<>[])[{}<>]])]><[<[(()())[()()]]{[{}{}][()()]}>]{<<(<>())[<>[]]>>[<(()())<[] +{<[[({<[{[{[({{}<>}<<>{}>){<{}[]><{}{}>}]{{<<>>}<(<>()){()<>}>}}]}{{<[({{}()})([()<>]{[]}) +(((([[(((<(<<(<>())[(){}]>([[][]]{()()})>{<[<>[]][()<>]>(([]{})[<>{}])})[({{()<>}({}())})]>([[[(()<>)][[<>() +[<({([[[<<([<<<>()>>{[<>()](<>{})}]){<[[{}<>]]<{{}<>}([]{})>>}>[<<<{{}()}>>[[([][])<<><>>]({<> +{<(<<{{<<[({{({}<>)[[][]]}(<()[]>[<>])}<{<()>{{}()}}>){{<[(){}][(){}>><{{}<>}[[]{}]>}}]<{<{ +{(<{<[<(<<(<[([][])<[]{}>]><{([]())(<>{})}{<(){}><[][]>}>><({({}[]){<>()}}{<{}>(())})>>{({{(<><>)[{}()]} +[(<{[<<[{((({[{}<>][[]<>]}){(<{}<>>({}<>))<(<><>)[<>]>})<({{<>[]}}}>)}]>([{{<<{{[][]}(<>())}<{{} +<<<[[{[<<{[{<(()[])<[]()>>}[(({}[]))(({}<>){<>})]](([{(){}}[<>]]<[{}{}]<{}<>>>))}>{{[(((()[]){(){}})(<(){}>{ +[{{<<([[[[<<[{(){}}{(){}}]{<{}()>(()())}>{{<{}()>}<[<><>][()()]>)>([{{()<>}<()[]>}[[<><>]{[]<>}]]{[{ +<[[{[{<[(({({[<>{}]}([<>[]]))<<[<><>]([]<>)>([{}()][()()))>}[((<{}>{[]{}}))[[{{}}[<><>]]]])(([{{[]()}<[] +{{{<{([[[<[<{{(){}}({}{})}>[[{()[]}<[]{}>]<<()<>>[()[]]>]}<{[[[]()]<<><>>]({<><>}<[][]>)}{(<[]()><{}<>>)}>> +([<<[(([{{[([{<>{}}])<[(()){<>{}}]>](<{([][])[<>()]}<{{}()}>>)}>{[{{(<()()>[<><>])((<><>){()()})}{([[][]])[[{ +[{((({<([{(<{(<>)[[]()]}({{}<>}<[]<>>)>)}(<<[({}{})]{({}<>)[<>()]}>>[<[[()<>]{<>()}][<[][]><<>{}>]>[<([][])[( +(<[{<[<((<{<[{<>[]}{{}<>}]([[][]]{{}[]})>[<[(){}]{[]()}>({<>}{<><>}}]}>[[(<{()[]}<()[]>>[{{}()}<<>[]> +({{<([{<<<<<{{()}({}())}<[()]{[]{}}>>[([()<>]([][]))<{()<>}>]>>>>}<{[[[<<<<>()>{[]{}}>[({}()){(){}}] +<[{([<<(<(<[(({}()){[][]})[[(){}][[]()]]][<[{}]([][])>]>}>(<{[({[]<>}{(){}})<(<>)({}())>]<[({}<>)<{ +(({[{<({<<[<{{[]<>}[<><>]}{{{}}<()[]>}>]<<<{()()}{{}<>}>([[]()][<>()])>>><[([(<><>)<()[]>] +{([<<[{[(({[{{()<>}({}{})}<[{}[]]<(){}>>]<{<[]<>>}<[()()][()()]>>}<<[[(){}](<><>)]><(<{}[]> +{([<{<([(<(<(<[]{}>{<>()}){{<>{}}}>({<[][]>(<>{})}))<[<[{}{}]<{}{}>>{(<>[])(<>())}][<{{}<>}(()[] +[{[(<(({{(<{(<[][]>){[<>{}]}}{<{(){}}{<><>}>({[]{}}{[]{}})}>)(<[(([]<>))[[[]()]<[]<>>]]<{({}[])<{}<>>}[ +[{<{({((<{[{[<<>{}>[[]]]{<{}()>[<>]}>]}>[{([(<[][]>{[]()}){<(){}>[<>[]]}][[([]<>)<[]{}>]{{{}}[{}]}])([ +{<(<({[{[(({((<>[])<{}{}>)({[]<>>[{}[]])}([{(){}}([])]{<<>[]>[{}()]}))[[<[<><>]<<><>>>{[()()]< +{{{<[<<{(<(([({}<>)<()>]{<()()><[]<>>})(<[()[]]{[]{}}>{{{}{}}(<>())}))({([[]()]{()[]}){{{}<>}[ +(<({{[{<{[<[[{[]()}]([[]()][<>{}])][[[<>]{<>}]<([]{})<{}[]>>]>{[{{<><>}[[][]]}<{[]{}}({}())>](<[[]()]({ +<<[{[<{<[[(<[<(){}>(<>)](<[]<>>[[]()])>{<<{}[]><{}()>>[(()[])(<>{})]})({({{}<>}[<>{}])}{({[][]}<<><>>)})]({<[ +[[([<{{(<({[{([][])(()<>)}]{{{[]<>}[()()]}[({}{})[()[]]]}}>><[[{{[()()]({}())}}({(()[])[[]()]}{ +<<{(<(([{((<([()()]){{[]<>}{{}()}}>)<<<({}<>)[<>{}]><({}[])<[]<>>>>(<[[]]{(){}}>(([][])([])))>)[{[{([]<>)[<>{ +[(<(<<{{[([[[({}{})<()())]({[]}{{}[]})]({[()()]([]{})}{({}[])[[]()]})]<[[{<><>}<[][]>][[[]()]{(){}}]]>){(([<( +[[<[<{<[{({{{{<>{}}<[]()>}<<(){}>>}<{{{}{}}{()<>}}[([]{}){<>{}})>})[[((<<><>>[{}[]])<{()<>}<() +((<(<<[[{{{(<<{}()>[()()]><((){})>)[[[[][]][()[]]][{[]<>}<{}{}>]])((([[]{}]<[]{}>))([{<>{}}(< +{{(({[{[({({<<<>[]><<>[]>>{{()[]}[{}()]}})[{(<{}<>><[]>)(({}[]){{}[]})}<<<[]()><[]{}>><{<><>}<()<>>>>]}) +[(<[{[<({{[{<[<>()]([]{})><{(){})[()[]]>}<(<()()>(()()))(<{}[]>({}[]))>]([([[]()]({}{}))({(){}}([]( +{({(({[<{<[<<{<><>}(<>[])>[<{}{}><[]<>>]><<{<>}>>](<<<()<>>(()<>)>{<{}[]>([]<>)>>)>}<{<{{<< +<([[[(({[{<(({<>[]}<<>[]>){<{}[]>([]()))){<({}<>)<[][]>>{<{}{}>{()[]}}}>{((({}{})[{}[]]))({[<><>]}<<<>{} +[{[[[{<<(({[({[][]}<[]<>>)<{{}()}{{}}>]{({[]}[<>{}]){<[][]>[{}{}]}}})[[([<<>()>(<>[])]<<<> +<({<<[(({[([{[()[]]({}{})}{{()[]}[()[]]}])[([((){}){<><>}]{<()[]>{{}{}}})]][[{<[()()]({}<> +[{<([<({{<<[((<>[])(<><>))((()[])([][]))][(([][])<<>()>)]>[<<{()<>}<{}<>>><<{}<>>{()<>}]><{[()[ +[<{[<([{((({<<[]{}><{}{}>>([{}<>]<<><>>)})[[(<{}<>>([]<>))<<{}()>{{}<>}>]{[<[]<>>][[{}<>]<{}[ +({<{<[<({([<{<()()>{{}[]}}({<>{}})><{[[]<>]<[]()>}<[{}()]{{}[]}>>])}([<<[{[]<>}<<><>>][<[]{ +{[({<[[<<<(<{<{}<>>(()())}[{()}{()<>}]>)[<{{(){}}(()<>)}>{[([]{})(()<>)]{[(){}]{()[]}}}]>{{[<<[]()><()<>>>{ +<[{{({({(<<{[(()<>){[]()}][<[]>(()())]}{<(<><>}{()[]}>}>(<<{{}()}<[][]>>[{()<>}{{}{}}]>)>)}<[(<((<( +[<{[<[{[[[([<(<>{}){{}<>}>(<<>{}>{{}{}})]{({<>[]}<{}()>)<({}<>)<{}{}>>}){<[[[]{}]{{}{}}]<<[]<>>(()[])> +{[{((<(<[(((<({}[]){[]()}>)))]([{<(<<>{}>[[]])<[[][]][(){}]>>[{[{}<>]{[]<>}}{(<>())(())}]}[{<{{}()}<<>[]>>}[[ +[[[{{(<<<<((<((){}){[]<>}><<(){}><()<>>>){[<[][]>({}[])](({}{})<[][]>)})[[{(<>>}([{}()][{}()])]]>>>((<{[ +[[({<[[<([<[((<>{})({}<>))({{}{}}(()[]))]<<((){})<()()>>[<[][]><{}{}>]>>({[<()()>[[][]]]})])> +([<(<{(<<[<{{([]<>)[<>[]]}<<()()>[(){}]>}><([[<>[]]](({}[])))({(()())<(){}>}<[<>>[()()]>)>]>>) +{(({([{[{(<[[((){})(<>{})]<(()())(()[]>>]{([{}]{[][]})}>)<((((()<>)[<><>])<((){})[()()]>)[[<()<>>][<{} +{{([{{<<({{<<<[]()><()[]>}{{()<>}}>[<{()<>}<[]{}>><{{}[]}[{}[]]>]}})({{({[<>{}]<(){}>}(<<>>(()<>)))(< +<[[{<(<{{<[<{({}())}({[][]}{<><>})>{[<<>{}><()[]>]<[<>[]]<<>[]]>}]>(({[{[]()}<[]<>>]{<<>><<>[]>}}))}([[< +([{<[{<({{[((<{}<>>[(){}])({()<>})){{<<>{}>}[<{}()>{()[]}]}]}[{(({()[]}(<><>))[<{}()>(<>[])])<[{()[]}[ +{<[{({<((<({[{[][]}{{}[]}]{[<>()][<>{}]}}((({}())([]()))<([][])[[][]]>)}({[[[][]]({}())]}{({{}}){(<>[]) +{{[{{({(<(<[[[[]{}](<><>)]([[]{}]{<>[]})]>({{{()[]}([])}[{[][]}(<><>)]](({[]}<<>[]>))))>)}({{[{{(([]) +[<[{[{<<({{[{[<>{}]<(){}>}]}<{(({}()){{}<>})(<{}()>{[]<>})}{<[[]{}]>}>}[<[[([][])[[]]]]{{([]())[{}<>]}<({ +([<({{[((<({(([]<>){[]<>))[({}())([]<>)]})[{{{{}<>}{{}[]}}}<<([]{})({}[])>[<()[]>]>]><([[[{}<>]<()>]([<>{} +<[<{[[[<{{[[([[]<>]{(){}))[[{}<>][[][]]]](([<>[]]<<>[]>)[(()<>){()<>}])]}[<<((())(<>{}))<{[]()}<[ +[{[[{[[(<<(<({{}[])[<>()])><[[[]()](<>())]<{[]<>}({}())>>){[<{<>{}}[[]<>]>]}>{{<[<<>()>[()[]] +[(([{(([{<[<[<{}[]>[<><>]]>({[{}{}]{[]{}}})]>}{{[[<[{}{})<()()>>{{<>}{{}[]}}]{{([][])[<>{}]}{ +(((<<<<[(<[<{{()()}<[]{}>}<{[][]}{<>[]}>>[[[()()]{{}{}}]]]({[<()()><[]{}>]{{[]{}}([]())}}<{<()<>>[<>]}<<{} +<([<[[<<[(({(({}())<{}>)<(()[])[{}[]]>})<<({{}}[[]{}])[({}{})((){})]><<{{}()}{[][]}>>>)]>[[( +<<<{(<{{(([{<{()[]}><<[]{}>{{}[]}>}{(<[][]>[[]<>])[{()<>}]}](<[[<><>][<>[]]]([()[]]<{}()>)>))<( +{[[([<<[[(<((<()()>(<><>))[[{}()]<()<>>]){[((){}){[]{}}][[{}[]]]}>([<((){}){(){}}>[([]{}){()[]}]]))]<[<<{<<>< +([{[{([<{{{[<(()[])({}())>{[<>{}]{{}<>}}]{(([]<>){<>}){[()<>]<()[]>}}}(<<[{}<>](()[])>{[[]()] +[<((<<<[{[((([<>{}]){[(){}]})<<{<>{}}({}[])>[[[]{}]<()[]>]>){<(<<>()>(<>[]))[(()[]){[]<>}]> +<(([({[[[[<<[[[]()]]<<()[]>>>>{{<[()[]]{{}{}}>{<[]()>{()[]}}}<[({}<>)[()<>]]>}]([([[()()]<<>{}>])<{<{ +<{([([((<{{[([()[]]{{}()})][(<<><>>){{{}[]}[[]<>]}]}}>[((<(<<>>[[]<>])[(()[]){()<>]]><<{{} +{<[[({{<<[<<<{()[]}<<>{}>>([[]<>]((){}))>[{[<>()]{{}<>}}[({}())]]>{[{{[]()}({}())>]({{(){}}<()<>>}{<(){}>{ +{(<[{[{{[<(<({(){}}<[]()>)(<<>[]>{<>{}})>)>]({[{[({}()){<><>}]<[[]<>]{<>{}}>}(<<{}{}>([]<>)>(({}{}){( +((<((((<<({[{{{}[]}(()())}{<()()>}]<[[<>[]][(){}]][{()[]}]>}(([<[][]>][<[]()>({}[])])[<<[]()>[{}<> +[[{<[(<{{<{<[{<>()}[[]]]({{}<>})>{{{()[]}<{}<>>}}}<({{()[]}}[[<>()](()[])])<[([]<>)[{}<>]][([][] +<{{[{[{(<<({{{{}[]}{[]<>}}<[[]{}]{{}[]}>}[({{}{}}<[]()>){[[]<>]}])((<{()()}[[]{}]>({()<>}< +({{{<({{<<[<[{()<>}[[]<>]]<[{}{}][<>[]]>>({[{}()]<<>>}[(<><>)({}())])][([{<>()}{<>{}}]{(<>()){()[] +[((<({(<<({[<(<>{})<[]<>>>[([])[(){}]]][{{()<>}(<><>)]<[{}{}][{}<>]>]}{<{(<><>)[{}[]]}(([])[[]{ +(([{<[<{<<{{({()[]}{<>{}})[[[]<>]<[][]>]}<{<()<>>[()()]}{<<>><()<>>}>}>>[<[[<[{}<>]{[]{}}><(< +(<<<[<<<(([{{<()<>>[<>[]]}{<()[]>([][])}}])(([{{{}{}}[<>()]}({<>()}{[]{}})]<[<<>[]>]{({}<>){[]<>} +{<<[<([[({({{<{}[]>([]{})}[<()<>>[(){}]]}{[(()<>)]{<()[]>({}[])}}){<([{}[]]({}<>))(({}{})(()<>))><([(){}]<{} +{{[[<(<({<([[{[]{}}(()[])]<(<>[])>])({[[{}[]]({}<>)]})>[(<<[{}{}]<[]<>>>>{{{[]<>}[{}()]}({<><>}<<>()])}){([< +{[[{{<(({(([[<()>{()<>}]([[]()]([][]))]<<({}[]){[][]}>([<><>]{()()})>)((<[<><>]{[]<>}>[[[][])[[]<>] +[[<<{[[{([<{({[]()}({}())){{{}<>}<()()>}}([{{}()}<{}()>][{<>}[()<>]])><{{{{}[]}(<><>)}{[[]<>]<[]<>>}}>])} +({(({(<({[{((<{}><{}>){<{}()>[{}[]]})}]{[<({<><>}({}()))>{{({}<>)(()<>)}{<{}{}>{<>[]}}}]}}({[{([[]{}])[<() +({[{<<[<<[{{{{[][]}[()[]]}<<{}()>{{}()}>}{({<><>}([]))[<{}<>>(<>{})]}}{[([<>{}][()<>])([()[]])]}]<{[(<<><> +{<{<(({[[{(({{()<>}[[]<>]}[[()<>][()()]])(<((){})[<><>]>{[[]<>][()<>}}))}<[<{{{}}[{}[]]}<( +{{<[[(<[{<<((<{}<>>{(){}}))((<[]<>><{}{}>)({<>()}<()<>>))>>({[{(()[])<{}<>}}(<()()>)][<[<>{}]{<> +<[<{((([<(<<((<>())<<>()>)>(<{<>()}{[]<>]>)>)[[{{{()[]}(()())}{({}())({}<>)}}](<[{()()}<()()>]((()){<>[]})><[ +(<([{[[[[[(<(<<>{}>)<{{}{}}>>(<<[]()><[]()>>(<(){}]{()()})))]]]({(<{(<()>([]{}))[{()<>}{[][]}]}([< +(([({{{<(<{[<[<>[]](<>())>(<{}{}>)]<(([]<>>(()[]))(<()()>(<>{}))>}>({{[{[][]}][{[]<>}([]{})]}(<(() +({({<{[{{([(<(()())><{[]{}}(<>())>)(<[()()]{()()}>[[[]()](<><>)])]{<<[[]<>](<>())>(({}{})<<><>>)> +((([[{[<[{(<[{<><>}{{}<>})>{<[<>{}](<>)>[<[][]><[]{}>]}){[{{()[]}<<><>>}<(<><>)({}{})>]{<{( +[[<({([{{[<{<(()())<[]<>>>{[[]{}]}}(<[{}<>]<<>()>><(()<>)[()()]>)>>{<{<[[]<>]{[]()}>{<[]<>>{[ +(({[[(([[<{([[{}<>]{()()}][[{}[]]{[]{}}]){[[()[]]<[][]>][<{}{}><{}{}>]}}[{[({}())[[]<>]]}{[({})<<>>]<{<>[]} \ No newline at end of file