Add 2017 day 11 puzzles
This commit is contained in:
60
2017/puzzle-11-01.cc
Normal file
60
2017/puzzle-11-01.cc
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using Coord = std::pair<int, int>;
|
||||
|
||||
auto move(Coord const& c, std::string const& dir) -> Coord
|
||||
{
|
||||
if (dir == "nw") {
|
||||
return {c.first - 1, c.second + 1};
|
||||
}
|
||||
if (dir == "n") {
|
||||
return {c.first, c.second + 2};
|
||||
}
|
||||
if (dir == "ne") {
|
||||
return {c.first + 1, c.second + 1};
|
||||
}
|
||||
if (dir == "se") {
|
||||
return {c.first + 1, c.second - 1};
|
||||
}
|
||||
if (dir == "s") {
|
||||
return {c.first, c.second - 2};
|
||||
}
|
||||
if (dir == "sw") {
|
||||
return {c.first - 1, c.second - 1};
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::string line;
|
||||
if (!std::getline(std::cin, line)) {
|
||||
std::cerr << "Unable to read line\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::size_t idx{0};
|
||||
Coord pos{0, 0};
|
||||
while (idx < line.size()) {
|
||||
auto next{line.find(',', idx)};
|
||||
if (next == std::string::npos) {
|
||||
next = line.size();
|
||||
}
|
||||
pos = move(pos, line.substr(idx, next - idx));
|
||||
|
||||
idx = next + 1;
|
||||
}
|
||||
|
||||
std::cout << "Ended up at (" << pos.first << ", " << pos.second << ") moves away.\n";
|
||||
/* We move horizontally pos.first steps - which gets us up to pos.first steps vertically as well.
|
||||
* Then we move the remaining vertical distances. */
|
||||
auto x_dist{std::abs(pos.first)};
|
||||
auto y_dist{std::abs(pos.second)};
|
||||
auto moves{x_dist};
|
||||
if (y_dist > x_dist) {
|
||||
moves += (y_dist - x_dist) / 2;
|
||||
}
|
||||
std::cout << "Ended up " << moves << " moves away.\n";
|
||||
return 0;
|
||||
}
|
69
2017/puzzle-11-02.cc
Normal file
69
2017/puzzle-11-02.cc
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using Coord = std::pair<int, int>;
|
||||
|
||||
auto move(Coord const& c, std::string const& dir) -> Coord
|
||||
{
|
||||
if (dir == "nw") {
|
||||
return {c.first - 1, c.second + 1};
|
||||
}
|
||||
if (dir == "n") {
|
||||
return {c.first, c.second + 2};
|
||||
}
|
||||
if (dir == "ne") {
|
||||
return {c.first + 1, c.second + 1};
|
||||
}
|
||||
if (dir == "se") {
|
||||
return {c.first + 1, c.second - 1};
|
||||
}
|
||||
if (dir == "s") {
|
||||
return {c.first, c.second - 2};
|
||||
}
|
||||
if (dir == "sw") {
|
||||
return {c.first - 1, c.second - 1};
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
auto dist(Coord const& pos) -> int
|
||||
{
|
||||
/* We move horizontally pos.first steps - which gets us up to pos.first steps vertically as well.
|
||||
* Then we move the remaining vertical distances. */
|
||||
auto x_dist{std::abs(pos.first)};
|
||||
auto y_dist{std::abs(pos.second)};
|
||||
auto moves{x_dist};
|
||||
if (y_dist > x_dist) {
|
||||
moves += (y_dist - x_dist) / 2;
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::string line;
|
||||
if (!std::getline(std::cin, line)) {
|
||||
std::cerr << "Unable to read line\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::size_t idx{0};
|
||||
Coord pos{0, 0};
|
||||
int max_dist{0};
|
||||
while (idx < line.size()) {
|
||||
auto next{line.find(',', idx)};
|
||||
if (next == std::string::npos) {
|
||||
next = line.size();
|
||||
}
|
||||
pos = move(pos, line.substr(idx, next - idx));
|
||||
if (max_dist < dist(pos)) {
|
||||
max_dist = dist(pos);
|
||||
}
|
||||
idx = next + 1;
|
||||
}
|
||||
|
||||
std::cout << "Ended up at (" << pos.first << ", " << pos.second << ") moves away.\n";
|
||||
std::cout << "Ended up " << dist(pos) << " moves away.\n";
|
||||
std::cout << "Max distance away " << max_dist << '\n';
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user