-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13_care-package.cpp
120 lines (101 loc) · 3.55 KB
/
day13_care-package.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <algorithm>
#include "IntcodeComputer.cpp"
#include "coordinate.cpp"
enum class Tile {Empty, Wall, Block, HorizontalPaddle, Ball};
enum class JoystickMove {Left=-1, Neutral=0, Right=1};
using Screen = unordered_map<Coordinate, Tile, CoordinateHash>;
Screen readGameScreen(const vector<IntCode> &programOutputs) {
Screen screen;
for(size_t i = 0; i < programOutputs.size(); i += 3) {
const int x = programOutputs.at(i);
const int y = programOutputs.at(i+1);
if(x >= 0 && y >= 0) {
const Tile currentTile = static_cast<Tile>(programOutputs.at(i+2));
screen.insert_or_assign({x, y}, currentTile);
}
}
return screen;
}
IntCode readGameScore(const vector<IntCode> &programOutputs) {
IntCode score = 0;
for(size_t i = 0; i < programOutputs.size(); i += 3) {
const int x = programOutputs.at(i);
const int y = programOutputs.at(i+1);
if(x == -1 && y == 0) {
score = programOutputs.at(i+2);
}
}
return score;
}
string screenToString(const Screen &s) {
size_t maxX = 0;
size_t maxY = 0;
for(const auto [coord, tile] : s) {
if(maxX < coord.x) maxX = coord.x;
if(maxY < coord.y) maxY = coord.y;
}
string res = "";
for (size_t y = 0; y <= maxY; y++) {
for (size_t x = 0; x <= maxX; x++) {
const auto t = s.at({x,y});
res +=
t == Tile::Empty ? " " :
t == Tile::Wall ? "%" :
t == Tile::Block ? "#" :
t == Tile::HorizontalPaddle ? "_" :
"O";
}
res += "\n";
}
return res;
}
int main(int argc, char const *argv[])
{
testComputer();
const auto gameInput = parseIntcode(getPuzzleInput("inputs/aoc_day13_1.txt").front());
// Part 1
const auto gameProgram = runProgram(gameInput);
const auto screen = readGameScreen(gameProgram.outputs);
const auto blockTileCount = count_if(screen.cbegin(), screen.cend(), [](pair<Coordinate, Tile> entry){
return entry.second == Tile::Block;
});
cout << "Part1, count of Block Tiles: " << blockTileCount << "\n";
// Part 2
auto freeGameInput = gameInput;
freeGameInput[0] = 2;
auto freeGameProgram = runProgram(freeGameInput);
while (!freeGameProgram.terminated) {
// Screen reading could be optimized, only updated tiles are outputed, no need to read everything
const auto screen = readGameScreen(freeGameProgram.outputs);
const auto ball = find_if(screen.cbegin(), screen.cend(), [](pair<Coordinate, Tile> entry){
return entry.second == Tile::Ball;
});
const auto paddle = find_if(screen.cbegin(), screen.cend(), [](pair<Coordinate, Tile> entry){
return entry.second == Tile::HorizontalPaddle;
});
if(ball == screen.cend()) {
cerr << "Ball not found\n";
throw;
}
if(paddle == screen.cend()) {
cerr << "Paddle not found\n";
throw;
}
const Coordinate ballCoordinate = ball->first;
const Coordinate paddleCoordinate = paddle->first;
const JoystickMove nextMove =
ballCoordinate.x < paddleCoordinate.x ? JoystickMove::Left :
ballCoordinate.x > paddleCoordinate.x ? JoystickMove::Right :
JoystickMove::Neutral;
cout << screenToString(screen);
cout << "Score: " << readGameScore(freeGameProgram.outputs) << "\n";
cout << ballCoordinate << ", ";
cout << paddleCoordinate << " => ";
cout << static_cast<IntCode>(nextMove) << "\n\n";
freeGameProgram.inputs.push(static_cast<IntCode>(nextMove));
freeGameProgram = runProgram(freeGameProgram);
}
cout << "Part2: game over, final score is " << readGameScore(freeGameProgram.outputs) << "\n";
return 0;
}