commit 7135454e80ee5026743a72fc7b957855b1c6c62f
parent c72df487f665a9a74670e0aecb6449348c6e85e0
Author: bsandro <email@bsandro.tech>
Date: Sat, 14 Dec 2024 22:07:33 +0200
Day 14 parsing
Diffstat:
4 files changed, 601 insertions(+), 0 deletions(-)
diff --git a/day14/Makefile b/day14/Makefile
@@ -0,0 +1,28 @@
+NAME=$(shell basename ${PWD})
+SRC=$(wildcard *.cpp)
+DEPS:=$(wildcard *.hpp)
+OBJ:=$(SRC:.cpp=.o)
+CXXFLAGS=-O2 -std=c++23 -Werror -Wall -Wextra -I. -I../include
+LDFLAGS=-lstdc++
+
+all: $(NAME)
+
+.PHONY: clean run
+
+clean:
+ rm -f $(OBJ) $(NAME)
+
+%.o : %.c $(DEPS)
+ @$(CC) $(CFLAGS) -c $< -o $@
+
+$(NAME): $(OBJ)
+ @$(CC) $(OBJ) -o $@ $(LDFLAGS)
+
+run: $(NAME)
+ @./$(NAME) input.txt
+
+test1: $(NAME)
+ @./$(NAME) test1.txt
+
+test2: $(NAME)
+ @./$(NAME) test2.txt
diff --git a/day14/input.txt b/day14/input.txt
@@ -0,0 +1,500 @@
+p=33,38 v=-42,-36
+p=49,81 v=-22,-10
+p=57,30 v=22,-36
+p=80,66 v=58,19
+p=74,85 v=-47,36
+p=43,75 v=-88,-13
+p=67,100 v=-25,21
+p=95,83 v=-72,85
+p=100,52 v=-50,46
+p=39,95 v=69,-37
+p=17,23 v=-30,-12
+p=51,79 v=-79,22
+p=86,58 v=-19,95
+p=100,29 v=-18,-28
+p=7,84 v=6,-99
+p=80,5 v=-59,-1
+p=19,3 v=-9,-72
+p=82,8 v=-60,99
+p=80,43 v=99,-98
+p=18,50 v=38,-24
+p=12,35 v=67,-42
+p=35,82 v=16,-37
+p=18,30 v=45,-26
+p=11,78 v=-54,-58
+p=40,28 v=-42,73
+p=27,2 v=-76,23
+p=62,39 v=33,35
+p=84,35 v=-69,81
+p=75,49 v=31,-8
+p=61,48 v=-68,19
+p=75,70 v=98,-62
+p=5,67 v=-95,-51
+p=36,30 v=2,18
+p=52,44 v=55,78
+p=66,37 v=-67,-38
+p=1,9 v=40,64
+p=69,17 v=33,-33
+p=0,47 v=12,20
+p=39,77 v=-38,-71
+p=7,90 v=-73,-45
+p=21,84 v=98,31
+p=0,97 v=38,20
+p=1,39 v=28,-98
+p=79,63 v=8,22
+p=29,73 v=-99,-24
+p=67,84 v=99,-99
+p=88,85 v=55,96
+p=40,18 v=-70,-30
+p=47,90 v=-67,-45
+p=7,21 v=63,59
+p=1,81 v=-84,44
+p=56,15 v=13,-45
+p=61,55 v=-46,69
+p=71,75 v=48,-44
+p=91,63 v=52,-51
+p=89,53 v=-60,11
+p=2,30 v=-95,-60
+p=64,93 v=10,-72
+p=43,36 v=76,-74
+p=81,29 v=42,-60
+p=43,21 v=-55,-58
+p=82,20 v=53,-90
+p=62,6 v=73,34
+p=38,59 v=-56,-5
+p=60,34 v=78,89
+p=28,41 v=93,81
+p=36,15 v=-30,-36
+p=64,101 v=86,74
+p=95,55 v=-99,-32
+p=16,11 v=-86,-85
+p=33,27 v=59,-6
+p=93,8 v=-27,45
+p=3,4 v=8,2
+p=70,20 v=77,34
+p=21,19 v=-74,-1
+p=55,18 v=68,99
+p=31,71 v=14,-34
+p=62,30 v=74,-84
+p=23,46 v=34,-44
+p=50,11 v=-28,-25
+p=76,101 v=-47,50
+p=86,66 v=43,-19
+p=84,45 v=-15,-30
+p=47,95 v=55,-45
+p=55,54 v=44,99
+p=91,26 v=75,-36
+p=13,64 v=-52,87
+p=24,90 v=69,-29
+p=75,21 v=42,18
+p=92,70 v=72,77
+p=4,62 v=-97,-2
+p=31,5 v=70,69
+p=75,29 v=-69,78
+p=34,94 v=53,-13
+p=22,67 v=-43,33
+p=75,57 v=99,30
+p=7,55 v=4,71
+p=55,80 v=-90,-94
+p=70,37 v=-35,-29
+p=89,74 v=-28,60
+p=68,74 v=77,-89
+p=68,41 v=45,-22
+p=41,46 v=36,54
+p=89,16 v=9,51
+p=88,66 v=8,-54
+p=19,96 v=-40,85
+p=12,17 v=-18,83
+p=93,56 v=-16,-92
+p=9,31 v=-97,-17
+p=54,3 v=90,-23
+p=84,92 v=30,39
+p=36,45 v=82,87
+p=89,32 v=-60,86
+p=99,56 v=-63,-77
+p=4,24 v=97,-47
+p=72,19 v=-25,67
+p=0,95 v=-27,85
+p=86,30 v=56,57
+p=62,54 v=89,84
+p=98,70 v=28,-56
+p=79,75 v=-47,98
+p=67,69 v=90,-35
+p=79,79 v=-37,-32
+p=98,69 v=-19,86
+p=86,57 v=6,35
+p=72,8 v=-13,49
+p=36,55 v=34,65
+p=60,68 v=-35,-24
+p=54,56 v=-22,19
+p=99,0 v=-27,-9
+p=40,3 v=93,-91
+p=75,88 v=75,90
+p=24,20 v=11,89
+p=14,27 v=72,-93
+p=82,24 v=-39,-65
+p=36,64 v=-76,-63
+p=94,12 v=-72,-47
+p=40,1 v=-65,31
+p=74,80 v=54,17
+p=2,97 v=28,1
+p=48,16 v=36,97
+p=51,82 v=-56,-29
+p=100,41 v=-18,-90
+p=44,2 v=54,-59
+p=83,79 v=-38,45
+p=31,78 v=-44,25
+p=34,68 v=48,-31
+p=94,65 v=-38,22
+p=18,60 v=71,-89
+p=63,3 v=-13,23
+p=77,47 v=7,84
+p=68,41 v=39,27
+p=87,7 v=52,-61
+p=69,42 v=31,-73
+p=28,40 v=13,-93
+p=81,101 v=-82,-4
+p=96,86 v=29,-53
+p=39,47 v=69,-60
+p=45,87 v=52,-10
+p=58,35 v=61,95
+p=9,44 v=-96,27
+p=80,44 v=76,54
+p=65,64 v=79,90
+p=20,42 v=26,5
+p=72,68 v=-71,-70
+p=88,89 v=10,-88
+p=44,54 v=68,-51
+p=47,17 v=-33,-96
+p=29,2 v=-19,-61
+p=91,76 v=-93,58
+p=24,67 v=-44,90
+p=11,62 v=-53,49
+p=15,53 v=51,-10
+p=49,1 v=12,99
+p=84,8 v=19,15
+p=6,33 v=-30,32
+p=88,90 v=-71,-99
+p=35,66 v=14,41
+p=16,82 v=-39,-26
+p=38,77 v=-42,77
+p=94,28 v=18,86
+p=8,22 v=-52,-87
+p=37,1 v=92,-34
+p=3,41 v=-5,-92
+p=79,71 v=-83,-27
+p=49,36 v=57,-49
+p=50,76 v=12,36
+p=55,6 v=-79,67
+p=70,94 v=-45,61
+p=1,72 v=5,-43
+p=16,55 v=37,-22
+p=21,70 v=36,-68
+p=4,45 v=-4,-87
+p=61,38 v=57,54
+p=4,43 v=98,-80
+p=93,99 v=-38,-50
+p=83,40 v=44,-88
+p=96,87 v=96,-32
+p=40,23 v=92,91
+p=68,18 v=-12,56
+p=54,46 v=-79,-22
+p=13,92 v=-86,-94
+p=48,102 v=-95,-76
+p=57,38 v=46,-29
+p=48,3 v=36,-64
+p=18,76 v=-47,-63
+p=11,48 v=-84,-87
+p=29,89 v=-18,-83
+p=5,10 v=95,64
+p=58,27 v=-12,97
+p=77,8 v=-25,18
+p=3,55 v=66,32
+p=4,30 v=8,48
+p=85,12 v=-48,57
+p=25,51 v=48,-54
+p=13,77 v=31,51
+p=75,37 v=20,99
+p=42,38 v=-34,21
+p=13,88 v=-51,93
+p=30,27 v=1,-49
+p=7,46 v=-57,57
+p=10,48 v=-63,-57
+p=91,30 v=29,97
+p=18,94 v=4,-10
+p=7,3 v=44,-63
+p=71,79 v=-92,32
+p=25,98 v=-30,88
+p=78,8 v=69,-47
+p=19,55 v=83,-65
+p=78,81 v=64,82
+p=33,76 v=-53,52
+p=73,36 v=-70,-41
+p=74,88 v=-70,12
+p=29,2 v=3,69
+p=63,9 v=-57,-20
+p=18,95 v=92,54
+p=24,72 v=-62,-44
+p=71,33 v=-24,13
+p=12,5 v=40,-91
+p=40,83 v=-88,88
+p=89,31 v=41,78
+p=81,45 v=53,97
+p=4,102 v=89,-97
+p=85,38 v=66,-10
+p=99,88 v=-94,55
+p=72,75 v=60,-94
+p=99,39 v=51,-38
+p=66,58 v=89,-92
+p=67,62 v=20,-15
+p=16,20 v=-96,-28
+p=1,101 v=-7,-50
+p=61,101 v=33,-34
+p=7,49 v=-41,-43
+p=15,42 v=-19,24
+p=99,95 v=-5,-89
+p=89,73 v=40,79
+p=73,25 v=-36,-52
+p=59,98 v=22,20
+p=99,71 v=96,-40
+p=21,73 v=-98,11
+p=6,27 v=-65,-48
+p=26,98 v=-64,-99
+p=35,79 v=-24,86
+p=25,101 v=-19,-80
+p=9,53 v=-87,-28
+p=86,26 v=15,93
+p=79,65 v=19,-73
+p=48,42 v=30,82
+p=15,14 v=15,-58
+p=54,76 v=69,-62
+p=43,76 v=69,71
+p=17,30 v=-89,-71
+p=95,79 v=52,93
+p=26,6 v=54,99
+p=68,69 v=-35,-78
+p=32,41 v=13,24
+p=99,41 v=51,46
+p=31,69 v=25,-70
+p=6,30 v=50,2
+p=76,83 v=-13,17
+p=59,16 v=68,61
+p=53,85 v=-40,3
+p=36,18 v=13,2
+p=92,46 v=36,-69
+p=71,92 v=-91,53
+p=62,15 v=-87,-18
+p=4,32 v=-29,34
+p=69,28 v=-24,-33
+p=32,31 v=59,37
+p=18,86 v=-53,59
+p=5,19 v=55,-87
+p=14,19 v=51,53
+p=79,74 v=-95,-83
+p=32,42 v=13,30
+p=99,3 v=-16,-61
+p=72,97 v=-92,-4
+p=85,34 v=-50,-11
+p=44,40 v=86,55
+p=23,43 v=38,-12
+p=72,15 v=-47,-39
+p=90,21 v=-94,-90
+p=94,87 v=3,-26
+p=0,48 v=73,92
+p=49,11 v=91,62
+p=49,5 v=34,-69
+p=28,44 v=25,-30
+p=31,82 v=3,44
+p=36,39 v=58,81
+p=22,59 v=-41,41
+p=24,80 v=-87,-42
+p=45,50 v=80,-68
+p=61,94 v=-68,61
+p=53,44 v=25,-35
+p=45,48 v=-25,-85
+p=78,40 v=33,-12
+p=45,10 v=12,-96
+p=10,56 v=-97,-57
+p=81,76 v=42,82
+p=8,93 v=-19,-18
+p=59,28 v=-57,32
+p=71,102 v=-70,-99
+p=8,7 v=82,10
+p=92,91 v=18,-59
+p=66,41 v=-57,46
+p=48,41 v=-44,5
+p=90,43 v=-73,54
+p=3,38 v=40,-3
+p=90,66 v=-26,8
+p=97,76 v=5,-45
+p=45,78 v=61,-54
+p=52,69 v=21,-2
+p=63,35 v=-57,-63
+p=78,61 v=55,-71
+p=49,84 v=92,-10
+p=58,83 v=-90,58
+p=23,63 v=4,76
+p=58,48 v=91,3
+p=97,20 v=70,25
+p=81,2 v=72,52
+p=90,19 v=97,29
+p=41,7 v=-55,-29
+p=37,86 v=-88,12
+p=54,0 v=-57,-99
+p=52,31 v=90,13
+p=30,0 v=-99,4
+p=62,46 v=-25,19
+p=67,82 v=90,-13
+p=38,72 v=-8,-8
+p=54,61 v=-26,-23
+p=55,76 v=-34,-60
+p=31,44 v=-87,19
+p=65,73 v=75,-25
+p=80,80 v=10,-67
+p=22,33 v=71,-44
+p=66,19 v=98,53
+p=92,13 v=-70,-30
+p=15,96 v=-44,-59
+p=23,82 v=17,-87
+p=54,86 v=77,-51
+p=68,63 v=-79,27
+p=2,39 v=-5,76
+p=71,83 v=75,-37
+p=5,16 v=-17,18
+p=48,68 v=56,60
+p=70,89 v=83,78
+p=23,58 v=48,-81
+p=100,40 v=-95,-95
+p=97,21 v=59,45
+p=86,82 v=41,47
+p=71,78 v=-34,-7
+p=59,60 v=-1,-73
+p=7,20 v=-40,48
+p=39,101 v=-55,-72
+p=95,20 v=28,24
+p=69,19 v=99,-85
+p=46,62 v=88,-88
+p=47,6 v=81,-55
+p=3,41 v=17,-90
+p=79,40 v=-90,45
+p=52,81 v=-55,-43
+p=67,13 v=99,91
+p=71,43 v=75,38
+p=14,88 v=-50,-34
+p=84,97 v=-11,47
+p=56,81 v=63,32
+p=32,66 v=-81,53
+p=70,10 v=21,-1
+p=50,3 v=-27,99
+p=19,51 v=15,-5
+p=73,45 v=9,54
+p=90,86 v=-50,-51
+p=18,46 v=-46,-35
+p=33,58 v=46,56
+p=56,97 v=-23,-64
+p=79,72 v=-23,-37
+p=3,46 v=-38,77
+p=7,48 v=-28,-11
+p=73,55 v=28,-81
+p=32,59 v=-82,-81
+p=31,102 v=74,91
+p=68,24 v=-14,-78
+p=73,53 v=9,-49
+p=57,80 v=16,93
+p=55,68 v=20,-51
+p=12,58 v=3,-32
+p=39,73 v=69,1
+p=30,0 v=1,-50
+p=90,14 v=-36,-28
+p=56,76 v=67,-21
+p=37,46 v=81,8
+p=0,89 v=-19,-70
+p=42,65 v=67,17
+p=50,24 v=48,40
+p=98,39 v=-62,-17
+p=43,68 v=1,-62
+p=85,73 v=98,71
+p=87,66 v=-31,26
+p=88,2 v=19,-34
+p=98,51 v=-5,-57
+p=70,21 v=65,94
+p=72,100 v=-92,50
+p=93,26 v=7,-58
+p=69,91 v=-24,-94
+p=12,1 v=-86,75
+p=95,99 v=26,-57
+p=40,26 v=94,-2
+p=73,91 v=-58,-83
+p=96,30 v=84,-36
+p=26,94 v=80,77
+p=68,28 v=12,36
+p=64,17 v=-49,-93
+p=82,38 v=21,5
+p=92,100 v=-61,58
+p=54,57 v=90,-84
+p=92,27 v=40,97
+p=23,18 v=90,-91
+p=64,39 v=81,46
+p=1,93 v=26,95
+p=32,48 v=-87,54
+p=23,94 v=-53,47
+p=84,70 v=-60,-89
+p=100,24 v=-50,-63
+p=36,98 v=71,-26
+p=67,71 v=-12,60
+p=46,6 v=-66,72
+p=27,19 v=-86,-85
+p=30,76 v=-41,58
+p=77,98 v=8,-53
+p=58,46 v=56,62
+p=60,80 v=54,-62
+p=61,70 v=-78,57
+p=17,101 v=-7,-50
+p=95,37 v=-61,5
+p=1,0 v=96,7
+p=36,52 v=-48,-6
+p=20,28 v=-75,73
+p=88,90 v=-19,-52
+p=44,38 v=35,-68
+p=81,2 v=-93,-12
+p=38,56 v=-18,-22
+p=13,45 v=-3,58
+p=65,9 v=-12,-96
+p=53,18 v=-34,83
+p=43,93 v=-55,58
+p=56,33 v=-21,77
+p=24,68 v=5,52
+p=73,8 v=88,12
+p=75,83 v=-92,-15
+p=59,73 v=-43,32
+p=53,48 v=77,22
+p=52,64 v=68,30
+p=63,57 v=-32,75
+p=83,102 v=-93,12
+p=26,23 v=16,-28
+p=4,37 v=-34,44
+p=6,17 v=-28,-28
+p=47,48 v=80,-49
+p=96,81 v=53,-43
+p=73,98 v=99,4
+p=12,66 v=-31,64
+p=45,89 v=-10,-91
+p=73,49 v=-21,9
+p=83,2 v=75,-34
+p=20,85 v=-41,39
+p=9,44 v=83,-25
+p=15,35 v=61,28
+p=2,7 v=-92,-38
+p=55,52 v=-89,-11
+p=41,51 v=81,-98
+p=51,48 v=-93,99
+p=73,54 v=22,27
+p=69,32 v=-91,5
+p=68,28 v=-35,21
+p=2,35 v=37,-12
+p=56,29 v=67,-36
+p=19,36 v=-30,-22
+p=51,93 v=79,-76
+p=81,12 v=-81,-31
+p=19,13 v=-96,-50
+p=42,26 v=57,-55
diff --git a/day14/main.cpp b/day14/main.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include <filesystem>
+#include <fstream>
+#include <vector>
+#include <cstring>
+#include <cstdio>
+#include <tuple>
+#include <algorithm>
+#include "utils.hpp"
+
+struct Vec2 {
+ int x;
+ int y;
+};
+
+struct Robot {
+ Vec2 p; // position
+ Vec2 v; // velocity
+ Robot(const std::string &s) {
+ std::sscanf(s.c_str(), "p=%d,%d v=%d,%d", &p.x, &p.y, &v.x, &v.y);
+ }
+};
+
+typedef std::vector<Robot> Data;
+
+template<typename T, typename N>
+T read_file(const std::string &path) {
+ std::ifstream ifs(path, std::ios::binary);
+ if (!ifs.is_open()) {
+ throw std::runtime_error(path+":"+std::strerror(errno));
+ }
+ T buf;
+ while (1) {
+ std::string str;
+ std::getline(ifs, str);
+ if (!str.empty()) buf.push_back(N(str));
+ if (!ifs) break;
+ }
+ return buf;
+}
+
+int64_t part1(Data input [[ maybe_unused ]]) {
+ 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);
+ }
+ return 0;
+}
+int64_t part2(Data &input [[ maybe_unused ]]) {
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ Performance perf;
+ const std::string fname = argc>1 ? argv[1] : "test1.txt";
+ std::cout << "AoC 2024 day 14 " << fname << std::endl;
+ Data input = read_file<Data, Robot>(fname);
+ std::cout << "part1: " << part1(input) << std::endl;
+ std::cout << "part2: " << part2(input) << std::endl;
+
+ return 0;
+}
diff --git a/day14/test1.txt b/day14/test1.txt
@@ -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