mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 24 part 1.
This commit is contained in:
5
2019/inputs/24.txt
Normal file
5
2019/inputs/24.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
###..
|
||||
.##..
|
||||
#....
|
||||
##..#
|
||||
.###.
|
||||
@@ -1,10 +1,74 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
#include "days.hpp"
|
||||
|
||||
namespace {
|
||||
using field_t = std::array<std::array<bool, 5>, 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<field_t> 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";
|
||||
}
|
||||
|
||||
5
2019/tests/samples/24-1-1.in
Normal file
5
2019/tests/samples/24-1-1.in
Normal file
@@ -0,0 +1,5 @@
|
||||
....#
|
||||
#..#.
|
||||
#..##
|
||||
..#..
|
||||
#....
|
||||
1
2019/tests/samples/24-1-1.out
Normal file
1
2019/tests/samples/24-1-1.out
Normal file
@@ -0,0 +1 @@
|
||||
2129920
|
||||
Reference in New Issue
Block a user