From 3c682b31e96cc362c3d257b3ecf9af1b19a82a47 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 24 Dec 2019 16:38:36 +0100 Subject: [PATCH] Implement day 24 part 1. --- 2019/inputs/24.txt | 5 +++ 2019/src/day24.cpp | 68 +++++++++++++++++++++++++++++++++-- 2019/tests/samples/24-1-1.in | 5 +++ 2019/tests/samples/24-1-1.out | 1 + 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 2019/inputs/24.txt create mode 100644 2019/tests/samples/24-1-1.in create mode 100644 2019/tests/samples/24-1-1.out diff --git a/2019/inputs/24.txt b/2019/inputs/24.txt new file mode 100644 index 0000000..63749fd --- /dev/null +++ b/2019/inputs/24.txt @@ -0,0 +1,5 @@ +###.. +.##.. +#.... +##..# +.###. diff --git a/2019/src/day24.cpp b/2019/src/day24.cpp index 632ed30..efec840 100644 --- a/2019/src/day24.cpp +++ b/2019/src/day24.cpp @@ -1,10 +1,74 @@ #include +#include +#include +#include #include "days.hpp" +namespace { + using field_t = std::array, 5>; + + field_t read_input(std::istream &input) { + std::string buffer; + field_t map; + + int y = 0; + + while (std::getline(input, buffer)) { + auto &row = map[y++]; + + std::transform(buffer.begin(), buffer.end(), row.begin(), [](char c) { return c == '#'; }); + } + + return map; + } + + void next_gen(const field_t &source, field_t &sink) { + for (int y = 0; y < source.size(); ++y) { + for (int x = 0; x < source[y].size(); ++x) { + int neighbours = source[y][x] ? -1 : 0; + for (int dy = -1; dy <= 1; ++dy) { + if (dy + y < 0 || dy + y >= source.size()) { + continue; + } + for (int dx = -1; dx <= 1; ++dx) { + if (dx + x < 0 || dx + x >= source[y].size() || dx * dy) { + continue; + } + neighbours += source[y + dy][x + dx]; + } + } + + sink[y][x] = neighbours == 1 || (!source[y][x] && neighbours == 2); + } + } + } +} + void aoc2019::day24_part1(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + auto map = read_input(input); + auto copy = map; + + std::set seen; + do { + seen.insert(map); + next_gen(map, copy); + std::swap(map, copy); + } while (!seen.count(map)); + + unsigned int pow = 1; + unsigned int diversity = 0; + for (auto &row : map) { + for (auto b : row) { + if (b) { + diversity += pow; + } + + pow <<= 1u; + } + } + output << diversity << std::endl; } void aoc2019::day24_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + output << "Not implemented\n"; } diff --git a/2019/tests/samples/24-1-1.in b/2019/tests/samples/24-1-1.in new file mode 100644 index 0000000..704a112 --- /dev/null +++ b/2019/tests/samples/24-1-1.in @@ -0,0 +1,5 @@ +....# +#..#. +#..## +..#.. +#.... diff --git a/2019/tests/samples/24-1-1.out b/2019/tests/samples/24-1-1.out new file mode 100644 index 0000000..39f6036 --- /dev/null +++ b/2019/tests/samples/24-1-1.out @@ -0,0 +1 @@ +2129920