From d9fbaa155185220ee141f75d764890983ef35ca2 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sat, 14 Dec 2024 08:38:52 +0100 Subject: Day 14 2024 (I did not like this one) --- 2024/14/Makefile | 24 +++++++++++++++ 2024/14/clean.sh | 3 ++ 2024/14/day14a.cpp | 40 +++++++++++++++++++++++++ 2024/14/day14b.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2024/template.cpp | 1 + 5 files changed, 153 insertions(+) create mode 100644 2024/14/Makefile create mode 100755 2024/14/clean.sh create mode 100644 2024/14/day14a.cpp create mode 100644 2024/14/day14b.cpp (limited to '2024') diff --git a/2024/14/Makefile b/2024/14/Makefile new file mode 100644 index 0000000..7f11c27 --- /dev/null +++ b/2024/14/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day14a.cpp + +b: + ${CC} -o b.out day14b.cpp + +clean: + rm -f a b + +atest: a + ./a.out + +btest: b + ./b.out + +arun: a + ./a.out < input + +brun: b + ./b.out < input + +.PHONY: a b clean atest btest arun brun diff --git a/2024/14/clean.sh b/2024/14/clean.sh new file mode 100755 index 0000000..ee99b15 --- /dev/null +++ b/2024/14/clean.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +sed 's/p=//; s/v=//; s/,/ /g' diff --git a/2024/14/day14a.cpp b/2024/14/day14a.cpp new file mode 100644 index 0000000..dff0ae9 --- /dev/null +++ b/2024/14/day14a.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 101 +#define M 103 +#define T 100 + +int quadrant(pair p) { + if (p.first < N/2 && p.second < M/2) + return 1; + if (p.first < N/2 && p.second > M/2) + return 2; + if (p.first > N/2 && p.second < M/2) + return 3; + if (p.first > N/2 && p.second > M/2) + return 4; + return 0; +} + +int main() { + string line; + int64_t p1, p2, v1, v2, q[5] = {0}; + while (cin >> p1 >> p2 >> v1 >> v2) { + pair final((p1 + T*(v1+N)) % N, (p2 + T*(v2+M)) % M); + q[quadrant(final)]++; + } + + cout << q[1] * q[2] * q[3] * q[4] << endl; + + return 0; +} diff --git a/2024/14/day14b.cpp b/2024/14/day14b.cpp new file mode 100644 index 0000000..2402c0c --- /dev/null +++ b/2024/14/day14b.cpp @@ -0,0 +1,85 @@ +/* +I ran the simulation at 1FPS for a few minutes, staring at the screen and +hoping to see a Christmas tree, without luck. But I noticed that the robots +formed a strange "vertical" pattern every 103 timesteps, starting at t=30, +and a strange "horizontal" pattern every 101 timesteps, starting at t=81. +So I solved the diophantine equation 30+103y = 81+101x, and the smallest +solution (y=76, x=77) gave the correct answer t=30+103*76=7858 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 101 +#define M 103 +#define T 100 + +char board[N][M]; + +struct Robot { + pair p, v; + Robot(int64_t p1, int64_t p2, int64_t v1, int64_t v2) : p(p1, p2), v(v1, v2) {} +}; + +int quadrant(pair p) { + if (p.first < N/2 && p.second < M/2) + return 1; + if (p.first < N/2 && p.second > M/2) + return 2; + if (p.first > N/2 && p.second < M/2) + return 3; + if (p.first > N/2 && p.second > M/2) + return 4; + return 0; +} + +pair pos(Robot r, int64_t t) { + return make_pair((r.p.first + t*(r.v.first+N))%N, + (r.p.second + t*(r.v.second+M))%M); +} + +void visualize(vector& robots, int64_t t) { + for (int i = 0; i < N; i++) + for (int j = 0; j < M; j++) + board[i][j] = ' '; + for (const auto& r : robots) { + auto [x,y] = pos(r, t); + board[x][y] = '*'; + } + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) + cout << board[i][j]; + cout << endl; + } + cout << endl; +} + +int main() { + string line; + int64_t p1, p2, v1, v2, q[5] = {0}; + vector r; + while (cin >> p1 >> p2 >> v1 >> v2) + r.push_back(Robot(p1, p2, v1, v2)); + + + /* Starts at the solution */ + for (int64_t t = 7858; ; t++) { + cout << "visualization of time " << t << ": " << endl; + sleep(1); + visualize(r, t); + } + + cout << q[1] * q[2] * q[3] * q[4] << endl; + + return 0; +} diff --git a/2024/template.cpp b/2024/template.cpp index 2475ae7..e3fadb3 100644 --- a/2024/template.cpp +++ b/2024/template.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include -- cgit v1.3