Finish day 14

This commit is contained in:
Leon Vatthauer 2024-12-17 00:10:28 +01:00
parent 968ff8cec6
commit a235d54267
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
3 changed files with 693 additions and 0 deletions

12
14/example Normal file
View file

@ -0,0 +1,12 @@
p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3

500
14/input Normal file
View file

@ -0,0 +1,500 @@
p=73,39 v=-17,38
p=30,80 v=27,22
p=36,10 v=-86,-24
p=56,22 v=6,-46
p=75,64 v=98,-83
p=78,34 v=33,-38
p=83,88 v=-83,55
p=4,0 v=-48,44
p=45,75 v=42,2
p=27,47 v=75,90
p=75,100 v=-90,-61
p=45,29 v=-36,-76
p=73,64 v=48,-7
p=40,7 v=-87,-62
p=43,35 v=-57,11
p=74,77 v=77,70
p=11,42 v=9,4
p=98,28 v=75,34
p=34,42 v=66,-55
p=15,97 v=2,6
p=26,43 v=57,-72
p=84,24 v=-34,36
p=56,79 v=64,67
p=59,30 v=61,-81
p=94,19 v=68,-20
p=55,9 v=-37,83
p=11,69 v=18,75
p=3,45 v=24,76
p=48,88 v=20,-75
p=10,88 v=-13,59
p=77,66 v=47,36
p=28,46 v=73,88
p=2,6 v=-96,18
p=98,80 v=89,-75
p=1,71 v=-77,62
p=68,99 v=89,-42
p=84,35 v=-10,-8
p=26,102 v=-94,-93
p=44,72 v=-84,-51
p=11,39 v=-56,65
p=78,5 v=-38,-63
p=99,101 v=59,-21
p=87,90 v=-77,27
p=58,38 v=-29,75
p=45,69 v=86,-3
p=74,4 v=44,55
p=23,92 v=-71,44
p=97,70 v=-41,-83
p=55,89 v=-9,70
p=55,99 v=-14,58
p=82,72 v=-75,-60
p=10,90 v=58,-38
p=29,49 v=76,-24
p=25,83 v=-88,40
p=19,3 v=1,52
p=5,40 v=-84,19
p=40,28 v=70,27
p=85,0 v=69,-58
p=72,65 v=39,24
p=52,92 v=-73,90
p=75,6 v=41,94
p=93,79 v=75,51
p=15,35 v=-85,46
p=2,8 v=-49,-32
p=57,20 v=-87,-47
p=67,32 v=78,50
p=15,53 v=-55,92
p=12,64 v=59,-48
p=7,50 v=60,-99
p=27,13 v=-92,-44
p=25,27 v=72,41
p=29,31 v=-50,-50
p=64,39 v=49,-46
p=8,95 v=-99,33
p=0,49 v=-3,35
p=19,99 v=89,14
p=35,68 v=-14,66
p=36,55 v=94,81
p=40,32 v=22,45
p=100,64 v=68,-47
p=38,30 v=87,89
p=69,8 v=-30,72
p=54,71 v=-37,62
p=37,68 v=-65,85
p=81,56 v=62,54
p=41,1 v=42,10
p=7,94 v=31,59
p=7,17 v=18,16
p=41,42 v=-29,38
p=17,25 v=-63,60
p=72,22 v=87,5
p=6,42 v=-70,42
p=100,87 v=-98,-59
p=35,88 v=15,-86
p=50,68 v=-1,-79
p=30,29 v=-86,30
p=40,90 v=-29,-78
p=51,80 v=-88,-52
p=10,15 v=-45,-25
p=50,88 v=29,-75
p=4,16 v=17,-31
p=73,34 v=62,-46
p=81,90 v=-2,9
p=25,101 v=87,29
p=68,9 v=-2,-5
p=80,61 v=98,-4
p=35,17 v=-78,78
p=4,41 v=89,-61
p=76,22 v=-81,60
p=54,9 v=57,75
p=69,18 v=41,-1
p=77,46 v=25,38
p=69,58 v=67,7
p=4,97 v=90,-1
p=90,65 v=-12,66
p=63,82 v=96,-95
p=15,84 v=-55,-29
p=81,13 v=33,7
p=82,33 v=91,53
p=3,51 v=-69,-15
p=68,102 v=12,3
p=2,52 v=-52,79
p=79,56 v=-74,-76
p=69,29 v=-73,9
p=34,35 v=72,73
p=93,83 v=97,-42
p=95,63 v=17,77
p=80,101 v=32,44
p=51,57 v=-22,3
p=95,79 v=-40,-25
p=65,101 v=-2,-40
p=91,74 v=-46,-63
p=69,74 v=56,63
p=12,86 v=31,55
p=13,32 v=-57,8
p=21,29 v=30,38
p=67,28 v=-32,-96
p=41,86 v=-15,-48
p=7,82 v=-40,-90
p=64,60 v=63,5
p=29,48 v=-43,1
p=1,28 v=46,-46
p=48,63 v=-51,-26
p=67,54 v=40,-75
p=91,63 v=-32,13
p=51,94 v=43,-29
p=28,12 v=-71,34
p=98,99 v=17,-59
p=53,0 v=90,-80
p=86,76 v=47,39
p=59,69 v=56,-60
p=80,55 v=50,50
p=89,23 v=25,91
p=79,97 v=-10,-74
p=13,69 v=-92,89
p=22,102 v=-36,83
p=7,82 v=-67,93
p=85,85 v=69,-2
p=37,40 v=-58,-31
p=70,31 v=42,-45
p=57,95 v=12,33
p=21,93 v=-64,-40
p=47,29 v=14,57
p=5,101 v=-42,-56
p=78,53 v=-39,73
p=92,34 v=5,-16
p=77,98 v=17,57
p=69,11 v=-72,-14
p=65,41 v=99,-15
p=66,90 v=-9,9
p=76,62 v=98,-22
p=22,35 v=-78,84
p=72,95 v=-38,-17
p=59,35 v=12,-30
p=52,94 v=-59,67
p=52,100 v=77,-25
p=16,62 v=-50,-91
p=57,23 v=-59,-77
p=90,38 v=-33,-46
p=55,39 v=85,-99
p=13,84 v=-92,-59
p=62,48 v=42,2
p=4,13 v=62,86
p=74,0 v=-24,-55
p=22,87 v=95,48
p=20,73 v=-32,-34
p=19,81 v=-6,-33
p=42,82 v=65,32
p=52,45 v=-80,-27
p=3,35 v=18,30
p=70,77 v=41,-18
p=9,48 v=-39,-30
p=39,7 v=43,-28
p=98,102 v=-47,29
p=37,39 v=-8,-66
p=75,47 v=-17,-41
p=70,102 v=-15,-84
p=24,39 v=-64,-15
p=70,91 v=76,56
p=25,98 v=65,41
p=88,60 v=-4,88
p=82,79 v=-97,18
p=7,11 v=-78,90
p=41,35 v=65,38
p=92,23 v=65,-65
p=82,40 v=26,-31
p=42,27 v=26,-40
p=92,102 v=32,67
p=64,88 v=-74,-17
p=30,41 v=81,-15
p=26,8 v=73,-70
p=44,10 v=-51,-50
p=15,10 v=15,-58
p=34,102 v=72,-32
p=40,73 v=-43,-52
p=47,63 v=87,-22
p=90,43 v=-83,-76
p=24,93 v=-58,90
p=37,15 v=-15,52
p=71,45 v=-59,42
p=6,14 v=-92,10
p=34,20 v=-14,7
p=22,4 v=-93,-51
p=60,91 v=-23,-13
p=35,54 v=-42,69
p=34,9 v=51,79
p=29,65 v=58,16
p=96,5 v=-67,31
p=22,19 v=-6,7
p=63,62 v=27,41
p=94,0 v=32,71
p=31,24 v=-64,-73
p=51,39 v=94,82
p=37,44 v=-79,-34
p=77,33 v=37,-8
p=67,78 v=34,-78
p=25,57 v=23,-17
p=23,57 v=-70,-91
p=12,28 v=-93,-61
p=37,44 v=58,-57
p=27,81 v=28,63
p=68,50 v=-44,42
p=89,10 v=11,22
p=40,51 v=-38,55
p=91,64 v=55,5
p=11,94 v=96,79
p=3,90 v=3,78
p=51,58 v=-51,77
p=87,42 v=60,-91
p=35,87 v=73,91
p=38,27 v=-66,57
p=71,71 v=84,-10
p=39,65 v=79,-64
p=22,37 v=69,-72
p=65,21 v=53,-31
p=35,3 v=27,-42
p=80,33 v=67,17
p=8,87 v=-20,-9
p=59,63 v=73,50
p=58,34 v=48,-88
p=93,102 v=11,29
p=53,81 v=-1,59
p=4,20 v=-47,62
p=23,29 v=-20,-81
p=61,89 v=78,59
p=34,72 v=85,19
p=52,41 v=-36,31
p=50,48 v=57,-4
p=95,64 v=97,58
p=17,36 v=-78,-80
p=60,85 v=50,-44
p=31,22 v=46,-15
p=78,78 v=-22,-56
p=80,44 v=14,-2
p=57,45 v=-52,73
p=81,42 v=-41,-36
p=67,102 v=9,20
p=0,20 v=-26,30
p=80,64 v=4,55
p=93,67 v=-20,83
p=48,57 v=-94,68
p=28,14 v=-57,-93
p=2,22 v=-12,-61
p=78,62 v=-68,-54
p=0,22 v=68,91
p=14,22 v=-49,91
p=85,48 v=-90,-15
p=89,6 v=-11,79
p=81,89 v=98,82
p=1,14 v=89,87
p=11,48 v=12,-11
p=57,21 v=-63,73
p=42,5 v=-77,-46
p=40,6 v=6,7
p=83,56 v=-68,-75
p=17,5 v=88,-9
p=38,16 v=-29,-81
p=85,45 v=-68,-91
p=75,0 v=-24,-9
p=77,98 v=-82,86
p=57,25 v=95,-65
p=72,13 v=-4,-31
p=51,7 v=-52,52
p=62,39 v=-10,88
p=23,54 v=99,-57
p=43,29 v=86,-88
p=86,39 v=69,23
p=64,91 v=-32,-21
p=61,36 v=78,-69
p=71,8 v=84,36
p=20,46 v=22,47
p=54,64 v=85,62
p=62,27 v=63,-54
p=81,67 v=42,70
p=44,26 v=43,68
p=2,45 v=89,31
p=0,59 v=-91,-13
p=35,92 v=72,-78
p=48,38 v=86,-95
p=66,15 v=-37,-58
p=57,94 v=-66,91
p=100,12 v=45,72
p=93,5 v=-32,-59
p=8,24 v=-63,-85
p=69,6 v=5,75
p=59,84 v=49,-52
p=0,66 v=74,-13
p=69,43 v=71,-23
p=11,95 v=-9,-60
p=65,30 v=84,-73
p=91,65 v=-76,-14
p=80,44 v=55,50
p=13,26 v=44,-23
p=11,93 v=25,57
p=30,62 v=-9,-3
p=43,69 v=-57,-52
p=98,19 v=-90,14
p=44,83 v=-30,24
p=7,36 v=-71,15
p=50,67 v=-37,-71
p=54,92 v=-68,37
p=58,47 v=78,46
p=95,10 v=48,-67
p=36,79 v=94,40
p=37,35 v=58,-57
p=16,40 v=-93,-25
p=82,22 v=-67,-54
p=66,15 v=41,41
p=29,37 v=44,49
p=82,10 v=79,63
p=37,95 v=43,-36
p=13,14 v=-5,-66
p=60,93 v=85,29
p=46,83 v=21,-67
p=75,74 v=-68,93
p=88,53 v=43,-8
p=47,67 v=-22,-83
p=8,93 v=31,2
p=88,38 v=-29,51
p=65,6 v=53,74
p=20,28 v=94,-54
p=63,64 v=-14,77
p=56,93 v=-64,-63
p=25,0 v=-99,-62
p=40,90 v=37,-59
p=72,29 v=-60,-88
p=42,6 v=65,-43
p=51,37 v=-66,-46
p=32,1 v=-14,-28
p=10,42 v=23,58
p=7,31 v=89,34
p=71,102 v=-89,37
p=49,10 v=58,-31
p=1,83 v=-85,25
p=59,80 v=-8,-75
p=50,13 v=27,-82
p=28,79 v=8,28
p=4,7 v=38,56
p=76,99 v=26,-51
p=4,76 v=77,-27
p=18,9 v=44,-4
p=7,29 v=60,-26
p=96,7 v=33,41
p=27,79 v=15,-25
p=92,36 v=-19,47
p=45,102 v=-22,-93
p=31,32 v=-72,-27
p=8,73 v=-73,8
p=29,39 v=-6,-64
p=68,46 v=-97,8
p=31,99 v=46,-80
p=90,50 v=19,61
p=79,100 v=98,52
p=63,57 v=34,-3
p=39,83 v=-30,-29
p=2,4 v=32,10
p=49,95 v=-26,15
p=60,65 v=-74,43
p=74,73 v=-89,-79
p=42,66 v=-85,10
p=94,0 v=-6,5
p=92,89 v=25,-32
p=67,26 v=-56,-21
p=79,53 v=13,47
p=65,78 v=-3,1
p=49,74 v=36,-45
p=76,39 v=-99,23
p=87,62 v=47,-7
p=16,82 v=-85,44
p=38,64 v=49,39
p=46,22 v=-44,-21
p=93,55 v=96,-68
p=12,17 v=-87,-34
p=90,2 v=-98,29
p=47,64 v=50,5
p=82,21 v=48,-81
p=4,87 v=53,32
p=23,43 v=8,-42
p=7,72 v=-61,46
p=58,53 v=-37,8
p=89,9 v=33,75
p=14,30 v=20,8
p=26,35 v=-3,60
p=1,44 v=-98,-34
p=7,61 v=-91,-84
p=40,96 v=-80,-19
p=71,28 v=11,-88
p=61,78 v=55,93
p=51,36 v=-59,19
p=19,88 v=-20,-44
p=42,4 v=-22,98
p=81,35 v=-69,73
p=96,95 v=97,52
p=43,49 v=19,-80
p=97,70 v=-98,-56
p=53,38 v=56,15
p=18,33 v=-11,34
p=64,63 v=-39,-56
p=94,83 v=-39,90
p=4,36 v=60,-57
p=34,5 v=-86,-47
p=31,80 v=-77,-52
p=81,73 v=5,-82
p=54,56 v=57,73
p=42,91 v=-20,-27
p=0,6 v=-48,-93
p=48,26 v=92,-39
p=62,86 v=-80,-14
p=5,24 v=2,43
p=99,69 v=-91,-83
p=26,85 v=73,25
p=54,56 v=-8,-7
p=2,1 v=60,90
p=43,26 v=44,-23
p=2,0 v=-74,24
p=63,35 v=92,80
p=95,50 v=39,-45
p=20,62 v=-35,39
p=85,92 v=-59,11
p=39,76 v=1,-21
p=25,23 v=63,74
p=92,35 v=24,68
p=59,51 v=13,92
p=55,99 v=-23,56
p=50,59 v=22,37
p=55,7 v=77,-72
p=8,52 v=-84,-34
p=21,73 v=2,21
p=17,40 v=-63,61
p=86,76 v=46,-22
p=18,12 v=-72,-60
p=3,49 v=-44,-69
p=89,92 v=-98,-2
p=47,79 v=-73,-10
p=53,84 v=57,-6
p=62,31 v=-96,72
p=86,47 v=3,57
p=46,85 v=73,-42
p=35,84 v=-19,64
p=12,10 v=14,47
p=96,87 v=-83,-67
p=50,71 v=93,93
p=17,4 v=-6,79
p=94,51 v=10,69
p=5,66 v=-12,-41
p=82,16 v=-90,-73
p=39,63 v=-36,-68
p=15,79 v=-6,-33
p=27,58 v=-78,-72
p=68,42 v=59,-84
p=19,30 v=30,-33
p=0,77 v=89,-22
p=93,94 v=-3,-24
p=22,64 v=-13,-25
p=2,47 v=89,-11
p=11,49 v=-20,-95
p=35,42 v=-42,-91
p=22,93 v=-6,-32
p=100,75 v=10,-56
p=41,94 v=-94,-55

