From fe9c21aea9762a45f9dd54468e0195f2143a52d6 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Wed, 11 Dec 2019 18:06:47 +0100 Subject: [PATCH] Implement day 11. --- 2019/inputs/11.txt | 1 + 2019/src/day11.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 2019/inputs/11.txt diff --git a/2019/inputs/11.txt b/2019/inputs/11.txt new file mode 100644 index 0000000..1ecd4c3 --- /dev/null +++ b/2019/inputs/11.txt @@ -0,0 +1 @@ +3,8,1005,8,330,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,29,2,9,4,10,1006,0,10,1,1103,17,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,61,1006,0,21,1006,0,51,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,89,1,102,19,10,1,1107,17,10,1006,0,18,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,123,1,9,2,10,2,1105,10,10,2,103,9,10,2,1105,15,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,161,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,182,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,101,0,8,205,2,1102,6,10,1006,0,38,2,1007,20,10,2,1105,17,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,241,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,263,1006,0,93,2,5,2,10,2,6,7,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,296,1006,0,81,1006,0,68,1006,0,76,2,4,4,10,101,1,9,9,1007,9,1010,10,1005,10,15,99,109,652,104,0,104,1,21102,825594262284,1,1,21102,347,1,0,1105,1,451,21101,0,932855939852,1,21101,358,0,0,1106,0,451,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,1,235152649255,1,21101,405,0,0,1105,1,451,21102,235350879235,1,1,21102,416,1,0,1106,0,451,3,10,104,0,104,0,3,10,104,0,104,0,21102,988757512972,1,1,21101,439,0,0,1106,0,451,21102,1,988669698828,1,21101,0,450,0,1106,0,451,99,109,2,22101,0,-1,1,21102,40,1,2,21102,1,482,3,21102,472,1,0,1106,0,515,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,477,478,493,4,0,1001,477,1,477,108,4,477,10,1006,10,509,1101,0,0,477,109,-2,2106,0,0,0,109,4,1202,-1,1,514,1207,-3,0,10,1006,10,532,21102,1,0,-3,21202,-3,1,1,21202,-2,1,2,21102,1,1,3,21102,1,551,0,1106,0,556,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,579,2207,-4,-2,10,1006,10,579,22101,0,-4,-4,1105,1,647,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,598,1,0,1105,1,556,21202,1,1,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,617,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,639,21202,-1,1,1,21102,1,639,0,105,1,514,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 diff --git a/2019/src/day11.cpp b/2019/src/day11.cpp index 508ed43..2dd8302 100644 --- a/2019/src/day11.cpp +++ b/2019/src/day11.cpp @@ -1,10 +1,87 @@ #include +#include #include "days.hpp" +#include "utils.hpp" +#include "point.hpp" + +namespace { + typedef aoc2019::Point point_t; + using aoc2019::IntCodeComputer; + + inline point_t turn_right(point_t direction) { + return {-direction[1], direction[0]}; + } + + inline point_t turn_left(point_t direction) { + return {direction[1], -direction[0]}; + } + + std::unordered_map simulate(std::istream &input, bool background = false) { + std::unordered_map image; + + point_t direction{0, -1}; + point_t pos = {0, 0}; + + IntCodeComputer computer(IntCodeComputer::read_intcode(input), {}); + std::deque outputs; + + computer.connectOutput(outputs); + + while (!computer.isTerminated()) { + const auto it = image.find(pos); + computer.sendInput(it != image.end() ? it->second : background); + computer.run(); + + if (!outputs.empty()) { + assert(outputs.size() == 2); + auto color = outputs.front(); + auto turn = outputs.back(); + outputs.clear(); + + image[pos] = color; + + if (turn) { + direction = turn_right(direction); + } else { + direction = turn_left(direction); + } + + pos += direction; + } + } + + return image; + } +} void aoc2019::day11_part1(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + const auto result = simulate(input); + + output << result.size() << std::endl; } void aoc2019::day11_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + const auto result = simulate(input, true); + + // Determine bounding box + using limits = std::numeric_limits; + int left_edge = limits::max(), right_edge = limits::min(), top_edge = limits::max(), bottom_edge = limits::min(); + for (auto& entry : result) { + left_edge = std::min(entry.first[0], left_edge); + right_edge = std::max(entry.first[0], right_edge); + top_edge = std::min(entry.first[1], top_edge); + bottom_edge = std::max(entry.first[1], bottom_edge); + } + + for (int y = top_edge; y <= bottom_edge; ++y) { + for (int x = left_edge; x <= right_edge; ++x) { + if (auto it = result.find({x, y}); it != result.end() && it->second) { + output << '#'; + } else { + output << ' '; + } + } + + output << '\n'; + } }