commit 0707d69626781fad2f435f8827b3796e1f8b9898
parent a87ad3701305cabdc637c1d94922014b4c0cb925
Author: bsandro <email@bsandro.tech>
Date: Sat, 14 Dec 2024 23:54:05 +0200
Day 14 p2
Diffstat:
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/day14/main.cpp b/day14/main.cpp
@@ -6,6 +6,8 @@
#include <cstdio>
#include <tuple>
#include <algorithm>
+#include <chrono>
+#include <thread>
#include "utils.hpp"
struct Vec2 {
@@ -70,10 +72,7 @@ void moveRobots(Data &robots, const Vec2 &mapSize, int seconds) {
int64_t part1(Data input, Vec2 mapSize) {
int64_t out = 1;
- //std::printf("map size: %dx%d\n", mapSize.x, mapSize.y);
- //printMap(input, mapSize);
moveRobots(input, mapSize, 100);
- //printMap(input, mapSize);
out *= countRobots(input, Vec2(0, mapSize.y/2-1), Vec2(mapSize.x/2-1, 0));
out *= countRobots(input, Vec2(mapSize.x/2+1, mapSize.y/2-1), Vec2(mapSize.x-1, 0));
@@ -82,8 +81,20 @@ int64_t part1(Data input, Vec2 mapSize) {
return out;
}
-int64_t part2(Data &input [[ maybe_unused ]]) {
- return 0;
+
+int64_t part2(Data &input, Vec2 mapSize) {
+ using namespace std::chrono_literals;
+ int i=1;
+ const int minRobots = mapSize.y*0.12;
+ for (; i<INT_MAX; ++i) {
+ moveRobots(input, mapSize, 1);
+ if (countRobots(input, Vec2(mapSize.x/2, mapSize.y-1), Vec2(mapSize.x/2, 0))>minRobots &&
+ countRobots(input, Vec2(0, mapSize.y/2), Vec2(mapSize.x-1, mapSize.y/2))>minRobots) {
+ break;
+ }
+ }
+
+ return i;
}
int main(int argc, char **argv) {
@@ -93,7 +104,7 @@ int main(int argc, char **argv) {
Data input = read_file<Data, Robot>(fname);
Vec2 mapSize = fname.contains("test") ? Vec2(11,7) : Vec2(101,103);
std::cout << "part1: " << part1(input, mapSize) << std::endl;
- std::cout << "part2: " << part2(input) << std::endl;
-
+ std::cout << "part2: " << part2(input, mapSize) << std::endl;
+ printMap(input, mapSize);
return 0;
}