You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.5 KiB
140 lines
3.5 KiB
#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; |
|
} |
|
}
|
|
|