-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
128 lines (115 loc) · 4.17 KB
/
main.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
121
122
123
124
125
126
127
128
#include "file.h"
#include "intcode.h"
#include <iostream>
namespace aoc2019_day23 {
struct packet {
unsigned long long x {0};
unsigned long long y {0};
};
unsigned long long part_1(std::string_view path) {
std::vector<unsigned long long> input = file::readFileAsArrayULL(path, ",");
std::vector<intcode::IntCode<unsigned long long>> computers(50, {input, {}});
std::vector<std::vector<packet>> packets(50);
for (int i = 0; i < 50; ++i) {
computers[i].inputIDs.push_back(i);
}
while (true) {
for (int i = 0; i < 50; ++i) {
auto exit = computers[i].advance();
while (!exit.needInput && !exit.halt) {
unsigned long long id = exit.returnValue;
packet p;
p.x = computers[i].advance().returnValue;
p.y = computers[i].advance().returnValue;
if (id == 255) {
return p.y;
}
else if (id < 50) {
packets[id].push_back(p);
}
exit = computers[i].advance();
}
}
for (int i = 0; i < 50; ++i) {
for (const auto& p : packets[i]) {
computers[i].inputIDs.push_back(p.x);
computers[i].inputIDs.push_back(p.y);
}
packets[i].clear();
}
for (int i = 0; i < 50; ++i) {
if (computers[i].inputIDs.empty()) {
computers[i].inputIDs.push_back(-1);
}
}
}
}
unsigned long long part_2(std::string_view path) {
std::vector<unsigned long long> input = file::readFileAsArrayULL(path, ",");
std::vector<intcode::IntCode<unsigned long long>> computers(50, {input, {}});
std::vector<std::vector<packet>> packets(50);
packet NAT;
unsigned long long formerNATy = 0;
for (int i = 0; i < 50; ++i) {
computers[i].inputIDs.push_back(i);
computers[i].advance();
computers[i].inputIDs.push_back(-1);
}
while (true) {
for (int i = 0; i < 50; ++i) {
auto exit = computers[i].advance();
while (!exit.needInput && !exit.halt) {
unsigned long long id = exit.returnValue;
packet p;
p.x = computers[i].advance().returnValue;
p.y = computers[i].advance().returnValue;
if (id == 255) {
NAT = p;
}
else if (id < 50) {
packets[id].push_back(p);
}
exit = computers[i].advance();
}
}
for (int i = 0; i < 50; ++i) {
for (const auto& p : packets[i]) {
computers[i].inputIDs.push_back(p.x);
computers[i].inputIDs.push_back(p.y);
}
packets[i].clear();
}
bool idle = true;
for (const auto& c : computers) {
if (!c.inputIDs.empty()) {
idle = false;
}
}
for (const auto& p : packets) {
if (!p.empty()) {
idle = false;
}
}
if (idle) {
if (formerNATy == NAT.y) {
return formerNATy;
}
formerNATy = NAT.y;
computers[0].inputIDs.push_back(NAT.x);
computers[0].inputIDs.push_back(NAT.y);
}
for (int i = 0; i < 50; ++i) {
if (computers[i].inputIDs.empty()) {
computers[i].inputIDs.push_back(-1);
}
}
}
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2019_day23::part_1("../2019/day23/input.in") << std::endl;
std::cout << "Part 2: " << aoc2019_day23::part_2("../2019/day23/input.in") << std::endl;
return 0;
}
#endif