Skip to content

Commit

Permalink
Day 11.
Browse files Browse the repository at this point in the history
  • Loading branch information
f00ale committed Dec 11, 2019
1 parent 777303b commit 7eaed40
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions f00ale-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ add_executable(p07 src/p07.cpp)
add_executable(p08 src/p08.cpp)
add_executable(p09 src/p09.cpp)
add_executable(p10 src/p10.cpp)
add_executable(p11 src/p11.cpp)
1 change: 1 addition & 0 deletions f00ale-cpp/data/p11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3,8,1005,8,321,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,50,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,73,1,1105,16,10,2,1004,8,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,103,1006,0,18,1,105,14,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,131,1006,0,85,1,1008,0,10,1006,0,55,2,104,4,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1001,8,0,168,2,1101,1,10,1006,0,14,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,196,1006,0,87,1006,0,9,1,102,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,228,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,1002,8,1,250,2,5,0,10,2,1009,9,10,2,107,17,10,1006,0,42,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1001,8,0,287,2,102,8,10,1006,0,73,1006,0,88,1006,0,21,101,1,9,9,1007,9,925,10,1005,10,15,99,109,643,104,0,104,1,21102,1,387353256856,1,21101,0,338,0,1105,1,442,21101,936332866452,0,1,21101,349,0,0,1105,1,442,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,21101,0,179357024347,1,21101,0,396,0,1105,1,442,21102,1,29166144659,1,21102,407,1,0,1105,1,442,3,10,104,0,104,0,3,10,104,0,104,0,21102,1,718170641252,1,21102,430,1,0,1106,0,442,21101,825012151040,0,1,21102,441,1,0,1106,0,442,99,109,2,21202,-1,1,1,21102,1,40,2,21102,1,473,3,21102,463,1,0,1105,1,506,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,468,469,484,4,0,1001,468,1,468,108,4,468,10,1006,10,500,1102,1,0,468,109,-2,2105,1,0,0,109,4,1202,-1,1,505,1207,-3,0,10,1006,10,523,21101,0,0,-3,22101,0,-3,1,21202,-2,1,2,21102,1,1,3,21102,1,542,0,1105,1,547,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,570,2207,-4,-2,10,1006,10,570,22102,1,-4,-4,1105,1,638,22102,1,-4,1,21201,-3,-1,2,21202,-2,2,3,21101,0,589,0,1106,0,547,22102,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,608,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,630,21202,-1,1,1,21102,630,1,0,105,1,505,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0
103 changes: 103 additions & 0 deletions f00ale-cpp/src/p11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
#include <map>

#include "intcode.h"

void p11(std::istream & is) {
int ans1 = 0;
std::vector<std::string> ans2;

auto input = readIntcode(is);

for(int problem : {1,2}) {
intcodemachine mach(input);
bool done = false;
int x = 0, y = 0;
int dx = 0, dy = -1;
std::map<std::tuple<int, int>, char> painted;
int out1 = 0, out2 = 0;
bool has1 = false;
if(problem == 2) {
painted[std::tuple(0, 0)] = '#';
}
while (!done) {
while (mach.step());
switch (mach.state) {
case intcodemachine::RUNNING:
// do nothing, we should not end up here
break;
case intcodemachine::WAITING: {
auto it = painted.find(std::tuple(x, y));
char col = '.';
if (it != painted.end()) col = it->second;
mach.addInput(col == '#' ? 1 : 0);
}
break;
case intcodemachine::OUTPUT:
if (has1) {
out2 = mach.output;
has1 = false;
painted[std::tuple(x, y)] = (out1 ? '#' : '.');
bool right = out2;
if (dy < 0) {
dy = 0;
dx = right ? 1 : -1;
} else if (dy > 0) {
dy = 0;
dx = right ? -1 : 1;
} else if (dx < 0) {
dx = 0;
dy = right ? -1 : 1;
} else {
dx = 0;
dy = right ? 1 : -1;
}
x += dx;
y += dy;
} else {
out1 = mach.output;
has1 = true;
}
break;
case intcodemachine::TERMINATED:
done = true;
break;
}
}
if(problem == 1) {
ans1 = painted.size();
} else {
int minx = std::numeric_limits<int>::max();
int miny = std::numeric_limits<int>::max();
int maxx = std::numeric_limits<int>::min();
int maxy = std::numeric_limits<int>::min();
for (auto[t, c] : painted) {
auto[x, y] = t;
if (x < minx) minx = x;
if (y < miny) miny = y;
if (x > maxx) maxx = x;
if (y > maxy) maxy = y;
}

auto xs = maxx - minx + 1;
auto ys = maxy - miny + 1;
ans2.resize(ys);
for (auto &v : ans2) v.resize(xs, ' ');
for (auto[t, c] : painted) {
auto[x, y] = t;
if(c == '#') ans2[y - miny][x - miny] = '*';
}
}
}
std::cout << ans1 << std::endl;
for (auto &s : ans2) {
std::cout << s << std::endl;
}
}
// 1918 fel
int main() {
p11(std::cin);
}

0 comments on commit 7eaed40

Please sign in to comment.