181
14/main.cc Normal file
View file

@ -0,0 +1,181 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <tuple>
#include <set>
using namespace std;
#ifdef DEBUG
#define DBG(str) do { std::cout << str << std::endl; } while( false )
#else
#define DBG(str) do { } while ( false )
#endif
struct Point {
int x;
int y;
};
struct Guard {
Point p;
Point v;
};
vector<Guard> parseGuards(const char *path) {
vector<Guard> guards;
ifstream input(path);
string line;
while(getline(input, line)) {
Guard guard;
stringstream ss;
char c;
ss << line;
ss >> c >> c >> guard.p.x >> c >> guard.p.y >> c >> c >> guard.v.x >> c >> guard.v.y;
guards.push_back(guard);
}
return guards;
}
void printGuards(vector<Guard> guards) {
for (auto guard : guards) {
cout << "p=" << guard.p.x << "," << guard.p.y << " v=" << guard.v.x << "," << guard.v.y << endl;
}
}
void printField(vector<Guard> guards, int rows, int columns) {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
int num = 0;
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) num++;
}
if (num) cout << num;
else cout << '.';
}
cout << endl;
}
cout << endl;
}
// Function to perform Modular Addition
int modAdd(int a, int b, int m)
{
// Adding m to handle negative numbers
return ((a % m) + (b % m) + m) % m;
}
void doStep(vector<Guard> &guards, int rows, int columns) {
for (auto &guard : guards) {
guard.p.x = modAdd(guard.p.x, guard.v.x, columns);
guard.p.y = modAdd(guard.p.y, guard.v.y, rows);
}
}
long calculateResult(vector<Guard> guards, int rows, int columns) {
long quadrants[4];
int count = 0;
for (int y = 0; y < rows / 2; y++) {
for (int x = 0; x < columns / 2; x++) {
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) count++;
}
}
}
quadrants[0] = count;
count = 0;
for (int y = rows / 2 + 1; y < rows; y++) {
for (int x = 0; x < columns / 2; x++) {
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) count++;
}
}
}
quadrants[1] = count;
count = 0;
for (int y = 0; y < rows / 2; y++) {
for (int x = columns / 2 + 1; x < columns; x++) {
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) count++;
}
}
}
quadrants[2] = count;
count = 0;
for (int y = rows / 2 + 1; y < rows; y++) {
for (int x = columns / 2 + 1; x < columns; x++) {
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) count++;
}
}
}
quadrants[3] = count;
return quadrants[0] * quadrants[1] * quadrants[2] * quadrants[3];
}
long part1(const char *path, int rows, int columns) {
vector<Guard> guards = parseGuards(path);
// printField(guards, rows, columns);
for (int i = 0; i < 100; i++) {
doStep(guards, rows, columns);
}
// printField(guards, rows, columns);
return calculateResult(guards, rows, columns);
}
bool isUnique(vector<Guard> guards, int rows, int columns) {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
bool flag = false;
for (auto guard : guards) {
if (guard.p.x == x && guard.p.y == y) {
if (flag) return false;
else flag = true;
}
}
}
}
return true;
}
long part2(const char *path, int rows, int columns) {
vector<Guard> guards = parseGuards(path);
long step = 0;
while (true) {
if (isUnique(guards, rows, columns)) return step;
doStep(guards, rows, columns);
step++;
}
}
int main(int argc, char *argv[]) {
int example1 = part1("example", 7, 11);
cout << "example1: " << example1 << endl;
int input1 = part1("input", 103, 101);
cout << "input1: " << input1 << endl;
unsigned long long input2 = part2("input", 103, 101);
cout << "input2: " << input2 << endl;
return 0;
}