From d0135de39a0369042863c20e06108320c4769407 Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Sat, 11 Dec 2021 18:30:51 +0000 Subject: [PATCH] Day 11 --- day11/Makefile | 10 +++ day11/example.txt | 10 +++ day11/realinput.txt | 10 +++ day11/solution-part1.c | 130 ++++++++++++++++++++++++++++++++++++++ day11/solution-part2.c | 140 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+) create mode 100644 day11/Makefile create mode 100644 day11/example.txt create mode 100644 day11/realinput.txt create mode 100644 day11/solution-part1.c create mode 100644 day11/solution-part2.c diff --git a/day11/Makefile b/day11/Makefile new file mode 100644 index 0000000..da2e708 --- /dev/null +++ b/day11/Makefile @@ -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) diff --git a/day11/example.txt b/day11/example.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/day11/example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/day11/realinput.txt b/day11/realinput.txt new file mode 100644 index 0000000..6fab2c0 --- /dev/null +++ b/day11/realinput.txt @@ -0,0 +1,10 @@ +5723573158 +3154748563 +4783514878 +3848142375 +3637724151 +8583172484 +7747444184 +1613367882 +6228614227 +4732225334 diff --git a/day11/solution-part1.c b/day11/solution-part1.c new file mode 100644 index 0000000..007bfe2 --- /dev/null +++ b/day11/solution-part1.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include + +#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; + } +} diff --git a/day11/solution-part2.c b/day11/solution-part2.c new file mode 100644 index 0000000..772a54b --- /dev/null +++ b/day11/solution-part2.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include + +#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; + } +}