2024 day 14

This commit is contained in:
eriedaberrie 2024-12-13 21:46:14 -08:00
parent d82693c074
commit 137aa570e3
2 changed files with 131 additions and 0 deletions

58
2024/14/main-1.cpp Normal file
View file

@ -0,0 +1,58 @@
#include <bits/stdc++.h>
using namespace std;
const int mx = 101;
const int my = 103;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<pair<pair<int, int>, pair<int, int>>> robots;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
l.ignore(2);
int px, py, vx, vy;
l >> px;
l.ignore(1);
l >> py;
l.ignore(3);
l >> vx;
l.ignore(1);
l >> vy;
if (vx < 0) {
vx += mx;
}
if (vy < 0) {
vy += my;
}
robots.push_back({{px, py}, {vx, vy}});
}
int q1 = 0, q2 = 0, q3 = 0, q4 = 0;
for (auto &[p, v] : robots) {
auto &[px, py] = p;
auto &[vx, vy] = v;
for (int i = 0; i < 100; i++) {
px = (px + vx) % mx;
py = (py + vy) % my;
}
if (px < mx / 2) {
if (py < my / 2) {
q1 += 1;
} else if (py > my / 2) {
q2 += 1;
}
} else if (px > mx / 2) {
if (py < my / 2) {
q3 += 1;
} else if (py > my / 2) {
q4 += 1;
}
}
}
cout << q1 * q2 * q3 * q4 << '\n';
}

73
2024/14/main-2.cpp Normal file
View file

@ -0,0 +1,73 @@
#include <bits/stdc++.h>
using namespace std;
const int mx = 101;
const int my = 103;
int main(int argc, char *argv[]) {
ios::sync_with_stdio(0);
ifstream fin(argc > 0 ? argv[1] : "data.txt");
// NOTE: 14 and 101 for my dataset
int start = argc >= 3 ? atoi(argv[2]) : 0;
int step = argc >= 3 ? atoi(argv[3]) : 1;
vector<pair<pair<int, int>, pair<int, int>>> robots;
string line;
while (getline(fin, line) && !line.empty()) {
istringstream l(line);
l.ignore(2);
int px, py, vx, vy;
l >> px;
l.ignore(1);
l >> py;
l.ignore(3);
l >> vx;
l.ignore(1);
l >> vy;
if (vx < 0) {
vx += mx;
}
if (vy < 0) {
vy += my;
}
robots.push_back({{px, py}, {vx, vy}});
}
char m[my][mx];
int n = start;
for (auto &[p, v] : robots) {
auto &[px, py] = p;
auto &[vx, vy] = v;
for (int i = 0; i < start; i++) {
px = (px + vx) % mx;
py = (py + vy) % my;
}
}
for (;;) {
memset(m, ' ', mx * my);
for (auto &[p, v] : robots) {
auto &[px, py] = p;
auto &[vx, vy] = v;
m[py][px] = '*';
for (int i = 0; i < step; i++) {
px = (px + vx) % mx;
py = (py + vy) % my;
}
}
for (int y = 0; y < my; y++) {
for (int x = 0; x < mx; x++) {
cout << m[y][x];
}
cout << '\n';
}
cout << n << '\n';
n += step;
cin.ignore();
}
}