Move 2021 into folder

This commit is contained in:
Jonathan Hodgson 2022-12-01 08:55:31 +00:00
parent dbebb7acf3
commit dff4b9cc14
84 changed files with 0 additions and 0 deletions

10
2021/day1/Makefile Normal file
View file

@ -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)

10
2021/day1/example.txt Normal file
View file

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

2000
2021/day1/realinput.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,30 @@
#include <stdio.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int last = ~0;
unsigned int increasing = 0;
unsigned int value;
// Read the values into an array
FILE *fp=fopen(argv[1], "r");
while (!feof (fp)){
fscanf(fp, "%d\n", &value);
printf( "value %d\n", value );
if ( value > last )
increasing++;
last = value;
}
fclose(fp);
printf( "%d were increasing\n", increasing );
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

17
2021/day1/solution-part1.sh Executable file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
# Give file as option 1
file="$1"
increasing=0
last=99999999
while read line; do
if [ "$line" -gt "$last" ]; then
increasing="$(( increasing + 1 ))"
fi
last="$line"
done < <(cat "$file")
echo "$increasing were increasing"

View file

@ -0,0 +1,54 @@
#include <stdio.h>
int countLines(char filename[]){
FILE *fp;
fp=fopen(filename, "r");
long int lines =0;
if ( fp == NULL ) {
return -1;
}
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
fclose(fp); // closing file
return lines;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
// 1 less because file ends with trailing new line
int lines=countLines(argv[1]);
int values[lines];
int line=0;
FILE *fp=fopen(argv[1], "r");
int increasing = 0;
// Read the values into an array
while (!feof (fp)){
fscanf(fp, "%d\n", &values[line]);
if ( (line >= 3) && (values[line] > values[line-3]) ){
increasing++;
}
line++;
}
fclose(fp); // closing file
printf( "Increasing: %d\n", increasing );
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

22
2021/day1/solution-part2.sh Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Give file as option 1
file="$1"
increasing=0
readarray -t "arr" < "$file"
length="${#arr[@]}"
for i in $(seq 3 "$length"); do
curr="${arr[$i]}"
prev3="${arr[$((i - 3))]}"
if [ -n "$curr" ] && [ "$curr" -gt "$prev3" ]; then
increasing="$((increasing + 1))"
fi
done
echo "$increasing were increasing"

10
2021/day10/Makefile Normal file
View file

@ -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)

10
2021/day10/example.txt Normal file
View file

@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]

90
2021/day10/realinput.txt Normal file
View file

