advent2024

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

commit 4fa261e7e673f777b63de4f98918d71a8d6c3490
parent 92ab835688bf0b22bf7707d1890e82db304e8282
Author: bsandro <email@bsandro.tech>
Date:   Sat, 14 Dec 2024 23:02:32 +0200

Day 14 p1

Diffstat:
Mday14/main.cpp | 47++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/day14/main.cpp b/day14/main.cpp @@ -39,17 +39,58 @@ T read_file(const std::string &path) { return buf; } +void printMap(const Data &robots, const Vec2 &mapSize) { + for (int y=0; y<mapSize.y; ++y) { + for (int x=0; x<mapSize.x; ++x) { + if (std::count_if(robots.begin(), robots.end(), [&x, &y](const Robot &r) {return r.p.x==x&&r.p.y==y;})>0) { + std::printf("+"); + } else std::printf("."); + } + std::printf("\n"); + } + std::printf("\n"); +} + +int countRobots(const Data &robots, const Vec2 &from, const Vec2 &to) { + return std::count_if(robots.begin(), robots.end(), [&from, &to](const Robot &r) { + return (r.p.x>=from.x && r.p.y<=from.y && r.p.x<=to.x && r.p.y>=to.y); + }); +} + 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); + // 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; + 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); } - return 0; + 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; + }; + + 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)); + + 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";