Add 2016 day 1 puzzles.
This commit is contained in:
69
2016/puzzle-01-01.cc
Normal file
69
2016/puzzle-01-01.cc
Normal file
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 02/12/2021.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
enum Direction { north, east, south, west };
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::string line;
|
||||
if (!std::getline(std::cin, line)) {
|
||||
std::cerr << "Failed to read line.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Direction direction{north};
|
||||
int horiz{0};
|
||||
int vert{0};
|
||||
for (std::size_t pos{0}; pos < line.size(); pos += 2) {
|
||||
switch (line.at(pos)) {
|
||||
case 'L':
|
||||
if (direction == Direction::north) {
|
||||
direction = Direction::west;
|
||||
}
|
||||
else {
|
||||
direction = static_cast<Direction>(static_cast<int>(direction) - 1);
|
||||
}
|
||||
break;
|
||||
case 'R':
|
||||
if (direction == Direction::west) {
|
||||
direction = Direction::north;
|
||||
}
|
||||
else {
|
||||
direction = static_cast<Direction>(static_cast<int>(direction) + 1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
++pos;
|
||||
assert(pos < line.size());
|
||||
std::size_t idx{0};
|
||||
auto dist{static_cast<int>(std::stoul(line.substr(pos), &idx))};
|
||||
pos += idx;
|
||||
switch (direction) {
|
||||
case Direction::north:
|
||||
vert += dist;
|
||||
break;
|
||||
case Direction::east:
|
||||
horiz += dist;
|
||||
break;
|
||||
case Direction::south:
|
||||
vert -= dist;
|
||||
break;
|
||||
case Direction::west:
|
||||
horiz -= dist;
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
horiz = std::abs(horiz);
|
||||
vert = std::abs(vert);
|
||||
std::cout << "We end up " << horiz << " + " << vert << " = " << horiz + vert << " blocks away.\n";
|
||||
return 0;
|
||||
}
|
58
2016/puzzle-01-02.cc
Normal file
58
2016/puzzle-01-02.cc
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 02/12/2021.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
enum Direction { north, east, south, west };
|
||||
using Location = std::pair<int, int>;
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::string line;
|
||||
if (!std::getline(std::cin, line)) {
|
||||
std::cerr << "Failed to read line.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::set<Location> visited;
|
||||
Location current{0, 0};
|
||||
Location direction{0, 1};
|
||||
visited.insert(current);
|
||||
|
||||
for (std::size_t pos{0}; pos < line.size(); pos += 2) {
|
||||
switch (line.at(pos)) {
|
||||
case 'L':
|
||||
direction = {-direction.second, direction.first};
|
||||
break;
|
||||
case 'R':
|
||||
direction = {direction.second, -direction.first};
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
|
||||
++pos;
|
||||
assert(pos < line.size());
|
||||
std::size_t idx{0};
|
||||
auto dist{static_cast<int>(std::stoul(line.substr(pos), &idx))};
|
||||
while (dist-- > 0) {
|
||||
current.first += direction.first;
|
||||
current.second += direction.second;
|
||||
if (visited.find(current) != visited.end()) {
|
||||
auto horiz = std::abs(current.first);
|
||||
auto vert = std::abs(current.second);
|
||||
std::cout << "We end up " << horiz << " + " << vert << " = " << horiz + vert
|
||||
<< " blocks away.\n";
|
||||
return 0;
|
||||
}
|
||||
visited.insert(current);
|
||||
}
|
||||
|
||||
pos += idx;
|
||||
}
|
||||
std::cerr << "Didn't find an answer.\n";
|
||||
return 1;
|
||||
}
|
Reference in New Issue
Block a user