@ -0,0 +1,90 @@
{<{<[{<[<([[(<<>[]>[<>()]){(()<>){<>{}}}]<{<{}<>){()<>}}[[()[]]{(){}}]>]{<([[]][{}<>]){<{}<>>{[]<>}}><[(
[<{(([{<[(<{[{()()}{()[]}]{[<>[]]}}>)[[{[<[][]>(()[])]<({}<>)<()<>>>}([({}{}){[]<>}])]<{{([][])[()
([<[{[{[{(<{<[[]][[][]]>[(<>[])<[]()>]]<({()<>}([]<>))>>)((([[{}{}]](<(){}><[]{}>))<<{{}[]}[<>()]><[[][]](()(
(<<<{{{[[<(((<[]()><{}{}>)([<>{}](<>{}}))([(()())[[]<>]]<{[]()}(<>[])>))[[[<{}[]>({}{})]]<{[<>()]((){})}<<[][
{{[<{([[[([(<(<><>)>{([][]){()[]}})<{{{}()}<()<>>}>])]]<({<{(<()<>>([][]))<<(){}>]}{{<()()
<[{({[(<[<{{<(<>[])<[][]>]}}((((()<>)<<>[]>)[[<>{}]<{}>]){{(()[])}})>(<<(([]{})<<>[]>)[([][])
((<[[(([{[[{((()())[{}{}])([(){}]<{}[]>)][[{{}{}}[[][]]][(()())(<>[])]]]{<[[{}[]]]<<()<>>[
([(<<(<<[[<{{([]())[()<>]}<{<><>}[<>[]]>}{<<[][]>[[][]]><[[]<>}({}<>)>}>[[<([]{})>{({}{})[()[]]}][<{[]
<<[[<{[<<[<<[{<>[]}({}[])]<[[][]]<{}()>>>((<{}[]>{{}{}}>(<<>{}>([]{})))>(<(<[]()>(<>{}))<((){
<{<[[<([[[[[<[{}{}](<><>)><{()<>}[{}()]>]{<<()()>{{}{}}>[[()<>][{}()]]}]{{({[]()}[[]<>])(<<>{}>
([{[[<(({<((({()()}({}{}))<<{}()><[]<>>>)([{{}<>}][<[][]>(<><>)]))>{{[[[{}[]]([]<>)][{()<>}]][([[]{}]{<>[]})]
(<(<{<{{({<({[{}[]]<(){}>}[<<>[])(<>{})])[[<{}<>>[[]<>]][([]{})<<>[]>]]>[{([{}<>]([]<>))<[<>()
(({[[[<{([[{<[{}()]<[]<>>>}]{<[<{}>{<><>}]>{{(()[])}<[<>{}]{<>[]})}}]{[[<[<>]{{}[]}><[()[]]{{}
{[[{{<({{<<<{[<>()]{{}()}}[([]<>){()<>}]>>(<{(<><>){[][]}}[{[]{}}([][])]>{[{()[]}{<>}}{{{}{}}<
(<<[{[[{<<{{{<[]()>}}<[{()()}(<><>)](<(){}>[{}[]])>}<{{[()[]]([]{})}}{{((){}){[]()}}[({}{})<
<<<{<{{([<<{[{<><>}((){})]{(<><>)(()<>)})<{[{}<>]{<>()}}{<()[]><(){}>}>>>{{<<<[]()>{()[]}>({
{[(<[[{[(({<(<{}<>>{<>()}>({[]{}})>[[(()<>){<>[]}][[<><>]{<>{}}]]}))[[{{[<()[]>(()<>)]}}([([<>{
<([({((<((({[<()<>>]<<()[]>(()[])>}{({()[]}[[]{}])})<[{[()<>](<>{})}<(<>{}){()[]}>]<((()())[[]()])>>))><
{{([([(<((([[({}[])(<>{})][<()()>{<>()}]])[{{[()<>]{{}()}}<[()()][()[]]>}]){{{[{[]()}]}<[<<>(
<[[{((<{{{(<((<>{})[{}[]])(<()[]><()()>)>{[{<>()}[()[]]]{{[]<>}<<><>>}})}(<{<(<>)[()]>}>)}<{<[([<>{}][(){}])
<{[<[{[<{[(<[[<>[]]<{}<>>]>[[<<>[]>{{}[]}]{({}[])[[]<>]}])]{{{<<{}{}>{<>()}>[{()<>}{(){}}]}{<[()()][(){}]
({<([<[<((<({({}[])<[][]>})[<[<>()]([]{})><(<><>){{}[]}>]>))[<[<([[][]]{{}[]})>](<(<<>()>)(<{}<>>)>)>]>]>]
{{{[{{<<{<[<(<<>[]>[{}{}])>[[{{}<>}<<>[]>][[[]<>]<[]{}>]]]{<({()<>}((){})){[[][]]<[]>}>[<(<>[])>([{
{<({[{{{(<{<[<[]<>>]{{{}[]}<<><>>}>{[[<>[]]<{}[]>]}}(<(<()()><{}[]>)>{[[()()][()<>]]})><(({[()()]}[[[
<<[{<{{<(<({[<()()>{()<>}]((()[]){[]{}})}{[(()[])[{}{})]{({}<>){<>()}}}){({<{}<>>[{}]}(<{}[]>))[{[[]
<[((<[{<<<(({[[]{}](<>)}{({}{})})(({{}{}})<{()()}>})<<([()[]]{<>()}){([]()){<><>}}>>>>{(<<{<{}()>{()<>}}(
{(({[[<{[{[(({{}<>}(()[]))[([]{}){()()}])](<<{{}()}{<>{}}><<{}[]><{}()>>><[[<>()][()]]({<>
[[({<(<[[({[{<{}[]>(()<>)}<{()()}>][[[{}<>](()[])]{(<><>){()<>}}]}[([{<><>}<<>()>]<{{}()}<<>{}>>)[{{(
[[<(([<{{{<{({<>{}}{<>{}})}<[<{}[]>(<>[])]<((){})>>>{[{<{}[]>{{}[]}}{<[][]>{{}<>}}]{(<[][]>{()))<<[]{}>{(
(<[{<<({<<{{(([]()){[]()})[({}{})]}({({}{})<{}{}>}(({}){{}<>}))}{<<<{}{}><{}()>><[{}()][[]]>>}>>[<{({({})[<>
{([(<([[<{(<({()[]>[<><>]){[[]<>](<><>)}>{{[()<>]({}{})}<{<>[]}[(){}]>})}{(({<[]()>{{}()}}{{<>()}(<
(<<[{([<[{{[[([]{})]<(()<>){<><>}>]<[{<>[]}<{}[]>]>}<<{([]<>)[{}<>]}(([]<>)<<>()>)>[<{<>{}}
<[([[<<<<<({<[()[]]>}[<({}<>)>])[[({{}()}(()<>))<<<><>>{[]{}}>]{{[<>()][[][]]}{<{}{}>({}())}}]>[{{{[
[<{[(<[<<{<(<{<>()}>[{{}()}{{}{}}])>}>([<[<[[][]]>((()<>)<(){}>)]{(<()>)(<<>[]>({}()))}>([[<[]<>>
<[(([<{(<<<{{<[]<>><[]<>>}{<()[]>(<>())}}<{<<>()>{[]}}(<()<>>[[]{}])>>>[<{([()[]]<{}()>)<<{}{}>{{}[]}>}{((<
[{<[{[([(<({<<<>{}><<><>>>{(()()){()[]}}}({[<>[]]<<><>>}([()]({}<>)))){{{[[]{}]{{}<>}}[(<>{}){<>}]}[<{[][]}
<<<{({[(({[{({<><>}([][])){{<><>}[(){}]}}(([{}{}][{}[]]))]({<({}<>){{}()}>[[{}[]]{[]{}}]}}}))]<[[<(<{{()
[(([[(<({{({(<()()>(<>{}))}({<[]<>>{[]<>}}<[<><>]{[]<>}>)]<([[[]{}][[]{}]]{([][]){()<>}}){{[{}
<{(<[<{<{([{([()[]]<(){}>)([[]<>]({}()))}](<([[]<>]<<>{}>)[{[]{}}[<><>]]>))}>}>[{<({<<[(()()
[{({[({<[((({[<>()]<[][]>}{{<>}{(){}}})([(()<>)<<><>>][({}{})<<>[]]]))<<<{{}()}<[]<>>>(<()[]><[]()>)>
[[<{((<<(<<{(<<>[]>[(){}])(([]<>)([]{}))}<[[(){}]<[]<>>]{(<>())(<>)}>>>)>[{<[{{[<>{}]{[]()}}[
<([{{{<([([([<(){}><()[]>])([[{}<>]]{[()<>>({}())})]<[<{()[]}{()()}><[[]()][<><>]>]>)]<{({((()()
<<{([<[[{<[<(({}<>)(()()))({{}[]>[<>()])>{[<{}>]<[{}{}]>}]([{{[]{}}[{}()]}{[()<>]{<>}}]{([<>{}][[]()
({[[[[(<<{{[{(<>[])[{}]}[<[]<>>[<><>]]]}}([<([<><>]{<>{}})[<[]{}>[<><>]]>{(<()<>>[{}])[([][])[{}{}]]}])>>(
<({<(<<[({[{{{{}[]}<<><>>}[([][])(()<>)]}]<<[[{}]{<>()}]>>}<<<{<<>[]>([]())}<({}())[<><>]>>{<{<>{}
(<({{<{[{[({[{[]<>}{()[]}]}<{{{}()}{[]<>}}<<{}<>>[{}{}]>>)[[({{}<>}[<>()])<[{}[]][()()]>]}][{{<[[]{}][[]()]
(<<[((<{<<{[[{<>{}}[<><>]]{{{}{}}{<>[]}}]<[{()()}([]())](({}()){()[]})>}>)<((<<[{}[]]>[{<>[]}([]{})]>)({({[]<
([({<<(({((<{[{}[]]<()<>>}{[{}<>][{}()]}>)<<(<{}[]>[[][]]){<()<>)<<>()>}><(<<><>><[]{}>)[<<>()>[<
[([<[[(((((({<(){}>[{}{}]}([[]()]{()[]})))<[(({}<>)[<>{}])(([][])([]{}))](<[()()]{<>()}><[()[]]{()(
({[({[{<<({<[(<>())<{}<>>]>[[(<><>)][{{}{}}[{}()]]]}<[([[]<>]([]<>))<[()<>]<()()>>]<{<{}[]>({}())}{<<
(<<{(<<({{<<<<(){}>([]())>{[()()][{}{}]}>[([[]<>]{{}}){{{}{}}}]>}}{(({{(<><>){<>()}}}[({[]()})[[[]]{[]{}}]]
((<[([((<[([{[<>()][()()]}{[{}<>][<>[]]}]<(([][])({}())){<()[]>([]())}>)[{{<<><>>([]{}}}[{<><>}<{}[]
<{(<<[<<<{[{<<[]()>[()<>]>(([]{})([]<>))]{<([][])<<>{}>>{(<>()){()}}}](<{[<>[]]}([()()]{<>{}})>(<[()[]][
[<{{[[[<[[{((([]())<{}[]>){(<><>)})[{{{}}}[({}[])({}{})]}}{{(([]<>)<<><>>)<([][])({}<>)>}{<<()[]><()()>>[<{}
<<([(<<({<<(<(<>[]){()[]}>{[()<>)(<><>)})>{((<{}[]>(()[]))({[]<>}[{}{}]))<{<<><>>(<>())}<({}<>)[[]]
<(<<{[<<[[[{<[<>{}]{<><>}>[<<>[]>[(){}]}}{{<{}{}>}(<<>()><[]()>)}]]{{([[[]{}][[]()]][[<>()][{}[]]]
(<{<<{<{[{<{<[()()]<{}()]>{<<>()>({})}}>{[{<{}<>>}<<[]<>><[][]>>]<([{}{}][[]<>]){{[][]}{<>}}>}}]
{{[[([<[[{<[[<[]()>[()<>]]<({}[])[{}[]]>]>}<[((([][]){[]}){<{}{}>((){})})][([([][])({}[])](<{}<>><<>{}>))]>
[[{{({{[[[{<[{(){}}{[]<>}>[<{}[]>]>}]((<{<(){}><{}()>}[<<><>>({}{})]><<{{}{}}>(({}[]){()<>}
[<<([<<({[<({{()()}[[]()]}({{}<>}[<>()]))>([[[{}<>]([][])][({}()){[]()}]])]((([[(){}](()<>)](({}{}>))
<[{<{(<<([({[({}<>)[[][]]]{{[][]}<{}{}>}})([[[[]{}](<>{})]([<><>](()()))]{{([])[()()]}({<>[]})])]<<(({[](
[((<<[<[{<[{{[()[]]({}())}([[]{}]<()[]>)}[(<[]()>{()[]})<<<>()>[()()]>}]({{[{}{}][()[]]}{{<>()}
{{{{<[([([<({{[]()}<(){}>}{(()[])<<>()>})>{([({}[])<[]<>>][[[]()]])}]<{<((<>)[()<>])<(()<>)
[[<{<([[([<[{[<><>]{(){}}}{(<>()){{}}}]>[[{{<>[]}{[]}}]]][<{[<{}()>]<[{}()][<><>]>}(<{[]<>
((<[<{<<<{<<{[[]{}]}>{<(<><>)<<>()>>{(<><>)({}())})>{{<([]{})<[]()>>{[<>{}][()()]}}[<<<>{}>([]{})>[{{}}<()()
<<({(<{[[<((<{<>[]}(()())>>[<[[]<>][<>{}]>])[(<{{}{}}><<<>()>>)(([[]()]{[]<>}))]><<[{<[]{}><()[
(({({({<(([[{({}[])([]())}]({<<>{}>[()<>]}{<[][]>({}[])})][{{[[]<>]}}{[<<>><{}{}]]}])<<[{{()[]}
<[(([<(<{[[([((){}){{}<>}]<[{}{}]{()<>}>)<<{<>[]}[(){}]><({}())([][])>>]<[{<()()>{<><>}}[(<>{}]([]())]][{
{[<{([{<[{<[<<{}<>>[{}{}]>[[<><>](<>[])]]<{<(){}>([]<>)}{{()<>}([]())}>>{(<(<>)[()<>]>{[{}[]]
{(((((<<[[{(([(){}]<[]()>)[<<><>>])<<{()}{<>[]}><(<>[]){{}{}}>>}({<[{}<>]([]<>)>{{{}{}}<<>{}>}}{([
<{{<<<(<{{<([<()()>([]<>)]<{()()}<()<>>>){<<[]{}><[][]>><{{}<>}>}>{([[<>]{[]()}][<{}><[][]>])[{((){})<
{{[{[{([{<{<[{()()}{()}]([<>{}][{}{}])><<[[]()]>({()<>}<[][]>)>}({((<>())({}()))[[{}()][<>[]]]}<{([]<>)<{}<
{([<[<(<[([<[{<><>}[{}{}]](<{}<>>[[]<>])>[{{[][]}[{}()]}(<{}[]><{}{}>)]])]<<<[<(<><>)<<>>>{[{}()]}]<
<<[(([[{{({(<[<>()][()<>]>[(()())((){})])(<((){})[()()]><[()<>]<{}[]>>)}{{<(()){{}<>}><<{}{}>{{}()}>}
{({([({{(<((<(()[]){()<>}>[[()<>]]))>{[<[({}()){{}<>}){<<>[]>{()<>}}><[[{}()][<><>]][[<>()]
[<([<{<<[[(<[{{}<>}{{}[]}]<(()[])<()()>>>[{[<><>][{}{}]}])>{[{<([]())[<>{}]>{<<><>>{(){}}}}{[(<
({{(<{((<({{([()[]](<>))({[][]}{()()})}{(<<>[]><()[]>){((){})[<><>]}}})((<{(<>{}}<(){}>}{{<>{}}{<><>}}>{(
[({[{{([({[{<{<>[]}[[]()]>}[(({}<>){<>[]})[{{}{}}<{}()>]]]}{[{[{<>()}](<<>()>(<>()))}][<(({}
<{([[(<[{[{(<<{}<>>(<>{})><{(){}}<{}<>>>)(({()()}([]())){<[]{}>({}<>)})}[{<[(){}][(){}]>{{<>()}<(
{[(<<<[<{[{({[<>()]{<>[]}})[<<<>()><{}())>([[]()]((){}))]}][[{<[<>[]]<<>()>>(({}()))}](<{<<>>{<>[]
{{{({{<[{([[(([]())<<><>>)(([]{})[[]()])]((<{}[]>{[]()}))]<{<{<>[]}(()())>({<>[]}[<><>])}(((()()){<>[]}
<[{[([{([[<(<([][]){{}[]}>([(){}][<>]))><({<()[]>[<>()]}[([]{})])>]({{[(<><>)(<>)]{([]())}}<<{<><>}<{}()
[[(<[[(<([([{[{}{}]{{}()}}(({}<>)<[]{}>)]{<(()())>})]({{(([]<>)[[]<>]){[<><>]{<><>}}}}[<{<<><>>[<>{
([[(({(<({<{<[<>{}]([]{})>([{}<>]<{}{}>)}[{{[]<>}[[]{}]}]>}{([([[]{}][{}()])([<>]({}<>))])<([[{
{[[<[({{<({[{({}<>){[]<>}}[({})([]{})]]{<{{}[]}{[]{}}>}}(((<{}>[[]<>]))<(<{}[]>([]<>))>))[[{
<((<(([(<<[[<{()<>}<{}<>>>{<<><>>{[]<>}}]({{[]<>}[[]{}]}<(<><>)([]<>)>)]><{<[[[]{}]<[]{}>]>}<(<{<>[]
<[[<({[[<{<[<([][]){<>[]}><((){}){()[]}>]>}(<<<{(){}}[[]()]>({[]{}}(()<>))>><[({{}{}}[[]{}])(<<>()><<>[
[<<[[[[({<{[[{()()}<()()>]{[[][]][[]<>]}][<<<>{}>[<>]>{{<><>}}]}<{[<<>{}>{{}()}]{<{}[]>})<[(())<{}{}>]
{{{<<({(([{{[(<>{})<<>[]>][[[]{}]{()[]}]}<<({}())>{{<>()}<{}<>>}>}]<[<[<()()>{{}()}]<([]<>)>>[([[]]<<>[]>){[
([[[(<{(({({<[{}()]<[]{}>>[<<><>>(<><>)]}[{<<>>([]{})}([{}[]]{<><>])]){[({()()}((){}))[[[]

132
2021/day10/solution-part1.c Normal file
View file

@ -0,0 +1,132 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
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;
}
}

155
2021/day10/solution-part2.c Normal file
View file

@ -0,0 +1,155 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
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;
}
}

10
2021/day11/Makefile Normal file
View file

@ -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)

10
2021/day11/example.txt Normal file
View file

@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

10
2021/day11/realinput.txt Normal file
View file

@ -0,0 +1,10 @@
5723573158
3154748563
4783514878
3848142375
3637724151
8583172484
7747444184
1613367882
6228614227
4732225334

130
2021/day11/solution-part1.c Normal file
View file

@ -0,0 +1,130 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define GRIDROWS 10
#define GRIDCOLS 10
typedef struct Octopus {
unsigned int energy;
bool flashed;
} Octopus;
unsigned int flashCount = 0;
void printGrid(Octopus grid[GRIDCOLS][GRIDROWS]);
void flash(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row);
void increaseBy1(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row);
void increaseAllBy1(Octopus grid[GRIDCOLS][GRIDROWS]);
void setToZero(Octopus grid[GRIDCOLS][GRIDROWS]);
void step(Octopus grid[GRIDCOLS][GRIDROWS]);
void resetFlashes(Octopus grid[GRIDCOLS][GRIDROWS]);
void printGrid(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ ){
for ( unsigned int col = 0; col < GRIDCOLS; col++ ){
if ( grid[col][row].flashed ) printf(ANSI_COLOR_RED);
printf("%i", grid[col][row].energy );
if ( grid[col][row].flashed ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void flash(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row){
flashCount++;
grid[col][row].flashed = true;
if ( col > 0 ){
if ( row > 0 ) increaseBy1(grid, col-1, row-1);
increaseBy1(grid, col-1, row);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col-1, row+1);
}
if ( row > 0 ) increaseBy1(grid, col, row-1);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col, row+1);
if ( col < GRIDCOLS - 1 ){
if ( row > 0 ) increaseBy1(grid, col+1, row-1);
increaseBy1(grid, col+1, row);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col+1, row+1);
}
}
void increaseBy1(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row){
grid[col][row].energy++;//Increase the enegry
if ( grid[col][row].energy > 9 && !grid[col][row].flashed ){ // if it hasn't already flashed and is over 9 it should
flash(grid, col, row);
}
}
void increaseAllBy1(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ ){
increaseBy1( grid, col, row );
}
}
void setToZero(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ )
if (grid[col][row].energy > 9) grid[col][row].energy = 0;
}
void resetFlashes(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ )
grid[col][row].flashed = false;
}
void step(Octopus grid[GRIDCOLS][GRIDROWS]){
increaseAllBy1(grid);
setToZero(grid);
printGrid(grid);
resetFlashes(grid);
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
Octopus octopuses[10][10]={0};
char ch;
unsigned int row = 0, col=0;
while ( fscanf( fp, "%c",&ch ) && !feof(fp)){
if ( ch == '\n' ){
col=0;
row++;
} else {
octopuses[col++][row].energy = atoi(&ch);
}
}
printGrid(octopuses);
for ( unsigned int i = 0; i < 100; i++ ){
printf("\n\n");
step(octopuses);
}
printf("\nThere were %i flashes\n", flashCount);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

140
2021/day11/solution-part2.c Normal file
View file

@ -0,0 +1,140 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define GRIDROWS 10
#define GRIDCOLS 10
typedef struct Octopus {
unsigned int energy;
bool flashed;
} Octopus;
unsigned int flashCount = 0;
unsigned int countZeros(Octopus grid[GRIDCOLS][GRIDROWS]);
void printGrid(Octopus grid[GRIDCOLS][GRIDROWS]);
void flash(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row);
void increaseBy1(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row);
void increaseAllBy1(Octopus grid[GRIDCOLS][GRIDROWS]);
void setToZero(Octopus grid[GRIDCOLS][GRIDROWS]);
void step(Octopus grid[GRIDCOLS][GRIDROWS]);
void resetFlashes(Octopus grid[GRIDCOLS][GRIDROWS]);
unsigned int countZeros(Octopus grid[GRIDCOLS][GRIDROWS]){
unsigned int count = 0;
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ )
if ( grid[col][row].energy == 0 ) count++;
return count;
}
void printGrid(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ ){
for ( unsigned int col = 0; col < GRIDCOLS; col++ ){
if ( grid[col][row].flashed ) printf(ANSI_COLOR_RED);
printf("%i", grid[col][row].energy );
if ( grid[col][row].flashed ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void flash(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row){
flashCount++;
grid[col][row].flashed = true;
if ( col > 0 ){
if ( row > 0 ) increaseBy1(grid, col-1, row-1);
increaseBy1(grid, col-1, row);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col-1, row+1);
}
if ( row > 0 ) increaseBy1(grid, col, row-1);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col, row+1);
if ( col < GRIDCOLS - 1 ){
if ( row > 0 ) increaseBy1(grid, col+1, row-1);
increaseBy1(grid, col+1, row);
if ( row < GRIDROWS - 1 ) increaseBy1(grid, col+1, row+1);
}
}
void increaseBy1(Octopus grid[GRIDCOLS][GRIDROWS], int col, int row){
grid[col][row].energy++;//Increase the enegry
if ( grid[col][row].energy > 9 && !grid[col][row].flashed ){ // if it hasn't already flashed and is over 9 it should
flash(grid, col, row);
}
}
void increaseAllBy1(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ ){
increaseBy1( grid, col, row );
}
}
void setToZero(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ )
if (grid[col][row].energy > 9) grid[col][row].energy = 0;
}
void resetFlashes(Octopus grid[GRIDCOLS][GRIDROWS]){
for ( unsigned int row = 0; row < GRIDROWS; row++ )
for ( unsigned int col = 0; col < GRIDCOLS; col++ )
grid[col][row].flashed = false;
}
void step(Octopus grid[GRIDCOLS][GRIDROWS]){
increaseAllBy1(grid);
setToZero(grid);
printGrid(grid);
resetFlashes(grid);
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
Octopus octopuses[10][10]={0};
char ch;
unsigned int row = 0, col=0;
while ( fscanf( fp, "%c",&ch ) && !feof(fp)){
if ( ch == '\n' ){
col=0;
row++;
} else {
octopuses[col++][row].energy = atoi(&ch);
}
}
printGrid(octopuses);
for ( unsigned int i = 0; countZeros(octopuses) != GRIDCOLS * GRIDROWS; i++ ){
printf("\n\n");
printf("Step %i\n",i+1);
step(octopuses);
}
printf("\nThere were %i flashes\n", flashCount);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

16
2021/day12/Makefile Normal file
View file

@ -0,0 +1,16 @@
solution-part%: solution-part%.c
gcc $< -o $@ -lm
test1-%: solution-% example1.txt
$< example1.txt
test2-%: solution-% example2.txt
$< example2.txt
test3-%: solution-% example3.txt
$< example3.txt
run-%: solution-% realinput.txt
$< realinput.txt
.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2)

7
2021/day12/example1.txt Normal file
View file

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

10
2021/day12/example2.txt Normal file
View file

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

18
2021/day12/example3.txt Normal file
View file

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

24
2021/day12/realinput.txt Normal file
View file

@ -0,0 +1,24 @@
LA-sn
LA-mo
LA-zs
end-RD
sn-mo
end-zs
vx-start
mh-mo
mh-start
zs-JI
JQ-mo
zs-mo
start-JQ
rk-zs
mh-sn
mh-JQ
RD-mo
zs-JQ
vx-sn
RD-sn
vx-mh
JQ-vx
LA-end
JQ-sn

163
2021/day12/solution-part1.c Normal file
View file

@ -0,0 +1,163 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct VertexList VertexList;
typedef struct Vertex Vertex;
struct Vertex{
char name[10];
VertexList *adjacent;
bool bigCave;
bool visited;
};
struct VertexList{
Vertex *vertex;
//This next pointer is to create a linked list of verticies, it does not reperesent graph position or adjacency
VertexList *next;
};
void printVertexList(VertexList *start){
VertexList *next = start;
while (next != NULL){
if ( next->vertex->bigCave ) printf(ANSI_COLOR_RED);
printf("%s ",next->vertex->name);
if ( next->vertex->bigCave ) printf(ANSI_COLOR_RESET);
next=next->next;
}
printf("\n");
}
int indexOf(VertexList *first, char *name ){
VertexList *current = first;
for ( unsigned int count = 0; current != NULL; count++ ){
if ( strcmp(current->vertex->name, name) == 0 ) return count;
else current = current->next;
}
return -1;
}
Vertex* getVertex(VertexList *first, char name[20]){
VertexList *current = first;
while ( current != NULL ){
if ( strcmp(current->vertex->name, name) == 0 ) return current->vertex;
else current = current->next;
}
return NULL;
}
Vertex* newVertex(char name[20]){
Vertex *new = (Vertex*)malloc(sizeof(Vertex));
new->adjacent = NULL;
new->visited = false;
new->bigCave = ( name[0] >= 'A' && name[0] <= 'Z' );
strcpy(new->name,name);
return new;
}
VertexList* newVertexListItem(Vertex *virtex){
VertexList *newList = (VertexList*)malloc(sizeof(VertexList));
newList->next = NULL;
newList->vertex = virtex;
return newList;
}
VertexList* getLast(VertexList *current){
if ( current == NULL ) return NULL;
while ( current->next != NULL ) current = current->next;
return current;
}
unsigned int countPaths(Vertex *current){
// This is the break for the recursion - when we reach the end
if ( strcmp( current->name, "end" ) == 0 ) return 1;
current->visited = true;
unsigned int count = 0;
VertexList *toTry = current->adjacent;
while ( toTry != NULL ){
if ( toTry->vertex->bigCave || !toTry->vertex->visited )
count += countPaths(toTry->vertex);
toTry = toTry->next;
}
current->visited = false;
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
VertexList *firstVirtex = NULL;
VertexList *lastVertex = getLast(firstVirtex);
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
char vertex1[20];
char vertex2[20];
char vertexStart[20] = "start";
Vertex *v1, *v2;
while ( !feof(fp) && fscanf( fp, "%[^-]-%s\n", vertex1,vertex2 ) ){
//Ensures the first virtex exists
v1 = getVertex(firstVirtex, vertex1);
v2 = getVertex(firstVirtex, vertex2);
if ( v1 == NULL ){
v1 = newVertex(vertex1);
if ( firstVirtex == NULL ){
firstVirtex = newVertexListItem(v1);
lastVertex = firstVirtex;
} else {
lastVertex->next = newVertexListItem(v1);
lastVertex = lastVertex->next;
}
}
//Ensures the second vertex exists
if ( v2 == NULL ){
v2 = newVertex(vertex2);
lastVertex->next = newVertexListItem(v2);
lastVertex = lastVertex->next;
}
//Links v1 and v2
if ( v1->adjacent == NULL ){
v1->adjacent = newVertexListItem(v2);
} else if ( indexOf(v1->adjacent, v2->name) < 0 ) {
getLast(v1->adjacent)->next = newVertexListItem(v2);
}
if ( v2->adjacent == NULL ){
v2->adjacent = newVertexListItem(v1);
} else if ( indexOf(v2->adjacent, v1->name) < 0 ) {
getLast(v2->adjacent)->next = newVertexListItem(v1);
}
//break;
}
printVertexList(firstVirtex);
printf("\n\nThere are %i valid paths", countPaths(getVertex(firstVirtex, vertexStart)));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

167
2021/day12/solution-part2.c Normal file
View file

@ -0,0 +1,167 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct VertexList VertexList;
typedef struct Vertex Vertex;
struct Vertex{
char name[10];
VertexList *adjacent;
bool bigCave;
unsigned int visited;
};
struct VertexList{
Vertex *vertex;
//This next pointer is to create a linked list of verticies, it does not reperesent graph position or adjacency
VertexList *next;
};
void printVertexList(VertexList *start){
VertexList *next = start;
while (next != NULL){
if ( next->vertex->bigCave ) printf(ANSI_COLOR_RED);
printf("%s ",next->vertex->name);
if ( next->vertex->bigCave ) printf(ANSI_COLOR_RESET);
next=next->next;
}
printf("\n");
}
int indexOf(VertexList *first, char *name ){
VertexList *current = first;
for ( unsigned int count = 0; current != NULL; count++ ){
if ( strcmp(current->vertex->name, name) == 0 ) return count;
else current = current->next;
}
return -1;
}
Vertex* getVertex(VertexList *first, char name[20]){
VertexList *current = first;
while ( current != NULL ){
if ( strcmp(current->vertex->name, name) == 0 ) return current->vertex;
else current = current->next;
}
return NULL;
}
Vertex* newVertex(char name[20]){
Vertex *new = (Vertex*)malloc(sizeof(Vertex));
new->adjacent = NULL;
new->visited = 0;
new->bigCave = ( name[0] >= 'A' && name[0] <= 'Z' );
strcpy(new->name,name);
return new;
}
VertexList* newVertexListItem(Vertex *virtex){
VertexList *newList = (VertexList*)malloc(sizeof(VertexList));
newList->next = NULL;
newList->vertex = virtex;
return newList;
}
VertexList* getLast(VertexList *current){
if ( current == NULL ) return NULL;
while ( current->next != NULL ) current = current->next;
return current;
}
unsigned int countPaths(Vertex *current, bool smallVisitedTwice){
// This is the break for the recursion - when we reach the end
if ( strcmp( current->name, "end" ) == 0 ) return 1;
current->visited++;
unsigned int count = 0;
VertexList *toTry = current->adjacent;
while ( toTry != NULL ){
if ( toTry->vertex->bigCave || toTry->vertex->visited == 0 )
count += countPaths(toTry->vertex, smallVisitedTwice);
else if ( !smallVisitedTwice && toTry->vertex->visited < 2 && strcmp(toTry->vertex->name, "start") != 0 ) {
count += countPaths(toTry->vertex, true);
}
toTry = toTry->next;
}
current->visited--;
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
VertexList *firstVirtex = NULL;
VertexList *lastVertex = getLast(firstVirtex);
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
char vertex1[20];
char vertex2[20];
char vertexStart[20] = "start";
Vertex *v1, *v2;
while ( !feof(fp) && fscanf( fp, "%[^-]-%s\n", vertex1,vertex2 ) ){
//Ensures the first virtex exists
v1 = getVertex(firstVirtex, vertex1);
v2 = getVertex(firstVirtex, vertex2);
if ( v1 == NULL ){
v1 = newVertex(vertex1);
if ( firstVirtex == NULL ){
firstVirtex = newVertexListItem(v1);
lastVertex = firstVirtex;
} else {
lastVertex->next = newVertexListItem(v1);
lastVertex = lastVertex->next;
}
}
//Ensures the second vertex exists
if ( v2 == NULL ){
v2 = newVertex(vertex2);
lastVertex->next = newVertexListItem(v2);
lastVertex = lastVertex->next;
}
//Links v1 and v2
if ( v1->adjacent == NULL ){
v1->adjacent = newVertexListItem(v2);
} else if ( indexOf(v1->adjacent, v2->name) < 0 ) {
getLast(v1->adjacent)->next = newVertexListItem(v2);
}
if ( v2->adjacent == NULL ){
v2->adjacent = newVertexListItem(v1);
} else if ( indexOf(v2->adjacent, v1->name) < 0 ) {
getLast(v2->adjacent)->next = newVertexListItem(v1);
}
//break;
}
printVertexList(firstVirtex);
printf("\n\nThere are %i valid paths", countPaths(getVertex(firstVirtex, vertexStart), false));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day13/Makefile Normal file
View file

@ -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)

21
2021/day13/example.txt Normal file
View file

@ -0,0 +1,21 @@
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5

936
2021/day13/realinput.txt Normal file
View file

@ -0,0 +1,936 @@
1148,688
1020,159
857,707
1176,415
388,275
50,849
544,520
1,418
1119,280
217,26
358,110
1302,684
910,791
45,287
544,806
423,859
529,866
1150,686
977,668
666,245
380,603
892,343
437,483
1109,411
296,534
763,676
711,218
139,774
923,397
1202,341
664,12
416,89
522,54
524,401
541,796
1153,523
1101,427
977,308
102,856
769,546
966,523
1218,820
833,682
872,806
457,542
333,586
238,537
528,103
169,840
321,44
102,408
825,346
1220,791
1223,584
907,597
785,766
147,654
201,707
775,674
746,651
221,354
213,460
843,105
59,110
224,773
654,368
666,21
1134,736
475,121
445,799
1141,411
835,688
810,12
810,882
1064,51
1240,805
266,723
125,812
1048,299
213,68
129,738
100,256
666,551
1004,632
1250,505
119,674
565,264
937,220
387,497
1235,259
8,658
902,294
159,884
222,159
184,645
700,646
1251,110
53,469
1228,282
157,779
862,759
694,31
10,876
652,110
1104,505
554,582
574,742
985,642
410,830
873,420
623,42
70,89
922,395
627,446
724,848
610,534
1042,283
852,731
370,193
109,420
1275,239
582,546
88,537
1133,682
584,847
835,654
299,649
735,82
467,547
408,51
1278,233
758,325
1044,171
233,364
490,16
326,187
455,114
1231,504
1300,876
403,830
1185,418
731,187
256,354
1200,725
1101,338
781,399
1207,147
1153,502
335,752
716,389
811,747
619,311
865,95
221,443
567,310
503,112
646,658
296,617
314,40
813,483
1223,252
813,502
566,724
445,95
832,437
191,614
268,611
162,78
303,5
865,114
373,108
688,9
692,511
403,696
1071,724
786,401
544,164
150,457
75,198
1006,607
441,68
300,873
160,686
1059,646
53,47
850,847
458,182
1265,259
869,826
571,241
408,395
1007,889
515,95
458,163
154,761
642,774
571,653
199,266
364,719
109,474
700,360
160,14
671,345
448,583
711,666
296,277
984,187
1096,824
381,469
649,624
1310,635
1206,607
392,35
381,698
520,479
482,145
498,444
239,170
601,21
522,840
1067,770
415,259
827,595
52,725
251,597
0,10
150,736
194,840
504,630
586,275
140,387
716,284
619,127
1066,178
827,299
858,634
652,336
738,612
1016,233
431,595
760,656
909,803
960,679
171,376
45,194
687,852
923,385
1116,840
112,696
1193,575
119,786
290,159
401,803
515,662
708,110
1054,354
1019,351
1103,408
1250,610
979,430
213,434
574,395
1153,859
654,635
671,526
912,873
664,236
1126,697
157,819
1129,271
460,46
494,196
604,849
1131,206
975,80
1111,655
798,473
716,262
493,644
378,607
1087,285
490,526
914,400
485,444
938,584
735,812
639,549
408,724
244,178
934,166
739,241
325,700
139,120
1067,124
950,681
356,863
639,345
618,735
1242,270
848,302
222,107
663,142
1302,658
441,98
850,495
759,733
386,516
488,250
892,691
869,684
1136,886
1067,572
671,318
890,560
82,53
692,735
1061,234
1097,460
922,275
1006,679
631,341
100,424
1032,166
959,553
1046,22
422,64
537,266
340,830
945,562
190,357
1206,516
541,546
656,78
756,190
146,411
723,15
1148,816
602,744
602,560
1171,351
262,859
761,578
410,206
923,110
223,285
554,246
420,425
251,248
1287,229
90,651
691,311
879,161
445,418
32,9
97,681
1032,280
782,551
1228,53
479,334
869,98
1221,264
959,889
57,120
795,120
435,452
750,830
529,674
666,721
996,152
596,740
1072,203
1141,252
937,200
825,444
192,262
117,266
969,10
45,700
202,634
909,539
930,291
852,289
1113,459
447,668
1174,145
766,806
234,493
497,168
725,479
243,572
36,308
231,35
653,700
1155,742
967,352
90,567
1210,256
26,133
164,430
852,182
550,835
575,278
1096,518
408,618
1126,645
291,203
843,547
393,589
251,646
403,597
956,873
504,158
438,730
1042,611
36,383
622,233
954,255
686,336
1072,89
882,312
1012,184
1056,231
1240,89
639,368
1185,226
37,530
1240,133
1033,619
1116,847
624,336
1227,770
298,38
418,243
60,877
817,112
554,638
1148,766
495,94
363,239
171,518
167,884
192,632
1227,124
119,332
1279,42
1275,655
387,385
843,211
145,271
416,203
1042,560
560,275
378,597
199,239
1242,723
1200,54
684,255
649,270
709,425
234,137
52,560
36,159
1298,264
1201,420
1143,570
769,796
773,19
1181,128
474,712
535,184
713,120
1273,635
267,385
768,849
848,592
94,718
1101,767
1054,21
156,819
827,518
1258,560
243,770
1043,497
1278,9
234,849
962,262
602,150
720,35
1054,522
758,38
1287,341
1207,733
1114,849
254,572
1114,493
836,712
951,751
410,64
1282,255
818,414
776,759
813,392
1198,824
150,158
850,757
489,334
1272,415
23,344
1043,833
110,840
104,656
750,512
766,730
1116,137
594,610
358,534
474,182
408,276
282,870
172,749
619,799
418,19
653,418
795,792
1034,119
1191,786
1255,228
562,220
410,688
139,214
806,630
683,446
57,827
508,147
1193,266
766,520
147,78
186,835
441,826
756,638
80,499
1141,483
1089,443
932,597
1044,569
1185,28
937,108
915,687
165,334
954,863
468,835
246,499
671,121
89,264
745,30
542,824
311,352
395,207
878,159
981,252
104,387
623,303
333,859
972,469
1225,155
364,287
1216,830
668,264
560,718
902,276
766,390
528,551
445,875
606,54
1242,171
1305,668
268,507
1118,144
1076,493
112,198
773,394
403,259
1203,228
1148,78
276,735
22,378
1029,694
1124,835
162,206
169,483
467,120
900,830
668,64
49,367
1037,308
760,59
736,742
820,480
411,770
663,752
500,425
500,469
104,159
756,582
103,147
68,270
873,698
1146,318
1310,240
105,764
642,64
102,38
1148,592
890,21
497,194
145,308
266,171
873,411
258,714
870,719
500,882
1253,120
261,81
744,651
1258,520
542,70
1298,885
1115,518
813,75
852,163
587,15
460,399
552,653
89,712
806,158
88,651
381,420
726,47
547,676
925,67
1205,92
716,610
281,786
537,319
1268,247
1208,856
497,726
460,847
181,271
981,194
1171,571
592,200
154,133
594,505
687,303
1200,358
1255,666
159,772
535,108
453,476
1170,112
542,268
281,694
705,815
117,866
746,203
1253,67
1191,75
119,75
328,411
850,46
1088,607
44,646
682,761
1231,726
1200,838
445,114
176,158
335,682
129,290
1265,194
639,318
1059,597
1116,54
45,259
701,252
644,873
1037,831
832,736
738,393
131,70
1205,316
239,259
828,761
529,259
1086,241
1179,824
1011,245
1295,325
791,425
282,24
792,313
879,285
441,684
1150,219
140,283
49,115
256,873
504,264
668,824
552,508
858,260
1042,287
1146,32
594,465
431,285
1266,646
278,166
1280,336
447,226
704,840
438,806
1298,233
687,591
1114,737
294,885
1179,70
776,135
459,518
1144,219
666,873
1014,617
60,389
602,179
1160,437
1153,115
915,207
100,190
1148,480
920,263
199,655
10,245
485,848
1071,635
290,271
1054,746
387,397
177,682
586,270
658,336
1163,654
282,758
1094,515
94,830
623,852
38,522
549,92
564,327
744,243
1200,851
495,800
1240,668
775,786
1298,70
1200,374
835,121
186,465
415,892
851,518
564,203
1056,663
709,873
837,623
246,395
756,424
398,204
587,187
1266,310
628,133
924,334
455,655
398,873
320,222
1170,334
83,124
739,124
959,540
1220,679
390,631
544,88
290,607
102,632
820,576
278,280
1310,187
80,395
105,443
70,668
1044,395
251,382
68,848
267,61
1111,332
127,556
1258,179
1044,505
738,282
82,612
934,614
1054,204
72,348
254,663
1043,397
85,291
877,155
417,91
460,137
565,579
564,567
1041,550
565,30
1000,859
1056,791
688,233
595,336
1012,479
140,334
256,148
1220,651
542,178
209,767
534,759
1287,547
1213,681
1076,849
508,747
273,831
276,149
256,298
194,54
872,88
441,656
214,175
266,569
865,875
12,630
1000,523
1242,494
873,196
157,35
420,469
445,780
551,733
760,835
1118,750
551,161
709,831
448,311
1275,319
1057,192
136,749
628,761
365,332
1153,75
846,117
546,133
1288,378
900,512
223,609
350,159
1287,344
1103,668
125,418
440,719
832,826
36,586
776,57
1171,214
480,456
668,120
646,12
947,95
378,159
31,852
1170,387
535,786
572,612
619,583
1086,101
432,159
560,619
2,320
1143,10
53,847
256,220
894,805
152,186
136,817
32,233
1101,786
579,288
117,351
1043,845
731,707
666,18
1208,408
32,885
268,283
929,698
326,570
590,35
104,607
654,436
139,571
619,655
403,417
1076,401
1220,243
990,672
1308,574
1109,707
1032,614
499,152
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6

View file

@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void printPaper( unsigned int width, unsigned int height, bool paper[width][height] ){
for ( unsigned int y = 0; y < height; y++ ){
printf("Line %i: ", y);
for ( unsigned int x = 0; x < width; x++ ){
if ( paper[x][y] == true ) printf("#");
else printf(".");
}
printf("\n");
}
}
void foldPaperX( unsigned int width, unsigned int height, unsigned int col, bool paper[width][height], bool newPaper[col][height]){
for ( unsigned int x = 1; x < width - col; x++ )
for ( unsigned int y=0; y < height; y++ )
newPaper[col - x][y] = paper[col - x][y] || paper[col + x][y];
}
void foldPaperY( unsigned int width, unsigned int height, unsigned int row, bool paper[width][height], bool newPaper[width][row]){
for ( unsigned int y=1; y < height - row; y++ )
for ( unsigned int x = 0; x < width; x++ )
newPaper[x][row - y] = ( paper[x][row - y] || paper[x][row + y] );
}
unsigned int countDots( unsigned int width, unsigned int height, bool paper[width][height] ){
unsigned int count=0;
for ( unsigned int y = 0; y < height; y++ )
for ( unsigned int x = 0; x < width; x++ )
if ( paper[x][y] == true ) count++;
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int x=0,y=0,maxx=0,maxy=0;
while ( fscanf( fp, "%i,%i\n",&x,&y )){
maxx = MAX(maxx,x);
maxy = MAX(maxy,y);
}
bool paper[maxx+1][maxy+1];
memset(paper,0,sizeof(bool) * (maxx+1) * (maxy+1) );
rewind(fp);
while ( fscanf( fp, "%i,%i\n",&x,&y )){
paper[x][y] = true;
}
printPaper( maxx+1, maxy+1, paper );
printf("\n");
char direction = '\0';
unsigned int position = 0, dots = 0;
fscanf(fp, "fold along %c=%i\n", &direction, &position);
if ( direction == 'x' ){
bool newPaper[position][maxy+1];
foldPaperX(maxx+1,maxy+1,position, paper, newPaper);
printPaper( position, maxy+1, newPaper );
dots=countDots( position, maxy+1, newPaper );
} else {
bool newPaper[maxx+1][position];
foldPaperY(maxx+1,maxy+1,position, paper, newPaper);
printPaper( maxx+1, position, newPaper );
dots=countDots( maxx+1, position, newPaper );
}
printf("\nThere are %i dots\n", dots);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

102
2021/day13/solution-part2.c Normal file
View file

@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void printPaper( unsigned int width, unsigned int height, bool **paper ){
for ( unsigned int y = 0; y < height; y++ ){
printf("Line %i: ", y);
for ( unsigned int x = 0; x < width; x++ ){
if ( paper[x][y] == true ) printf("#");
else printf(".");
}
printf("\n");
}
}
bool** foldPaperX( unsigned int width, unsigned int height, unsigned int col, bool **paper){
bool **newPaper=(bool**)malloc( col * sizeof(bool *));
for(unsigned int x=0; x < col;x++) {
newPaper[x]=(bool*)malloc( (height) *sizeof(bool));
memset(newPaper[x], 0, sizeof(bool) * ( height ));
}
for ( unsigned int x = 1; x < width - col; x++ )
for ( unsigned int y=0; y < height; y++ )
newPaper[col - x][y] = paper[col - x][y] || paper[col + x][y];
return newPaper;
}
bool** foldPaperY( unsigned int width, unsigned int height, unsigned int row, bool **paper){
bool **newPaper=(bool**)malloc( (width) * sizeof(bool *));
for(unsigned int x=0; x < width; x++) {
newPaper[x]=(bool*)malloc( (row) *sizeof(bool));
memset(newPaper[x], 0, sizeof(bool) * ( row ));
}
for ( unsigned int y=1; y < height - row; y++ )
for ( unsigned int x = 0; x < width; x++ )
newPaper[x][row - y] = ( paper[x][row - y] || paper[x][row + y] );
return newPaper;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int x=0,y=0,maxx=0,maxy=0;
while ( fscanf( fp, "%i,%i\n",&x,&y )){
maxx = MAX(maxx,x);
maxy = MAX(maxy,y);
}
printf("Max x: %i Max y: %i\n", maxx, maxy);
bool **paper=(bool**)malloc( (maxx+1) * sizeof(bool *));
for(unsigned int i=0; i<=maxx;i++)
{
paper[i]=(bool*)malloc( (maxy+1) *sizeof(bool));
memset(paper[i], 0, sizeof(bool) * ( maxy+1 ));
}
rewind(fp);
while ( fscanf( fp, "%i,%i\n",&x,&y )){
paper[x][y] = true;
}
printPaper( maxx+1, maxy+1, paper );
printf("\n");
char direction = '\0';
unsigned int position = 0;
while (!feof(fp) && fscanf(fp, "fold along %c=%i\n", &direction, &position)){
printf("Trying to fold in %c direction at position %i\n", direction, position);
if ( direction == 'x' ){
paper = foldPaperX(maxx+1, maxy+1, position, paper);
maxx = position - 1;
printPaper( maxx + 1, maxy + 1, paper );
} else {
paper = foldPaperY(maxx+1, maxy+1, position, paper);
maxy = position - 1;
printPaper( maxx+1, maxy+1, paper );
}
}
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day14/Makefile Normal file
View file

@ -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)

18
2021/day14/example.txt Normal file
View file

@ -0,0 +1,18 @@
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C

102
2021/day14/realinput.txt Normal file
View file

@ -0,0 +1,102 @@
CNBPHFBOPCSPKOFNHVKV
CS -> S
FB -> F
VK -> V
HO -> F
SO -> K
FK -> B
VS -> C
PS -> H
HH -> P
KH -> V
PV -> V
CB -> N
BB -> N
HB -> B
HV -> O
NC -> H
NF -> B
HP -> B
HK -> S
SF -> O
ON -> K
VN -> V
SB -> H
SK -> H
VH -> N
KN -> C
CC -> N
BF -> H
SN -> N
KP -> B
FO -> N
KO -> V
BP -> O
OK -> F
HC -> B
NH -> O
SP -> O
OO -> S
VC -> O
PC -> F
VB -> O
FF -> S
BS -> F
KS -> F
OV -> P
NB -> O
CF -> F
SS -> V
KV -> K
FP -> F
KC -> C
PF -> C
OS -> C
PN -> B
OP -> C
FN -> F
OF -> C
NP -> C
CK -> N
BN -> K
BO -> K
OH -> S
BH -> O
SH -> N
CH -> K
PO -> V
CN -> N
BV -> F
FV -> B
VP -> V
FS -> O
NV -> P
PH -> C
HN -> P
VV -> C
NK -> K
CO -> N
NS -> P
VO -> P
CP -> V
OC -> S
PK -> V
NN -> F
SC -> P
BK -> F
BC -> P
FH -> B
OB -> O
FC -> N
PB -> N
VF -> N
PP -> S
HS -> O
HF -> N
KK -> C
KB -> N
SV -> N
KF -> K
CV -> N
NO -> P

163
2021/day14/solution-part1.c Normal file
View file

@ -0,0 +1,163 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef struct Element Element;
struct Element{
char letter;
Element *next;
};
typedef struct Interaction Interaction;
struct Interaction{
char letter1;
char letter2;
char result;
Interaction *next;
};
Element* newElement(char letter){
Element *new = malloc(sizeof(Element));
new->letter = letter;
new->next = NULL;
return new;
}
Interaction* newInteraction(char letter1, char letter2, char result){
Interaction *new = malloc(sizeof(Interaction));
new->letter1 = letter1;
new->letter2 = letter2;
new->result = result;
new->next = NULL;
return new;
}
void printElements(Element *current){
while ( current != NULL ){
printf("%c ",current->letter);
current = current->next;
}
printf("\n");
}
void printInteractions(Interaction *current){
while ( current != NULL ){
printf("Merging %c and %c will give you %c\n",current->letter1, current->letter2, current->result);
current = current->next;
}
printf("\n");
}
unsigned int countElements(Element *current){
unsigned int count = 0;
while ( current != NULL ){
count++;
current = current->next;
}
return count;
}
Element* interact(char firstCh, char secondCh, Interaction *interaction){
while ( interaction != NULL ){
if ( interaction->letter1 == firstCh && interaction->letter2 == secondCh )
return newElement( interaction->result );
interaction = interaction->next;
}
return NULL;
}
unsigned int calculateScore(Element *current){
unsigned int min = ~0;
unsigned int max = 0;
unsigned int counts[26] = {0};
while ( current != NULL ){
counts[current->letter - 'A']++;
current = current->next;
}
for(unsigned int i = 0; i < 26; i++){
max = MAX( max, counts[i] );
if ( counts[i] != 0 ) min = MIN( min, counts[i] );
printf("%c: %i\n", i+'A' , counts[i]);
}
printf("Max: %i, Min: %i, Difference: %i\n", max, min, max-min);
return 0;
}
void step(Element *element, Interaction *interaction){
while ( element != NULL && element->next != NULL ){
Element *toInsert = interact(element->letter, element->next->letter, interaction);
toInsert->next = element->next;
element->next = toInsert;
element = toInsert->next;
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
char ch;
Element *firstEl = NULL;
Element *previousEl = NULL;
while ( fscanf( fp, "%c",&ch )){
if ( ch == '\n' ) break;
printf("%c\n",ch);
if ( firstEl == NULL ){
firstEl = newElement(ch);
previousEl = firstEl;
} else {
previousEl->next = newElement(ch);
previousEl = previousEl->next;
}
}
//Skip over empty lines
while( !feof(fp) && fscanf(fp, "\n\n") ) continue;
char firstCh, secondCh, resultCh;
Interaction *firstInt = NULL;
Interaction *previousInt = NULL;
while( !feof(fp) && fscanf(fp, "%c%c -> %c\n", &firstCh, &secondCh, &resultCh) ){
if ( firstInt == NULL ){
firstInt = newInteraction(firstCh, secondCh, resultCh);
previousInt = firstInt;
} else {
previousInt->next = newInteraction(firstCh, secondCh, resultCh);
previousInt = previousInt->next;
}
}
printElements(firstEl);
for ( unsigned int i = 0; i < 10; i++ ){
printf("\nStep %i:\n", i+1);
step(firstEl, firstInt);
printf("Length %i\n", countElements(firstEl));
}
calculateScore(firstEl);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

159
2021/day14/solution-part2.c Normal file
View file

@ -0,0 +1,159 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define pairId(a,b) ( (26 * ( a - 'A' )) + ( b - 'A' ) )
typedef struct Interaction Interaction;
struct Interaction{
char letter1;
char letter2;
char result;
Interaction *next;
};
Interaction* newInteraction(char letter1, char letter2, char result){
Interaction *new = malloc(sizeof(Interaction));
new->letter1 = letter1;
new->letter2 = letter2;
new->result = result;
new->next = NULL;
return new;
}
void printElementPairs(unsigned long *pairs){
for ( unsigned int i = 'A'; i <= 'Z'; i++ )
for ( unsigned int j = 'A'; j <= 'Z'; j++ ){
if ( pairs[pairId(i,j)] > 0 ) printf("%c%c: %lu\n", i, j, pairs[pairId(i,j)]);
}
}
void printElements(unsigned long *letters){
for ( unsigned int i = 'A'; i <= 'Z'; i++ )
if ( letters[i-'A'] > 0 ) printf("%c: %lu\n", i, letters[i-'A']);
}
void printInteractions(Interaction *current){
while ( current != NULL ){
printf("Merging %c and %c will give you %c\n",current->letter1, current->letter2, current->result);
current = current->next;
}
printf("\n");
}
char interact(char firstCh, char secondCh, Interaction *interaction){
while ( interaction != NULL ){
if ( interaction->letter1 == firstCh && interaction->letter2 == secondCh )
return interaction->result;
interaction = interaction->next;
}
return 0;
}
unsigned long calculateScore(unsigned long *letters){
unsigned long min = ~0;
unsigned long max = 0;
for ( unsigned int i = 0; i < 26; i++ ){
unsigned long count = letters[i];
if ( count > 0 ){
min = MIN( min, count );
max = MAX( max, count );
}
}
printf("Max: %lu, Min: %lu, Difference: %lu\n", max, min, max-min);
return 0;
}
unsigned long* step(unsigned long *oldPairsCount, unsigned long *letterCount, Interaction *interaction){
unsigned long *newPairsCount = malloc( sizeof(long) * ( 1 + pairId('Z','Z') ) );
memset(newPairsCount, 0, sizeof(long) * ( 1 + pairId('Z','Z') ) );
for ( unsigned int i = 'A'; i <= 'Z'; i++ )
for ( unsigned int j = 'A'; j <= 'Z'; j++ ){
unsigned long count = oldPairsCount[pairId(i,j)];
if ( count > 0 ){
char newChar = interact(i,j,interaction);
newPairsCount[pairId(i,newChar)] += count;
newPairsCount[pairId(newChar,j)] += count;
letterCount[newChar - 'A'] += count;
}
}
free(oldPairsCount);
return newPairsCount;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
char line[80];
unsigned long *pairsCount = malloc( sizeof(long) * ( 1 + pairId('Z','Z') ) );
memset(pairsCount, 0, sizeof(long) * ( 1 + pairId('Z','Z') ) );
unsigned long *letterCount = malloc( sizeof(long) * 26 );
memset(letterCount, 0, sizeof(long) * 26 );
//first line
fscanf( fp, "%s", line );
for ( unsigned int i = 0; i < strlen(line); i++ ){
if ( line[i+1] != '\0' ){
printf("Pair: %c %c\n", line[i], line[i+1]);
pairsCount[pairId(line[i], line[i+1])]++;
}
letterCount[line[i] - 'A']++;
}
//Skip over empty lines
while( !feof(fp) && fscanf(fp, "\n\n") ) continue;
char firstCh, secondCh, resultCh;
Interaction *firstInt = NULL;
Interaction *previousInt = NULL;
while( !feof(fp) && fscanf(fp, "%c%c -> %c\n", &firstCh, &secondCh, &resultCh) ){
if ( firstInt == NULL ){
firstInt = newInteraction(firstCh, secondCh, resultCh);
previousInt = firstInt;
} else {
previousInt->next = newInteraction(firstCh, secondCh, resultCh);
previousInt = previousInt->next;
}
}
//printInteractions(firstInt);
printElementPairs(pairsCount);
printf("\n\n");
for ( unsigned int i = 0; i < 40; i++ ){
printf("\nStep %i:\n", i+1);
pairsCount = step(pairsCount,letterCount,firstInt);
printElementPairs(pairsCount);
printElements(letterCount);
}
calculateScore(letterCount);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day15/Makefile Normal file
View file

@ -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)

10
2021/day15/example.txt Normal file
View file

@ -0,0 +1,10 @@
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581

100
2021/day15/realinput.txt Normal file
View file

@ -0,0 +1,100 @@
4644191171337732143712186124233573969322319997149343221542323211321212169142231235619969739483792471
2221293392911371411593639962951141752813313921357129613938931533621918583683134152965492646642538116
2795791297555313411382761825567331471819212315341825911232943151936415218119321513121916912261697597
1214537613992125712382233916937264885938549131332876391646112221213144242126287714516436119191917362
1412811515321221111439182464221331329974283623974241113548427128422112192118983384463232121843121141
3245255675412351991191259591561111991221265214221291175137165299373379821323142951719393624496355173
7821636439137114454815234184431131151885565811162122141111291739141236111319913333811111656913241523
1418191421242372241523343828141122624329116111754397113548982293191411111144261229944124833111847376
1415223811928588114168213319591148928517427169381184991691112118229218263638951854741122722159931213
1185112211216296846141231621592158511243111111346117648971911331811279689234291837116151264761299521
2157175132842918592376386235452686111324627214122733298544113111534712212591393262313832165437912113
1116112493319299432145649149992697711725421154424224312213396977142985512232427326338299642149836196
6549229499212395113283291496151231611431251843344129142294319947252811251219755391642553173113821296
4113393621316222283852914519968238256112711138455548538191592324236277743141198979959471862712168221
1741128134354414241419176117119212641541117881955126161946213118193312324857619874348283423325548598
3751141123712163925113282172764932353534116546231281293118591422522113149152261498874515989313554314
4514551971341761251629465312511329291978512148461991612957411327621224192111126296379215871233811481
3293217144136116298991343111524616863893234462111723531219141172273261192421911186619159615299151135
7774221151285219996117174373446146141833811666627831912921211686311331622326641123291596353856612959
1351122214751889195351764143332617127231529144667721221129811142151686112447161472232912114911498432
7225121617125257598871963211815844737221578124248727132498851251391513323133165922818784851344161916
1112187345141297179198141512813725433119213925696911577227241222816471151855194112221672612468122119
1121991355992132552111213132191442529433379222141737721118515814411171128371158391185211772681121151
3611317119849285611931216381814841117111553118123151123429121933733781431918246438319192543378241244
2216973154335197135211535821482816136121216777138722357726122631253431191217262219637112927427564151
5191998411823118893385217392312149124881216981988142455391121911741599925291712131939112984622594162
9512532169823462117945393432541745813525811441272984121112552932917817923383181475411441151349226821
3411874821911569271324123211326163841991128122341482721111115399651299314699266658849911711231323912
7694685861132348951471861817731787685615352753172139744392721512255361251492221911127122613845334296
1113697413329171211439739511931411924968888839312278319759419239814972811121854533399269742318264862
3261667141191721233392311941113633191795822122212119221174142635691755551245192319949176814814316991
2128111325114212336337636612529217112293426976253814989893331386522713418281571915911922457712133131
8388911376618344994992583184752685539238543959953246364821659122425889299729352751111921721511272199
2122155921932542114611151421752127825727141198197413114255419142717265333392684821477226612894862559
1951121655921881375819253119554413525181757211271911799915211551299921612352952113953924955851737855
8195214285611188396611335793711117151312316188149873422613973825989185924514991359346339783843843334
5456833919619253912174733996922156217884375932117373542943268995816113232121789731392898427391181116
4777217181611414812166144323146981138677578415811253842294432341423919922422381614691619319263292673
1871985126952132273312531611829113511331968253631512251863889446196571436982662319393312117814312223
4144118646856288731789179912621365532752715112255935546128459221171311137762973879129462196861223341
5331743126787154471212425952845476245991112234222998154272399175132112413123715171136641911131729182
6459321122282315131822523312117117212329381828311268157731316333126392285181951389374121861573611883
1129519121932136168211173395152156158311198521161721877242125629612171323591229332881655592295117452
4341822521595939622189164981116331219261274132118781411796193741169311918311822217877764111699911254
4194536129171715722286327511111124828745972271325116713569374751324429292311753151131516413241834449
2654142945631129956123367481172912142744199122191626922239395112592217148883514168932621184272178615
4157713294921232144519182476441189526311311114777342965936356951743225131121191824416924178512318511
6912411821531292292181267613662222516132574199134177394148798111514136319121566731314235317164439911
1437321155329217229142678137571111359263414123211224312882335825119927798139832929124119648129273158
1732391818138229589491914619358266961163398213919395213291514999415122435132113734813288381419819249
4113346921421917866111857654393412952122411689932611554121571489921198684193331141319469559362897418
3936181514241719511931294342174121311321234132821683441143221627121222912219121123417289754991212193
5231211979311961116116212251613682828181291627731949883315367221227411122625338116897436595199253162
2316316823133527186851941188913312213143341419114417621723676119643866413111512137248492799272283874
3119192116842719272523136623911313937534137524214481191735181477319325633146114939176412112636323417
2492714192521612141248169319193626151911919321234131296181642619511212191466114432116195132659299995
8479281136811514162331236428357598113492342253565417152872842112725149934866538744331141447117166111
8119623422439216225228196551954272643443171919589198617821835123212386869188415911424183945151334241
2441131613411116653297822116736662114797231228699315262527411234919279976921765231169112216112919848
3831541198992925558534136856443141276992238182251969817995131132423277612211112197312711991832328931
4857223123934311294131922752993114976915557976818181275728832322272499252323121313288171744324765443
6288391134124416473924343716745649381453218212143281127971317811511748813199842175114288225452114924
2198193117421166796123194192539312141215519112972453238891813111365441112114115347214113916577619519
4829218134763351139297321322168924116121484533211291255719399319238352153167888261311881156385931422
1128131518795612511811698671411951513233914459321461852952978173818789621743591166392521464181816178
1238497511773695513118432223193651911824333381942211111917892631655191328368558712842618374164192141
5197814252643194532182168116535512142118911412974122512425237539834313411221165712489961923581131921
5911812939579518465461451173198646171335111185511687173891229811734589488111952535421341871929712588
1671651526181764772599923144212312319733142231365158737371635371423522142829181192112264525227179493
7421112826829293192119831269314941212923232123538851869151292117144743917691154582417433994491521943
9191346716972561945245244324428371279373282126116111451618129133112293262414113689369614111711445421
2316166312199999129455391528271939493421961982164811491114318795721435615291362213311318321186121151
9931241122891924292311432346646113846761512131981437311511562199358147246719196723818191337753291332
1222114361952914555221222296169622784558836751121518966219244215831746229685414172141737963133114493
5218313115227314139655246183769219129243229111742851151185617522413973822128342227812919165581163524
9291132423113289278354324575153169229992171127124135461837121234526253159331631693469211222922141974
8452363325897641925169932431428139199613218111915136812112119111995795222511113131372723121212711118
2197227213274819481124191141229711865192112947469114147211523239645218213151193132942921122111792371
9275351123144952331922211614314212365538513531132558123126944517881183166133275219943389521111173193
1676123311913129662665831191951759436541912564748652528469419649571161712679147469912553225741913996
3191353128953131423711131716711282864474834951441111829112836519349724223297611712534725619191814221
4173761111187311916392959649155953281454675178259425336991997957211941375122951384157137917823346849
6231866821187632943121892433531142499383519821114756411214331534451221651412644522124791217848229112
4218513612717224329629361592483513734819131413112231512426831431594336223618413472242122315818213821
1672513521112672575721281934545322131861691451188212512246411624946313848511278356324319145934746199
7277435215413485681311177121429997231927125112162783124381311646223581365917794813269411132811948118
1171256993195581123322244392112531112921826117456633951228891118213977135113912128736772927396511155
7513423614827252911431224428129419471913857316347921811289615226221512981231328492565212872149824422
4172392667172923414112281271468455913321542234539111143311488844993986926526232111912899467919673129
2266423275271832192191927133161851111718244711617612592821169634215492721212529519332171925375214519
2961251383676393331912784833198316856114611437958511134729539811313332428111416135419161136827381511
2191313432414523594719493126599684313513119243281212311968932355513797175111911888954498999447129422
4116222439791495331225415853138639142821696183279788473226912132161296116634441352242287514831641639
2456122986411166492928291613291461786611191281413582887271831192125366139197116411714187313721143319
3137321286181773323297599141911589613214333724715721771326962716833251289731121417114311929432923141
4471591643429412532212412921891262226734323514259112982727616315933911299142522382749595219893774213
8361434437271446111531163739832741723991191369215895121472249713859411711382822931149329516384858813
7162151376995138977914242232912263921431263824116411237115179847816871745736216484215173821453691814
1221412352976445211311141195476196991326389881615412212286967548955857131279126264116711124668334891
6982434645865366398413542122219772368658463742631121963313814929811193197282524243293233246445611618

159
2021/day15/solution-part1.c Normal file
View file

@ -0,0 +1,159 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef struct Square Square;
struct Square{
unsigned int risk;
Square *previous;
unsigned int distanceFromStart;
Square *up;
Square *down;
Square *left;
Square *right;
bool onShortestPath;
bool used;
};
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
void printGrid( unsigned int cols, unsigned int rows, Square **grid){
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( grid[col][row].onShortestPath ) printf(ANSI_COLOR_RED);
printf("%i ", grid[col][row].risk);
if ( grid[col][row].onShortestPath ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void maybeUpdate(Square *current, Square *adjacent){
if ( adjacent == NULL ) return ;
if ( current->distanceFromStart + adjacent->risk < adjacent->distanceFromStart ){
adjacent->distanceFromStart = current->distanceFromStart + adjacent->risk;
adjacent->previous = current;
}
}
void markAdjacent(Square *current){
unsigned int currentDistance = current->distanceFromStart;
maybeUpdate(current, current->up);
maybeUpdate(current, current->down);
maybeUpdate(current, current->left);
maybeUpdate(current, current->right);
}
Square* getLowestEndScoredSquare( Square**grid, unsigned int cols, unsigned int rows ){
Square *lowest = &grid[cols][rows];
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
//printf("%i\n",lowest->distanceFromStart);
if ( (!grid[col][row].used) && (grid[col][row].distanceFromStart < lowest->distanceFromStart) ){
printf("col: %i row: %i\n",col,row);
lowest = &grid[col][row];
}
}
}
return lowest;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int cols=countCols(fp);
rewind(fp);
unsigned int rows=countRows(fp);
rewind(fp);
Square **grid=(Square**)malloc( cols * sizeof(Square *));
for(unsigned int i=0; i<=rows;i++) {
grid[i]=(Square*)malloc( (rows) *sizeof(Square));
memset(grid[i], 0, sizeof(Square) * ( rows ));
}
char ch;
unsigned int row=0, col=0;
while ( !feof(fp) && fscanf(fp, "%c", &ch) ){
if ( ch == '\n' ){
col = 0;
row++;
continue;
}
grid[col][row].risk = atoi(&ch);
grid[col][row].distanceFromStart = ~0;
grid[col][row].up = ( row > 0 ) ? &grid[col][row-1] : NULL;
grid[col][row].down = ( row < rows - 1 ) ? &grid[col][row+1] : NULL;
grid[col][row].left = ( col > 0 ) ? &grid[col-1][row] : NULL;
grid[col][row].right = ( col < cols - 1 ) ? &grid[col+1][row] : NULL;
grid[col][row].onShortestPath = false;
grid[col][row].used = false;
col++;
}
Square *current = &grid[0][0];
unsigned int i = 0;
while (true){
markAdjacent(current);
current->used = true;
if (current == &grid[cols-1][rows-1] ) break;
current = getLowestEndScoredSquare(grid, cols, rows);
i++;
printf("---\n");
}
unsigned int total = 0;
while ( true ){
current->onShortestPath = true;
total += current->risk;
if ( current != &grid[0][0] )
current = current->previous;
else
break;
}
printGrid(cols, rows, grid);
//grid[0][0].distanceFromStart = 0;
printf("Total risk: %i\n", total - grid[0][0].risk);
//markAdjacent(grid, 0, 0);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

233
2021/day15/solution-part2.c Normal file
View file

@ -0,0 +1,233 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_BOLD "\x1b[1m"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
typedef struct Square Square;
typedef struct SquareList SquareList;
struct Square{
unsigned int risk;
Square *previous;
unsigned int distanceFromStart;
Square *up;
Square *down;
Square *left;
Square *right;
bool onShortestPath;
bool used;
};
struct SquareList{
Square *square;
SquareList *next;
};
SquareList* newSqareListItem(Square *square){
SquareList *newSquare = malloc(sizeof(SquareList));
newSquare->square = square;
newSquare->next = NULL;
return newSquare;
}
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
void printGrid( unsigned int cols, unsigned int rows, Square **grid){
for ( unsigned int row = 0; row < rows; row++ ){
if ( row % ( rows / 5 ) == 0 ) printf("\n");
for ( unsigned int col = 0; col < cols; col++ ){
if ( col % ( cols / 5 ) == 0 ) printf(" ");
if ( grid[col][row].onShortestPath ) printf(ANSI_COLOR_RED);
if ( col < cols / 5 && row < rows/5 ) printf(ANSI_COLOR_BOLD);
printf("%i ", grid[col][row].risk);
if ( grid[col][row].onShortestPath ) printf(ANSI_COLOR_RESET);
if ( col < cols / 5 && row < rows/5 ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void maybeUpdate(Square *current, Square *adjacent, SquareList *lastEndItem){
if ( adjacent == NULL ) return ;
if ( current->distanceFromStart + adjacent->risk < adjacent->distanceFromStart ){
adjacent->distanceFromStart = current->distanceFromStart + adjacent->risk;
adjacent->previous = current;
lastEndItem->next = newSqareListItem(adjacent);
}
}
void markAdjacent(Square *current, SquareList *firstEndItem){
SquareList *lastItem = firstEndItem;
while ( lastItem->next != NULL ) lastItem = lastItem->next;
unsigned int currentDistance = current->distanceFromStart;
maybeUpdate(current, current->up, lastItem);
if ( lastItem->next != NULL ) lastItem = lastItem->next;
maybeUpdate(current, current->down, lastItem);
if ( lastItem->next != NULL ) lastItem = lastItem->next;
maybeUpdate(current, current->left, lastItem);
if ( lastItem->next != NULL ) lastItem = lastItem->next;
maybeUpdate(current, current->right, lastItem);
}
Square* getLowestEndScoredSquare( SquareList *current ){
Square *lowest = current->square;
while ( current != NULL ){
if ( ( current->square->distanceFromStart < lowest->distanceFromStart ) ){
lowest = current->square;
}
current = current -> next;
}
return lowest;
}
SquareList* pruneEndList( SquareList *first ){
// First item is a sliphtly special case
while ( first != NULL && first->square->used ){
first = first->next;
}
if ( first == NULL ) return NULL;
SquareList *previous = first;
SquareList *current = first->next;
while ( current != NULL ){
while ( current->square->used ){
previous->next = current->next;
current = previous->next;
}
previous = current;
current = current->next;
}
return first;
}
unsigned int endListLength( SquareList *current ){
unsigned int count = 0;
while ( current != NULL ){
count++;
current = current->next;
}
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int cols=countCols(fp);
rewind(fp);
unsigned int rows=countRows(fp);
rewind(fp);
Square **grid=(Square**)malloc( cols * 5 * sizeof(Square *));
for(unsigned int i=0; i<=rows*5;i++) {
grid[i]=(Square*)malloc( rows * 5 * sizeof(Square));
memset(grid[i], 0, sizeof(Square) * rows * 5);
}
char ch;
unsigned int row=0, col=0;
while ( !feof(fp) && fscanf(fp, "%c", &ch) ){
if ( ch == '\n' ){
col = 0;
row++;
continue;
}
for (unsigned int j=0; j < 5; j++){
for (unsigned int i=0; i < 5 ; i++){
unsigned int realcol = col + (i*cols), realrow = row + (j*rows);
grid[realcol][realrow].risk = (( ch - '0') + i + j ) % 9;
if ( grid[realcol][realrow].risk == 0 ) grid[realcol][realrow].risk = 9;
grid[realcol][realrow].distanceFromStart = ~0;
grid[realcol][realrow].up = ( realrow > 0 ) ? &grid[realcol][realrow-1] : NULL;
grid[realcol][realrow].down = ( realrow < rows*5 - 1 ) ? &grid[realcol][realrow+1] : NULL;
grid[realcol][realrow].left = ( realcol > 0 ) ? &grid[realcol-1][realrow] : NULL;
grid[realcol][realrow].right = ( realcol < cols*5 - 1 ) ? &grid[realcol+1][realrow] : NULL;
grid[realcol][realrow].onShortestPath = false;
grid[realcol][realrow].used = false;
}
}
col++;
}
//grid[cols*5-1][rows*5-1].onShortestPath = true;
//printGrid(cols*5, rows*5, grid);
Square *current = &grid[0][0];
SquareList *firstEndItem = newSqareListItem(current);
unsigned int i = 0;
while (true){
markAdjacent(current, firstEndItem);
current->used = true;
if ( current == &grid[cols*5-1][rows*5-1] ) break;
firstEndItem = pruneEndList(firstEndItem);
current = getLowestEndScoredSquare(firstEndItem);
i++;
}
//while ( firstEndItem != NULL ){
// firstEndItem->square->onShortestPath = true;
// printf("Risk: %i Length: %i\n", firstEndItem->square->risk, firstEndItem->square->distanceFromStart);
// firstEndItem = firstEndItem->next;
//}
unsigned long total = 0;
while ( true ){
current->onShortestPath = true;
total += current->risk;
if ( current != &grid[0][0] )
current = current->previous;
else
break;
}
printGrid(cols*5, rows*5, grid);
//printGrid(cols*5, rows*5, grid);
//grid[0][0].distanceFromStart = 0;
printf("Total risk: %lu\n", total - grid[0][0].risk);
//markAdjacent(grid, 0, 0);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day2/Makefile Normal file
View file

@ -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)

6
2021/day2/example.txt Normal file
View file

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

1000
2021/day2/realinput.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int horizontal = 0;
unsigned int depth = 0;
FILE *fp=fopen(argv[1], "r");
while (!feof (fp)){
char direction[7] = "";
int amount = 0;
fscanf(fp, "%s %d\n", direction, &amount);
if ( strcmp(direction,"forward") == 0 )
horizontal+=amount;
else if ( strcmp(direction,"down") == 0 )
depth+=amount;
else if ( strcmp(direction,"up") == 0 )
depth-=amount;
else
printf("Hopefully we don't get here\n");
}
printf("Horizontal: %d\n", horizontal);
printf("Depth: %d\n", depth);
printf("Product: %d\n", horizontal * depth);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

20
2021/day2/solution-part1.sh Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env bash
file="$1"
horizontal=0
depth=0
while read line; do
direction="$(echo "$line" | cut -d ' ' -f 1)"
amount="$(echo "$line" | cut -d ' ' -f 2)"
case "$direction" in
forward) horizontal=$((horizontal + amount)) ;;
down) depth="$((depth + amount))" ;;
up) depth="$((depth - amount))" ;;
esac
done < <(cat "$file")
echo "Horizontal: $horizontal"
echo "Depth: $depth"
echo "Product: $(( depth * horizontal ))"

View file

@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int horizontal = 0;
unsigned int depth = 0;
unsigned int aim = 0;
FILE *fp=fopen(argv[1], "r");
while (!feof (fp)){
char direction[7] = "";
int amount = 0;
fscanf(fp, "%s %d\n", direction, &amount);
if ( strcmp(direction,"forward") == 0 ){
horizontal+=amount;
depth+=(aim*amount);
} else if ( strcmp(direction,"down") == 0 )
aim+=amount;
else if ( strcmp(direction,"up") == 0 )
aim-=amount;
else
printf("Hopefully we don't get here\n");
}
printf("Horizontal: %d\n", horizontal);
printf("Depth: %d\n", depth);
printf("Product: %d\n", horizontal * depth);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

26
2021/day2/solution-part2.sh Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env bash
file="$1"
horizontal=0
depth=0
aim=0
while read line; do
#direction="$(echo "$line" | cut -d ' ' -f 1)"
direction="${line%% *}"
#amount="$(echo "$line" | cut -d ' ' -f 2)"
amount="${line##* }"
case "$direction" in
forward)
horizontal=$((horizontal + amount))
depth="$((depth + (aim * amount) ))"
;;
down) aim="$((aim + amount))" ;;
up) aim="$((aim - amount))" ;;
esac
done < <(cat "$file")
echo "Horizontal: $horizontal"
echo "Depth: $depth"
echo "Product: $(( depth * horizontal ))"

10
2021/day3/Makefile Normal file
View file

@ -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)

12
2021/day3/example.txt Normal file
View file

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

1000
2021/day3/realinput.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,56 @@
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int gamma = 0;
unsigned int epsilon = 0;
unsigned int lineLength = 0;
unsigned int noOfLines = 0;
// Read the values into an array
FILE *fp=fopen(argv[1], "r");
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
rewind(fp);
unsigned int digitCount[lineLength];
memset( &digitCount, 0, lineLength*sizeof(unsigned int) );
for ( int i = 0; !feof(fp); i++){
ch=fgetc(fp);
switch (ch) {
case '\n':
// I will be incremented at the end of the iteration
i=-1;
noOfLines++;
break;
case '1':
digitCount[i]++;
break;
}
}
for (unsigned int i=0; i < lineLength; i++ ){
if ( digitCount[i] > ( (double)noOfLines ) / 2 ){
gamma = gamma | 1 << (lineLength - i - 1);
} else {
epsilon = epsilon | 1 << (lineLength - i - 1);
}
}
printf("Answer: %d", gamma * epsilon);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

43
2021/day3/solution-part1.sh Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env bash
file="$1"
ones=(0 0 0 0 0 0 0 0 0 0 0 0)
lines=0
gamma=""
epsilon=""
while read line; do
bitn=0
while read bit; do
if [ "$bit" -eq 1 ]; then
ones[$bitn]=$((${ones[$bitn]} + 1))
fi
bitn=$(( bitn + 1 ))
done < <(echo "$line" | grep -o .)
lines="$((lines + 1))"
done < <(cat "$file")
echo "${ones[@]}"
for i in ${ones[@]}; do
if [ "$i" -gt "$(( lines / 2 ))" ]; then
gamma="${gamma}1"
epsilon="${epsilon}0"
else
gamma="${gamma}0"
epsilon="${epsilon}1"
fi
done
gammaDecimal="$( echo "ibase=2; $gamma" | bc )"
epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )"
echo "Gamma: $gamma -> $gammaDecimal"
echo "Epsilon: $epsilon -> $epsilonDecimal"
echo "Answer: $((gammaDecimal * epsilonDecimal))"

125
2021/day3/solution-part2.c Normal file
View file

@ -0,0 +1,125 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int countLines(char filename[]){
FILE *fp;
fp=fopen(filename, "r");
long int lines =0;
if ( fp == NULL ) {
return -1;
}
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
fclose(fp); // closing file
return lines;
}
void countPositionBits(int linesize, int noOfLines, char *lines[noOfLines], unsigned int digitCount[]){
for ( int i = 0; i < noOfLines; i++){
for ( int j = 0; j < linesize; j++ ){
if ( lines[i][j] == '1' )
digitCount[j]++;
}
}
}
char* filterO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){
if ( noOfLines == 1 )
return lines[0];
unsigned int digitCount[linesize];
memset( &digitCount, 0, linesize*sizeof(unsigned int) );
countPositionBits(linesize, noOfLines, lines, digitCount);
char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '1' : '0';
unsigned int newLinesIndex=0;
//Not sure why +4 here but any less and it buffer overflows
char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*));
for ( unsigned int i=0; i < noOfLines; i++ ){
if (lines[i][iteration] == bitKeep)
newLines[newLinesIndex++] = lines[i];
}
return filterO2List( linesize, newLinesIndex, newLines, ++iteration );
}
char* filterCO2List(unsigned int linesize, unsigned int noOfLines, char **lines, unsigned int iteration){
if ( noOfLines == 1 )
return lines[0];
unsigned int digitCount[linesize];
memset( &digitCount, 0, linesize*sizeof(unsigned int) );
countPositionBits(linesize, noOfLines, lines, digitCount);
char bitKeep = ( digitCount[iteration] >= ((double)noOfLines) / 2 ) ? '0' : '1';
unsigned int newLinesIndex=0;
//Not sure why +4 here but any less and it buffer overflows
char **newLines = (char**)malloc((digitCount[iteration]+4) * sizeof(char*));
for ( unsigned int i=0; i < noOfLines; i++ ){
if (lines[i][iteration] == bitKeep)
newLines[newLinesIndex++] = lines[i];
}
return filterCO2List( linesize, newLinesIndex, newLines, ++iteration );
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
unsigned int gamma = 0;
unsigned int epsilon = 0;
unsigned int lineLength = 0;
unsigned int noOfLines = countLines( argv[1] );
unsigned int iteration=0;
// Read the values into an array
FILE *fp=fopen(argv[1], "r");
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
rewind(fp);
unsigned int digitCount[lineLength];
memset( &digitCount, 0, lineLength*sizeof(int) );
// Make a big array to hold the lines
char **lines = (char**)malloc(noOfLines * sizeof(char*));
// Loop through the file and populate the array
for ( unsigned int i = 0; !feof(fp); i++){
char *line = (char*)malloc(lineLength);
fscanf(fp, "%s\n", line);
lines[i] = line;
}
char *o2Rating = filterO2List( lineLength, noOfLines, lines, 0 );
char *co2Rating = filterCO2List( lineLength, noOfLines, lines, 0 );
int o2RatingDec = (int)strtol(o2Rating, NULL, 2);
int co2RatingDec = (int)strtol(co2Rating, NULL, 2);
printf("O2 Rating: %s -> %i\n", o2Rating, o2RatingDec);
printf("CO2 Rating: %s -> %i\n", co2Rating, co2RatingDec);
printf("Answer: %i\n", o2RatingDec * co2RatingDec);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

108
2021/day3/solution-part2.sh Executable file
View file

@ -0,0 +1,108 @@
#!/usr/bin/env bash
file="$1"
gamma=""
epsilon=""
countPositionBits(){
ones=(0 0 0 0 0 0 0 0 0 0 0 0)
#ones=(0 0 0 0 0)
stdin="$(cat -)"
while read line; do
bitn=0
while read bit; do
if [ "$bit" -eq 1 ]; then
ones[$bitn]=$((${ones[$bitn]} + 1))
fi
bitn=$(( bitn + 1 ))
done < <(echo "$line" | grep -o .)
done < <(echo "$stdin")
printf '%s\n' "${ones[@]}"
}
dots(){
seq $i | sed 's/.*/./' | tr -d '\n'
}
ogr="$(cat "$1")"
csr="$(cat "$1")"
echo "OGR"
i=0
while [ "$(echo "$ogr" | wc -l)" -gt 1 ]; do
counts="$(echo "$ogr" | countPositionBits)"
noofogr="$(echo "$ogr" | wc -l)"
noofones="$( echo "$counts" | sed -n "$((i + 1))p" )"
echo "$ogr"
echo "No of ones $noofones"
echo "No of ogr $noofogr"
if [ "$((noofones * 10))" -ge "$(( noofogr * 10 / 2 ))" ]; then
echo "Filtering only 1s in position $i"
echo "^$(dots $i)1"
ogr="$(echo "$ogr" | grep "^$(dots $i)1" )"
else
echo "Filtering only 0s in position $i"
ogr="$(echo "$ogr" | grep "^$(dots $i)0" )"
fi
echo -e "\n---\n"
i="$((i + 1))"
done
echo "CSR"
i=0
while [ "$(echo "$csr" | wc -l)" -gt 1 ]; do
counts="$(echo "$csr" | countPositionBits)"
noofcsr="$(echo "$csr" | wc -l)"
noofones="$( echo "$counts" | sed -n "$((i + 1))p" )"
echo "$csr"
echo "No of ones $noofones"
echo "No of csr $noofcsr"
if [ "$((noofones * 10))" -ge "$(( noofcsr * 10 / 2 ))" ]; then
echo "Filtering only 0s in position $i"
echo "^$(dots $i)1"
csr="$(echo "$csr" | grep "^$(dots $i)0" )"
else
echo "Filtering only 1s in position $i"
csr="$(echo "$csr" | grep "^$(dots $i)1" )"
fi
echo -e "\n---\n"
i="$((i + 1))"
done
ogrDecimal="$( echo "ibase=2; $ogr" | bc )"
csrDecimal="$( echo "ibase=2; $csr" | bc )"
echo "Oxxygen Generator Rating: $ogr -> $ogrDecimal"
echo "CO2 Scrubber Rating: $csr -> $csrDecimal"
echo "Answer: $((ogrDecimal * csrDecimal))"
#while read i; do
# if [ "$i" -gt "$(( lines / 2 ))" ]; then
# gamma="${gamma}1"
# epsilon="${epsilon}0"
# else
# gamma="${gamma}0"
# epsilon="${epsilon}1"
# fi
#done < <(echo "")
#
#gammaDecimal="$( echo "ibase=2; $gamma" | bc )"
#epsilonDecimal="$( echo "ibase=2; $epsilon" | bc )"
#
#echo "Gamma: $gamma -> $gammaDecimal"
#echo "Epsilon: $epsilon -> $epsilonDecimal"
#
#echo "Answer: $((gammaDecimal * epsilonDecimal))"

10
2021/day4/Makefile Normal file
View file

@ -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)

19
2021/day4/example.txt Normal file
View file

@ -0,0 +1,19 @@
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7

View file

@ -0,0 +1,60 @@
--- Day 4: Giant Squid ---
You're already almost 1.5km (almost a mile) below the surface of the ocean, already so deep that you can't see any sunlight. What you can see, however, is a giant squid that has attached itself to the outside of your submarine.
Maybe it wants to play bingo?
Bingo is played on a set of boards each consisting of a 5x5 grid of numbers. Numbers are chosen at random, and the chosen number is marked on all boards on which it appears. (Numbers may not appear on all boards.) If all numbers in any row or any column of a board are marked, that board wins. (Diagonals don't count.)
The submarine has a bingo subsystem to help passengers (currently, you and the giant squid) pass the time. It automatically generates a random order in which to draw numbers and a random set of boards (your puzzle input). For example:
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
After the first five numbers are drawn (7, 4, 9, 5, and 11), there are no winners, but the boards are marked as follows (shown here adjacent to each other to save space):
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
After the next six numbers are drawn (17, 23, 2, 0, 14, and 21), there are still no winners:
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
Finally, 24 is drawn:
22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
At this point, the third board wins because it has at least one complete row or column of marked numbers (in this case, the entire top row is marked: 14 21 17 24 4).
The score of the winning board can now be calculated. Start by finding the sum of all unmarked numbers on that board; in this case, the sum is 188. Then, multiply that sum by the number that was just called when the board won, 24, to get the final score, 188 * 24 = 4512.
To guarantee victory against the giant squid, figure out which board will win first. What will your final score be if you choose that board?

601
2021/day4/realinput.txt Normal file
View file

@ -0,0 +1,601 @@
6,69,28,50,36,84,49,13,48,90,1,33,71,0,94,59,53,58,60,96,30,34,29,91,11,41,77,95,17,80,85,93,7,9,74,89,18,25,26,8,87,38,68,5,12,43,27,46,62,73,16,55,22,4,65,76,54,52,83,10,21,67,15,47,45,40,35,66,79,51,75,39,64,24,37,72,3,44,82,32,78,63,57,2,86,31,19,92,14,97,20,56,88,81,70,61,42,99,23,98
22 59 7 10 6
33 36 96 55 23
13 85 18 29 28
75 46 83 73 58
34 40 87 56 98
73 96 47 14 10
28 11 79 84 20
74 30 0 59 71
80 93 42 22 17
44 2 81 29 15
73 32 37 93 39
2 87 38 99 97
15 12 42 49 33
9 23 25 94 19
57 4 18 70 79
43 79 59 71 78
51 97 37 28 26
46 21 84 8 86
96 30 17 34 49
55 90 99 75 61
43 86 35 51 24
16 25 29 21 3
2 13 4 36 54
89 27 8 85 34
96 59 65 14 56
5 57 43 4 1
86 80 67 30 20
22 16 99 0 14
40 25 59 91 54
82 95 96 37 24
97 20 87 66 21
25 40 9 78 89
52 75 95 63 62
32 43 13 47 69
53 48 56 29 4
24 4 65 12 8
76 3 81 99 49
45 33 31 66 34
17 94 75 35 88
64 10 7 93 95
88 20 99 15 83
81 40 5 6 98
49 74 18 27 9
43 69 28 37 71
87 82 90 14 47
21 97 92 42 60
11 65 98 95 29
2 49 75 20 74
56 40 78 66 81
68 4 46 77 61
26 14 39 76 11
41 74 34 91 4
51 9 97 29 75
83 86 23 35 77
90 2 49 30 89
34 97 0 22 15
65 60 82 66 47
30 9 49 95 48
54 73 67 17 78
33 96 44 1 32
90 29 10 49 63
8 26 95 61 0
54 13 51 12 44
32 48 16 47 84
55 46 93 14 3
68 55 46 16 54
97 59 49 6 21
52 32 15 96 70
57 56 61 51 0
40 31 62 24 48
75 56 70 42 14
77 9 23 62 25
29 27 54 17 2
55 72 43 15 96
45 30 88 86 1
65 12 22 6 51
77 81 15 91 19
80 71 27 89 52
75 92 41 83 57
40 44 66 84 98
89 80 73 59 18
74 55 67 71 10
78 27 37 57 3
92 26 17 5 72
52 83 28 76 51
64 0 87 4 13
14 43 42 62 31
17 84 72 91 83
53 80 59 61 74
47 22 38 28 77
65 49 91 94 19
25 81 64 57 9
50 95 82 12 66
53 54 85 36 11
31 56 83 18 60
6 12 76 37 33
14 96 16 31 40
17 82 27 94 83
1 50 49 9 54
29 70 59 91 77
85 0 33 41 16
7 86 80 53 65
94 19 28 35 32
4 55 93 97 63
83 22 67 56 36
28 70 65 17 19
88 51 15 73 12
11 49 80 23 42
26 54 90 75 29
74 77 91 86 66
88 22 83 10 43
57 34 15 23 8
94 42 68 67 37
84 3 6 71 62
89 45 72 55 38
68 27 93 7 15
85 96 1 56 6
32 2 30 55 17
99 90 37 80 50
77 78 58 98 57
5 95 22 13 97
19 2 52 92 20
75 49 86 31 41
51 63 56 33 10
82 27 79 94 57
15 44 91 26 4
41 78 53 95 86
90 92 46 56 33
80 93 68 66 70
36 10 38 0 6
11 47 52 64 17
16 60 39 55 78
46 18 33 83 35
13 92 4 23 50
40 5 29 6 90
20 60 2 23 74
43 39 91 75 12
58 67 50 29 79
10 40 92 95 25
13 24 53 72 36
69 64 15 53 86
61 11 48 71 27
26 93 38 70 73
96 29 30 98 45
13 7 31 59 84
13 55 50 54 57
32 24 45 5 77
82 86 0 4 99
75 72 14 40 56
76 94 89 17 30
66 71 86 41 0
80 64 3 29 78
23 21 42 99 87
96 60 75 61 49
2 83 85 12 1
78 96 4 82 6
73 36 57 99 8
11 83 15 35 30
85 87 25 22 66
32 64 16 50 43
6 39 55 2 4
1 61 99 71 80
46 88 22 31 60
62 66 37 97 76
74 34 10 52 24
42 38 13 65 12
4 39 60 62 21
81 99 1 69 40
59 15 11 95 53
91 78 72 30 73
43 68 82 72 2
34 92 33 97 89
63 70 9 0 45
69 10 12 65 21
40 20 55 57 49
41 22 8 16 49
71 64 19 65 92
17 10 26 36 29
30 88 7 54 11
77 55 70 72 47
15 94 90 6 39
99 55 16 54 91
18 7 82 44 51
25 34 83 14 12
30 2 77 27 66
44 50 74 97 58
87 42 56 92 28
49 82 52 17 78
54 89 63 77 27
83 14 24 16 84
47 32 8 55 22
19 14 24 82 4
76 73 9 10 64
40 6 92 67 17
68 44 43 3 54
26 10 9 92 81
46 98 13 0 14
68 99 35 18 72
74 33 22 61 93
80 38 71 6 75
17 23 54 55 22
10 8 60 76 24
71 28 16 62 82
13 12 21 78 39
26 66 89 64 79
72 12 91 79 99
84 18 37 98 41
10 71 88 23 24
11 34 26 83 74
58 27 77 5 8
9 90 13 0 46
6 58 74 92 8
75 77 56 76 80
55 60 44 68 91
67 28 96 66 18
14 26 54 21 35
69 67 16 76 62
78 45 87 44 94
27 89 39 85 28
3 81 93 64 74
10 28 35 84 76
40 11 95 59 57
53 4 24 50 45
7 43 78 17 81
1 74 82 16 27
6 26 72 53 52
51 91 80 11 18
20 63 74 25 33
79 4 8 59 67
3 13 55 81 83
98 85 27 84 42
90 15 17 61 34
40 64 86 96 45
59 47 53 5 35
11 7 41 80 13
47 48 54 31 76
99 32 98 20 15
61 41 30 94 37
34 59 86 55 45
9 83 92 53 3
3 80 24 94 25
17 23 64 76 71
20 97 0 56 72
95 73 28 59 21
14 81 46 67 88
2 95 5 38 90
63 62 11 24 34
19 31 57 84 80
47 86 36 85 74
13 39 73 94 42
28 6 60 34 15
63 36 51 30 92
43 10 7 88 49
78 76 31 19 66
22 20 35 45 79
19 42 49 57 73
99 50 97 93 43
67 52 40 16 33
2 55 0 71 46
21 75 59 66 83
19 86 30 25 3
32 39 65 54 29
38 6 85 52 13
43 95 18 44 15
53 70 16 31 71
68 73 74 83 70
56 15 12 78 4
43 87 63 90 86
41 16 23 17 77
80 14 61 30 50
88 28 45 80 65
64 11 68 33 27
29 70 44 82 37
42 66 9 32 87
10 24 15 3 46
99 81 5 62 97
4 36 23 38 35
42 16 37 98 54
34 41 25 30 48
8 60 63 89 72
25 33 94 23 14
45 10 79 30 3
22 28 95 27 11
74 13 39 84 83
72 88 56 53 97
31 92 91 84 71
54 90 89 80 0
98 96 65 66 68
35 39 70 11 82
15 34 42 52 2
68 71 86 82 37
28 48 12 34 54
62 55 10 25 89
60 4 50 21 22
3 2 18 40 84
18 96 95 47 45
14 51 2 88 43
94 56 19 15 8
48 65 62 6 75
35 28 25 72 30
43 85 69 92 26
61 75 5 73 66
16 87 4 99 48
18 19 79 23 83
37 88 31 38 40
23 10 89 84 76
45 39 62 55 66
25 73 79 43 60
12 69 36 93 71
9 77 14 58 49
9 24 26 53 79
99 15 30 50 16
14 95 12 25 33
54 40 58 46 66
5 23 39 29 48
64 67 11 10 92
59 99 80 97 66
45 51 88 47 82
35 27 72 85 16
54 58 2 1 52
10 97 27 54 66
89 85 39 99 98
8 68 95 51 19
4 17 79 87 3
72 43 76 58 33
95 72 0 18 17
3 42 24 86 34
52 79 46 58 98
76 77 78 19 10
81 61 88 85 54
74 70 15 80 72
77 89 11 19 22
34 59 56 65 91
58 6 50 40 16
93 30 95 26 85
47 62 57 6 25
40 79 22 95 29
42 11 70 10 92
60 53 84 96 17
75 86 74 89 18
30 42 4 19 92
40 58 72 7 70
17 98 45 76 50
93 57 65 79 2
56 94 73 84 62
51 0 14 7 53
63 36 48 81 84
50 22 11 88 6
83 99 21 31 91
86 17 72 42 94
83 0 20 26 7
42 24 37 86 65
73 82 63 21 27
30 35 9 47 80
79 53 3 14 84
50 99 83 86 42
81 36 24 4 76
0 71 66 41 57
7 54 94 78 97
12 8 82 45 31
4 91 57 50 37
22 46 86 24 26
39 54 48 7 42
2 45 95 29 12
38 25 52 0 72
51 94 46 44 62
95 60 0 48 61
38 13 85 32 8
22 56 53 30 80
43 65 58 68 88
49 76 41 43 51
57 46 45 82 90
48 33 36 5 23
81 30 3 61 75
56 70 29 91 59
58 74 50 47 84
2 1 62 36 60
32 16 95 43 27
79 12 39 56 11
9 33 4 25 61
23 84 16 51 39
72 19 53 64 43
9 44 10 52 26
45 68 29 56 74
62 42 46 95 0
16 83 27 85 56
13 41 49 79 53
18 63 7 60 3
45 15 48 69 29
46 86 35 34 32
85 2 96 15 43
33 30 29 53 98
21 55 61 73 40
31 4 66 75 59
26 32 91 38 80
69 81 65 30 77
82 22 83 0 38
2 3 29 47 94
42 55 9 18 97
53 45 90 31 44
23 86 0 35 84
27 80 3 64 12
1 96 48 93 85
69 24 61 15 22
91 72 62 13 76
81 51 67 60 16
65 48 86 39 97
92 93 49 77 59
15 94 88 52 19
80 83 23 61 4
47 84 46 79 55
92 38 65 42 76
9 58 26 95 86
30 49 56 69 59
94 4 25 89 44
73 10 29 0 48
56 40 19 84 61
52 31 25 86 21
79 55 53 51 5
81 9 35 72 15
41 95 30 58 73
26 80 4 21 96
61 92 76 93 74
2 69 60 8 20
46 98 70 72 83
1 99 31 4 86
93 64 8 43 61
33 36 75 90 50
52 13 3 42 34
22 65 60 18 76
17 63 6 66 92
51 67 86 88 18
82 83 32 74 30
5 33 9 28 61
72 75 25 23 60
43 28 40 53 52
54 12 77 10 83
21 44 63 0 1
15 22 33 49 2
80 41 3 46 55
1 97 75 37 4
47 33 13 21 40
27 62 15 90 30
11 83 63 36 35
0 12 60 91 42
0 45 17 88 18
66 10 63 62 8
36 5 47 39 67
21 3 61 29 19
82 58 33 6 59
37 92 69 56 52
46 66 20 78 13
83 99 16 31 0
36 35 2 68 9
70 82 94 96 29
62 65 85 37 3
74 95 34 96 58
15 33 49 21 93
19 83 66 6 25
81 84 23 0 76
95 84 71 92 52
54 36 66 59 82
0 76 32 45 83
69 27 25 88 38
81 96 63 4 61
73 51 28 48 40
3 38 11 14 35
66 91 86 20 81
53 39 46 71 1
97 60 21 93 23
92 70 4 60 95
58 49 20 15 25
55 68 21 84 80
56 41 82 23 19
30 74 65 27 29
81 97 68 46 75
62 73 63 36 41
1 5 91 84 37
45 92 20 49 7
25 26 3 88 56
25 9 94 37 26
44 58 84 91 38
39 46 57 98 50
96 42 73 24 70
71 32 53 48 13
98 72 25 96 77
80 64 88 53 23
21 37 45 24 18
41 86 59 68 5
76 50 36 26 12
77 79 88 74 12
21 9 85 26 68
11 62 64 4 5
47 33 76 63 87
55 19 2 60 95
74 79 30 14 35
90 52 17 29 63
18 69 78 34 26
92 42 85 71 56
12 2 5 0 98

174
2021/day4/solution-part1.c Normal file
View file

@ -0,0 +1,174 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct {
unsigned int value;
bool marked;
} Cell;
typedef struct {
Cell cells[5][5];
} Grid;
unsigned int getEmptyLines(FILE *fp){
char line[500];
unsigned int emptyLines = 0;
while (!feof (fp)){
fgets(line, 500, fp);
//Length of 1 because it's a new line character only
if ( strlen(line) == 1 ) emptyLines ++;
}
return emptyLines;
}
//fp should be pointing to the start of the file as rounds are the first line
unsigned int countRounds(FILE *fp){
unsigned int rounds=0;
char ch = fgetc(fp);
while ( ch != EOF ){
switch (ch){
case ',':
rounds++;
break;
case '\n':
rounds++;
return rounds;
break;
}
ch = fgetc(fp);
}
return rounds;
}
void printGrid(Grid *grid){
for ( unsigned int row = 0; row < 5; row++ ){
for ( unsigned int col = 0; col < 5; col++ ){
if ( grid->cells[row][col].marked ) printf(ANSI_COLOR_RED);
printf("%3i", grid->cells[row][col].value);
if ( grid->cells[row][col].marked ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void printAllGrids(Grid grids[], unsigned int no){
for( unsigned int i = 0; i<no; i++ ){
printf("Grid %i\n", i);
printGrid(&grids[i]);
printf("\n");
}
}
unsigned int sumUnmarked(Grid *grid){
unsigned int sum = 0;
for ( unsigned int row = 0; row < 5; row++ ){
for ( unsigned int col = 0; col < 5; col++ ){
if ( !grid->cells[row][col].marked ) sum += grid->cells[row][col].value;
}
}
return sum;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
//The number of empty linse is the same as the number of grids
unsigned int gridCount = getEmptyLines(fp);
rewind(fp);
Grid grids[gridCount];
unsigned int rounds = countRounds(fp);
int draws[rounds];
memset( &draws, 0, rounds*sizeof(int) );
rewind(fp);
char ch;
for ( unsigned int i = 0; i < rounds; i++ ){
fscanf(fp, "%i%c,", &draws[i], &ch);
}
// Put all the numbers into the grid array
unsigned int no;
unsigned int grid = 0, row = 0, col = 0;
while ( !feof(fp) && fscanf(fp, "%i", &no) ){
grids[grid].cells[row][col].value = no;
grids[grid].cells[row][col].marked = false;
col = (col + 1) % 5;
if ( col == 0 ) row = (row + 1) % 5;
if ( col == 0 && row == 0 ) grid++;
}
//Start playing the game
bool bingo=false;
for (unsigned int round = 0; round < rounds && !bingo; round++){
for( unsigned int grid = 0; grid < gridCount && !bingo; grid++ ){
for( unsigned int row = 0; row < 5 && !bingo; row++ ){
for( unsigned int col = 0; col < 5 && !bingo; col++ ){
if ( grids[grid].cells[row][col].value == draws[round] ){
grids[grid].cells[row][col].marked = true;
//If we are above round 4, it's possible to have won
if ( round > 4 ){
bool maybebingorow=true;
bool maybebingocol=true;
// As soon as neither row or column is bingo, stop cheking
for (unsigned int i = 0; ( maybebingocol || maybebingorow ) && i < 5; i++){
// Go through the row and column for the matched number
// As soon as we find one in the row or column that isn't marked, we know that won't be bingo
if ( !grids[grid].cells[row][i].marked ) maybebingorow = false;
if ( !grids[grid].cells[i][col].marked ) maybebingocol = false;
}
//If we get here and maybebingocol or maybebingorow is true, we have bingo
if ( maybebingorow || maybebingocol ){
bingo=true;
unsigned int sum = sumUnmarked( &grids[grid] );
printf("Bingo!!\n");
printf("Round: %i\n", round);
printf("Grid: %i\n", grid);
printf("Unmarked Sum: %i\n", sum);
printf("Last Called: %i\n", draws[round]);
printf("Answer: %i\n", sum * draws[round]);
printf("\n");
printGrid(&grids[grid]);
}
}
}
}
}
}
//No point checking before round 5
}
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

185
2021/day4/solution-part2.c Normal file
View file

@ -0,0 +1,185 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct {
unsigned int value;
bool marked;
} Cell;
typedef struct {
Cell cells[5][5];
} Grid;
unsigned int getEmptyLines(FILE *fp){
char line[500];
unsigned int emptyLines = 0;
while (!feof (fp)){
fgets(line, 500, fp);
//Length of 1 because it's a new line character only
if ( strlen(line) == 1 ) emptyLines ++;
}
return emptyLines;
}
//fp should be pointing to the start of the file as rounds are the first line
unsigned int countRounds(FILE *fp){
unsigned int rounds=0;
char ch = fgetc(fp);
while ( ch != EOF ){
switch (ch){
case ',':
rounds++;
break;
case '\n':
rounds++;
return rounds;
break;
}
ch = fgetc(fp);
}
return rounds;
}
void printGrid(Grid *grid){
for ( unsigned int row = 0; row < 5; row++ ){
for ( unsigned int col = 0; col < 5; col++ ){
if ( grid->cells[row][col].marked ) printf(ANSI_COLOR_RED);
printf("%3i", grid->cells[row][col].value);
if ( grid->cells[row][col].marked ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
void printAllGrids(Grid grids[], unsigned int no){
for( unsigned int i = 0; i<no; i++ ){
printf("Grid %i\n", i);
printGrid(&grids[i]);
printf("\n");
}
}
unsigned int sumUnmarked(Grid *grid){
unsigned int sum = 0;
for ( unsigned int row = 0; row < 5; row++ ){
for ( unsigned int col = 0; col < 5; col++ ){
if ( !grid->cells[row][col].marked ) sum += grid->cells[row][col].value;
}
}
return sum;
}
unsigned int countWon( bool *wonGrids, unsigned int gridCount ){
unsigned int won = 0;
for( unsigned int i = 0; i < gridCount; i++ ) if( wonGrids[i] ) won++;
return won;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
//The number of empty linse is the same as the number of grids
unsigned int gridCount = getEmptyLines(fp);
rewind(fp);
Grid grids[gridCount];
bool wonGrids[gridCount];
memset( &wonGrids, 0, gridCount*sizeof(bool) );
unsigned int rounds = countRounds(fp);
int draws[rounds];
memset( &draws, 0, rounds*sizeof(int) );
rewind(fp);
char ch;
for ( unsigned int i = 0; i < rounds; i++ ){
fscanf(fp, "%i%c,", &draws[i], &ch);
}
// Put all the numbers into the grid array
unsigned int no;
unsigned int grid = 0, row = 0, col = 0;
while ( !feof(fp) && fscanf(fp, "%i", &no) ){
grids[grid].cells[row][col].value = no;
grids[grid].cells[row][col].marked = false;
col = (col + 1) % 5;
if ( col == 0 ) row = (row + 1) % 5;
if ( col == 0 && row == 0 ) grid++;
}
//Start playing the game
bool allwon=false;
for (unsigned int round = 0; round < rounds; round++){
for( unsigned int grid = 0; grid < gridCount; grid++ ){
for( unsigned int row = 0; row < 5; row++ ){
for( unsigned int col = 0; col < 5; col++ ){
if ( grids[grid].cells[row][col].value == draws[round] ){
grids[grid].cells[row][col].marked = true;
//If we are above round 4, it's possible to have won
if ( round > 4 ){
bool maybebingorow=true;
bool maybebingocol=true;
// As soon as neither row or column is bingo, stop cheking
for (unsigned int i = 0; ( maybebingocol || maybebingorow ) && i < 5; i++){
// Go through the row and column for the matched number
// As soon as we find one in the row or column that isn't marked, we know that won't be bingo
if ( !grids[grid].cells[row][i].marked ) maybebingorow = false;
if ( !grids[grid].cells[i][col].marked ) maybebingocol = false;
}
//If we get here and maybebingocol or maybebingorow is true, we have bingo
if ( maybebingorow || maybebingocol ){
wonGrids[grid] = true;
if ( countWon(wonGrids, gridCount) == gridCount ){
unsigned int sum = sumUnmarked( &grids[grid] );
printf("Last Bingo!!\n");
printf("Round: %i\n", round);
printf("Grid: %i\n", grid);
printf("Unmarked Sum: %i\n", sum);
printf("Last Called: %i\n", draws[round]);
printf("Answer: %i\n", sum * draws[round]);
printf("\n");
printGrid(&grids[grid]);
return 0;
}
}
}
}
}
}
}
//No point checking before round 5
}
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day5/Makefile Normal file
View file

@ -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)

10
2021/day5/example.txt Normal file
View file

@ -0,0 +1,10 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2

500
2021/day5/realinput.txt Normal file
View file

@ -0,0 +1,500 @@
682,519 -> 682,729
852,131 -> 25,958
303,481 -> 206,481
199,682 -> 183,666
363,190 -> 571,190
930,290 -> 221,290
364,627 -> 952,39
234,309 -> 234,821
130,864 -> 130,886
462,347 -> 699,110
375,969 -> 481,969
989,859 -> 275,145
221,748 -> 212,748
870,173 -> 106,937
604,33 -> 604,142
780,35 -> 780,206
636,808 -> 721,808
944,989 -> 334,989
477,113 -> 192,113
879,265 -> 879,358
754,974 -> 17,974
10,989 -> 989,10
337,320 -> 674,657
225,96 -> 557,428
129,354 -> 299,354
717,695 -> 695,695
94,255 -> 317,478
90,87 -> 90,187
77,942 -> 952,67
804,315 -> 989,315
619,470 -> 491,342
466,90 -> 466,755
840,121 -> 840,469
638,127 -> 638,77
844,40 -> 151,40
653,987 -> 653,631
195,583 -> 195,623
88,985 -> 570,503
921,897 -> 165,141
230,27 -> 171,27
737,771 -> 832,771
563,365 -> 665,467
942,940 -> 776,774
12,903 -> 644,903
308,390 -> 308,971
572,943 -> 572,15
104,389 -> 104,73
346,721 -> 974,93
30,53 -> 562,53
804,682 -> 804,120
952,45 -> 15,982
475,456 -> 475,348
409,247 -> 664,247
345,18 -> 816,489
571,158 -> 505,158
59,195 -> 27,195
230,681 -> 23,681
258,711 -> 921,711
658,112 -> 366,404
842,220 -> 842,825
815,744 -> 540,744
192,314 -> 703,825
869,573 -> 869,888
603,268 -> 603,301
816,668 -> 816,189
148,606 -> 948,606
117,461 -> 506,461
986,955 -> 986,315
131,250 -> 192,189
988,148 -> 518,618
682,900 -> 31,900
652,839 -> 652,236
466,812 -> 466,611
881,346 -> 401,346
229,639 -> 731,639
104,476 -> 840,476
10,988 -> 988,10
29,15 -> 987,973
825,348 -> 825,240
989,989 -> 10,10
430,796 -> 926,796
49,293 -> 610,854
325,288 -> 918,288
625,309 -> 439,495
536,150 -> 356,150
834,558 -> 822,558
315,408 -> 315,635
257,973 -> 813,973
713,52 -> 122,52
323,970 -> 578,970
447,49 -> 829,49
941,709 -> 941,390
148,323 -> 391,80
23,171 -> 23,49
475,265 -> 322,112
506,407 -> 69,844
567,284 -> 483,368
114,745 -> 114,765
392,252 -> 109,535
65,188 -> 455,188
732,779 -> 732,52
233,214 -> 759,214
232,969 -> 957,244
20,669 -> 20,308
49,972 -> 285,972
501,383 -> 433,383
918,15 -> 32,901
255,268 -> 935,948
757,588 -> 757,919
530,803 -> 284,557
688,926 -> 48,286
331,245 -> 331,777
448,544 -> 209,544
10,970 -> 951,29
233,11 -> 897,11
145,392 -> 628,392
935,971 -> 935,280
169,632 -> 54,632
155,244 -> 155,334
56,284 -> 205,284
553,428 -> 553,520
977,176 -> 497,656
323,339 -> 971,987
616,355 -> 616,248
72,660 -> 72,334
644,822 -> 510,956
356,841 -> 587,841
413,468 -> 605,468
85,22 -> 645,582
924,850 -> 522,850
448,45 -> 345,148
102,566 -> 551,566
80,39 -> 847,806
936,436 -> 934,436
53,24 -> 495,466
234,173 -> 282,173
145,680 -> 456,680
960,759 -> 960,282
984,814 -> 308,138
398,808 -> 716,808
509,536 -> 25,52
289,777 -> 803,263
766,892 -> 257,892
301,733 -> 688,733
24,109 -> 887,972
180,32 -> 577,429
985,801 -> 687,503
901,582 -> 586,582
50,56 -> 50,267
344,373 -> 437,373
542,133 -> 905,496
420,624 -> 420,716
645,106 -> 645,574
356,37 -> 114,37
324,919 -> 357,919
126,797 -> 120,797
288,689 -> 435,836
93,915 -> 639,369
106,391 -> 478,19
277,501 -> 714,64
253,277 -> 643,277
568,972 -> 350,972
213,235 -> 213,406
595,888 -> 595,233
577,63 -> 37,603
649,732 -> 931,732
469,892 -> 549,892
953,895 -> 953,457
222,213 -> 290,213
841,800 -> 841,336
685,143 -> 25,143
441,127 -> 441,146
646,586 -> 56,586
698,122 -> 465,122
641,502 -> 641,240
111,91 -> 185,91
927,755 -> 874,808
108,151 -> 108,567
309,453 -> 309,210
890,657 -> 491,657
404,244 -> 404,123
939,28 -> 26,941
596,970 -> 596,870
489,556 -> 489,589
607,621 -> 903,325
912,284 -> 571,284
921,702 -> 743,524
719,36 -> 719,394
100,905 -> 798,207
316,260 -> 316,887
799,940 -> 885,940
835,287 -> 199,923
422,760 -> 64,760
727,113 -> 727,679
733,56 -> 59,730
141,399 -> 485,743
769,629 -> 769,797
62,486 -> 62,205
192,332 -> 800,332
15,931 -> 727,931
854,915 -> 988,915
349,610 -> 886,610
72,110 -> 72,903
955,110 -> 78,987
591,553 -> 591,428
708,467 -> 516,467
397,589 -> 353,589
930,336 -> 930,532
639,50 -> 228,50
472,17 -> 472,244
420,825 -> 420,562
203,197 -> 203,35
984,964 -> 223,203
944,269 -> 935,260
933,119 -> 87,965
696,290 -> 696,580
925,960 -> 52,87
451,470 -> 235,254
562,71 -> 562,149
405,126 -> 405,67
356,424 -> 356,673
859,649 -> 859,291
210,651 -> 210,544
403,783 -> 403,122
672,87 -> 586,87
409,668 -> 984,668
917,352 -> 511,758
395,953 -> 141,953
152,795 -> 152,313
839,344 -> 811,372
114,649 -> 650,649
60,517 -> 60,27
448,392 -> 845,392
33,849 -> 719,163
151,988 -> 876,988
805,556 -> 124,556
361,538 -> 706,193
974,941 -> 141,108
271,813 -> 968,116
500,697 -> 80,277
586,731 -> 586,480
128,147 -> 174,101
882,681 -> 882,745
531,730 -> 677,730
989,11 -> 11,989
74,332 -> 234,492
360,326 -> 932,898
136,288 -> 113,311
666,645 -> 916,895
977,478 -> 561,62
20,83 -> 566,83
331,942 -> 331,646
180,291 -> 405,291
637,763 -> 637,941
120,138 -> 120,820
951,24 -> 14,961
204,304 -> 204,51
84,168 -> 880,168
955,145 -> 955,903
437,427 -> 437,354
875,67 -> 189,753
46,767 -> 802,11
52,59 -> 889,896
926,56 -> 102,880
500,30 -> 964,30
329,488 -> 329,972
63,11 -> 889,837
707,168 -> 707,584
580,10 -> 735,10
105,620 -> 105,110
187,531 -> 323,531
82,947 -> 82,941
737,199 -> 737,851
612,650 -> 217,650
971,15 -> 82,904
16,590 -> 506,100
950,877 -> 832,759
570,470 -> 570,276
213,411 -> 213,195
670,755 -> 89,755
906,963 -> 906,984
458,463 -> 442,463
956,969 -> 10,23
87,215 -> 195,107
819,454 -> 819,467
594,793 -> 686,793
395,724 -> 787,332
315,461 -> 644,461
448,247 -> 249,48
620,302 -> 247,675
607,134 -> 932,134
312,776 -> 312,289
850,942 -> 54,146
31,538 -> 851,538
729,126 -> 640,126
702,199 -> 702,706
402,783 -> 254,783
110,59 -> 203,59
27,10 -> 965,948
747,261 -> 47,261
628,742 -> 972,742
712,742 -> 657,797
877,871 -> 877,758
665,313 -> 449,529
498,157 -> 498,68
870,922 -> 27,79
856,697 -> 856,429
447,271 -> 963,787
495,302 -> 495,520
526,47 -> 721,47
826,179 -> 826,741
565,461 -> 893,461
512,328 -> 127,328
487,920 -> 522,920
614,452 -> 614,146
331,574 -> 331,840
985,79 -> 285,779
812,320 -> 985,320
118,69 -> 429,69
644,525 -> 644,878
271,132 -> 156,132
955,782 -> 565,392
630,939 -> 630,372
51,203 -> 840,203
202,490 -> 202,479
579,868 -> 579,92
979,336 -> 979,623
843,865 -> 260,282
685,872 -> 685,503
721,193 -> 721,510
908,661 -> 908,955
19,950 -> 715,254
233,730 -> 233,101
922,954 -> 27,954
399,444 -> 399,403
380,712 -> 380,718
238,264 -> 849,875
458,577 -> 458,139
418,244 -> 469,295
460,375 -> 964,879
276,445 -> 815,445
463,910 -> 648,725
26,384 -> 968,384
955,385 -> 955,143
942,775 -> 733,566
425,326 -> 531,326
364,545 -> 364,873
182,759 -> 182,819
390,757 -> 390,475
217,417 -> 217,157
669,286 -> 65,890
257,11 -> 257,858
557,397 -> 557,20
888,946 -> 32,90
971,938 -> 971,578
874,248 -> 874,485
87,268 -> 87,135
756,679 -> 103,26
771,250 -> 771,107
320,711 -> 967,711
293,219 -> 293,706
103,565 -> 103,538
388,256 -> 388,261
468,953 -> 503,953
424,142 -> 287,142
24,930 -> 850,930
316,167 -> 316,161
481,421 -> 208,148
938,926 -> 938,933
701,653 -> 701,780
536,390 -> 536,559
40,954 -> 829,165
404,985 -> 247,985
94,628 -> 94,500
441,637 -> 441,271
766,946 -> 97,277
428,363 -> 428,37
542,694 -> 542,347
11,16 -> 979,984
938,651 -> 632,957
779,127 -> 243,663
636,294 -> 636,787
533,744 -> 636,641
521,950 -> 458,950
988,12 -> 18,982
954,621 -> 954,271
638,951 -> 813,951
822,911 -> 632,911
714,849 -> 512,849
696,88 -> 385,88
65,451 -> 65,687
976,973 -> 976,907
368,489 -> 368,571
358,831 -> 690,499
436,704 -> 178,704
690,619 -> 606,535
96,701 -> 358,701
885,562 -> 420,562
581,480 -> 613,512
44,970 -> 970,44
216,796 -> 892,120
72,623 -> 72,72
896,283 -> 896,326
794,195 -> 22,967
134,326 -> 134,889
420,141 -> 944,665
941,194 -> 941,421
940,525 -> 298,525
653,300 -> 769,300
227,424 -> 406,603
275,850 -> 113,850
648,850 -> 92,850
638,389 -> 638,10
379,404 -> 584,609
833,931 -> 833,520
772,286 -> 500,558
372,262 -> 333,262
689,18 -> 131,576
687,499 -> 687,188
344,499 -> 37,806
778,496 -> 134,496
938,87 -> 344,681
788,401 -> 479,401
828,903 -> 756,903
423,625 -> 285,763
218,489 -> 218,819
488,384 -> 891,787
817,532 -> 788,532
512,27 -> 512,149
369,794 -> 54,794
534,590 -> 827,883
84,310 -> 39,265
357,545 -> 665,545
539,807 -> 539,781
378,683 -> 22,327
71,909 -> 943,37
740,552 -> 348,552
698,315 -> 45,968
516,835 -> 360,835
629,141 -> 629,385
695,908 -> 303,908
795,707 -> 386,707
211,397 -> 291,397
64,620 -> 236,620
97,638 -> 97,445
46,103 -> 893,950
468,122 -> 979,122
810,486 -> 433,486
532,899 -> 461,970
232,60 -> 235,60
549,708 -> 549,90
294,978 -> 294,124
865,406 -> 640,406
755,305 -> 664,305
12,989 -> 987,14
275,249 -> 260,234
502,783 -> 67,783
863,938 -> 297,372
516,961 -> 516,272
67,510 -> 611,510
980,951 -> 312,283
325,512 -> 325,169
142,429 -> 542,29
273,964 -> 822,964
370,217 -> 508,217
131,131 -> 331,331
734,824 -> 734,817
75,89 -> 687,701
155,255 -> 702,802
577,395 -> 130,395
684,94 -> 555,94
393,881 -> 173,881
894,750 -> 773,750
380,269 -> 380,338
427,36 -> 427,77
637,107 -> 637,846
53,437 -> 53,221
128,979 -> 960,147
838,211 -> 838,645
898,39 -> 849,39
862,495 -> 951,495
754,406 -> 76,406
951,960 -> 113,122
830,125 -> 15,940
190,117 -> 190,973
192,956 -> 718,430
895,162 -> 88,969
135,196 -> 70,131
578,642 -> 578,789
713,268 -> 625,268
938,719 -> 938,604
30,863 -> 99,863
844,309 -> 287,309
131,837 -> 459,509
61,206 -> 722,867
95,974 -> 283,974
746,672 -> 558,672
552,32 -> 352,32
21,637 -> 21,781
945,847 -> 945,303

View file

@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void printGrid(unsigned int maxx, unsigned int maxy, unsigned int grid[maxx+1][maxy+1]){
for( unsigned int y = 0; y <= maxy; y++ ){
for( unsigned int x = 0; x <= maxx; x++ ){
printf("%3i", grid[x][y]);
}
printf("\n");
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int maxx=0, maxy=0;
while (!feof (fp)){
unsigned int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
maxx= MAX( maxx, MAX( x1, x2 ) );
maxy= MAX( maxy, MAX( y1, y2 ) );
}
unsigned int grid[maxx+1][maxy+1];
memset( &grid, 0, (maxx+1)*(maxy+1)*sizeof(int) );
rewind(fp);
unsigned int cellsWithOverlap=0;
while (!feof (fp)){
unsigned int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
if ( x1 == x2 ){
for( unsigned int y = MIN(y1,y2); y<=MAX(y1,y2); y++ )
if ( 1 == grid[x1][y]++) cellsWithOverlap++;
}
if ( y1 == y2 ){
for( unsigned int x = MIN(x1,x2); x<=MAX(x1,x2); x++ )
if ( 1 == grid[x][y1]++) cellsWithOverlap++;
}
}
printf("%i cells with overlap\n", cellsWithOverlap);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

View file

@ -0,0 +1,90 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void printGrid(unsigned int maxx, unsigned int maxy, unsigned int grid[maxx+1][maxy+1]){
for( unsigned int y = 0; y <= maxy; y++ ){
for( unsigned int x = 0; x <= maxx; x++ ){
printf("%3i", grid[x][y]);
}
printf("\n");
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int maxx=0, maxy=0;
while (!feof (fp)){
int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
maxx= MAX( maxx, MAX( x1, x2 ) );
maxy= MAX( maxy, MAX( y1, y2 ) );
}
unsigned int grid[maxx+1][maxy+1];
memset( &grid, 0, (maxx+1)*(maxy+1)*sizeof(int) );
rewind(fp);
unsigned int cellsWithOverlap=0;
unsigned int iteration = 0;
while (!feof (fp)){
int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
//printf("Iteration: %i\n", iteration);
if ( x1 == x2 ){ // Virtical lines
for( unsigned int y = MIN(y1,y2); y<=MAX(y1,y2); y++ )
if ( 1 == grid[x1][y]++) cellsWithOverlap++;
} else if ( y1 == y2 ){ // Horisontal lines
for( unsigned int x = MIN(x1,x2); x<=MAX(x1,x2); x++ )
if ( 1 == grid[x][y1]++) cellsWithOverlap++;
} else { // Diaganal lines
// Since we know all diagonals are 45 deg we only need to worry about if this is 1 or -1
int dx = (x2 - x1) / abs(x2 - x1);
int dy = (y2 - y1) / abs(y2 - y1);
for (unsigned int i = 0; i <= MAX(x1,x2) - MIN(x1,x2); i++){
unsigned int x = x1 + ( dx * i );
unsigned int y = y1 + ( dy * i );
if ( 1 == grid[x][y]++) cellsWithOverlap++;
}
}
iteration++;
}
printf("%i cells with overlap\n", cellsWithOverlap);
//printGrid(maxx, maxy, grid);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day6/Makefile Normal file
View file

@ -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)

1
2021/day6/example.txt Normal file
View file

@ -0,0 +1 @@
3,4,3,1,2

1
2021/day6/realinput.txt Normal file
View file

@ -0,0 +1 @@
4,1,1,4,1,1,1,1,1,1,1,1,3,4,1,1,1,3,1,3,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,5,1,2,1,1,5,3,4,2,1,1,4,1,1,5,1,1,5,5,1,1,5,2,1,4,1,2,1,4,5,4,1,1,1,1,3,1,1,1,4,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,1,2,1,1,1,1,1,1,1,2,4,4,1,1,3,1,3,2,4,3,1,1,1,1,1,2,1,1,1,1,2,5,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,4,1,5,1,3,1,1,1,1,1,5,1,1,1,3,1,2,1,2,1,3,4,5,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,3,1,1,3,1,1,4,1,1,1,1,1,2,1,1,1,1,3,2,1,1,1,4,2,1,1,1,4,1,1,2,3,1,4,1,5,1,1,1,2,1,5,3,3,3,1,5,3,1,1,1,1,1,1,1,1,4,5,3,1,1,5,1,1,1,4,1,1,5,1,2,3,4,2,1,5,2,1,2,5,1,1,1,1,4,1,2,1,1,1,2,5,1,1,5,1,1,1,3,2,4,1,3,1,1,2,1,5,1,3,4,4,2,2,1,1,1,1,5,1,5,2

103
2021/day6/solution-part1.c Normal file
View file

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
typedef struct LanternFishListItem LanternFishListItem;
struct LanternFishListItem {
unsigned int daysToSpawn;
LanternFishListItem *next;
};
void printFrom( LanternFishListItem *first ){
printf("%i,", first->daysToSpawn);
if ( first->next != NULL ) printFrom(first->next);
}
unsigned int countFrom( LanternFishListItem *current ){
unsigned int count = 1;
while ( current->next != NULL ){
count++;
current = current->next;
}
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
LanternFishListItem *first = NULL;
LanternFishListItem *previous = NULL;
unsigned int no;
char ch;
while ( fscanf( fp, "%i%c", &no, &ch ) ){
LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem));
lf->next = NULL;
lf->daysToSpawn = no;
if ( first == NULL ){
first = lf;
} else {
previous->next = lf;
}
previous = lf;
if ( ch == '\n' ) break;
}
for ( unsigned int i = 0; i < 80; i++ ){
unsigned int toAdd=0;
LanternFishListItem *current = first;
while (current != NULL){
if ( current->daysToSpawn == 0 ){
toAdd++;
current->daysToSpawn = 7;
}
current->daysToSpawn--;
if ( current->next != NULL )
current = current->next;
else
break;
}
previous = current;
//printf("On day %i add %i fish\n", i, toAdd);
for (unsigned int j = 0; j < toAdd; j++){
LanternFishListItem *lf = (LanternFishListItem*)malloc(sizeof(LanternFishListItem));
lf->next = NULL;
lf->daysToSpawn = 8;
previous->next = lf;
previous = lf;
}
//printf("After %3i day: ", i+1);
//printFrom(first);
//printf("\n");
}
printf("There are %i Fish.\n",countFrom(first));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

View file

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define POSSIBLE_SPAWN_AGES 9
unsigned long countFish(unsigned long *fishCount){
unsigned long count = 0;
for( unsigned int i = 0; i < POSSIBLE_SPAWN_AGES; i++ ){
count += fishCount[i];
}
return count;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int no;
char ch;
// I realised that the ordering of the fish doesn't matter. What matters is how many of them are of each age.
// This array will contain the number of fish of age 1 in fishCount[1] and so on.
unsigned long fishCount[POSSIBLE_SPAWN_AGES] = {0};
while ( fscanf( fp, "%i%c", &no, &ch ) ){
fishCount[no]++;
if ( ch == '\n' ) break;
}
for ( unsigned int i = 0; i < 256; i++ ){
// "backup" the number of fish of age 0 as the memmove will loose this
unsigned long age0 = fishCount[0];
memmove( &fishCount[0], &fishCount[1], sizeof(unsigned long) * (POSSIBLE_SPAWN_AGES - 1) );
// All the fish that had a spawn_age of 0 will now have a spawn age of 6
fishCount[6] += age0;
// They will also all have a new fish with age of 8
fishCount[8] = age0;
//printf("\nDay %i:\n", i+1);
//for( unsigned int j = 0; j < POSSIBLE_SPAWN_AGES; j++){
// printf("Fish with spawn age %i: %lu\n", j, fishCount[j]);
//}
}
printf("There are %lu Fish.\n",countFish(fishCount));
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day7/Makefile Normal file
View file

@ -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)

1
2021/day7/example.txt Normal file
View file

@ -0,0 +1 @@
16,1,2,0,4,2,7,1,2,14

1
2021/day7/realinput.txt Normal file
View file

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,3,443,2,394,403,176,454,60,126,959,781,426,212,194,65,26,100,1047,718,29,1124,515,723,74,969,717,228,50,39,1074,1799,1276,505,477,9,742,804,191,133,730,26,272,139,501,549,318,1164,1019,399,2,224,1807,421,191,594,168,336,13,271,571,385,1071,1316,1796,673,417,1518,1152,196,1307,262,217,167,696,64,84,844,312,1770,371,517,11,450,745,80,382,367,1527,79,1002,878,298,147,57,24,1282,291,338,911,60,622,508,513,53,404,339,672,440,168,1302,246,116,446,502,1402,398,1686,118,600,865,131,48,162,1614,255,1104,1379,85,115,38,9,483,629,28,157,238,727,27,142,766,716,393,19,115,220,1103,397,1330,1027,667,0,678,464,1431,549,976,456,1403,683,749,600,711,159,98,1000,606,478,428,612,790,1202,1461,713,533,1643,53,29,1,261,268,466,478,187,256,134,373,1239,17,727,574,1206,854,88,27,51,544,793,100,1146,49,166,317,325,506,64,3,255,951,747,274,926,664,531,72,443,1088,91,259,725,100,41,1709,239,101,119,1472,437,91,360,539,96,498,78,825,103,621,216,274,402,133,446,263,1167,212,607,1596,17,113,814,528,56,128,388,686,1179,43,886,58,633,33,186,808,220,98,15,947,1074,612,328,979,1203,562,938,207,35,484,812,616,218,207,249,117,136,39,914,951,122,252,23,1,194,132,366,1004,4,1079,832,510,103,1024,203,398,1543,17,995,961,195,79,1070,1222,53,143,91,223,6,645,175,512,424,471,829,153,126,332,586,543,460,594,105,122,220,432,197,243,73,121,454,765,99,186,35,478,113,1526,305,596,102,365,38,1053,628,24,425,962,570,170,457,268,518,339,32,78,600,8,154,530,641,1889,74,58,1047,166,100,923,687,1280,1144,1260,405,1786,22,694,243,284,1109,503,650,633,52,236,661,620,54,642,130,697,718,259,244,52,387,347,459,724,661,535,354,53,105,246,493,538,894,0,368,67,151,2,127,724,705,237,512,557,573,829,789,61,415,131,526,982,206,73,46,82,411,1670,1350,34,654,190,160,575,669,21,830,34,1233,371,819,360,271,173,420,38,112,273,115,493,40,51,634,844,281,613,193,10,111,567,52,17,1137,117,105,122,250,72,70,1038,210,1008,191,99,1123,1641,90,1306,47,385,835,395,1057,1036,4,127,103,486,205,138,929,286,46,641,1041,490,399,178,1492,421,106,275,741,1196,15,174,700,892,8,818,215,436,624,459,415,812,28,196,921,119,333,896,541,117,926,1084,276,222,361,325,23,1295,205,384,207,863,659,22,89,21,110,269,157,385,422,941,629,1511,544,701,404,489,38,495,507,238,132,753,119,459,145,120,984,313,973,1049,158,372,317,596,1024,177,256,23,6,527,1439,592,743,104,414,256,418,425,271,58,583,0,164,277,1285,809,125,391,843,70,69,194,250,611,287,2,389,1469,555,552,28,236,278,178,314,368,254,86,240,72,204,8,856,28,312,236,464,144,405,433,28,42,16,587,22,403,1234,744,484,48,180,238,47,384,1315,13,78,9,882,70,551,58,756,111,777,340,1046,185,83,412,19,527,514,355,350,1198,913,154,87,507,76,1108,392,361,1081,75,1083,201,896,1161,1267,46,420,79,702,879,348,622,68,611,180,515,37,186,435,1485,438,699,1441,1122,85,145,1372,68,2,621,147,503,156,993,496,536,359,485,657,152,585,681,908,294,398,4,190,33,197,304,157,38,77,922,1033,166,85,411,544,1688,81,192,111,561,212,176,291,44,76,465,35,6,110,1329,1702,53,70,82,4,1141,278,483,66,9,115,733,856,18,1167,282,100,11,605,673,1093,1268,1633,1632,570,847,575,160,8,335,5,27,489,3,1669,1072,571,144,460,15,200,15,108,473,527,775,818,87,359,46,665,191,635,1122,289,457,169,17,42,395,1258,189,253,1292,69,370,419,47,79,22,668,655,274,60,1012,428,246,248,1181,520,760,1274,320,608,65,137,722,386,275,252,1221,729,421,265,171,84,9,3,8,1328,86,939,890,986,34,156,245,1896,617,1733,346,751,1281,244,346,56,196,380,426,810,233,353,28,147,307,131,1529,1173,68,862,1048,774,571,699,163,306,385,368,150,651,316,517,47,508,407,452,315,257,1652,73,222,1187,1635,1268,48,301,247,308,211,149,1300,141,113,57,152,498,450,34,376,748,442,670,358,105,587,66,0,285,46

View file

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
double average(unsigned int length, unsigned int *list) {
double sum = 0.0;
/* access all the arguments assigned to valist */
for (unsigned int i = 0; i < length; i++) {
sum += list[i];
}
return sum/length;
}
unsigned int totalDistance(unsigned int crabLength, unsigned int *crabs, unsigned int target){
unsigned int totalDistance = 0;
for (unsigned int i = 0; i < crabLength; i++){
totalDistance += abs( (int)target - (int)crabs[i] );
}
return totalDistance;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int count=0;
unsigned int no;
char ch;
// Count the number of crab positions
while ( fscanf( fp, "%i%c", &no, &ch ) ){
count++;
if ( ch == '\n' ) break;
}
rewind(fp);
unsigned int min = ~0;
unsigned int max = 0;
unsigned int crabs[count];
for (unsigned int i= 0; i<count; i++ ){
fscanf( fp, "%i%c", &no, &ch );
crabs[i] = no;
min = MIN( min, no );
max = MAX( max, no );
}
unsigned int smallestCost = ~0;
for ( unsigned int i = 0; i <= max; i++ ){
smallestCost = MIN( smallestCost, totalDistance( count, crabs, i ) );
}
printf("Smallest Cost: %i\n", smallestCost);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

View file

@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
double average(unsigned int length, unsigned int *list) {
double sum = 0.0;
/* access all the arguments assigned to valist */
for (unsigned int i = 0; i < length; i++) {
sum += list[i];
}
return sum/length;
}
unsigned int totalDistance(unsigned int crabLength, unsigned int *crabs, unsigned int target){
unsigned int totalDistance = 0;
for (unsigned int i = 0; i < crabLength; i++){
unsigned int distance = abs((int)crabs[i] - (int)target);
unsigned int cost = distance * (distance + 1) / 2;
//printf( "Move from %i to %i: %i fuel\n", crabs[i], target, cost );
totalDistance += cost;
}
return totalDistance;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int count=0;
unsigned int no;
char ch;
// Count the number of crab positions
while ( fscanf( fp, "%i%c", &no, &ch ) ){
count++;
if ( ch == '\n' ) break;
}
rewind(fp);
unsigned int min = ~0;
unsigned int max = 0;
unsigned int crabs[count];
for (unsigned int i= 0; i<count; i++ ){
fscanf( fp, "%i%c", &no, &ch );
crabs[i] = no;
min = MIN( min, no );
max = MAX( max, no );
}
unsigned int smallestCost = ~0;
for ( unsigned int i = 0; i <= max; i++ ){
smallestCost = MIN( smallestCost, totalDistance( count, crabs, i ) );
}
printf("Smallest Cost: %i\n", smallestCost);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day8/Makefile Normal file
View file

@ -0,0 +1,10 @@
solution-part%: solution-part%.c
gcc $< -o $@ -lm
test-%: solution-% example.txt
$< example.txt
run-%: solution-% realinput.txt
$< realinput.txt
.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2)

10
2021/day8/example.txt Normal file
View file

@ -0,0 +1,10 @@
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce

200
2021/day8/realinput.txt Normal file
View file

@ -0,0 +1,200 @@
cdeba gadec dafcgb bedg bd cdb cdebga fadecg adcfbeg bacfe | aedgbcf gcfead dbacfeg gecabd
ef fbceag dfbe cef cdbae gfadc cabfged debcga aecdf cebfad | abgefcd cef bdfgace dbfe
cdbgea eagdfcb fdbec decfga fbca bedcfa fc efc dbaec bfgde | bcdfe deagcb ceagdbf daebc
baefgcd bceda bedgca egc cadfeb cagedf abgfe cg gecab bgcd | eacbg fdgbcea gefdacb dbgc
ea dgcbef bgae dgebaf geacfd aefbd gefbd aef adbfegc adcbf | fae ea dcgbeaf ae
febd egcbf fgadec cbe ecfgd eb gdbecfa ebdgac gacfb begfcd | dfbe ceb bgacf febgcd
caf afgb fdbcag fcgbead gacbd gadbce efcdb efagcd af abcdf | fa fca caf gfab
gcbde fdceb agdbcf bcgfaed gd bedcga cgd cefagb bgcea egda | gd cadfgb dcfgab gd
cebdg ebaf bca cdeba ecabdf ab dgbacf fcaedg gdabefc cadfe | gdbacf bac fgdaebc eafcd
gbda gcdef ebacg ad bdfcae agdceb egbacf dgcae dbafegc cad | efgcd ad begcaf agebfcd
fgb cgfba ecdbfga agbd eadbfc dafbc gfbdce ecagf gb cagfdb | dgacebf fcbda gb afgce
gabfdec fdabc daeb bd afebc bdf adbfec fdcga fgeabc degfcb | dbcfgae gcebfa fbd bfd
cgf ebfdc dafgcb fcdegba acgbed gdbcf ebcfga fg fgad dabcg | cgf fg dfag fdga
gbdfa eafgcd aefdg adfcbge egbfad bcagd dbfe bgeacf bf fba | fb bf fb bf
afgbed fg dcgeba fgb abfcd bcafg baceg cegf afdgcbe gcfeba | adbcegf gf cbefdag gf
ebgca ega bcdage agdfbe cbged cgad cbfdeg ag ecdfgba fabce | eafbc dgbcae bfdgea dbceg
ebcadf dgaf ebgad debfa adfebcg gebcfd dg baegc bgd efgabd | afgd dg defab gd
bfgca cfbged dgc dgbaef efdbcag bdec dcbfg dc fagced gfbed | cd dcbe gcd cdgbafe
gebdacf cebdag fda egdba egadf fgba fa agfedb fedgc acdfeb | debcfa decbfa fgdbace daf
cdbfe ea dfgceb gdbcaef debafc cgbaed afbde eacf aeb abgdf | ae dagbf ae beafd
dfaebg fegbd ebfdcag cdgbf eb fedgac abfe edfga cagebd bge | egabcdf cfbdg bafe defgacb
gdcbfe gcd dfecb geadf gc egbc cfeadgb cfdeab gadfbc fgdec | egbc abdefc ecbfd cgeb
bdeca ceabfg abg gfeb afgbdc dagfebc bg efgca gabce egadfc | fbdagc gb gfeb fegb
cfe dgebc bfcage fbeag efgcb eafcbdg gfedba faecdg cbaf fc | cfe ebgaf cbfa cbgfade
egdbac fe deagcbf gdfbae dcagf fae eabdg edgaf fabdce bgfe | fae edfag gfbaced fea
fcbaed gbdef bdegaf becgafd bef efag fgbdca cebdg fe bgfda | fe fagebd ef fe
edfcg ecfabdg cefbg bf bcf bdagcf aegcdb cgbae afgcbe ebaf | bf fb gfecdab beaf
gdefcab caefb gfceb fedagc gcfde gb gfb fdagbc dgbe dgcfbe | bgadfc fgdcae gb fbg
cbfgda fegbca eagbf cgfdbae cg egdabf defbc gcb gcea febcg | ebfadg bfdaeg gace afecgdb
dbeac adbgc fcbeag bedg aedbcgf adecbg abe be faced cfgabd | bcade bae be bae
ge cegab bcafg bcefdag bge dafbce becda acegbd decg edgfab | agebc ge ge ge
gefa dbecgf abgcf fbdca bcaegd cag ebcfg egdfbac egafbc ag | gcbfe ag afbcg gac
cgebf ba fbdgae cbgfae bagc baf eabfdgc gfdbec cfaed cbafe | efcbdag cbdfeg gabefc feagdbc
dcbeaf fdabgec ac ecaf facbgd dac gecdb badfe edagfb bcade | dagfbc fcae cfea acdeb
cgdbfe facdg bfaceg caefg eag dbaefg ea egfbc gebdcaf beac | fedabgc egbacf aegcdfb gacfd
eabgc abf bfagc cefbga aebcgd fdgbc efcgabd efca deagfb af | gceadbf fbgaed gbcfaed dfgbc
fdaecb cabg bcafe gfbde fecbga fgeba eag fegacbd ag cadfeg | gbafe fegcab bdefg ag
ad cdeafg fdcgbe afgeb cfgbdae fabdgc dag fgead dace fedcg | fbgdcea dga aegfd fdaebgc
gabc dagfec cgdfb ga fdabg bfgcad fbade fag ebgcdaf fbgecd | cbga ag fdbcaeg fga
gabf dacgb bf gfedbc deabgc afbdgc ceafd dabcf dfabgec cfb | bagf fb cfb egadbc
dbgf bdegfa bf egacdb adegb bacfeg aebgdfc ebf afbed adcef | bf abedcg dgabe fbead
gdebcf acfegb fecgd ced dfbgaec edgaf cd gbfec gcedba cbfd | edc cbgdafe egacdb ebagcf
bdgfeac cegb gcadef bfedc defbg gfe befdca gbfad ge defcbg | feg fge egcb gfe
adfgec cbgef cgbfa debc dcefbg fbcaedg eb bfe agfbed cfdge | ebf fegdcb fcebg fdegbc
cabdfe badcf fbce fc cdf gecdfba dabcg gadcfe abgfed baefd | dcbfea dafeb dacgbef fabegd
caegdf bcfdg ga agd eagf cbfdage ecabfd ebdcga cfaed adgcf | efcdga gda adfceb agd
fedbca ebdg ebf cfbeg bfdceg bgfdca dfbgc gebfcad be cfage | dgcbeaf ebf cdfgab cbgfe
dgfbec ecbafdg cg cge efadgb egfabc gfac abgec dbaec agbef | ecagb agfc gc cfag
bcdae fgbedac fa facdb dbfcea aecfdg cfa faeb dgfcb dcagbe | ebacd dbcaef fabdc afbced
aedgcf fagce cadbg de dfgecb fead egadc dec dcafegb bgface | dafegbc de dec dbfecag
ecfab fegbd agfbe cebagd fgac dbcafe ag bafgec gea fdegcba | gdaecb bdafec aedcfb dbegf
bcdeg ega fgda gfabce afgedb baedcf beadg bfaed cdbefga ga | gdbae fbdgea ebdaf afdg
bgfdae ecbgfda bagec cgeaf ef dfacg defacg fdec egf dcfbga | dafgbec facge fgaec ef
dabef fgbad becagf fcbag gadc gd cgefdb gbfedca dbg gfcdba | gdac dcafgb cagbdf dg
bg gbdec abedgfc ecdbga ebadc bgda bfcage gbe dgecf cdabef | gcebd edabgc dcegf beg
bcdfea gd degbac adebc dgac cgebf fcgadbe gdbeaf gdb dcebg | dceba dgb gdca agcd
ad cfdaeb gdecab bgfdaec fead edcfb cebgdf cbdaf abd bfgca | dab adb gdecbf adb
gcfbd egcbaf cbgde afcdeg fb cbf cadfg fdbagc afdb efdbgca | fbda fb fb fbc
dfgbce cbaef ecdbf ca beafg dcaf bca cfaedgb ecafbd geabcd | fcaebd fcad afdc edfcb
becgfad adcfeb bg fbeagd gbf gfced badgfc abeg eafdb fbegd | bage gb bg gbf
acdebg faed fgeabcd cafge fca fa dgafce fbcge gabdfc gcdea | ebdcag fa af cbgefad
facebgd bfacg dcafe adfbc cfabed cdbe fcadge dba db ebfgda | cgbafed db fdcae fgbac
debca bacfgd gaefc adfcbge febg cgfaed fb baf ecafbg afbce | debcafg fb egbf fgbe
ecbdgfa cabfde faeg afdgc bdcga dcfae acfged fg gcf bcgedf | fecdgab cgf dbagcef fg
gefb edagfbc gdceb ge fecgdb dbcgfa gce fcgbd aegfdc bdaec | fbge gbfe egc fgcdb
cefgab dg dfg cegfb dcefag fgebdc dgcb dbafe afdbgce fdebg | dgfecba fcbedg fdgeb dgf
gb dbfg acefg agcfbed cabdef edacbg dbfca bga facgb bagdfc | ecbgafd bg gb bg
bdef bcgfd cbefdg bgceadf agecb fcgdea ecf efbgc afbgcd ef | cef febdgc fce fce
fegca egcdbaf gcfeb be agefbd aceb adcgfe geb eagbfc fdcbg | abgfdce ebac beg aecb
cabdgf dgbafec fedc egd agcbe dbcgf de bfdecg cbdge dfebga | efdc cdfe cedf bdcge
abgedc acfge dceafbg debcgf fd dfbe cdf cdabgf fgcde edgbc | edbacg agbfced dcf cfd
dgecf fcagdb fb gfbde gbf bgecdaf efcgad ecfb gbcedf dbaeg | gefbd bfg bgf efbc
dbgc gdcefab gcf gfdcab fceba aefdgb efacgd gcfab gc dabgf | bagecfd gacfdbe gfc aefcb
fb ecdabg cfaed fbegda gefabc bcgf febca gabec agbefdc afb | cgebda bfgc cafgeb fdgabce
efbdcg ceadgb fb fcb fbcda egfcabd fgcad abef dcaeb becafd | acfbd bdacf feabdc cbdfgae
cef degbf gcdefa cf bfca eabdc cbdfe caedbg acebfd efadbcg | febdc cedgab efc cef
edfabc efcab cgdfae efbdg ecfadbg acbd da bdafe gfaebc eda | beadf da fegbd aebdf
aebcg cef gfea gcbafde bedfac bcefg gdabec gcbaef fcgdb fe | gafdceb gbadec decafb cfe
acdbg gefbc decfab bcedag df gbcfd dafg cebagdf fgcbda fdc | df fdc dgfbc cgbdf
cg bgfca egbfa fgcaed acg fbcdae gcbd abdfc cafgdb fcegadb | bcagf abegf acg gcdb
ebdfgc geabfc facd ca adbce cdaefb efagbcd ecfbd acb dbeag | dacf bgacfde fadc dfbeac
cedbgf fagb gfabec fbdecag gbcae gf eagcdb aefcd cgf efgac | egbca aebcdg egcba eacbdgf
cgbdf db gdcef febacdg cdb bfed bfcgde bcagf gbedac defgca | cbd cabfdge bdfe bdc
cba egab gdbafc bcgefda ecgdb ba gbdeca cgedfb fdace aedcb | ab cab fcaebgd adecbg
cgdfb agbde cdgeab gce bcged agcefbd fdgaeb ce bfacge acde | ce cafgeb fdgcbae dgeafbc
fgbda cdfeab fgdaeb fgaed cedagfb aef bfcgda bgea ae dfecg | afdbg eaf bfdag eaf
fdgce gbcaf ace dcafgeb afecbg dagbce agfdbc ea eafcg ebfa | ebfagcd ae ae cafgbe
adfbgc dcgfbea efbad bef dbfcae cfae ef bcfda dbaeg bfgedc | fe bcadgf efbdcg afbcgde
ed eabgdcf bdfca aedg fecag ced cbfage aecfdg dgfebc dceaf | cde deagbcf cdbfage adge
fabc daebcg bdgecaf ca dcbgfe gbaecf fgeda acfge efcgb eca | bdegca ac ca efadg
ceagdf ce eadcf eacg edgafb dce ceadgfb defga abcfd dcgbef | ec dec fdgbace adebcfg
baf bgfdeca fa cgfaeb aefg bcafde cbgfd fbacg cgbae bacdeg | fbagc ceabg agbecf af
fabd db efcad debacg fbgce bdc ecdfb caebfgd egfdac bcfdae | dcb bcd bd dcbfe
dge dbeag acedb fcgdeb bfeag dg gbfeda fagd facbeg bfecgad | cabed egabf bafgcde edg
cebgf cfed gcdfabe cdgbfe bec ec fcbdg aebgf bcdgae cgdbaf | cdgbf bacefgd egfadcb fdacbg
baec cdeafg ce adgfcbe efabgd febgca gfbae dbgcf ceg fbecg | fgedab defgab bace ebdfgca
aebgf agefd eadcg dfab bdgafe efd dfgbeca fd fcdgeb fbaceg | dbefag df eadcg fd
fdcbe defbg gacedf gabe fgcbeda gb egadfb gfb gcabfd edfga | dgfbcea ebga fdgeabc gbea
gea bgadef fgcbead febcgd ae afde egabd cfgeab bfedg dbcga | eafd ceagbfd ae fcedbga
ebgcadf feacg dfbcag dfcgbe gefcd edbf de cde bgeadc dfbcg | dgecab cdebga gfdcb bfed
bdgfa egdfa afb aedbfg ab becafg beda cgafed fgdeacb dfcbg | fcgdea baf daeb aedb
cdgef ceafbd efadcg acgfd geadbcf ed gafbcd gcfeb aegd dfe | edag fcdagb edf cbdaef
fgcd gd ebfagcd cgfabe gcabdf dfeagb adg aebcd cfagb adbcg | cdbea bfgdace cfdg gd
gdafc dgbc cadfeg dbeafg acebf fgedabc dabcfg gba gb cgfba | gb egfdbca defcbga bg
bcedg aec eacgbdf cadge bcadfe dbgecf ebgdca ca fgdea bcga | bcag ca cbedg ac
dacbgf gc fcaedb egbfa dbcg fcaebgd edfcag cgf cafbd bcgaf | dcbg gfbca cdgb cadgfe
dcgabe eaf fgbaec cfdae ecadb fdgca ef fcdgeab dfbe cbadef | ebdf aedcgbf fe fae
edgbcf fe cegdfa ebcdg ecbfd gfeb facbd cef bcedag efcabdg | gabdce gdcbea ecf ecf
gdcab dgbcef fgdecab ca decfga gcdbf dac fcgabd debag bcaf | cbgafd cad facdgb facedg
afgec bgfaec bdefg dafeg dbcafg fgbeacd caed da dag gfecad | cade adce edca dgebf
bfdceg cbdgaf ec ecdfa egac dec dgfac deacfg agfcebd efabd | egca caegfd dec cgea
adfecbg gcfd afc fc geacb cdfeab ecagf gedcfa edgaf agfdbe | cf cagbe afc edbfagc
ebgdf abgec dabf acgdfe agd fbdgaec egadb da ecgdbf agefbd | ebadg ad da gad
ebdgca dacbf cb dafcbeg cbd afebdc ebcf cdgaf bfeda fadgeb | bc bcafed efcbadg fceb
agbfdc edag gafdecb cag afceg ga ecgbf aefdgc bdcfea cadef | ecgaf gade cag gac
cegadb gecd bgacd fdcaeb dbfag cg acbed agc bafecg gaedcbf | agc gacedfb adbgc dgec
cegfdab edba dabcg ceb egcdb bfacge dfecg cebdag fgabcd be | bec be dbea cfgabd
gacbfed gbfdac dfgeba edafcb gfd gefcb dg deag dfbge bfead | gfd aefcbgd gedbfca fgebd
gfcdab da afbgec adbfg afd gebdf fedacb cabgf dcag acgefbd | gfbad cdfaegb dfa cfbgad
gebafd fbgedc adgcfe cfbg cfd bdegf ecdfb gacdfeb edcba cf | fgebacd gfcb cdf cf
adebg ebdgac fadbe gebcfa gdfa dbfce afb gaefdb dgfabce af | fba afbde fa cgbaef
fcbead fc ecf acdeb cdegba fdbc fcedag baecf eabgf eabdfgc | ecf aedfcbg deagcfb cabefd
eba gbaedc bfacd fead ae fgabdce begfc dgcfba fcadeb faceb | bae dbagecf dfbac fadcgbe
edf bfeda dgface gbef aedgbc agdfecb aebdg acdbf ebdfag fe | edf fed gfeb begf
dcfabe gceba dbecgfa gbafe abf fcaged dfgb gaefd agdbfe fb | afb abf cbgea adgcbfe
bdgec fbe gefa acebfdg febgad abdgcf adbfec gfabd bedgf ef | ebf bfaedg ebf fe
debf aebfcg badcge dec dfcga gdbceaf ceafb ed edcfa debcaf | fadce afgbce fcdbgae dbeagc
gbfe cdfea febad fb egfbda fba agecbd dcgaebf bfacgd dbage | fgeb fbcaedg bf gefb
gecfbd gfdeab cgaedf fedcbag gbaf fb bfe dbfae bcead agefd | daecgf bfe bef bgaf
cfagd ec bcfdeag edagc decf bcafgd ceg fgcead gecbaf dbeag | ecg dacefgb baedg cadfg
be bedfga fdagb efagb cagfbed bge gdcfba ebdacg efdb eagcf | ebfdag fbed adfgb eb
fcbaeg gfb gcfbda bg eadgfcb edgfac bgadf cagfd deafb cdgb | fbadg bgcfda cbdg gadbfc
cbdaf db daebcg dfacbg fdgca aecgfd fdgb gbfdeca cebfa cbd | bfcea bdc cagdfb fdacb
gdfea debfa abfdgc dfb adbec fbge fgedca dfbcega bf dgefab | bf bfd fb baefd
baedcg egdba ea dgafb abce gbdcefa cebgfd aeg ecadfg bgedc | ae dabefgc ea ecba
dcafb bfdcg edbagcf bafdgc begafd gfac gf cafedb fdg bgcde | fdg dbfgc bdfeac gcaf
ba bac fdceb bedcgfa dafceg aecbdg fabg gdfac cdfba bgcdaf | gfadecb ceadfgb dabcegf ab
efgbdc decgb fecbd ebcgfa degf efc ef fbcad bgdaec gfdacbe | efc dbacf fe cgfbea
edafbc dgea egcbd fcegb ebdgcfa dcaeb cebgda gd acdbfg dgb | gdb degbfac ebdcg eadcb
efcgba becgf becdfg agc cbae fcbadg fgeca cgdafeb ac gdafe | abec gcfebd ac abce
fdc gfbcad cd fcadbe cdeb bedgfa efdab cegfa gfadbec eacfd | dceb bfeadg dcf bgecfad
dcb dbgfea cgaedb dc bacde begda acbef ecdg defgabc cdfgba | cedg dc bdc deacb
dbeafgc afcgb aefcb acg gbfda bdcg acgfdb cg cagefd begafd | cg gc acgbdef dfeacg
ecgbd fedgcb abgcd ebfdgca abgcf ad febgda deagcb eadc abd | dgecb da bedacg bacgf
cgfad afdec dcafbg efgadb dg dfbgeac gdf begfca dbcg afgcb | cafgb gdf ecbdagf bgdc
bf edfbgac fcb cadbgf gabf dceaf fadbc bdcga cgbade gcefbd | ebfdcga fb gdfcab agfb
gcef cagbfd edbcgf edgbf ef efb gbfcd eadgb edfbca gdfbcae | afebdgc fbe agbcfd abdge
bged fbaec fdecg eacfgd gfbec gb bgf cafdbeg gefbdc bgdcaf | gb fbagcde dgeb bdeg
gacefb gacfdb dgcebaf ac bagef bfaged egca efdcb acb fcbae | ac ecabgdf eagfb cgea
cbegd gfaedb bfgde ec bce efcd ecabdfg acdbg dfcbge bfaecg | cdef efdbg gfdeabc gdbafce
gebca edcagb ceafbd cde ebgcaf adgef cd cgdb aegdc dfgceab | fbedgac cd edgbafc caegfdb
cadbfe adbg cagbefd gbdace ebd fcebga cbeag fcedg dcgbe bd | bd bd degcf dfcaebg
efgabc agcedf cea gcead ec aedfg adgbef beacfgd bgcad cefd | adfeg ecgbdfa edfc ce
gcdeab aegcfd dacfbg fecg edc fedcagb dgfac ce adecf afedb | fdacge efacd cgabde bdeaf
fc facgeb eabgf cfb gcfa cgfbe edagbf afecgdb cgedb cedfba | cagf bfc dfbega ecbadf
gfeca da gfaecb fad cbefd fdcbgae beagdf ceadfg afedc adcg | fad cdebf cdga dfa
gdbace gabdfc fbdge ceaf cfadb ebc becfd fadceb cgfadeb ec | ce ecb fcbed ecgafdb
cfebad adfegc befca bcdag afd efdb fd gafbec efcbdag fbadc | fda fdeb fda dacfgeb
befgcd adgcbe bdgea fdg efdca gf fdacbge eagfbd efgda bfga | abgde gdf cgaebd ecfad
bgfe cbgfda gcedab defac fg eafgbc agceb bcdfaeg cgf cgefa | adbefcg gbfaec fbcaeg fcg
gfdacbe fdaeb afg fg fdbaeg gfdb cegba efgba cafdge ecdbfa | gfadbe fga afg gaf
dfgea eagdcb gfaec gedafb fadb dgbcefa ade fgbed defcbg da | ad da ad da
bfa bcfga ecgfab cfage cagdbfe gdecfa fdaecb bf dcagb gbef | ecbadf gacebf fab cfgbeda
bafedc fbace bfeg ge gecdab dcgfeab fgeacb ceg caegf dgafc | fbeg ceg eg eg
edgcab dafeb fad cdbae df acfdge bdcf bfgcaed aegbf bcadef | defgcab eadcgbf efbag adcfgbe
egafb afg egac gcfbe cabdfg fbdae ecfbdg caefbgd cgeabf ag | fegab agec fgcebda egac
bfegdc cdg gc gcfb dgaecb cdfeb gefdc egfad dgbefca acfebd | agdceb bcfegda gc cfbg
ebc edfcb gcadbfe bgde facde bfcged dfagcb be fcdgb aecfbg | cbe fbcdg bce eb
adfcgbe fbda dbeag gbdefa dfgcbe agcfe gedfa adcegb fd gdf | gfd fd gfeabdc fgade
fgdb degfca fgcdbe cdfeb afdgbce dg ceagb cdg ecbgd beacdf | gd cgd dcg bagcfed
baedfgc ade eadcf gdaf gface da dbcef egdafc eacbfg bagecd | ad afdg dea dea
acfdg aedbcf beagdc ebdfa age gebf eagfbd ge gdcbfea dfgae | bgcdae ega abdgfec gbfe
efbdg cdb edfac bcae gbcfeda adegcf bceafd abcgfd febcd cb | aebc fdaec gcfabd cb
bf fbgdc bcedg acedbgf bdafgc adfgec cbfa dbf befdga dfcga | dbf fdcega fbd debgaf
fecgba bedca egfdcab dg bcgdaf afdg bdgac gfedbc bfgca cgd | bgacf cagdbfe gafd cgfebd
gf fedg afg dacfg cgaebf bafcd aedcgb gcedaf geabcdf dgace | gfa gf cbaged cbegfad
afegd bgeadc ebcgaf gedac dc cgd cgbfde ebcga cgfabde dcab | gfabdce dc eadcg abcd
cdage df edafg baecdgf gcedab cafd egabf fed fadgce cdbgef | fd df fd def
agdefcb acdbge bdga bg abcegf dfgce gdcbe cebad ecadfb bgc | bagd adgb dgafceb bgc
bgdea cbegfa efdgca gfcebad bgdf fgeab dagefb eacdb deg dg | gd dg ged fgdb
adgfc afebdgc gaebcf cbaefd geba dfecgb bacef gce eg fgeac | dagfc gcfbdea efcgab gcafeb
gcfd fgedcb ecgbafd agcebf gbefc afdeb dbefc dc dcb gebdca | dfbce cdfg efagcbd gfcd
acgf aegdf gcdeb egafbd ceadbfg eac ca aegcd ecdbfa fegadc | gfdae gfbdace defag cea
fbgcda ecdbag degfc fg efbg edgafbc cgdbe fcdae cgf befgcd | gfc efcad bfegcd dgbcaef
df dgcae fdgcba fgbca fbcead fdgca dgefcab afd afbceg gfdb | df bfadcg dfbagc df
fdbe egacd bcgfe aebfcg fd decafgb fecdbg abfdgc fdc cfedg | dfcabge acged edfb df
ecfdg eg dfbcg fdcabg beafgd acefd efg egfcdb gcbe dbfeacg | dbcaefg eg egf gef
bdgfea dgeca fc cdbfge bcfgda gbfdeca cbef edfgc cdf gbfed | bfdcge dcf fgbde fecb
gcdfeb cgfabe cgbfe egcdb dbfe cdbgfa bd bcd aedfcgb aecdg | db fcageb gfbeadc bcd
bagec bfcg fceab adgcef fedab agbced gcfbea deagbcf cfe fc | ebacg gdefbca bfcea fce
bacg gae dbafcge becfg ceabfg efbad ga egabf fcgebd agcfde | dbaegcf aegdfc gcba abgfe
daceb dfgc bgdfe gdbec efdgba acgefbd abcefg bcfegd cg bcg | ebfdg bcfage dfcg cbdea
dbagf dfec agced cgf fbcgade cf efcdga decagb adfcg fabgce | edfc fdec fcde cfde
gcdfa dagcbf agcfb fabegd gd cgbd afcde fgaceb fgd afgbdce | gd gd dgbc gfcdeba
ecfd dfcagb ebcga ebdgf bcafdeg cf cfg bdgaef egfbdc bcfge | fbdge cf begca bcfegad
da gdabfe egdfa gfdbca dcgfeba gefdc deab fbgae fcabeg fda | fda ad dfa beafcg
bgdfce ed dcebfa febgc gedfc cfgaebd gbde fgecab cde acgdf | dgecf bdeacgf dec ed
gcde ebcfgda dbecfa cgfbea gc gfc fgdace dfagb gadfc acedf | abdgf abecdfg cgde gfc
bgeda efcgbad dbcaef fcaegd abd bfegda ab gcebd afgb eafgd | ab ba ba fagb
cebfa aebcfg cgbfa egbf ceb cbgfeda ebcadg bfagdc eacfd be | ebfgac ebc bagecf gfeb

View file

@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
// This will hold the number of each count
unsigned int counts[8] = {0};
// 4 output values, each with a maximum length of 9 (8 + \0)
while (!feof (fp)){
char output[4][9] = {0};
fscanf(fp, "%*[^|]| %s %s %s %s\n", output[0], output[1], output[2], output[3]);
for ( unsigned int i = 0; i < 4; i++ ){
counts[strlen(output[i])]++;
}
}
fclose(fp);
unsigned int sum=0;
// 1s have a count of 2
sum+=counts[2];
// 4s have a count of 4
sum+=counts[4];
// 7s have a count of 3
sum+=counts[3];
// 8s have a count of 7
sum+=counts[7];
printf("Answer: %i\n", sum);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

158
2021/day8/solution-part2.c Normal file
View file

@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
unsigned int countCommon( char larger[8], char smaller[8] ){
unsigned int common=0;
for ( unsigned int i = 0; i < strlen(smaller); i++ ){
if ( strchr( larger, smaller[i] ) != NULL ) common++;
}
return common;
}
// This will return true if the signals are the same length and contain the same letters
// Order doesn't matter though so ab if ab and ba are given, it will return true
bool sameSignals( char first[8], char second[8] ){
if ( strlen(first) == strlen(second) && countCommon(first, second) == strlen(first) ){
return true;
}
return false;
}
unsigned int decodeOutput( char signal[10][8], char output[4][8] ){
// This will hold the mappings.
// I.e. the string that should output 0 will be heald in mapping[0]
char mapping[10][8] = {0};
// 0 - Length 6
// 1 - Length 2
// 2 - Length 5
// 3 - Length 5
// 4 - Length 4
// 5 - Length 5
// 6 - Length 6
// 7 - Length 3
// 8 - Length 7
// 9 - Length 6
for ( int i = 0; i < 14; i++ ){
char *source = ( i < 10 ) ? signal[i] : output[i-10];
switch (strlen(source)) {
case 2:
strcpy(mapping[1],source);
break;
case 3:
strcpy(mapping[7],source);
break;
case 4:
strcpy(mapping[4],source);
break;
case 7:
strcpy(mapping[8],source);
break;
}
}
// When we get here, we know what 1, 7, 4 and 8 are
for ( int i = 0; i < 14; i++ ){
char *source = ( i < 10 ) ? signal[i] : output[i-10];
switch (strlen(source)) {
// We can determine what 2, 3 and 5 are by looking for numbers with a length of 5
// If it doesn shares all the same digits as 1, it's a 3
case 5:
if ( countCommon( source, mapping[1] ) == 2 ){
strcpy(mapping[3],source);
} else if (countCommon( source, mapping[4] ) == 3 ) {
strcpy(mapping[5],source);
} else {
strcpy(mapping[2],source);
}
break;
// We can determine what 0, 6 and 9 are by looking for numbers with a length of 6
// If it doesn't share the same digits as 1, it's a 6
// Otherwise, if it shares all 4 segments with 4, it's 9
// Otherwise it's 0
case 6:
if ( countCommon( source, mapping[1] ) != 2 ){
strcpy(mapping[6],source);
} else if (countCommon( source, mapping[4] ) == 4 ) {
strcpy(mapping[9],source);
} else {
strcpy(mapping[0],source);
}
break;
}
}
unsigned int decoded = 0;
for ( int i = 0; i < 4; i++ ){
unsigned int multiplyer = pow(10,(3-i));
for ( int j = 0; j < 10; j++ ){
if ( sameSignals(output[i], mapping[j] )){
decoded += (j * multiplyer);
break;
}
}
}
return decoded;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
// This will hold the number of each count
unsigned int total=0;
while (!feof (fp)){
// each didget has a maximum length of 8 (7 + \0)
char signal[10][8] = {0};
char output[4][8] = {0};
char ch;
char str[8];
for(unsigned int i = 0; 1 ; i++ ){
if ( i < 10 ){
fscanf(fp, "%s%*c", signal[i]);
} else if (i == 10){
// This is the pipe character - we can discard it
char pipe;
fscanf(fp, "%c%*c", &pipe);
} else if ( i < 15 ){
fscanf(fp, "%s%*c", output[i-11]);
} else {
break;
}
}
unsigned int decoded = decodeOutput( signal, output );
printf("%i\n",decoded);
total += decoded;
}
printf("Total: %i\n", total);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

10
2021/day9/Makefile Normal file
View file

@ -0,0 +1,10 @@
solution-part%: solution-part%.c
gcc $< -o $@ -lm
test-%: solution-% example.txt
$< example.txt
run-%: solution-% realinput.txt
$< realinput.txt
.PHONY: $(patsubst %,run-%, 1 2) $(patsubst %,test-%, 1 2)

5
2021/day9/example.txt Normal file
View file

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

100
2021/day9/realinput.txt Normal file
View file

@ -0,0 +1,100 @@
3567891987643457898879876567896543567999831045697654564567923989994345678987678789732349887675978921
2458910976432179987568965456789732679898753234598753213459205978989234899875467679549498765434569530
0199891987651099999699864346678901236799874447679832102398919867879345698685353568998999654323678991
9987689997543988942987954244599932345679975998799993219987998756567956796543212457896898765512356789
8876599898759867899876543123989893456893989899998789398996789645458967989954563568965989997434667990
6932465779998656789998756439876799578902398799987678987975696532367898979797678979654979876598798991
4321234569876549896569968998765698989653998698994569876564589643456799867689989989769865987679899989
5210156789965323597459879998874587898769896587989678995323578956567989754567891299879954598789998979
4321345899876434589567999987923456789899765445678999986414789967979876543488910198998767679899987867
6632397965987545678999999876212345693987654323567912398924567899899989764579329977899879795967986459
9543989894299866789987898754101656892198765214568903459435898979798999875789998756989999893456796598
8759878792101979897676987643212567894239899399979214567986789765687989989899889549878989901349987986
9898765689212591987565698654323459964349978987899935978999897654245678996947679698769979932598999995
9998654569423492989434599765434567895598867456987899899423987542134599875434568989654667894987987894
9789743458975989876325989976545678987697656349896798778912499651013987997645679876543458965976876792
8698652456896978965439879898876799398789543298775986567893599973129876798958893976402979976965345891
7597543467899767896998965679998895479997652109654323479994989984234984589767912993213567899879866920
6498794698998457989876534569999976569876543299865212569989876797545999679878929854323989924989977891
2379898789876345678921024678987898789987856987654323457976545987659898989999798769574567899798989999
4456999899765456889532125699896789898698997899765434567899432398798767897535699998765689987657492478
5578999921996567897656236989765678987569798999876645698957921239987656789649987659977799998743201367
7689689890989688998798759876524589765456659789987786789546890149876545699898999543989999876543212457
8994598799878999339899767998435678976342345678998987893435789349765434569976898932395699987654326568
9012987643456797123902979876549889887201956889769698942329895467976787678954967891234989999965437679
2179876732367896014594989988659997654319899998754569321018799578998998789123456789349879899896548989
4359865321298965123689892198768999766598678997653698932147678989859999896346569896498765789789679997
9498763210245954349799651099978989898976567896542987654234567898745899965487978976569654345678998986
8987654321239895498987543987989679999432459789693498765445679997656789876568999987698786467789987545
7899875434398789987898659876594568987991234678989999876656789998769897988979989998789987578999876434
6531989546497678976549898765423679876789545789767898987987898999878976599989879989999898999767997645
7432397659986568997932999976512398544897656899959987598998987999989765322398769879898769456989498867
6543498798775457889431098987624987632369767929898976439569976789999854310989654766789654349894329978
7654569987656345579943987698899764321248978909797895323457895678929875929876543245678921235789412399
8765679976542136467899876559987654320136799998676789214568954567919989899877610176899420126897543999
9876889987431012349913987432199766872345789987545795323459543456798998769754321398999321247897659897
2989999987542123567899877643019898953489899876436989435678954597897899659876432345678992348998798765
3599989997654344567998765432123989876578998765425678956999978989986899543989546797789989459359899954
4698979998865455878939998765234568987989987654314567997896989876775678932397656898899978978998999965
5997767899876666789323459654345689798995498765323456789985398765434567891239789999998768999887989876
9865456912987989895457998767858789649689349986465589899965269894323788910199898989987659998766578997
8754319899899198998667919898969891234589237697877679998764356983212669921989976878999846899542467898
9865456789765347899778929999989910346679104598999897659975569862101457899876795467897656987431298999
9976777895986456799899998999998434457895323679876998943498778954212345678965789346789879876540139898
8998998954987567897956987678976595978965455789765689899999899965323486789654567999897999987621998786
6659989543298998965344986567897989899876576897643476798789939876765797899865678987986567898749876545
5349867994039579999123975456789876789998697976532365698699421987978998949878989896575456899656987634
3298756889123459878939765345998765699998789765431234986578932398989459134989898765452345698768999523
2197545678939967967899874239877674578939899998420125987899543999797678999795698654321246789879998434
3986434567897899879997985645965432349649989987531349998935959898689789987654239765530124578999997645
4994323978976799989876976759878653468998767987642498989129898769456998998562123987641436789999898867
9875413899765689998765987868989965678987957899999987778998799954349887789421045699532647999789769978
9975326789984568949984398979099876789896548789878996567897659895459765678932156798743498987698754989
9875435689965989239876239989299987999765435699767423456998946789569874567893278897659999999549865998
5987749899899892049954345799989998998843323789654312599899834596978983479954399998998789899432977897
4597656998754679198765456789879789976532012698943201987689656975989994567895989879549697678953989945
3498767899843598939876567899765679865432124567895329876598967894699865678999876768998546568964590234
4569878978932467921987798999654578987543236689997435985467899943457988789998765449876323479987689345
7699999568901357890298899998843459899854457899876557897589965432356799899989854333985454567999789499
8789123479953456789349912997672699689765698912987668987699975431234898999976543212987565678999899978
9893234567894568997999439876531987578976899993498789098967896945949987678998765323498686789889999867
9965445679965678996788998987610398689987901989569892199656789899898796569999965434569797895678998756
8987896893496889985437987896321239789998999778999943987547898788789653467899879545679898934789349432
7898987942987999876545976895434549899999987656789994976432987656679732567893989698789999655891296551
6799898931998979998959875689545856999889976543499989984521298645578901246932198799898789867932987867
5689769649899758789879754678956767897679865431299878993210985432459913459951099912987678979999898978
4569998798799645678998763456968978987567987320987656789929976421567895598892989893976568999989769989
3498899899698934569898542347899999995439876459876545677898797533678999976789576789865459789878943294
2376789987566899689797655458987999986320998767966431256789698754789998764593445899879345698767892102
1235999987345678998659778969895498765421239978954310137996549965898889865692136901998976789456789264
2348969876459799997549889879789329876432357989876521245894321986987678976789029212987897891347894395
3467942987578899998634999989699567999943569795987432656789210197896546987899198933986789930456989989
4578931298689999899324799996588989987894698654599545867895423298965434699998987899865578921239879879
7679549499895398765435678965466898776895987542349657978999564569876986792397976798783467893398767956
8989698989943239986586889984345789654997899651298789989398979678998997891986545987632348965459854543
9998797678987458997697999875235799843689998910129891093297898999549049992398939876543567896598753652
6789986569896567989788987654356999754567897953256992129986767896432135689987824987654578987965432101
4579995445789879878999998976999898975678976894345789998765456798743546798776501298765989599876543213
3498986323589998968999989899878667896799875799556999899654345689656787987654312349989795467989754354
4987691012478987657898878789765456987899864678977898798765467899867899898766423756799654367899875565
5986543123567898946997654598654347998998743457898997679877568999998998769987534678978975256789988677
6987663248678999858987643569876767899597652368999876565989678989899987645697685989769976347999899898
7999854357789998767896542354989898976439763578998765424598789878678987656789876999659899869999789919
8998765469899949878989750123498969598929854689129873213659897654598998767895987898998789979898678901
9999879879999832999679971334597653469898765793234964302349953212387899878934699987987678997657567992
9987989989998721023457895499989432598789976789549875463598764301256789989123459876543589876545456789
8876899999987652134568999978678921997678998999799986654789975432345897993234567989542499987632345689
7654698989998543245979987856567939876567999102989899865678987543456976789345679898743459876721234678
8769987678987654346789996543459899987679789212976799976989398764689565678956789789954698765430123569
9998798582399765458898775432376789899889678929865689987993109865789454567899898697895899876521234567
3987654341349878579997654321235679765994569998654567898943212978894313456789976546896999989632376789
2498765210234989678998976492346789654323458999765679979764323989932102568999975435987898799843497997
4569886345695699899899989989959996543212356998986889768975434598754213479239894326798936598765567896
5699997556789789924689999877898799754901239897899997656899545679865674589998732107989323459877678965
6789898767899891013579888765789678969892398776798965345678959789996786679876545212378901266988789323
7998759978912942125698769434134567898789997654357893257789768999989899789987687354567892345699899212
9876532199909943286789654321023678987679876543234789357899879239878998990298798865679943466789978994
2987694367898765497898765432335789989467989862125678969987989498657897892989899976789955577895456789
3498986456789976567959876543566899865346995993013489878976597987545656789864945989898766698934349899
6569297678992988798943998678797897643219864987654899989497456986531248999943239899919897979210245678
9791098989321099899532109899899998856424965699766789994398577895432346898765356789323998964323345789

View file

@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
bool isMinimum(int rows, int cols, int grid[rows][cols], int row, int col){
// Check above
if ( row > 0 && grid[row-1][col] <= grid[row][col] ) return false;
//check below
if ( row < rows-1 && grid[row+1][col] <= grid[row][col] ) return false;
// Check left
if ( col > 0 && grid[row][col-1] <= grid[row][col] ) return false;
//check below
if ( col < cols-1 && grid[row][col+1] <= grid[row][col] ) return false;
return true;
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
unsigned int rows=countRows(fp);
rewind(fp);
unsigned int cols=countCols(fp);
rewind(fp);
int grid[rows][cols];
memset( &grid, 0, rows*cols*sizeof(int) );
unsigned int risk=0;
unsigned int row=0, col=0;
for ( char ch = fgetc(fp); EOF != ch; ch=fgetc(fp)){
if ( ch == '\n' ){
row++;
col=0;
} else {
grid[row][col] = atoi(&ch);
col++;
}
}
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( isMinimum(rows, cols, grid, row, col) ){
risk+= grid[row][col]+1;
}
}
}
printf("Total risk at lowpoints: %i\n", risk);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}

157
2021/day9/solution-part2.c Normal file
View file

@ -0,0 +1,157 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_RESET "\x1b[0m"
typedef struct Position {
int value;
bool marked;
bool lowpoint;
} Position;
int countRows(FILE *fp){
unsigned int lines=0;
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp,"%*c")))
++lines;
return lines;
}
int countCols(FILE *fp){
unsigned int lineLength=0;
char ch = fgetc(fp);
while ( ch != '\n' ){
lineLength++;
ch = fgetc(fp);
}
return lineLength;
}
bool isMinimum(int rows, int cols, Position grid[rows][cols], int row, int col){
// Check above
if ( row > 0 && grid[row-1][col].value <= grid[row][col].value ) return false;
//check below
if ( row < rows-1 && grid[row+1][col].value <= grid[row][col].value ) return false;
// Check left
if ( col > 0 && grid[row][col-1].value <= grid[row][col].value ) return false;
//check below
if ( col < cols-1 && grid[row][col+1].value <= grid[row][col].value ) return false;
return true;
}
int cmp_asc_int(const void *a, const void *b) {
return *(int*)a > *(int*)b;
}
unsigned int basinSize(int rows, int cols, Position grid[rows][cols], int row, int col){
unsigned int ret = 0;
if ( grid[row][col].value == 9 ){
return 0;
}
ret++;
grid[row][col].marked = true;
//Check above
if ( row > 0 && ! grid[row-1][col].marked )
ret+=basinSize(rows, cols, grid, row-1, col);
//Check below
if ( row < rows-1 && ! grid[row+1][col].marked )
ret+=basinSize(rows, cols, grid, row+1, col);
// Check left
if ( col > 0 && ! grid[row][col-1].marked )
ret+=basinSize(rows, cols, grid, row, col-1);
//check right
if ( col < cols-1 && ! grid[row][col+1].marked )
ret+=basinSize(rows, cols, grid, row, col+1);
return ret;
}
void printGrid(int rows, int cols, Position grid[rows][cols]){
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( grid[row][col].lowpoint ) printf(ANSI_COLOR_RED);
else if ( grid[row][col].marked ) printf(ANSI_COLOR_YELLOW);
printf("%i", grid[row][col].value );
if ( grid[row][col].marked ) printf(ANSI_COLOR_RESET);
}
printf("\n");
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
if ( fp == NULL ) {
return 1;
}
unsigned int rows=countRows(fp);
rewind(fp);
unsigned int cols=countCols(fp);
rewind(fp);
Position grid[rows][cols];
memset( &grid, 0, rows*cols*sizeof(Position) );
unsigned int risk=0, row=0, col=0;
unsigned int max[4] = {0};
for ( char ch = fgetc(fp); EOF != ch; ch=fgetc(fp)){
if ( ch == '\n' ){
row++;
col=0;
} else {
grid[row][col].value = atoi(&ch);
col++;
}
}
for ( unsigned int row = 0; row < rows; row++ ){
for ( unsigned int col = 0; col < cols; col++ ){
if ( isMinimum(rows, cols, grid, row, col) ){
risk+= grid[row][col].value+1;
grid[row][col].lowpoint = true;
max[0] = basinSize(rows, cols, grid, row, col);
qsort(max, 4, sizeof(unsigned int), cmp_asc_int);
}
}
}
unsigned int prouduct=1;
printf("The largest 3 basins are:\n");
for (unsigned int i = 1; i < 4; i++){
printf("* %i\n", max[i]);
prouduct *= max[i];
}
printf("\nProduct is %i\n", prouduct);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}