From da8f874574069a497d6d1d56be4f834e48031e1b Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 2 Dec 2021 20:45:07 +0000 Subject: [PATCH] Add 2016 day 1 puzzles. --- 2016/puzzle-01-01.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2016/puzzle-01-02.cc | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 2016/puzzle-01-01.cc create mode 100644 2016/puzzle-01-02.cc diff --git a/2016/puzzle-01-01.cc b/2016/puzzle-01-01.cc new file mode 100644 index 0000000..c90cfb3 --- /dev/null +++ b/2016/puzzle-01-01.cc @@ -0,0 +1,69 @@ +// +// Created by Matthew Gretton-Dann on 02/12/2021. +// + +#include +#include + +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(static_cast(direction) - 1); + } + break; + case 'R': + if (direction == Direction::west) { + direction = Direction::north; + } + else { + direction = static_cast(static_cast(direction) + 1); + } + break; + default: + abort(); + } + ++pos; + assert(pos < line.size()); + std::size_t idx{0}; + auto dist{static_cast(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; +} \ No newline at end of file diff --git a/2016/puzzle-01-02.cc b/2016/puzzle-01-02.cc new file mode 100644 index 0000000..04db078 --- /dev/null +++ b/2016/puzzle-01-02.cc @@ -0,0 +1,58 @@ +// +// Created by Matthew Gretton-Dann on 02/12/2021. +// + +#include +#include +#include + +enum Direction { north, east, south, west }; +using Location = std::pair; + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Failed to read line.\n"; + return 1; + } + + std::set 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(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; +} \ No newline at end of file