mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Small input utility.
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <regex>
|
|
||||||
#include "days.hpp"
|
#include "days.hpp"
|
||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
@@ -12,18 +11,10 @@ namespace {
|
|||||||
std::vector<point_t> read_moons(std::istream &input) {
|
std::vector<point_t> read_moons(std::istream &input) {
|
||||||
std::vector<point_t> moons;
|
std::vector<point_t> moons;
|
||||||
|
|
||||||
std::regex regex(R"(^<x=(-?\d+), y=(-?\d+), z=(-?\d+)>$)");
|
|
||||||
std::smatch results;
|
|
||||||
|
|
||||||
for (std::string buffer; std::getline(input, buffer);) {
|
|
||||||
if (!std::regex_match(buffer, results, regex)) {
|
|
||||||
throw std::domain_error(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
point_t moon;
|
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;
|
return moons;
|
||||||
|
|||||||
@@ -21,6 +21,25 @@ namespace aoc2019 {
|
|||||||
seed ^= hash(o) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
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::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 = {});
|
std::deque<int64_t> run_intcode(std::vector<std::int64_t> program, std::deque<std::int64_t> inputs = {});
|
||||||
|
|||||||
Reference in New Issue
Block a user