-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11_space-police.cpp
More file actions
108 lines (95 loc) · 3.16 KB
/
day11_space-police.cpp
File metadata and controls
108 lines (95 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include "IntcodeComputer.cpp"
#include "coordinate.cpp"
enum Color { Black, White };
enum Turn { LeftTurn, RightTurn };
enum Direction { Up, Right, Down, Left};
using PaintedPanels = unordered_map<Coordinate, Color, CoordinateHash>;
PaintedPanels paint(const Color startPanelColor = Black) {
PaintedPanels panels;
const auto getPanelColor = [&](Coordinate c) {
const auto panelColor = panels.find(c);
if(panelColor == panels.end()) {
panels.insert({c, Black});
return Black;
}
else {
return panelColor->second;
}
};
const auto painRobotProgram = parseIntcode(getPuzzleInput("inputs/aoc_day11_1.txt").front());
auto robotProgram = runProgram(painRobotProgram);
Coordinate robotCoordinate {0, 0};
Direction robotDirection = Up;
bool isFirstPanel = true;
while(!robotProgram.terminated) {
const auto currentPanelColor = isFirstPanel ? startPanelColor : getPanelColor(robotCoordinate);
robotProgram.inputs.push(currentPanelColor);
robotProgram = runProgram(robotProgram);
const auto paintedColor = static_cast<Color>(robotProgram.outputs.at(robotProgram.outputs.size() - 2));
const auto nextTurn = static_cast<Turn>(robotProgram.outputs.back());
panels.insert_or_assign(robotCoordinate, paintedColor);
if((robotDirection == Up && nextTurn == LeftTurn) || (robotDirection == Down && nextTurn == RightTurn)) {
robotDirection = Left;
robotCoordinate.x -= 1;
} else if((robotDirection == Up && nextTurn == RightTurn) || (robotDirection == Down && nextTurn == LeftTurn)) {
robotDirection = Right;
robotCoordinate.x += 1;
} else if((robotDirection == Left && nextTurn == RightTurn) || (robotDirection == Right && nextTurn == LeftTurn)) {
robotDirection = Up;
robotCoordinate.y += 1;
} else {
robotDirection = Down;
robotCoordinate.y -= 1;
}
isFirstPanel = false;
}
return panels;
};
string printPaintedPanels(const PaintedPanels &p) {
const int amplitude = p.size();
Coordinate minCoord = {0, 0};
Coordinate maxCoord = {0, 0};
for (int y = -amplitude; y <= amplitude; y++)
{
for (int x = -amplitude; x <= amplitude; x++)
{
const Coordinate c = {x, y};
const auto panelColor = p.find(c);
if(panelColor != p.end()) {
if(minCoord.x > x) minCoord.x = x;
if(minCoord.y > y) minCoord.y = y;
if(maxCoord.x < x) maxCoord.x = x;
if(maxCoord.y < y) maxCoord.y = y;
}
}
}
string res = "";
for (int y = minCoord.y; y <= maxCoord.y; y++)
{
string line = "";
for (int x = minCoord.x; x <= maxCoord.x; x++)
{
const Coordinate c = {x, y};
const auto panelColor = p.find(c);
if(panelColor == p.end()) {
line += " ";
}
else {
line += panelColor->second == Black ? " " : "#";
}
}
res += line;
res += "$\n";
}
return res;
}
int main(int argc, char const *argv[])
{
testComputer();
const auto p1Panels = paint();
cout << "Part1, covered panels (uniq): " << p1Panels.size() << "\n";
const auto p2Panels = paint(White);
cout << "Part2, panels:\n\n" << printPaintedPanels(p2Panels) << "\n";
return 0;
}