From 1968e4c5b4073351e01321b89a6b3ec937431ea2 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 17 Dec 2019 18:26:54 +0100 Subject: [PATCH] Really ugly but working day 17 part 2. --- 2019/src/day17.cpp | 37 +++++++++++++++++- 2019/src/day17/route-map.py | 76 +++++++++++++++++++++++++++++++++++++ 2019/src/utils.cpp | 6 +++ 2019/src/utils.hpp | 2 + 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 2019/src/day17/route-map.py diff --git a/2019/src/day17.cpp b/2019/src/day17.cpp index 29dc99a..ebce718 100644 --- a/2019/src/day17.cpp +++ b/2019/src/day17.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "days.hpp" #include "utils.hpp" #include "point.hpp" @@ -60,5 +62,38 @@ void aoc2019::day17_part1(std::istream &input, std::ostream &output) { } void aoc2019::day17_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + using namespace std::literals; + + aoc2019::IntCodeComputer computer(input); + computer[0] = 2; + std::deque output_buffer; + computer.connectOutput(output_buffer); + + std::array programs = { + "L,6,R,8,L,4,R,8,L,12\n", + "L,12,R,10,L,4\n", + "L,12,L,6,L,4,L,4\n", + }; + + auto combined_programs = "A,B,B,C,B,C,B,C,A,A\n"sv; + + computer.sendInputs(combined_programs); + + for (auto program : programs) { + computer.sendInputs(program); + } + + // Don't give me output. + computer.sendInputs("n\n"); + + computer.run(); + + assert(!output_buffer.empty()); + + if (output_buffer.size() == 1) { + output << output_buffer.front() << std::endl; + } else { + std::copy(output_buffer.begin(), output_buffer.end(), std::ostreambuf_iterator(output)); + output << output_buffer.back() << std::endl; + } } diff --git a/2019/src/day17/route-map.py b/2019/src/day17/route-map.py new file mode 100644 index 0000000..873b2af --- /dev/null +++ b/2019/src/day17/route-map.py @@ -0,0 +1,76 @@ +import fileinput +import sys + + +def turn_left(direction): + x, y = direction + return (y, -x) + + +def turn_right(direction): + x, y = direction + return (-y, x) + + +def add(pos, direction): + return tuple(a + b for a, b in zip(pos, direction)) + + +def main(): + chart = [line.strip() for line in fileinput.input()] + + pos = None + + for y, line in enumerate(chart): + x = line.find('^') + if x >= 0: + pos = (x, y) + break + + if not pos: + sys.exit('starting point not found') + + route = ['L'] + + direction = (-1, 0) + + def bounds_check(pos): + x, y = pos + + return x >= 0 and y >= 0 and y < len(chart) + + while True: + # try to move forward + next_pos = add(direction, pos) + dist = 0 + + while bounds_check(next_pos) and chart[next_pos[1]][next_pos[0]] == '#': + dist += 1 + pos = next_pos + next_pos = add(direction, pos) + + if dist: + route.append(dist) + else: + break + + for move, new_dir in zip(('L', 'R'), (turn_left(direction), turn_right(direction))): + next_pos = add(pos, new_dir) + if bounds_check(next_pos) and chart[next_pos[1]][next_pos[0]] == '#': + route.append(move) + direction = new_dir + break + + printable_route = [] + for x in route: + if x == 'L' or x == 'R': + printable_route.append(x) + else: + printable_route += ['M'] * x + + print(','.join(str(x) for x in route)) + print(','.join(printable_route)) + + +if __name__ == '__main__': + main() diff --git a/2019/src/utils.cpp b/2019/src/utils.cpp index 1c8f1a8..a037303 100644 --- a/2019/src/utils.cpp +++ b/2019/src/utils.cpp @@ -183,3 +183,9 @@ aoc2019::IntCodeComputer::value_t &aoc2019::IntCodeComputer::operator[](std::siz const aoc2019::IntCodeComputer::value_t &aoc2019::IntCodeComputer::operator[](std::size_t index) const { return program[index]; } + +void aoc2019::IntCodeComputer::sendInputs(std::string_view str) { + for (char c : str) { + sendInput(c); + } +} diff --git a/2019/src/utils.hpp b/2019/src/utils.hpp index 38b3992..c339813 100644 --- a/2019/src/utils.hpp +++ b/2019/src/utils.hpp @@ -99,6 +99,8 @@ namespace aoc2019 { void connectOutput(std::deque &sink); void sendInput(value_t input); + void sendInputs(std::string_view str); + [[nodiscard]] bool isTerminated() const; [[nodiscard]] const std::deque ¤tInputs() const;