-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdayNN.cpp
167 lines (150 loc) · 4.24 KB
/
dayNN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// c++ -Wall -Wextra -pedantic -O3 -std=c++20 dayNN.cpp -o dayNN && time ./dayNN dayNNin.txt 1
// vi:set noet ts=4 sw=4:
#include <algorithm>
#include <compare>
#include <concepts>
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
// beware: our origin is top-left and our y-axis is inverted
// from the usual Cartesian (our positive is down)
class Position {
public:
uint64_t x;
uint64_t y;
Position() : x(0), y(0) { }
Position(uint64_t xx, uint64_t yy) : x(xx), y(yy) { }
auto operator<=>(const Position&) const = default;
std::string str() const;
};
std::string Position::str() const {
std::stringstream out;
out << "(" << x << ", " << y << ")";
return out.str();
}
template<typename TT>
concept has_str = requires(const TT& obj) { obj.str(); };
template<typename TT>
concept printable = requires(std::ostream& out, const TT& obj) { out << obj; };
template<typename CC>
concept container = requires(const CC& cobj) {
{ cobj.begin() } -> std::same_as<typename CC::const_iterator>;
{ cobj.end() } -> std::same_as<typename CC::const_iterator>;
};
template<typename CC, typename VV = typename CC::value_type>
concept str_container = requires(CC cobj, VV vobj) {
requires container<CC>;
requires has_str<VV>;
requires std::same_as<std::remove_cvref_t<typename CC::value_type>, VV>;
requires std::same_as<std::remove_cvref_t<decltype(cobj[0])>, VV>;
};
template<typename CC, typename VV = typename CC::value_type>
concept printable_container = requires(CC cobj, VV vobj) {
requires container<CC>;
requires printable<VV>;
requires std::same_as<std::remove_cvref_t<typename CC::value_type>, VV>;
requires std::same_as<std::remove_cvref_t<decltype(cobj[0])>, VV>;
};
void dump(const printable_container auto& input) {
for (size_t aa=0; aa<input.size(); aa++) {
std::cout << aa << ": " << input[aa] << std::endl;
}
}
void dump(const str_container auto& input) {
for (size_t aa=0; aa<input.size(); aa++) {
std::cout << aa << ": " << input[aa].str() << std::endl;
}
}
std::string join(const str_container auto& input, const std::string& delim=" ") {
std::stringstream out;
bool first = true;
for (const auto& aa : input) {
if (first) { first = false; }
else { out << delim; }
out << aa.str();
}
return out.str();
}
std::string join(const printable_container auto& input, const std::string& delim=" ") {
std::stringstream out;
bool first = true;
for (const auto& aa : input) {
if (first) { first = false; }
else { out << delim; }
out << aa;
}
return out.str();
}
std::vector<std::string> split(const std::string& input, const std::string& delim=" ") {
std::vector<std::string> results;
size_t pos = 0;
size_t ppos = pos;
while ((pos = input.find(delim, ppos)) != input.npos) {
results.emplace_back(input.substr(ppos, pos-ppos));
ppos = pos + delim.size();
}
if (ppos < input.size()) {
results.emplace_back(input.substr(ppos));
}
return results;
}
std::string str_tolower(std::string input) {
std::transform(input.begin(), input.end(), input.begin(),
[](unsigned char cc) { return std::tolower(cc); });
return input;
}
std::vector<std::string> get_input(const std::string& file) {
std::vector<std::string> input;
std::ifstream instream(file);
if (! instream.is_open()) {
std::cerr << "file " << file << " did not open" << std::endl;
return {};
}
while (instream.good()) {
std::string one_line;
std::getline(instream, one_line);
input.push_back(std::move(one_line));
}
return input;
}
int part1_answer() {
return 0;
}
int part2_answer() {
return 0;
}
int main(int argc, char* argv[]) {
std::vector<std::string> input;
int part = 2;
if (argc > 2) {
try {
part = std::stoi(argv[2]);
}
catch (...) {
std::cerr << "Error: invalid part " << argv[2] << "; must be 1 or 2" << std::endl;
return 1;
}
input = get_input(argv[1]);
}
else {
std::cerr << "Usage: " << argv[0] << " [input-file] <1|2>" << std::endl;
return 1;
}
if (! ((part == 1) || (part == 2))) {
std::cerr << "Error: invalid part " << part << "; must be 1 or 2" << std::endl;
return 1;
}
if (part == 2) {
std::cout << part2_answer() << std::endl;
}
else {
std::cout << part1_answer() << std::endl;
}
std::cout << "done" << std::endl;
return 0;
}