advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit a87ad3701305cabdc637c1d94922014b4c0cb925
parent 4fa261e7e673f777b63de4f98918d71a8d6c3490
Author: bsandro <email@bsandro.tech>
Date:   Sat, 14 Dec 2024 23:07:10 +0200

Day 14 p1 cleanup

Diffstat:
Mday14/main.cpp | 37++++++++++++++++---------------------
1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/day14/main.cpp b/day14/main.cpp @@ -57,40 +57,35 @@ int countRobots(const Data &robots, const Vec2 &from, const Vec2 &to) { }); } -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); - for (auto &r:input) { - // std::printf("Robot pos: %d,%d; velocity: %d,%d\n", r.p.x, r.p.y, r.v.x, r.v.y); - r.p.x += r.v.x*100; - r.p.y += r.v.y*100; +void moveRobots(Data &robots, const Vec2 &mapSize, int seconds) { + for (auto &r:robots) { + r.p.x += r.v.x*seconds; + r.p.y += r.v.y*seconds; r.p.x %= mapSize.x; r.p.y %= mapSize.y; if (r.p.x<0) r.p.x += mapSize.x; if (r.p.y<0) r.p.y += mapSize.y; - // std::printf("100s later: %d,%d\n", r.p.x, r.p.y); } - printMap(input, mapSize); +} - auto quadrantCount = [&input](const Vec2 &from, const Vec2 &to) -> int { - int cnt = countRobots(input, from, to); - // std::printf("robots in (%d:%d->%d:%d): %d\n", from.x, from.y, to.x, to.y, cnt); - return cnt; - }; +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 *= quadrantCount(Vec2(0, mapSize.y/2-1), Vec2(mapSize.x/2-1, 0)); - out *= quadrantCount(Vec2(mapSize.x/2+1, mapSize.y/2-1), Vec2(mapSize.x-1, 0)); - out *= quadrantCount(Vec2(0, mapSize.y-1), Vec2(mapSize.x/2-1, mapSize.y/2+1)); - out *= quadrantCount(Vec2(mapSize.x/2+1, mapSize.y-1), Vec2(mapSize.x-1, mapSize.y/2+1)); + 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)); + out *= countRobots(input, Vec2(0, mapSize.y-1), Vec2(mapSize.x/2-1, mapSize.y/2+1)); + out *= countRobots(input, Vec2(mapSize.x/2+1, mapSize.y-1), Vec2(mapSize.x-1, mapSize.y/2+1)); return out; } int64_t part2(Data &input [[ maybe_unused ]]) { return 0; } -// 506 too low -// 246740364 too high + int main(int argc, char **argv) { Performance perf; const std::string fname = argc>1 ? argv[1] : "test1.txt";