From aec1c07260257ba7c28eff53f422ddb7daaf316a Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 31 Jul 2022 11:46:17 +0300 Subject: Big Bang --- Advent-of-Code-2021/AOC-11/build.sh | 7 ++ Advent-of-Code-2021/AOC-11/example.txt | 10 +++ Advent-of-Code-2021/AOC-11/input.txt | 10 +++ Advent-of-Code-2021/AOC-11/main | Bin 0 -> 21136 bytes Advent-of-Code-2021/AOC-11/main.c | 114 +++++++++++++++++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100755 Advent-of-Code-2021/AOC-11/build.sh create mode 100644 Advent-of-Code-2021/AOC-11/example.txt create mode 100644 Advent-of-Code-2021/AOC-11/input.txt create mode 100755 Advent-of-Code-2021/AOC-11/main create mode 100644 Advent-of-Code-2021/AOC-11/main.c (limited to 'Advent-of-Code-2021/AOC-11') diff --git a/Advent-of-Code-2021/AOC-11/build.sh b/Advent-of-Code-2021/AOC-11/build.sh new file mode 100755 index 0000000..6be9241 --- /dev/null +++ b/Advent-of-Code-2021/AOC-11/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -xe + +gcc -o main -Wall -Wextra main.c + +./main diff --git a/Advent-of-Code-2021/AOC-11/example.txt b/Advent-of-Code-2021/AOC-11/example.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/Advent-of-Code-2021/AOC-11/example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/Advent-of-Code-2021/AOC-11/input.txt b/Advent-of-Code-2021/AOC-11/input.txt new file mode 100644 index 0000000..e7b9665 --- /dev/null +++ b/Advent-of-Code-2021/AOC-11/input.txt @@ -0,0 +1,10 @@ +4472562264 +8631517827 +7232144146 +2447163824 +1235272671 +5133527146 +6511372417 +3841841614 +8621368782 +3246336677 diff --git a/Advent-of-Code-2021/AOC-11/main b/Advent-of-Code-2021/AOC-11/main new file mode 100755 index 0000000..72ff31b Binary files /dev/null and b/Advent-of-Code-2021/AOC-11/main differ diff --git a/Advent-of-Code-2021/AOC-11/main.c b/Advent-of-Code-2021/AOC-11/main.c new file mode 100644 index 0000000..b72ec5f --- /dev/null +++ b/Advent-of-Code-2021/AOC-11/main.c @@ -0,0 +1,114 @@ +#include +#include + +#if 0 +#define FILE_PATH "example.txt" +#else +#define FILE_PATH "input.txt" +#endif + +#define WIDTH 10 +#define HEIGHT 10 + +int board[WIDTH][HEIGHT] = {0}; +size_t flashes = 0; + +void parse() +{ + FILE *fp = fopen(FILE_PATH, "r"); + if(!fp) { + fprintf(stderr,"ERROR: Could not open file: %s", FILE_PATH); + return; + } + + char ch; + int i = 0; + while((ch = fgetc(fp)) != EOF) + { + if(ch == '\n') continue; + board[i%WIDTH][i/HEIGHT] = ch - '0'; + i++; + } + + fclose(fp); + +} + +int step() +{ + for(int i = 0; i < HEIGHT; i++) + for(int j = 0; j < WIDTH; j++) + board[j][i] += 1; + + int new_board[WIDTH][HEIGHT] = {0}; + size_t old_flashes = 0 - 1; + while(old_flashes != flashes) + { + old_flashes = flashes; + memcpy(new_board, board, sizeof(board)); + + for(int i = 0; i < HEIGHT; i++) + for(int j = 0; j < WIDTH; j++) + if(board[j][i] > 9) + { + flashes++; + new_board[j][i] = 0; + + for(int y = -1; y <= 1; y++) + for(int x = -1; x <= 1; x++) + if(i+y < 0 || i+y >= HEIGHT || + j+x < 0 || j+x >= WIDTH || + (y == 0 && x == 0)) continue; + else if(new_board[j+x][i+y] != 0) + new_board[j+x][i+y]++; + } + + memcpy(board, new_board, sizeof(board)); + } + + int empty_board[WIDTH][HEIGHT] = {0}; + if(memcmp(board, empty_board, sizeof(board)) == 0) + return 1; + + return 0; +} + +void print_board() +{ + for(int i = 0; i < HEIGHT; i++) + { + for(int j = 0; j < WIDTH; j++) + { + printf("%d", board[j][i]); + } + puts(""); + } +} + +void part_1() +{ + parse(); + + for(int i = 0; i < 100; i++) + step(); + + printf("flashes: %ld\n", flashes); +} + +void part_2() +{ + parse(); + + for(int i = 0; i < 1000000; i++) + if(step()) { + printf("synced at step: %d\n", i + 1); + return; + } +} + +int main(void) +{ + part_1(); + part_2(); + return 0; +} -- cgit v1.2.3