DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 11: Seating System

Collapse
 
bgaster profile image
Benedict Gaster • Edited

Well I had a little time while rewatching Buffy this evening with the kids and inspired by E. Choroba, I wrote a very (very) simple C++ program to generate an annimated GIF showing the process of set allocation. It uses the single header GIF library:

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include "gif.h"

int main() {
    GifWriter g;

    int width;
    int height;

    std::ifstream file("day11_test1_output");
    std::string temp;

    std::stringstream ss;
    std::getline(file, temp);
    ss << temp;
    ss >> width;
    std::getline(file, temp);
    ss << temp;
    ss >> height;

    GifBegin(&g, "day11.gif", width, height, 100);

    //Gif gif("day11.gif", width, height, 1);
    std::vector<uint8_t> image((width+2)*(height+2)*4);

    int y = 0;
    while (std::getline(file, temp)) {
        for (int x = 0; x < width; x++) {
            uint8_t r = 220;
            uint8_t g = 0;
            uint8_t b = 220;
            if (temp.at(x) == '#') {
                    r = 0;
                    g = 255;
                    b = 0;
            }
            if (temp.at(x) == '.') {
                    r = 0;
                    g = 0;
                    b = 0;
            }
            const int one_d_map = x + width  * y;
            image[(one_d_map * 4) + 0] = r;
            image[(one_d_map * 4) + 1] = g;
            image[(one_d_map * 4) + 2] = b;
            image[(one_d_map * 4) + 3] = 255;
        }

        if (y == 9) {
            GifWriteFrame(&g, image.data(), width, height, 100);
            //image.clear();
            y = 0;
        }
        else {
            y++;
        }

    }
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);
    GifWriteFrame(&g, image.data(), width, height, 100);

    GifEnd(&g);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The input is a file which has the width and height on the first two lines, followed by the grid for each step. For example, the example given in the AOC description for today:

10 
10
L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL
#.##.##.##
#######.##
#.#.#..#..
####.##.##
#.##.##.##
#.#####.##
..#.#.....
##########
#.######.#
#.#####.##
#.LL.L#.##
#LLLLLL.L#
L.L.L..L..
#LLL.LL.L#
#.LL.LL.LL
#.LLLL#.##
..L.L.....
#LLLLLLLL#
#.LLLLLL.L
#.#LLLL.##
#.##.L#.##
#L###LL.L#
L.#.#..#..
#L##.##.L#
#.##.LL.LL
#.###L#.##
..#.#.....
#L######L#
#.LL###L.L
#.#L###.##
#.#L.L#.##
#LLL#LL.L#
L.L.L..#..
#LLL.##.L#
#.LL.LL.LL
#.LL#L#.##
..L.L.....
#L#LLLL#L#
#.LLLLLL.L
#.#L#L#.##
Enter fullscreen mode Exit fullscreen mode