From 7116979d62bdfa3b09235b84454995fd72788cb7 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 19 Dec 2019 18:08:21 +0100 Subject: [PATCH] Part 18 not working --- 2019/inputs/18.txt | 81 +++++++++++++++++++++++++++++++++++ 2019/src/day18.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 2019/inputs/18.txt diff --git a/2019/inputs/18.txt b/2019/inputs/18.txt new file mode 100644 index 0000000..014bd04 --- /dev/null +++ b/2019/inputs/18.txt @@ -0,0 +1,81 @@ +################################################################################# +#.....#y....#.........#...#.#.....#.....#.......................#.......#.......# +#.###.#.#####.#####.#.#.#.#.#.#.#.#.#.#########.###############.#######.#.#####.# +#...#q#...#.......#.#.#.#.#...#.#.#.#...#...#...#......w#.....#.#.....#.#.#...#.# +###.#.###.#.#######.#.#F#.#####.#.#.###.#.#.#.#.#.#####.#.###.#.#.###.#.#.#.#.#.# +#...#.....#.#.#a....#.#.#.......#.#...#.#.#...#.#.#..v#...#...#...#...#.....#.#.# +#.#######.#.#.#.#####.#.#########.#####.#.#######.#.#######.#######.###########.# +#.#...#...#...#...#...#.#.....#...#...#.#.#.......#.....#..b#c..#...#.........#.# +#.#.#.#######.###.#.###.#.###.#.###.#.#.#.#.#######.###.#.###.#.#.###.#.#####.#.# +#.#.#.#o......#...#.....#...#.#.....#.#.#.#.....#...#...#...#.#.#.#...#...#.#...# +#.#.#.#.#######.###########.#.#######.#.#.#####.#####.#.###.#.#.#.#####.#.#.###.# +#.#.#...#.....#.....#.....#.#.....#.....#.....#...#...#...#...#.#.....#.#.#...#.# +#.#.#####E###.#####.#.#.#.#.#####.#########.#.###D#.#.#########.#####.###.#.###.# +#...#.....#.#.#...#...#.#.#.....#...#...#...#...#...#.#...#.....#...#.....#.....# +#.###.#####.#.#.#.#####.#####.#####.#.#.#.#####.#####.#.#.#.#######.#######.##### +#.....#.....#.#.#...#.#.#...#.#...#...#.#.#.........#.#.#...#...........#...#...# +#######.###.#.###.#.#.#.#.#.#.#.#.#####.#.###########.#.#####.#.#########.#####.# +#.........#.#.....#.#.#...#.#.#.#.#...#.#...#...#.....#...#...#.#.......#.#..h..# +#.#######.#########.#.#####.#.#.#.#.#.#.#.#.#.#.#.#######.#.#####.#####.#.#.###.# +#.....#.#...#...#.........#...#.#.#.#.#.#.#...#.....#.....#.....#.....#.#...#.#.# +#####.#.###.#.#.#.#########.###.#.#.#.#.#############.#.#######.#####.#.#####.#.# +#.#...#...#...#...#.#.#...#...#.#.#.#...#.L...........#.#.....#.......#.......#.# +#.#.###.#.#########.#.#.#.#####.#.#.#####.###.###########.#############.#.#####U# +#l#.#.#.#...#.......#.#.#.......#.#.....#.#...#.....P...#.M........m#...#.#...#.# +#.#.#.#.#.###.###.###.#.#########.#.###.#.#####.#######.###########.#.#####.#.#.# +#.#.#...#.....#...#...#...#.....#.#...#.#...#...#.#.J.#....j......#.#.#.....#.#.# +#.#.###########.#.#.###.#.###.###.#####.###.#.###.#.#.#####.#.#####.#.#.#####.#.# +#.#.............#.#.#...#...#.#...#.....#...#.#...#t#.......#.#.....#.#...#.#...# +#Z###############.#.#.#####.#.#.###.###.#.#.#.###.#.#########.#K#####.###.#.##### +#.............#...#.#.....#.#.#...#.#...#n#.#p..#.#.#.........#.#.........#.....# +#.#######.###.#.###.#####.#.#.###.#.#.###.#####N#.#.#####.#####.#.#########.#.#.# +#...#.....#...#.#...#.....#.#...#...#...#.......#.#.....#.#...#.#.#...#.....#.#.# +#.###C#########.#.#########.#.#.#######.#########.#####.###T#.#.#.#.#.#.#####.#.# +#.#...#.........#.#.....#...#.#...#.....#.....#.......#.....#k..#.#.#...#.....#.# +###.###.#########.#.###.#.#####.#.#.#######.#.#.#################.###.###.#####.# +#...#.B.#...#...#.#...#.#.#.....#.#.#...#...#...#...............#...#.#...#.#...# +#.###.###.#.#.#.#.###.#.#.#.#.###.#.###.#.#######.#########.###.###.#.#.###.#.### +#...#.#...#.#.#.#...#.#...#.#...#.#.#...#.#.....#.......#.#.#...#...#.#.#.....#.# +#.#.#.#.###.###.###.#.#####.###.###.#.#.#.#.###.#######G#.#.#####.#####.#.#####.# +#.#.....#.........#...#.......#.......#.......#...........#.............#.......# +#######################################.@.####################################### +#.......H.#.......#.....#.....#.....#.................#...................#.....# +#######.###.#####.#.#.###.#.#.###.#.###.#.#########.###.#######.#########.#.##### +#.....#.#...#...#...#..x..#.#.#...#...#.#.#.......#...........#.#.......#.#.#...# +#.###.#.#.###.#############.#.#.#####.#.#.#.#####.###########.#.#.#####X#.#.#.#.# +#.#.#.#.#...#...#.....#.....#...#...#...#.#.#.#...#...#...#...#...#.....#.#.#.#.# +#.#.#.#.###.#.#.#.###.#.###########.###.###.#.#.###.#.###.#.#######.#####.#.#.#.# +#.#.......#.#.#...#...#.#...#.......#...#...#.......#...#.....#...#.#.....#...#.# +#.#.#######.#.#####.###.###.#.###.###.###.#############.#####.#.#.#.#####.#####.# +#u#...#.....#...#...#...#...#.#...#...#.#.........#.....#...#.#.#...#...#.#...I.# +#.#.###.#########.#.#.###.###.#.###.###.#####.###.#####.#.#.#.#.#####.#.#.#.###.# +#.#.#.............#.#.#s..#...#...#g..#.#...#...#.#...#...#.#.#.......#.#...#.#.# +#.###.###############.#.###.#####.###.#.#.#.#####.#.#.#####.#########.#.#####.#.# +#...#.#.#.......#..i..#.........#...#.#.#.#.....#...#.....#.........#.#.#.#.....# +#.#.#.#.#.#####.#.#####.#########.#.#.#.#.#####.#########.#########.###.#.#.##### +#.#...#.#.#...#.#...#.......#.....#.#.#.#.....#.#z......#.......#.#.....#.#...#.# +#.#####.#.#.#.#.#.#.#######.#.#####.#.#.#.#####.#.#####.#.#####.#.#######.###.#.# +#.....#r#...#.#.#.#.....#.#.#...#...#.#.#.#.....#.#...#.#.....#.#.........#.#...# +#####.#.#####.#.#######.#.#.###.#####.#.#.#.#####.###.#.#######.###.#####.#.###.# +#.....#.....#.#.......#.#.#.#.#.........#.#.#...#.....#.......#...#.....#.....#.# +#.#####.#####.#######.#.#.#.#.#########.#.#.#.#.#####.#######.#.#.#.###.###.###.# +#.#.....#...#.#.....#.#.#.....#.......#.#.#.V.#.....#.....#.#.#.#.#...#...#.#.S.# +#.###.###.#.#.#.#######.#####.#.#####.###.#####.###.#####.#.#####.#######.#.#.### +#...#.#...#...#.......#...#...#.....#...#.#.#...#...#.#...#.......#.....#.#.#.#.# +###.#.#.###########.#.###.#######.#####.#.#.#.#####.#.#.###.#.#####.###.#.#.#.#.# +#.#.#.#.#...........#...#.......#.#.....#...#.#...#...#.#...#.#.....#f....#.#.#.# +#.#.#.#.#.###.#########.#####.###.#.###.###.#W#.#.###.#.###.#.#.###########O#.#.# +#.#...#.#...#.#.......#...#...#...#.#...#.#.#...#...#.#...#.#.#.........R.#.#...# +#.###.#.#####.#.#########.#.###.###.###.#.#.#######.#####.###.#.#########.#.###.# +#.#...#.....#.#.........#.#.....#.#...#.#.#.....#.#.......#...#.#.....#...#.#...# +#.#.#######.#.#######.#.#.#####.#.###.#.#.#####.#.#########.###.#####.#.###.#.### +#.#.#...#...#.#.....#.#.#.....#.#...#.#.#.#...#...#.......#...#.......#.#...#.#.# +#.#.#.#.#.###.#.###.#.#.#####.#.###.#.#.#.#.#.###.###.#.#.#.#.#######A#.#####.#.# +#...#.#.#.....#.#.#.#.#.#...#.#.....#.#.#...#.Y.#...#.#.#.#.#.#.......#.....#.#.# +#.#####.#######.#.#.#.#.#.#.#.#######.###.#####.###.###.###.#.#.###########.#.#.# +#.........#.....#...#.#.#.#.#.#.....#...#d#.#.....#...#.#...#.#.#...#.....#.#...# +#########.###.#.###.#.#.###.#.#.###.###.#.#.#.#######.#.#.###.#.#.#.#.#.###.###.# +#.#.....#...#.#...#...#...#.#...#.#...#.#...#...#...#.#.....#.#...#...#.#...#...# +#.#.#.#####.#####.#######.#.#####.###.#.###.###.#.#.#.#######.#########.#.###.### +#...#.............#................e#...#.....#.Q.#...........#.........#.......# +################################################################################# diff --git a/2019/src/day18.cpp b/2019/src/day18.cpp index 4783ee1..077d4a9 100644 --- a/2019/src/day18.cpp +++ b/2019/src/day18.cpp @@ -1,7 +1,110 @@ #include +#include +#include +#include +#include +#include +#include +#include #include "days.hpp" +#include "point.hpp" + +namespace { + typedef std::tuple state_t; + + 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 find(const std::vector &map, char needle) { + for (int y = 0; y < map.size(); ++y) { + auto x = map[y].find(needle); + if (x != std::string::npos) { + return {(int) x, y}; + } + } + + throw std::invalid_argument("Can't find it!"); + } +} void aoc2019::day18_part1(std::istream &input, std::ostream &output) { + const auto map = read_map(input); + + std::priority_queue, std::vector>, std::greater<>> todo; + std::map visited; + + const auto initial = find(map, '@'); + const state_t initial_state(initial.first, initial.second, ""); + visited[initial_state] = 0; + todo.emplace(0, initial_state); + + int keys_needed = 0; + for (auto &row : map) { + keys_needed += std::count_if(row.begin(), row.end(), isalpha); + } + + // Don't count keys and locks double + keys_needed /= 2; + + while (!todo.empty()) { + const auto[dist, state] = todo.top(); + todo.pop(); + + const auto[x, y, keys] = state; + + if (keys.size() == keys_needed) { + output << dist << std::endl; + return; + } + + std::array, 4> next_points = {{ + {x - 1, y}, + {x + 1, y}, + {x, y - 1}, + {x, y + 1} + }}; + + for (auto point : next_points) { + const auto [nx, ny] = point; + auto next_keys = keys; + + if (x < 0 || y < 0 || x >= map[0].size() || y >= map.size()) { + continue; + } + + char at_next = map[x][y]; + + if (at_next == '#') { + continue; + } else if (std::isupper(at_next)) { + // check if we have the key already + if (keys.find(at_next) == std::string::npos) { + continue; + } + } else if (std::islower(at_next)) { + if (keys.find(std::toupper(at_next)) == std::string::npos) { + next_keys += std::toupper(at_next); + // Ensure unique representation + std::sort(next_keys.begin(), next_keys.end()); + } + } + + state_t next_state{nx, ny, std::move(next_keys)}; + if (auto it = visited.find(next_state); it == visited.end() || it->second < dist + 1) { + visited[next_state] = dist + 1; + todo.emplace(dist + 1, std::move(next_state)); + } + } + } + output << "Not implemented\n"; }