From 91b232f9d6106d9e59af0e8f7563a683a78ee464 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Fri, 10 Dec 2021 16:12:39 +0000 Subject: [PATCH] Day 10 --- day10/Makefile | 10 +++ day10/example.txt | 10 +++ day10/realinput.txt | 90 ++++++++++++++++++++++++ day10/solution-part1.c | 132 +++++++++++++++++++++++++++++++++++ day10/solution-part2.c | 155 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 397 insertions(+) create mode 100644 day10/Makefile create mode 100644 day10/example.txt create mode 100644 day10/realinput.txt create mode 100644 day10/solution-part1.c create mode 100644 day10/solution-part2.c diff --git a/day10/Makefile b/day10/Makefile new file mode 100644 index 0000000..da2e708 --- /dev/null +++ b/day10/Makefile @@ -0,0 +1,10 @@ +solution-part%: solution-part%.c + gcc $< -o $@ + +test-%: solution-% example.txt + $< example.txt + +run-%: solution-% realinput.txt + $< realinput.txt + +.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2) diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/day10/example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/day10/realinput.txt b/day10/realinput.txt new file mode 100644 index 0000000..5bdcea7 --- /dev/null +++ b/day10/realinput.txt @@ -0,0 +1,90 @@ +{<{<[{<[<([[(<<>[]>[<>()]){(()<>){<>{}}}]<{<{}<>){()<>}}[[()[]]{(){}}]>]{<([[]][{}<>]){<{}<>>{[]<>}}><[( +[<{(([{<[(<{[{()()}{()[]}]{[<>[]]}}>)[[{[<[][]>(()[])]<({}<>)<()<>>>}([({}{}){[]<>}])]<{{([][])[() +([<[{[{[{(<{<[[]][[][]]>[(<>[])<[]()>]]<({()<>}([]<>))>>)((([[{}{}]](<(){}><[]{}>))<<{{}[]}[<>()]><[[][]](()( +(<<<{{{[[<(((<[]()><{}{}>)([<>{}](<>{}}))([(()())[[]<>]]<{[]()}(<>[])>))[[[<{}[]>({}{})]]<{[<>()]((){})}<<[][ +{{[<{([[[([(<(<><>)>{([][]){()[]}})<{{{}()}<()<>>}>])]]<({<{(<()<>>([][]))<<(){}>]}{{<()() +<[{({[(<[<{{<(<>[])<[][]>]}}((((()<>)<<>[]>)[[<>{}]<{}>]){{(()[])}})>(<<(([]{})<<>[]>)[([][]) +((<[[(([{[[{((()())[{}{}])([(){}]<{}[]>)][[{{}{}}[[][]]][(()())(<>[])]]]{<[[{}[]]]<<()<>>[ +([(<<(<<[[<{{([]())[()<>]}<{<><>}[<>[]]>}{<<[][]>[[][]]><[[]<>}({}<>)>}>[[<([]{})>{({}{})[()[]]}][<{[] +<<[[<{[<<[<<[{<>[]}({}[])]<[[][]]<{}()>>>((<{}[]>{{}{}}>(<<>{}>([]{})))>(<(<[]()>(<>{}))<((){ +<{<[[<([[[[[<[{}{}](<><>)><{()<>}[{}()]>]{<<()()>{{}{}}>[[()<>][{}()]]}]{{({[]()}[[]<>])(<<>{}> +([{[[<(({<((({()()}({}{}))<<{}()><[]<>>>)([{{}<>}][<[][]>(<><>)]))>{{[[[{}[]]([]<>)][{()<>}]][([[]{}]{<>[]})] +(<(<{<{{({<({[{}[]]<(){}>}[<<>[])(<>{})])[[<{}<>>[[]<>]][([]{})<<>[]>]]>[{([{}<>]([]<>))<[<>() +(({[[[<{([[{<[{}()]<[]<>>>}]{<[<{}>{<><>}]>{{(()[])}<[<>{}]{<>[]})}}]{[[<[<>]{{}[]}><[()[]]{{} +{[[{{<({{<<<{[<>()]{{}()}}[([]<>){()<>}]>>(<{(<><>){[][]}}[{[]{}}([][])]>{[{()[]}{<>}}{{{}{}}< +(<<[{[[{<<{{{<[]()>}}<[{()()}(<><>)](<(){}>[{}[]])>}<{{[()[]]([]{})}}{{((){}){[]()}}[({}{})< +<<<{<{{([<<{[{<><>}((){})]{(<><>)(()<>)})<{[{}<>]{<>()}}{<()[]><(){}>}>>>{{<<<[]()>{()[]}>({ +{[(<[[{[(({<(<{}<>>{<>()}>({[]{}})>[[(()<>){<>[]}][[<><>]{<>{}}]]}))[[{{[<()[]>(()<>)]}}([([<>{ +<([({((<((({[<()<>>]<<()[]>(()[])>}{({()[]}[[]{}])})<[{[()<>](<>{})}<(<>{}){()[]}>]<((()())[[]()])>>))>< +{{([([(<((([[({}[])(<>{})][<()()>{<>()}]])[{{[()<>]{{}()}}<[()()][()[]]>}]){{{[{[]()}]}<[<<>( +<[[{((<{{{(<((<>{})[{}[]])(<()[]><()()>)>{[{<>()}[()[]]]{{[]<>}<<><>>}})}(<{<(<>)[()]>}>)}<{<[([<>{}][(){}]) +<{[<[{[<{[(<[[<>[]]<{}<>>]>[[<<>[]>{{}[]}]{({}[])[[]<>]}])]{{{<<{}{}>{<>()}>[{()<>}{(){}}]}{<[()()][(){}] +({<([<[<((<({({}[])<[][]>})[<[<>()]([]{})><(<><>){{}[]}>]>))[<[<([[][]]{{}[]})>](<(<<>()>)(<{}<>>)>)>]>]>] +{{{[{{<<{<[<(<<>[]>[{}{}])>[[{{}<>}<<>[]>][[[]<>]<[]{}>]]]{<({()<>}((){})){[[][]]<[]>}>[<(<>[])>([{ +{<({[{{{(<{<[<[]<>>]{{{}[]}<<><>>}>{[[<>[]]<{}[]>]}}(<(<()()><{}[]>)>{[[()()][()<>]]})><(({[()()]}[[[ +<<[{<{{<(<({[<()()>{()<>}]((()[]){[]{}})}{[(()[])[{}{})]{({}<>){<>()}}}){({<{}<>>[{}]}(<{}[]>))[{[[] +<[((<[{<<<(({[[]{}](<>)}{({}{})})(({{}{}})<{()()}>})<<([()[]]{<>()}){([]()){<><>}}>>>>{(<<{<{}()>{()<>}}( +{(({[[<{[{[(({{}<>}(()[]))[([]{}){()()}])](<<{{}()}{<>{}}><<{}[]><{}()>>><[[<>()][()]]({<> +[[({<(<[[({[{<{}[]>(()<>)}<{()()}>][[[{}<>](()[])]{(<><>){()<>}}]}[([{<><>}<<>()>]<{{}()}<<>{}>>)[{{( +[[<(([<{{{<{({<>{}}{<>{}})}<[<{}[]>(<>[])]<((){})>>>{[{<{}[]>{{}[]}}{<[][]>{{}<>}}]{(<[][]>{()))<<[]{}>{( +(<[{<<({<<{{(([]()){[]()})[({}{})]}({({}{})<{}{}>}(({}){{}<>}))}{<<<{}{}><{}()>><[{}()][[]]>>}>>[<{({({})[<> +{([(<([[<{(<({()[]>[<><>]){[[]<>](<><>)}>{{[()<>]({}{})}<{<>[]}[(){}]>})}{(({<[]()>{{}()}}{{<>()}(< +(<<[{([<[{{[[([]{})]<(()<>){<><>}>]<[{<>[]}<{}[]>]>}<<{([]<>)[{}<>]}(([]<>)<<>()>)>[<{<>{}} +<[([[<<<<<({<[()[]]>}[<({}<>)>])[[({{}()}(()<>))<<<><>>{[]{}}>]{{[<>()][[][]]}{<{}{}>({}())}}]>[{{{[ +[<{[(<[<<{<(<{<>()}>[{{}()}{{}{}}])>}>([<[<[[][]]>((()<>)<(){}>)]{(<()>)(<<>[]>({}()))}>([[<[]<>> +<[(([<{(<<<{{<[]<>><[]<>>}{<()[]>(<>())}}<{<<>()>{[]}}(<()<>>[[]{}])>>>[<{([()[]]<{}()>)<<{}{}>{{}[]}>}{((< +[{<[{[([(<({<<<>{}><<><>>>{(()()){()[]}}}({[<>[]]<<><>>}([()]({}<>)))){{{[[]{}]{{}<>}}[(<>{}){<>}]}[<{[][]} +<<<{({[(({[{({<><>}([][])){{<><>}[(){}]}}(([{}{}][{}[]]))]({<({}<>){{}()}>[[{}[]]{[]{}}]}}}))]<[[<(<{{() +[(([[(<({{({(<()()>(<>{}))}({<[]<>>{[]<>}}<[<><>]{[]<>}>)]<([[[]{}][[]{}]]{([][]){()<>}}){{[{} +<{(<[<{<{([{([()[]]<(){}>)([[]<>]({}()))}](<([[]<>]<<>{}>)[{[]{}}[<><>]]>))}>}>[{<({<<[(()() +[{({[({<[((({[<>()]<[][]>}{{<>}{(){}}})([(()<>)<<><>>][({}{})<<>[]]]))<<<{{}()}<[]<>>>(<()[]><[]()>)> +[[<{((<<(<<{(<<>[]>[(){}])(([]<>)([]{}))}<[[(){}]<[]<>>]{(<>())(<>)}>>>)>[{<[{{[<>{}]{[]()}}[ +<([{{{<([([([<(){}><()[]>])([[{}<>]]{[()<>>({}())})]<[<{()[]}{()()}><[[]()][<><>]>]>)]<{({((()() +<<{([<[[{<[<(({}<>)(()()))({{}[]>[<>()])>{[<{}>]<[{}{}]>}]([{{[]{}}[{}()]}{[()<>]{<>}}]{([<>{}][[]() +({[[[[(<<{{[{(<>[])[{}]}[<[]<>>[<><>]]]}}([<([<><>]{<>{}})[<[]{}>[<><>]]>{(<()<>>[{}])[([][])[{}{}]]}])>>( +<({<(<<[({[{{{{}[]}<<><>>}[([][])(()<>)]}]<<[[{}]{<>()}]>>}<<<{<<>[]>([]())}<({}())[<><>]>>{<{<>{} +(<({{<{[{[({[{[]<>}{()[]}]}<{{{}()}{[]<>}}<<{}<>>[{}{}]>>)[[({{}<>}[<>()])<[{}[]][()()]>]}][{{<[[]{}][[]()] +(<<[((<{<<{[[{<>{}}[<><>]]{{{}{}}{<>[]}}]<[{()()}([]())](({}()){()[]})>}>)<((<<[{}[]]>[{<>[]}([]{})]>)({({[]< +([({<<(({((<{[{}[]]<()<>>}{[{}<>][{}()]}>)<<(<{}[]>[[][]]){<()<>)<<>()>}><(<<><>><[]{}>)[<<>()>[< +[([<[[(((((({<(){}>[{}{}]}([[]()]{()[]})))<[(({}<>)[<>{}])(([][])([]{}))](<[()()]{<>()}><[()[]]{()( +({[({[{<<({<[(<>())<{}<>>]>[[(<><>)][{{}{}}[{}()]]]}<[([[]<>]([]<>))<[()<>]<()()>>]<{<{}[]>({}())}{<< +(<<{(<<({{<<<<(){}>([]())>{[()()][{}{}]}>[([[]<>]{{}}){{{}{}}}]>}}{(({{(<><>){<>()}}}[({[]()})[[[]]{[]{}}]] +((<[([((<[([{[<>()][()()]}{[{}<>][<>[]]}]<(([][])({}())){<()[]>([]())}>)[{{<<><>>([]{}}}[{<><>}<{}[] +<{(<<[<<<{[{<<[]()>[()<>]>(([]{})([]<>))]{<([][])<<>{}>>{(<>()){()}}}](<{[<>[]]}([()()]{<>{}})>(<[()[]][ +[<{{[[[<[[{((([]())<{}[]>){(<><>)})[{{{}}}[({}[])({}{})]}}{{(([]<>)<<><>>)<([][])({}<>)>}{<<()[]><()()>>[<{} +<<([(<<({<<(<(<>[]){()[]}>{[()<>)(<><>)})>{((<{}[]>(()[]))({[]<>}[{}{}]))<{<<><>>(<>())}<({}<>)[[]] +<(<<{[<<[[[{<[<>{}]{<><>}>[<<>[]>[(){}]}}{{<{}{}>}(<<>()><[]()>)}]]{{([[[]{}][[]()]][[<>()][{}[]]] +(<{<<{<{[{<{<[()()]<{}()]>{<<>()>({})}}>{[{<{}<>>}<<[]<>><[][]>>]<([{}{}][[]<>]){{[][]}{<>}}>}}] +{{[[([<[[{<[[<[]()>[()<>]]<({}[])[{}[]]>]>}<[((([][]){[]}){<{}{}>((){})})][([([][])({}[])](<{}<>><<>{}>))]> +[[{{({{[[[{<[{(){}}{[]<>}>[<{}[]>]>}]((<{<(){}><{}()>}[<<><>>({}{})]><<{{}{}}>(({}[]){()<>} +[<<([<<({[<({{()()}[[]()]}({{}<>}[<>()]))>([[[{}<>]([][])][({}()){[]()}]])]((([[(){}](()<>)](({}{}>)) +<[{<{(<<([({[({}<>)[[][]]]{{[][]}<{}{}>}})([[[[]{}](<>{})]([<><>](()()))]{{([])[()()]}({<>[]})])]<<(({[]( +[((<<[<[{<[{{[()[]]({}())}([[]{}]<()[]>)}[(<[]()>{()[]})<<<>()>[()()]>}]({{[{}{}][()[]]}{{<>()} +{{{{<[([([<({{[]()}<(){}>}{(()[])<<>()>})>{([({}[])<[]<>>][[[]()]])}]<{<((<>)[()<>])<(()<>) +[[<{<([[([<[{[<><>]{(){}}}{(<>()){{}}}]>[[{{<>[]}{[]}}]]][<{[<{}()>]<[{}()][<><>]>}(<{[]<> +((<[<{<<<{<<{[[]{}]}>{<(<><>)<<>()>>{(<><>)({}())})>{{<([]{})<[]()>>{[<>{}][()()]}}[<<<>{}>([]{})>[{{}}<()() +<<({(<{[[<((<{<>[]}(()())>>[<[[]<>][<>{}]>])[(<{{}{}}><<<>()>>)(([[]()]{[]<>}))]><<[{<[]{}><()[ +(({({({<(([[{({}[])([]())}]({<<>{}>[()<>]}{<[][]>({}[])})][{{[[]<>]}}{[<<>><{}{}]]}])<<[{{()[]} +<[(([<(<{[[([((){}){{}<>}]<[{}{}]{()<>}>)<<{<>[]}[(){}]><({}())([][])>>]<[{<()()>{<><>}}[(<>{}]([]())]][{ +{[<{([{<[{<[<<{}<>>[{}{}]>[[<><>](<>[])]]<{<(){}>([]<>)}{{()<>}([]())}>>{(<(<>)[()<>]>{[{}[]] +{(((((<<[[{(([(){}]<[]()>)[<<><>>])<<{()}{<>[]}><(<>[]){{}{}}>>}({<[{}<>]([]<>)>{{{}{}}<<>{}>}}{([ +<{{<<<(<{{<([<()()>([]<>)]<{()()}<()<>>>){<<[]{}><[][]>><{{}<>}>}>{([[<>]{[]()}][<{}><[][]>])[{((){})< +{{[{[{([{<{<[{()()}{()}]([<>{}][{}{}])><<[[]()]>({()<>}<[][]>)>}({((<>())({}()))[[{}()][<>[]]]}<{([]<>)<{}< +{([<[<(<[([<[{<><>}[{}{}]](<{}<>>[[]<>])>[{{[][]}[{}()]}(<{}[]><{}{}>)]])]<<<[<(<><>)<<>>>{[{}()]}]< +<<[(([[{{({(<[<>()][()<>]>[(()())((){})])(<((){})[()()]><[()<>]<{}[]>>)}{{<(()){{}<>}><<{}{}>{{}()}>} +{({([({{(<((<(()[]){()<>}>[[()<>]]))>{[<[({}()){{}<>}){<<>[]>{()<>}}><[[{}()][<><>]][[<>()] +[<([<{<<[[(<[{{}<>}{{}[]}]<(()[])<()()>>>[{[<><>][{}{}]}])>{[{<([]())[<>{}]>{<<><>>{(){}}}}{[(< +({{(<{((<({{([()[]](<>))({[][]}{()()})}{(<<>[]><()[]>){((){})[<><>]}}})((<{(<>{}}<(){}>}{{<>{}}{<><>}}>{( +[({[{{([({[{<{<>[]}[[]()]>}[(({}<>){<>[]})[{{}{}}<{}()>]]]}{[{[{<>()}](<<>()>(<>()))}][<(({} +<{([[(<[{[{(<<{}<>>(<>{})><{(){}}<{}<>>>)(({()()}([]())){<[]{}>({}<>)})}[{<[(){}][(){}]>{{<>()}<( +{[(<<<[<{[{({[<>()]{<>[]}})[<<<>()><{}())>([[]()]((){}))]}][[{<[<>[]]<<>()>>(({}()))}](<{<<>>{<>[] +{{{({{<[{([[(([]())<<><>>)(([]{})[[]()])]((<{}[]>{[]()}))]<{<{<>[]}(()())>({<>[]}[<><>])}(((()()){<>[]} +<[{[([{([[<(<([][]){{}[]}>([(){}][<>]))><({<()[]>[<>()]}[([]{})])>]({{[(<><>)(<>)]{([]())}}<<{<><>}<{}() +[[(<[[(<([([{[{}{}]{{}()}}(({}<>)<[]{}>)]{<(()())>})]({{(([]<>)[[]<>]){[<><>]{<><>}}}}[<{<<><>>[<>{ +([[(({(<({<{<[<>{}]([]{})>([{}<>]<{}{}>)}[{{[]<>}[[]{}]}]>}{([([[]{}][{}()])([<>]({}<>))])<([[{ +{[[<[({{<({[{({}<>){[]<>}}[({})([]{})]]{<{{}[]}{[]{}}>}}(((<{}>[[]<>]))<(<{}[]>([]<>))>))[[{ +<((<(([(<<[[<{()<>}<{}<>>>{<<><>>{[]<>}}]({{[]<>}[[]{}]}<(<><>)([]<>)>)]><{<[[[]{}]<[]{}>]>}<(<{<>[] +<[[<({[[<{<[<([][]){<>[]}><((){}){()[]}>]>}(<<<{(){}}[[]()]>({[]{}}(()<>))>><[({{}{}}[[]{}])(<<>()><<>[ +[<<[[[[({<{[[{()()}<()()>]{[[][]][[]<>]}][<<<>{}>[<>]>{{<><>}}]}<{[<<>{}>{{}()}]{<{}[]>})<[(())<{}{}>] +{{{<<({(([{{[(<>{})<<>[]>][[[]{}]{()[]}]}<<({}())>{{<>()}<{}<>>}>}]<[<[<()()>{{}()}]<([]<>)>>[([[]]<<>[]>){[ +([[[(<{(({({<[{}()]<[]{}>>[<<><>>(<><>)]}[{<<>>([]{})}([{}[]]{<><>])]){[({()()}((){}))[[[] diff --git a/day10/solution-part1.c b/day10/solution-part1.c new file mode 100644 index 0000000..c69204c --- /dev/null +++ b/day10/solution-part1.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +unsigned int getScore(char symbol){ + switch (symbol) { + case ')': + return 3; + break; + case ']': + return 57; + break; + case '}': + return 1197; + break; + case '>': + return 25137; + break; + default: + return 0; + break; + } +} + +void corruptLine(char expected, char found, FILE *fp){ + printf(" Expected %c, but found %c instead.", expected, found); + fscanf(fp, "%*s"); +} + +int lineLength(FILE *fp){ + unsigned int lineLength=0; + char ch = fgetc(fp); + while ( ch != '\n' && ch != EOF ){ + lineLength++; + ch = fgetc(fp); + if ( lineLength > 30 ) break; + } + return lineLength; +} + + +char reverse(char ch){ + switch (ch) { + case '[': + return ']'; + break; + case ']': + return '['; + break; + case '(': + return ')'; + break; + case ')': + return '('; + break; + case '{': + return '}'; + break; + case '}': + return '{'; + break; + case '<': + return '>'; + break; + case '>': + return '<'; + break; + + } + return 0; +} + + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + + + FILE *fp=fopen(argv[1], "r"); + + unsigned int score=0; + + char ch = '\0'; + while ( !feof(fp) ){ + unsigned int lineStart = ftell(fp); + ch = getc(fp); + if ( EOF == ch ) break; + fseek(fp, lineStart, SEEK_SET); + unsigned int length = lineLength(fp); + fseek(fp, lineStart, SEEK_SET); + char line[length+1]; + memset(line, '\0', sizeof(char) * (length + 1)); + char depthArr[(length+1)/2]; + memset(line, 'a', sizeof(char) * (length + 1)); + unsigned int depth=0; + memset(line, '\0', sizeof(char) * ( ( length + 1 ) / 2 )); + while (1){ + ch = getc(fp); + printf("%c", ch); + // Cant break out of switch and loop at the same time + if ( ch == '\n' ) break; + switch (ch) { + case '[': + case '(': + case '{': + case '<': + depthArr[depth] = ch; + depth++; + break; + case ']': + case ')': + case '}': + case '>': + depth--; + if ( depthArr[depth] != reverse(ch) ){ + corruptLine( reverse(depthArr[ depth ]), ch, fp ); + score += getScore(ch); + } + break; + + } + } + } + + printf("Final Score: %i", score); + fclose(fp); + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +} diff --git a/day10/solution-part2.c b/day10/solution-part2.c new file mode 100644 index 0000000..495d43e --- /dev/null +++ b/day10/solution-part2.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include + +int countLines(FILE *fp){ + long int lines =0; + while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c"))) + ++lines; + return lines; +} + +int cmp_asc_unsigned_long(const void *a, const void *b) { + return *(unsigned long*)a > *(unsigned long*)b; +} + +unsigned int getScore(char symbol){ + switch (symbol) { + case ')': + return 1; + break; + case ']': + return 2; + break; + case '}': + return 3; + break; + case '>': + return 4; + break; + default: + return 0; + break; + } +} + +void corruptLine(char expected, char found, FILE *fp){ + fscanf(fp, "%*s"); +} + +int lineLength(FILE *fp){ + unsigned int lineLength=0; + char ch = fgetc(fp); + while ( ch != '\n' && ch != EOF ){ + lineLength++; + ch = fgetc(fp); + if ( lineLength > 30 ) break; + } + return lineLength; +} + + +char reverse(char ch){ + switch (ch) { + case '[': + return ']'; + break; + case ']': + return '['; + break; + case '(': + return ')'; + break; + case ')': + return '('; + break; + case '{': + return '}'; + break; + case '}': + return '{'; + break; + case '<': + return '>'; + break; + case '>': + return '<'; + break; + + } + return 0; +} + + +int main( int argc, char *argv[] ){ + if( argc == 2 ) { + FILE *fp=fopen(argv[1], "r"); + char ch = '\0'; + int lineCount = countLines(fp); + rewind(fp); + unsigned long completionScores[lineCount]; + memset(completionScores, '0', sizeof(unsigned long) * lineCount); + int compCount = 0; + while ( !feof(fp) ){ + unsigned int lineStart = ftell(fp); + ch = getc(fp); + if ( EOF == ch ) break; + fseek(fp, lineStart, SEEK_SET); + unsigned int length = lineLength(fp); + fseek(fp, lineStart, SEEK_SET); + char line[length+1]; + memset(line, '\0', sizeof(char) * (length + 1)); + char depthArr[(length+1)/2]; + memset(line, 'a', sizeof(char) * (length + 1)); + unsigned int depth=0; + memset(line, '\0', sizeof(char) * ( ( length + 1 ) / 2 )); + bool corrupt=false; + while (1){ + ch = getc(fp); + // Cant break out of switch and loop at the same time + if ( ch == '\n' ) break; + switch (ch) { + case '[': + case '(': + case '{': + case '<': + depthArr[depth] = ch; + depth++; + break; + case ']': + case ')': + case '}': + case '>': + depth--; + if ( depthArr[depth] != reverse(ch) ){ + corruptLine( reverse(depthArr[ depth ]), ch, fp ); + corrupt = true; + } + break; + + } + + } + if ( !corrupt ){ + unsigned long score=0; + for ( int i = depth-1; i >= 0; i-- ){ + score *= 5; + score += getScore(reverse(depthArr[i])); + } + completionScores[compCount++] = score; + } + } + qsort(completionScores, compCount, sizeof(unsigned long), cmp_asc_unsigned_long); + for ( int i = 0; i < compCount; i++ ){ + printf("Score %i: %lu\n", i, completionScores[i]); + } + + printf("Middle score: %lu\n", completionScores[( compCount / 2 )]); + fclose(fp); + return 0; + } else { + printf("You need to provide a file\n"); + return 1; + } +}