master
Jonathan Hodgson 2 years ago
parent 91b232f9d6
commit d0135de39a
  1. 10
      day11/Makefile
  2. 10
      day11/example.txt
  3. 10
      day11/realinput.txt
  4. 130
      day11/solution-part1.c
  5. 140
      day11/solution-part2.c

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

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

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

@ -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;
}
}

@ -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;
}
}
Loading…
Cancel
Save