Change transform generator in day 20 to not return functions
parent
1b851979a5
commit
c9d3c6207f
|
@ -5,12 +5,10 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
constexpr auto TILE_ID_PATTERN = R"(Tile (\d+):)";
|
constexpr auto TILE_ID_PATTERN = R"(Tile (\d+):)";
|
||||||
|
@ -206,7 +204,7 @@ class TransformGenerator {
|
||||||
class const_iterator {
|
class const_iterator {
|
||||||
public:
|
public:
|
||||||
using difference_type = int;
|
using difference_type = int;
|
||||||
using value_type = std::function<CameraFrame()>;
|
using value_type = CameraFrame;
|
||||||
using pointer = const value_type *;
|
using pointer = const value_type *;
|
||||||
using reference = const value_type &;
|
using reference = const value_type &;
|
||||||
using iterator_category = std::input_iterator_tag;
|
using iterator_category = std::input_iterator_tag;
|
||||||
|
@ -231,7 +229,8 @@ class TransformGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
const value_type operator*() {
|
const value_type operator*() {
|
||||||
return *(this->baseIterator);
|
auto transform = *(this->baseIterator);
|
||||||
|
return transform();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -239,12 +238,12 @@ class TransformGenerator {
|
||||||
const_iterator(const TransformGenerator &container)
|
const_iterator(const TransformGenerator &container)
|
||||||
: container(container), baseIterator(container.operationList.cbegin()) {
|
: container(container), baseIterator(container.operationList.cbegin()) {
|
||||||
}
|
}
|
||||||
const_iterator(const TransformGenerator &container, const std::vector<value_type>::const_iterator &baseIterator)
|
const_iterator(const TransformGenerator &container, const std::vector<std::function<value_type()>>::const_iterator &baseIterator)
|
||||||
: container(container), baseIterator(baseIterator) {
|
: container(container), baseIterator(baseIterator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const TransformGenerator &container;
|
const TransformGenerator &container;
|
||||||
std::vector<value_type>::const_iterator baseIterator;
|
std::vector<std::function<value_type()>>::const_iterator baseIterator;
|
||||||
};
|
};
|
||||||
|
|
||||||
TransformGenerator(const CameraFrame &frame)
|
TransformGenerator(const CameraFrame &frame)
|
||||||
|
@ -415,15 +414,14 @@ bool isBoardFilled(const std::map<std::pair<int, int>, CameraFrame> board, int m
|
||||||
std::optional<CameraFrame> findPossibleFrame(
|
std::optional<CameraFrame> findPossibleFrame(
|
||||||
const CameraFrame &frameToMatch, std::function<bool(const CameraFrame &)> frameMatches) {
|
const CameraFrame &frameToMatch, std::function<bool(const CameraFrame &)> frameMatches) {
|
||||||
auto transformations = TransformGenerator(frameToMatch);
|
auto transformations = TransformGenerator(frameToMatch);
|
||||||
auto result = std::find_if(transformations.cbegin(), transformations.cend(), [frameMatches](auto transformation) {
|
auto result = std::find_if(transformations.cbegin(), transformations.cend(), [frameMatches](CameraFrame transformedFrame) {
|
||||||
CameraFrame transformedFrame = transformation();
|
|
||||||
return frameMatches(transformedFrame);
|
return frameMatches(transformedFrame);
|
||||||
});
|
});
|
||||||
if (result == transformations.cend()) {
|
if (result == transformations.cend()) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (*result)();
|
return *result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -502,8 +500,7 @@ std::map<std::pair<int, int>, CameraFrame> findLinedUpArrangement(const std::vec
|
||||||
|
|
||||||
const TransformGenerator transforms(frame);
|
const TransformGenerator transforms(frame);
|
||||||
for (auto it = transforms.cbegin(); it != transforms.cend(); ++it) {
|
for (auto it = transforms.cbegin(); it != transforms.cend(); ++it) {
|
||||||
auto transformation = *it;
|
CameraFrame transformedFrame = *it;
|
||||||
CameraFrame transformedFrame = transformation();
|
|
||||||
auto res = findLinedUpArrangement(transformedFrame, availableFrames, boardSize, boardSize);
|
auto res = findLinedUpArrangement(transformedFrame, availableFrames, boardSize, boardSize);
|
||||||
if (res) {
|
if (res) {
|
||||||
return *res;
|
return *res;
|
||||||
|
@ -618,7 +615,7 @@ long part2(const std::map<std::pair<int, int>, CameraFrame> &rawBoard, int board
|
||||||
|
|
||||||
auto transforms = TransformGenerator(fullBoardFrame);
|
auto transforms = TransformGenerator(fullBoardFrame);
|
||||||
for (auto it = transforms.cbegin(); it != transforms.cend(); it++) {
|
for (auto it = transforms.cbegin(); it != transforms.cend(); it++) {
|
||||||
auto transformedFrame = (*it)();
|
auto transformedFrame = *it;
|
||||||
int totalMonsterChars = 0;
|
int totalMonsterChars = 0;
|
||||||
for (int i = 0; i < fullBoard.size() - monsterDimensions.first; i++) {
|
for (int i = 0; i < fullBoard.size() - monsterDimensions.first; i++) {
|
||||||
for (int j = 0; j < fullBoard.at(0).size() - monsterDimensions.second; j++) {
|
for (int j = 0; j < fullBoard.at(0).size() - monsterDimensions.second; j++) {
|
||||||
|
|
Loading…
Reference in New Issue