aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/14/Makefile24
-rwxr-xr-x2024/14/clean.sh3
-rw-r--r--2024/14/day14a.cpp40
-rw-r--r--2024/14/day14b.cpp85
-rw-r--r--2024/template.cpp1
5 files changed, 153 insertions, 0 deletions
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 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day14a.cpp
5
6b:
7 ${CC} -o b.out day14b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: b
22 ./b.out < input
23
24.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 @@
1#!/bin/sh
2
3sed '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 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <ranges>
6#include <set>
7#include <sstream>
8#include <string>
9#include <string_view>
10#include <vector>
11using namespace std;
12
13#define N 101
14#define M 103
15#define T 100
16
17int quadrant(pair<int64_t, int64_t> p) {
18 if (p.first < N/2 && p.second < M/2)
19 return 1;
20 if (p.first < N/2 && p.second > M/2)
21 return 2;
22 if (p.first > N/2 && p.second < M/2)
23 return 3;
24 if (p.first > N/2 && p.second > M/2)
25 return 4;
26 return 0;
27}
28
29int main() {
30 string line;
31 int64_t p1, p2, v1, v2, q[5] = {0};
32 while (cin >> p1 >> p2 >> v1 >> v2) {
33 pair<int64_t, int64_t> final((p1 + T*(v1+N)) % N, (p2 + T*(v2+M)) % M);
34 q[quadrant(final)]++;
35 }
36
37 cout << q[1] * q[2] * q[3] * q[4] << endl;
38
39 return 0;
40}
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 @@
1/*
2I ran the simulation at 1FPS for a few minutes, staring at the screen and
3hoping to see a Christmas tree, without luck. But I noticed that the robots
4formed a strange "vertical" pattern every 103 timesteps, starting at t=30,
5and a strange "horizontal" pattern every 101 timesteps, starting at t=81.
6So I solved the diophantine equation 30+103y = 81+101x, and the smallest
7solution (y=76, x=77) gave the correct answer t=30+103*76=7858
8*/
9
10#include <algorithm>
11#include <cstdint>
12#include <iostream>
13#include <map>
14#include <ranges>
15#include <set>
16#include <sstream>
17#include <string>
18#include <string_view>
19#include <unistd.h>
20#include <vector>
21using namespace std;
22
23#define N 101
24#define M 103
25#define T 100
26
27char board[N][M];
28
29struct Robot {
30 pair<int64_t, int64_t> p, v;
31 Robot(int64_t p1, int64_t p2, int64_t v1, int64_t v2) : p(p1, p2), v(v1, v2) {}
32};
33
34int quadrant(pair<int64_t, int64_t> p) {
35 if (p.first < N/2 && p.second < M/2)
36 return 1;
37 if (p.first < N/2 && p.second > M/2)
38 return 2;
39 if (p.first > N/2 && p.second < M/2)
40 return 3;
41 if (p.first > N/2 && p.second > M/2)
42 return 4;
43 return 0;
44}
45
46pair<int64_t, int64_t> pos(Robot r, int64_t t) {
47 return make_pair((r.p.first + t*(r.v.first+N))%N,
48 (r.p.second + t*(r.v.second+M))%M);
49}
50
51void visualize(vector<Robot>& robots, int64_t t) {
52 for (int i = 0; i < N; i++)
53 for (int j = 0; j < M; j++)
54 board[i][j] = ' ';
55 for (const auto& r : robots) {
56 auto [x,y] = pos(r, t);
57 board[x][y] = '*';
58 }
59 for (int i = 0; i < N; i++) {
60 for (int j = 0; j < M; j++)
61 cout << board[i][j];
62 cout << endl;
63 }
64 cout << endl;
65}
66
67int main() {
68 string line;
69 int64_t p1, p2, v1, v2, q[5] = {0};
70 vector<Robot> r;
71 while (cin >> p1 >> p2 >> v1 >> v2)
72 r.push_back(Robot(p1, p2, v1, v2));
73
74
75 /* Starts at the solution */
76 for (int64_t t = 7858; ; t++) {
77 cout << "visualization of time " << t << ": " << endl;
78 sleep(1);
79 visualize(r, t);
80 }
81
82 cout << q[1] * q[2] * q[3] * q[4] << endl;
83
84 return 0;
85}
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 @@
1#include <algorithm> 1#include <algorithm>
2#include <cstdint>
2#include <iostream> 3#include <iostream>
3#include <map> 4#include <map>
4#include <ranges> 5#include <ranges>

Generated with cgit - Back to sebastiano.tronto.net