Add 2017 day 13 puzzles
This commit is contained in:
59
2017/puzzle-13-01.cc
Normal file
59
2017/puzzle-13-01.cc
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
using UInt = unsigned long;
|
||||
|
||||
struct Layer
|
||||
{
|
||||
explicit Layer(UInt depth) : depth_(depth) {}
|
||||
|
||||
void advance()
|
||||
{
|
||||
pos_ += dir_;
|
||||
if (pos_ == 0 || pos_ == depth_ - 1) {
|
||||
dir_ = -dir_;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] auto pos() const noexcept -> UInt { return pos_; }
|
||||
[[nodiscard]] auto depth() const noexcept -> UInt { return depth_; }
|
||||
|
||||
private:
|
||||
UInt pos_{0};
|
||||
UInt depth_;
|
||||
UInt dir_{1};
|
||||
};
|
||||
|
||||
using LayerMap = std::map<UInt, Layer>;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
static std::regex line_re{"(\\d+): (\\d+)"};
|
||||
std::string line;
|
||||
LayerMap layers;
|
||||
|
||||
while (std::getline(std::cin, line) && !line.empty()) {
|
||||
std::smatch m;
|
||||
if (!std::regex_search(line, m, line_re)) {
|
||||
std::cerr << "Unable to match: " << line << '\n';
|
||||
return 1;
|
||||
}
|
||||
layers.insert({std::stoul(m.str(1)), Layer{std::stoul(m.str(2))}});
|
||||
}
|
||||
|
||||
auto severity{0};
|
||||
for (auto& layer : layers) {
|
||||
for (UInt time{0}; time != layer.first; ++time) {
|
||||
layer.second.advance();
|
||||
}
|
||||
if (layer.second.pos() == 0) {
|
||||
severity += layer.first * layer.second.depth();
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Severity: " << severity << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
63
2017/puzzle-13-02.cc
Normal file
63
2017/puzzle-13-02.cc
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
using UInt = unsigned long;
|
||||
|
||||
struct Layer
|
||||
{
|
||||
explicit Layer(UInt depth) : depth_(depth) {}
|
||||
|
||||
[[nodiscard]] auto pos_at(UInt time) const noexcept -> UInt
|
||||
{
|
||||
time %= 2 * (depth_ - 1);
|
||||
if (time < depth_ - 1) {
|
||||
return time;
|
||||
}
|
||||
else {
|
||||
return 2 * (depth_ - 1) - time;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] auto depth() const noexcept -> UInt { return depth_; }
|
||||
|
||||
private:
|
||||
UInt depth_;
|
||||
};
|
||||
|
||||
using LayerMap = std::map<UInt, Layer>;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
static std::regex line_re{"(\\d+): (\\d+)"};
|
||||
std::string line;
|
||||
LayerMap layers;
|
||||
|
||||
while (std::getline(std::cin, line) && !line.empty()) {
|
||||
std::smatch m;
|
||||
if (!std::regex_search(line, m, line_re)) {
|
||||
std::cerr << "Unable to match: " << line << '\n';
|
||||
return 1;
|
||||
}
|
||||
layers.insert({std::stoul(m.str(1)), Layer{std::stoul(m.str(2))}});
|
||||
}
|
||||
|
||||
UInt offset{0};
|
||||
UInt cont{true};
|
||||
while (cont) {
|
||||
cont = false;
|
||||
for (auto& layer : layers) {
|
||||
if (layer.second.pos_at(layer.first + offset) == 0) {
|
||||
++offset;
|
||||
cont = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Offset: " << offset << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user