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.
131 lines
3.2 KiB
131 lines
3.2 KiB
3 years ago
|
#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;
|
||
|
}
|
||
|
}
|