Small input utility.

This commit is contained in:
2019-12-14 19:50:56 +01:00
parent 5ce69e4dff
commit 6d388b068b
2 changed files with 22 additions and 12 deletions

View File

@@ -1,7 +1,6 @@
#include <iostream>
#include <numeric>
#include <vector>
#include <regex>
#include "days.hpp"
#include "point.hpp"
@@ -12,18 +11,10 @@ namespace {
std::vector<point_t> read_moons(std::istream &input) {
std::vector<point_t> moons;
std::regex regex(R"(^<x=(-?\d+), y=(-?\d+), z=(-?\d+)>$)");
std::smatch results;
point_t moon;
for (std::string buffer; std::getline(input, buffer);) {
if (!std::regex_match(buffer, results, regex)) {
throw std::domain_error(buffer);
}
point_t moon;
for (int i = 0; i < 3; ++i) from_chars(results[i + 1].str(), moon[i]);
moons.emplace_back(moon);
while (aoc2019::read_line_numbers_and_garbage<int>(input, moon.begin())) {
moons.push_back(moon);
}
return moons;

View File

@@ -21,6 +21,25 @@ namespace aoc2019 {
seed ^= hash(o) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename ValueType, typename OutputIt>
std::istream &read_line_numbers_and_garbage(std::istream &input, OutputIt output) {
ValueType v;
char c;
while (input && (c = input.peek()) != '\n') {
if (c == '-' || std::isdigit(c)) {
input >> v;
*output = v;
++output;
} else {
input.ignore();
}
}
input.get();
return input;
}
std::string_view strtok(std::string_view &str, char token = ',');
std::deque<int64_t> run_intcode(std::vector<std::int64_t> program, std::deque<std::int64_t> inputs = {});