Really ugly but working day 17 part 2.

This commit is contained in:
2019-12-17 18:26:54 +01:00
parent 3a9dcd0072
commit 1968e4c5b4
4 changed files with 120 additions and 1 deletions

View File

@@ -1,4 +1,6 @@
#include <iostream>
#include <string_view>
#include <cassert>
#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<std::int64_t> output_buffer;
computer.connectOutput(output_buffer);
std::array<std::string_view, 3> 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<char>(output));
output << output_buffer.back() << std::endl;
}
}

View File

@@ -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()

View File

@@ -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);
}
}

View File

@@ -99,6 +99,8 @@ namespace aoc2019 {
void connectOutput(std::deque<value_t> &sink);
void sendInput(value_t input);
void sendInputs(std::string_view str);
[[nodiscard]] bool isTerminated() const;
[[nodiscard]] const std::deque<value_t> &currentInputs() const;