2
1
Fork 0

Day 4: Add C solution

This commit is contained in:
Mia Herkt 2021-12-04 13:48:37 +01:00
parent dcaada736d
commit 836e8705cf
Signed by: mia
GPG Key ID: 72E154B8622EC191
2 changed files with 128 additions and 0 deletions

61
04/solution_1.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
unsigned score(signed char (*board)[5]) {
unsigned sum = 0;
for (unsigned r = 0; r < 5; r++) {
for (unsigned c = 0; c < 5; c++) {
if (board[r][c] > 0)
sum += board[r][c];
}
}
return sum;
}
int main(void) {
FILE *fp = fopen("input.txt", "r");
signed char seq[100], boards[100][5][5];
unsigned i, b, r, c;
for (i = 0; i < 100; i++) {
fscanf(fp, "%hhi%*c", &seq[i]);
}
for (b = 0; b < 100; b++) {
for (r = 0; r < 5; r++) {
for (c = 0; c < 5; c++)
fscanf(fp, "%hhi", &boards[b][r][c]);
}
}
for (i = 0; i < 100; i++) {
for (b = 0; b < 100; b++) {
for (r = 0; r < 5; r++) {
int rowscore = 0;
for (c = 0; c < 5; c++) {
if (boards[b][r][c] == seq[i])
boards[b][r][c] = -1;
rowscore += boards[b][r][c];
}
if (rowscore == -5)
goto end;
}
for (c = 0; c < 5; c++) {
int colscore = 0;
for (r = 0; r < 5; r++) {
colscore += boards[b][r][c];
}
if (colscore == -5)
goto end;
}
}
}
end:
printf("Answer: %u\n", score(boards[b]) * seq[i]);
}

67
04/solution_2.c Normal file
View File

@ -0,0 +1,67 @@
#include <stdio.h>
unsigned score(signed char (*board)[5]) {
unsigned sum = 0;
for (unsigned r = 0; r < 5; r++) {
for (unsigned c = 0; c < 5; c++) {
if (board[r][c] > 0)
sum += board[r][c];
}
}
return sum;
}
int main(void) {
FILE *fp = fopen("input.txt", "r");
signed char seq[100], boards[100][5][5];
unsigned i, b, r, c, lastwin;
for (i = 0; i < 100; i++) {
fscanf(fp, "%hhi%*c", &seq[i]);
}
for (b = 0; b < 100; b++) {
for (r = 0; r < 5; r++) {
for (c = 0; c < 5; c++)
fscanf(fp, "%hhi", &boards[b][r][c]);
}
}
for (i = 0; i < 100; i++) {
for (b = 0; b < 100; b++) {
if (boards[b][0][0] == -2)
continue;
for (r = 0; r < 5; r++) {
int rowscore = 0;
for (c = 0; c < 5; c++) {
if (boards[b][r][c] == seq[i])
boards[b][r][c] = -1;
rowscore += boards[b][r][c];
}
if (rowscore == -5)
goto win;
}
for (c = 0; c < 5; c++) {
int colscore = 0;
for (r = 0; r < 5; r++) {
colscore += boards[b][r][c];
}
if (colscore == -5)
goto win;
}
continue;
win:
lastwin = score(boards[b]) * seq[i];
boards[b][0][0] = -2;
}
}
printf("Answer: %u\n", lastwin);
}