From 46b2ae6ef0980ed3e1f55257fc6918cfcfd39cae Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 23 Dec 2019 18:36:48 +0100 Subject: [PATCH] Implement starting point for day 10. --- 2019/src/day20.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/2019/src/day20.cpp b/2019/src/day20.cpp index bb8a3a7..3bc489c 100644 --- a/2019/src/day20.cpp +++ b/2019/src/day20.cpp @@ -1,10 +1,119 @@ #include +#include +#include +#include +#include #include "days.hpp" +#include "point.hpp" + +namespace { + typedef aoc2019::Point point_t; + typedef std::vector map_t; + + std::array DIRECTIONS = {{ + {0, -1}, + {0, 1}, + {-1, 0}, + {1, 0}, + }}; + + std::vector read_map(std::istream &input) { + std::string buffer; + std::vector map; + + while (std::getline(input, buffer)) { + map.push_back(buffer); + } + + return map; + } + + std::pair, std::unordered_map>> + get_portals(const map_t &map) { + std::unordered_map half_portals; + std::unordered_map> portals; + + // First find horizontal portals + for (int y = 0; y < map.size(); ++y) { + for (int x = 0; x < map[y].size() - 1; ++x) { + if (std::isalpha(map[y][x]) && std::isalpha(map[y][x + 1])) { + // find out the entry point + point_t entry_point = {0, y}; + if (x > 0 && map[y][x - 1] == '.') { + entry_point[0] = x; + } else { + entry_point[0] = x + 1; + } + + auto name = map[y].substr(x, 2); + + if (auto it = half_portals.find(name); it != half_portals.end()) { + portals[name] = {entry_point, it->second}; + } else { + half_portals[name] = entry_point; + } + } + } + } + + char name[3] = {0, 0, 0}; + for (int x = 0; x < map[0].size(); ++x) { + for (int y = 0; y < map.size() - 1; ++y) { + if (std::isalpha(map[y][x]) && std::isalpha(map[y + 1][x])) { + name[0] = map[y][x]; + name[1] = map[y + 1][x]; + + point_t entry_point = {x, 0}; + if (y > 0 && map[y - 1][x] == '.') { + entry_point[1] = y; + } else { + entry_point[1] = y + 1; + } + + if (auto it = half_portals.find(name); it != half_portals.end()) { + portals[name] = {entry_point, it->second}; + } else { + half_portals[name] = entry_point; + } + } + } + } + + return std::make_pair(half_portals, portals); + } +} void aoc2019::day20_part1(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + const auto map = read_map(input); + auto[half_portals, portals] = get_portals(map); + + const auto starting_point = half_portals.at("AA"); + const auto goal = half_portals.at("ZZ"); + + std::unordered_map links; + for (auto &link : portals) { + links[link.second.first] = link.second.second; + links[link.second.second] = link.second.first; + } + + std::unordered_set visited{starting_point}; + std::queue> todo; + todo.emplace(0, starting_point); + + while (!todo.empty()) { + const auto[dist, pos] = todo.front(); + todo.pop(); + + if (pos == goal) { + output << dist << std::endl; + return; + } + + } + + output << "Not implemented\n"; } void aoc2019::day20_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + output << "Not implemented\n"; }