Skip to content
Permalink
Browse files

2019 - 11.1 + 11.2 C++

I originally solved this on the 11th, but as I had broken my IntCode VM
I had to borrow someone's VM to solve the problem quickly.

In this case I used the day09.py from
https://github.com/benediktwerner/AdventOfCode/tree/74b00d79a5da05eb2919fd2000ce3bb8b46f33e5

This is a quite literal translation of my python code, now rebased on my
day09/day13 vm.
  • Loading branch information
winks committed Dec 13, 2019
1 parent 1f68137 commit 9247215a10239e6cfc6f7d43648ad4b169d737c1
Showing with 136 additions and 17 deletions.
  1. +134 −16 2019/day11.cpp
  2. +1 −0 2019/input/day11/part1
  3. +1 −1 README.md
@@ -2,7 +2,6 @@
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <vector>

@@ -38,6 +37,16 @@ struct instruction {
OpList params;
};

struct Point {
int64_t x = 0;
int64_t y = 0;
};

struct Image {
int64_t image[100][100];
int done = 0;
};

void ppl(OpList params, std::string prefix, std::string sep = " ")
{
std::cout << prefix;
@@ -126,7 +135,6 @@ instruction parseop(int64_t op)
do {
int64_t p = tmp % 10;
rv.params.push_back(p);
//rv.params.insert(rv.params.begin(), p);
tmp -= p;
tmp = tmp / 10;
} while (tmp > 0);
@@ -228,7 +236,7 @@ data calc(data v)
ppl(params, "# ADD ");

int64_t z = params[0] + params[1];
if ((uint64_t) pos > v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
if ((uint64_t) pos >= v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
v.op[pos] = z;
std::cout << "# ADD " << pos << " = " << v.op[pos] << " []" << v.op.size() << std::endl;
std::cout << "@#op1 " << pos << " " << v.op[pos] << std::endl;
@@ -242,7 +250,7 @@ data calc(data v)
ppl(params, "# MUL ");

int64_t z = params[0] * params[1];
if ((uint64_t) pos > v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
if ((uint64_t) pos >= v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
v.op[pos] = z;
std::cout << "# MUL " << pos << " = " << v.op[pos] << " []" << v.op.size() << std::endl;
std::cout << "@#op2 " << pos << " " << v.op[pos] << std::endl;
@@ -262,7 +270,7 @@ data calc(data v)
return v;
}

if ((uint64_t) pos > v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
if ((uint64_t) pos >= v.op.size()) v.op.resize((uint64_t)pos+RESIZE);
v.op[pos] = n;
std::cout << "# IN val:" << n << " pos " << pos << " = " << v.op[pos] << std::endl;
std::cout << "@#op3 " << pos << " " << v.op[pos] << std::endl;
@@ -463,9 +471,102 @@ std::vector<OpList> get_perms(bool part1 = true)
return rv;
}

void paint(uint64_t code, uint64_t start)
Image paint(OpList code, int64_t start)
{
data mx;
mx.inputs.clear();
mx.outputs.clear();
mx.output = 0;

mx.op = code;
mx.position = 0;
mx.status = 0;
mx.inputs.push_back(start);

int64_t image[100][100] = {};
int64_t done[100][100] = {};
char dir = '^';
Point cur;
cur.x = 50;
cur.y = 50;
int i = 0;
// 0 = black
// 1 = white
image[cur.x][cur.y] = 0;

do {
mx = calc(mx);
print(mx);
if (mx.outputs.size() > 1) {
int64_t color = mx.outputs.front();
image[cur.x][cur.y] = color;
done[cur.x][cur.y] = 1;
std::cout << "XCM (" << cur.x << "/" << cur.y << ")=" << image[cur.x][cur.y] << std::endl;
int64_t turn = mx.outputs.back();
mx.outputs.clear();
std::cout << "XMM1 (" << cur.x << "/" << cur.y << ") " << dir << " " << turn << std::endl;

if (turn == 0) {
switch (dir) {
case '^':
dir = '<';
cur.x -= 1;
break;
case '<':
dir = 'v';
cur.y -= 1;
break;
case 'v':
dir = '>';
cur.x += 1;
break;
case '>':
dir = '^';
cur.y += 1;
break;
default:
break;
}
} else {
switch (dir) {
case '^':
dir = '>';
cur.x += 1;
break;
case '>':
dir = 'v';
cur.y -= 1;
break;
case 'v':
dir = '<';
cur.x -= 1;
break;
case '<':
dir = '^';
cur.y += 1;
break;
default:
break;
}
}
std::cout << "XMM2 (" << cur.x << "/" << cur.y << ") " << dir << std::endl;
mx.inputs.push_back(image[cur.x][cur.y]);
}

++i;
} while(mx.position < mx.op.size()-1 && mx.status != 10 && mx.status != 1);

Image im;
int rv = 0;
for (int i=0; i<100;++i) {
for (int j=0; j<100;++j) {
rv += done[i][j];
im.image[i][j] = image[i][j];
}
}

im.done = rv;
return im;
}

int main(int argc, char *argv[])
@@ -484,14 +585,31 @@ int main(int argc, char *argv[])
if (ops.size() < 1) return 1;

// part 1
data mx;
mx.inputs.clear();
mx.outputs.clear();
mx.output = 0;

mx.op = ops;
mx.position = 0;
mx.status = 0;
mx = runEngine(mx, {0}, 0);
print(mx);
Image image = paint(ops, 0);
std::cout << "XP1: " << image.done << std::endl;

// part 2
image = paint(ops, 1);

int minx = 100;
int maxx = 0;
int miny = 100;
int maxy = 0;
for (int i=0; i<100; ++i) {
for (int j=0; j<100; ++j) {
if (image.image[i][j] == 0) continue;
if (i > maxx) maxx = i;
if (j > maxy) maxy = j;
if (i < minx) minx = i;
if (j < miny) miny = j;
}
}
for (int i=maxy; i>=miny; --i) {
for (int j=minx; j<=maxx; ++j) {
if (image.image[j][i] == 1) std::cout << "\u2588";
else std::cout<< " ";
}
std::cout<< std::endl;
}
return 0;
}
@@ -0,0 +1 @@
3,8,1005,8,299,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,28,1006,0,85,1,106,14,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,58,1,1109,15,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,84,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,105,1006,0,48,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,130,1006,0,46,1,1001,17,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,160,2,109,20,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1002,8,1,185,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,207,1006,0,89,2,1002,6,10,1,1007,0,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,241,2,4,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,101,0,8,267,1,1107,8,10,1,109,16,10,2,1107,4,10,101,1,9,9,1007,9,1003,10,1005,10,15,99,109,621,104,0,104,1,21101,0,387239486208,1,21102,316,1,0,1106,0,420,21101,0,936994976664,1,21102,327,1,0,1105,1,420,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,29192457307,1,21102,1,374,0,1106,0,420,21101,0,3450965211,1,21101,0,385,0,1106,0,420,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,837901103972,1,21101,408,0,0,1106,0,420,21102,867965752164,1,1,21101,0,419,0,1105,1,420,99,109,2,22102,1,-1,1,21102,40,1,2,21102,451,1,3,21102,1,441,0,1106,0,484,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,446,447,462,4,0,1001,446,1,446,108,4,446,10,1006,10,478,1102,0,1,446,109,-2,2105,1,0,0,109,4,1201,-1,0,483,1207,-3,0,10,1006,10,501,21101,0,0,-3,22101,0,-3,1,22102,1,-2,2,21101,1,0,3,21101,520,0,0,1106,0,525,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,548,2207,-4,-2,10,1006,10,548,21201,-4,0,-4,1105,1,616,22101,0,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,567,0,1106,0,525,22101,0,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,586,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,608,21202,-1,1,1,21102,608,1,0,106,0,483,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0
@@ -30,7 +30,7 @@
* day10 - *Monitoring Station*
* **Python3** - 1+2
* day11 - *Space Police* - IntCode
* TBD
* *C++** - 1+2
* day12 - *The N-Body Problem*
* **Lua 5.2** - 1 + 2
* day13 - *Care Package* - IntCode

0 comments on commit 9247215

Please sign in to comment.
You can’t perform that action at this time.