diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..90422b9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "2022/2022/cpp/doctest"] + path = 2022/cpp/doctest + url = git@github.com:doctest/doctest diff --git a/2022/cpp/01/Makefile b/2022/cpp/01/Makefile new file mode 100644 index 0000000..445fa14 --- /dev/null +++ b/2022/cpp/01/Makefile @@ -0,0 +1,35 @@ +# VERBOSE := 0 +CXXFLAGS := -Wall -O3 -DPART=2 +OBJ_DIR = build +OBJ = answer.o + +.PHONY: clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +test: $(OBJ_DIR)/$(OBJ) + +sample1 sample2: + $(eval CXXFLAGS += -DSAMPLE$(@:sample%=%)) + @-rm test + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/01/answer.cpp b/2022/cpp/01/answer.cpp new file mode 100644 index 0000000..6e47528 --- /dev/null +++ b/2022/cpp/01/answer.cpp @@ -0,0 +1,68 @@ +// AOC - 2022 - 01 +#include "answer.hpp" +#include +#include +#include + +std::vector get_input(const char * filename){ + std::ifstream stream(filename); + std::string str; + std::vector ret; + ret.push_back(*new Input()); + + while (getline(stream, str)){ + if (str.length() == 0) + ret.push_back(*new Input()); + else + ret.back().calories.push_back(stoi(str)); + } + return ret; +} + +std::string get_result(std::vector inputs){ + std::string ret; +#if PART==1 + uint max_sum = 0; + for (auto& inp: inputs){ + + uint local_sum = 0; + for (auto& n : inp.calories) + local_sum += n; + std::cout << local_sum << std::endl; + if (local_sum > max_sum) + max_sum = local_sum; + } + ret = std::to_string(max_sum); + std::cout << ret << std::endl; +#else + std::vector top3(3); + for (int i = 0; i < 3; i++){ + uint max_sum = 0; + size_t max_idx = -1; + size_t idx = 0; + for (auto& inp: inputs){ + + uint local_sum = 0; + for (auto& n : inp.calories) + local_sum += n; + std::cout << local_sum << std::endl; + if (local_sum > max_sum) { + max_sum = local_sum; + max_idx = idx; + } + idx++; + } + top3.push_back(max_sum); + inputs.erase(inputs.begin() + max_idx); + } + + uint sum = 0; + for (auto& n : top3) + sum += n; + ret = std::to_string(sum); + + std::cout << ret << std::endl; +#endif + return ret; + +} diff --git a/2022/cpp/01/answer.hpp b/2022/cpp/01/answer.hpp new file mode 100644 index 0000000..312fa0f --- /dev/null +++ b/2022/cpp/01/answer.hpp @@ -0,0 +1,41 @@ +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &os, const std::vector &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} + +struct Input{ + std::vector calories; + + bool operator ==(const Input & rhs) const{ + return this->calories == rhs.calories; + }; +}; + +inline std::ostream& operator<<(std::ostream &os, Input & inp){ + os << inp.calories; + return os; +} + + +#ifndef VERBOSE +#define VERBOSE 0 +#endif +std::vector get_input(const char *); + +std::string get_result(std::vector); + diff --git a/2022/cpp/01/build/answer.o b/2022/cpp/01/build/answer.o new file mode 100644 index 0000000..c30e3b0 Binary files /dev/null and b/2022/cpp/01/build/answer.o differ diff --git a/2022/cpp/01/input b/2022/cpp/01/input new file mode 100644 index 0000000..f42822f --- /dev/null +++ b/2022/cpp/01/input @@ -0,0 +1,2251 @@ +18313 +2404 +10479 + +7011 +10279 +1496 +10342 +8918 +3162 +4525 +4368 + +17242 + +10920 +14072 +9754 +4435 +9396 + +5915 +2602 +4032 +3303 +2685 +1856 +1334 +4865 +6385 +1733 +5328 + +8899 +5482 +3195 +7837 +8986 +13794 + +25121 +22211 +21257 + +6360 +4007 +5124 +2266 +6943 +6966 +3887 +3427 +1255 +5266 +6119 +2841 + +6167 +3883 +2776 +5894 +2013 +6930 +6613 +4637 +5259 +2347 +3550 +5639 +5117 + +4530 +5961 +5956 +2503 +5060 +4770 +5240 +4123 +4089 +6794 +3333 +5244 +4415 + +1733 +1209 +4458 +1223 +3859 +7728 +9793 +8350 + +2278 +3241 +4668 +1836 +3669 +2996 +1772 +5016 +6294 +6367 +1080 +5964 +5547 + +4793 +13744 +9478 +7979 +1490 +12490 + +6770 +7282 +2059 +5242 +13237 +9365 + +2230 +6498 +1352 +3846 +3863 +6205 +5803 +2646 +4188 +4431 +6380 +1526 +2620 +4438 + +24331 +23578 + +4199 +5126 +5725 +1123 +2415 +5427 +5789 +4856 +2610 +4833 +3398 +4239 +1432 +5122 + +5013 +4939 +11760 +8419 +5706 +3765 +10182 + +11795 +9688 +1712 +2091 +9651 +13022 + +10393 +11862 +1672 +5431 +6812 +1866 +5599 + +2036 +6561 +4097 +3467 +4739 +7656 +3252 +4322 +5068 +2345 +4155 + +3090 +1228 +1270 +8407 +8118 +4996 +7157 +3944 +6230 +2728 + +5669 +2113 +3076 +1290 +7673 +1022 +5805 +5128 +7912 +7862 +4955 + +20640 + +4131 +4874 +5220 +9947 +9739 +1662 +9418 +9684 + +4875 +3073 +1151 +11911 +10310 +10793 +10307 + +6979 +4265 +8322 +3518 +1855 +4912 +5481 +7124 +4664 + +19975 +16112 +19450 +7231 + +3909 +4743 +3794 +4448 +3200 +3447 +3331 +7011 +3345 +3935 +6750 + +3900 +14850 +4890 +9625 +4832 + +23682 +1375 + +7463 +8112 +10624 +1105 +6625 +5157 +5839 +1019 + +19089 +36719 + +5372 +7085 +8536 +8717 +6255 +5961 +3198 +7729 +3055 + +4406 +2263 +6686 +1458 +2377 +2740 +1284 +3045 +7121 +3070 + +19161 +10515 +1027 + +7782 +1703 +1864 +3611 +1039 +2394 +3472 +6964 +5824 +8011 +3341 + +56690 + +5107 +1876 +4973 +5485 +4325 +6079 +3187 +3565 +2875 +7137 +3887 +6763 + +2633 +2151 +1105 +2400 +5338 +3382 +1330 +3945 +6556 +1670 +5526 +4281 +2168 + +5382 +2347 +4225 +1905 +4672 +6706 +5747 +5808 +1159 +4878 +5923 + +11148 +8722 +3896 +15697 +3499 + +46749 + +13315 +20966 + +1625 +6338 +7882 +1505 +4226 +9948 +6438 +6012 + +1003 +10033 +5066 +7756 +10151 +10719 +5299 +5629 + +4466 +6290 +7220 +3657 +8261 +7348 +8525 +4531 +4892 +7833 + +24723 +7817 + +4234 +4428 +5810 +2091 +4301 +1378 +2342 +3872 +1167 +2073 +6867 +7026 + +7005 +10018 +3954 +4497 +4951 +1283 +5383 + +4152 +4390 +8731 +8521 +5415 +1029 +8200 +3769 + +7224 +12281 +8337 +8969 +13938 +9651 + +10923 +14358 +8896 +8401 +7073 + +3934 +4424 +4538 +4371 +2335 +6363 +1264 +4331 +5107 +2270 +3109 +2547 +2304 +5911 + +4600 +5674 +2620 +1846 +3804 +2387 +4094 +3648 +4241 +5593 +2974 +2101 +1621 +3939 + +7429 +9425 +9653 +5835 + +13414 +4517 +15783 +3867 + +2287 +1019 +11604 +11910 +2591 +2468 +10543 + +13050 +1396 +13959 +6883 + +6303 +2889 +4047 +3954 +2245 +6672 +4574 +5731 +5879 +2837 +5859 +4812 +6785 + +10290 +10131 +5605 +3321 +2122 +6331 +9107 +7937 + +1786 +1362 +7409 +2093 +3901 +1306 +2089 +1558 +4163 +6118 +6270 +1929 + +4224 +3864 +3922 +3609 +2496 +2246 +5397 +1535 +5499 +4932 +3684 +1348 +3437 +3665 + +2973 +4506 +3971 +2511 +4563 +3168 +2687 +6249 +2593 +6470 +4100 +6309 +1316 +4362 + +14966 +2940 +11485 + +6675 +16873 +5695 +14706 + +3953 + +5620 +1678 +3630 +5015 +3277 +4957 +5288 +5555 +2860 +1750 +2795 +6170 +6308 + +3671 +1354 +4018 +4730 +4070 +1685 +4478 +1918 +5532 +3970 +3751 +4890 +4277 +2737 +5535 + +14799 +8129 + +5254 +2065 +1282 +2407 +5174 +4549 +4326 +1201 +3372 +3906 +6234 +4937 +4234 +2149 + +5194 +2519 +3555 +6929 +4367 +4547 + +1024 +37240 + +3693 +6526 +2679 +5434 +7924 +2974 +1754 +4287 +4744 +4187 +1348 + +6598 +5963 +6035 +5404 +3270 +1250 +5947 +3037 +3927 +3702 +5255 +1640 +6476 + +4499 +5207 +1991 +4613 +3088 +5024 +4737 +5160 +2450 +3582 +2605 +2573 +1100 +3732 + +1633 +5663 +2415 +4039 +4957 +2697 +5428 +5050 +3979 +3428 +4563 +3770 +3635 +4012 +4773 + +7377 +6189 +10812 +6905 +11196 +5394 +10135 + +10008 +2868 +2630 +8542 +8897 +2718 +5831 +4460 + +1303 +4771 +1574 +2960 +5070 +1596 +5530 +4177 +4210 +6465 +4179 +1000 +4460 +6173 + +6654 +1378 +2148 +8704 +6875 +4511 +7540 +6078 +5585 +4030 + +4333 +5953 +5095 +4782 +3651 +4673 +3267 +6389 +2358 +2120 +3684 +3034 +3420 +1322 + +8922 +9535 +16357 +16552 + +1378 +1039 +1262 +2067 +6491 +2776 +1352 +4914 +5832 +1369 +6105 +1384 +2474 +1704 + +6256 +2538 +4701 +4090 +4077 +5333 +2777 +4082 +4640 +2542 +6356 +3065 +4855 +2535 + +6510 +3709 +4197 +4344 +5906 +6852 +7398 +3578 +3308 +1239 +3817 +5676 + +5441 +3368 +1752 +2629 +1923 +4711 +1667 +3701 +1350 +4764 +6439 +2341 +4052 + +6407 +5327 +1229 +4834 +6008 +1302 +5595 +5260 +3680 +3220 +3154 +3353 +1942 +1740 + +2356 +1908 +3058 +4668 +3641 +1663 +4199 +2190 +6067 +3923 +3288 +1978 +6110 +5330 + +7020 +2487 +6521 +1084 +6582 +1784 +7861 +2759 +8077 +2961 + +68314 + +2160 +15170 +2267 +3867 +6851 + +8543 +4118 +22260 + +5313 +3525 +1483 +3613 +5798 +1472 +4020 +5607 +4391 +2502 +5512 +5535 +3755 +5590 +3668 + +25502 +7623 +15508 + +2138 +1227 +11355 +1288 +7062 +5391 +3994 + +7738 +3667 +5000 +1691 +7322 +8054 +4540 +2353 +6870 +8475 + +4289 +6415 +1251 +4933 +7224 +2608 +2320 +4432 +4637 +1395 +6575 +5581 + +6841 +1161 +5617 +12505 +6274 +9171 + +13548 +20154 +20682 + +2843 +7363 +5967 +15256 +16032 + +7940 +6992 +3940 +3100 +1334 +1389 +5182 +6666 +4030 +6116 +3036 + +35234 +19273 + +7268 +10689 + +1846 +4211 +1504 +3780 +3789 +2017 +6279 +5001 +2341 +6350 +5169 +1966 +2640 +5574 + +2125 +8432 +8339 +8130 +3506 +6115 +4684 +9548 +3300 + +6848 +1274 +6463 +12007 +10719 +10590 +5616 + +4471 +1304 +1635 +5960 +3702 +1747 +3658 +2446 +4724 +5675 +1739 +3170 +3556 + +9134 +12963 +6987 + +5728 +7877 +6499 +5909 +1119 +3090 +5033 +4884 +3151 + +10675 +8053 +9237 +14206 +11710 + +49154 + +5999 +3861 +1836 +3451 +3673 +5193 +2680 +6068 +1743 +5600 +4793 +6211 +2967 +1021 + +24720 + +8749 +11674 +4039 +5341 +14256 + +4553 +3893 +1528 +5490 +2417 +2392 +4023 +2430 +4649 +3163 +3673 +1378 +1957 +1055 + +10777 +4742 +16291 +16430 +12295 + +6502 +6052 +2944 +8166 +7567 +3580 +5957 +4691 +1623 +3269 + +1013 +5809 +16958 +4994 + +36633 + +8535 +13662 +24448 + +6651 +7911 + +19793 +2436 + +4671 +4420 +5322 +1215 +4952 +1193 +2387 +2799 +2748 +1343 +4123 +1715 +2978 +2148 +6050 + +2036 +4670 +5444 +4584 +4220 +6061 +2834 +2319 +1370 +4572 +4249 +4144 +3236 +3812 +3487 + +55521 + +3845 +1473 +6794 +4597 +5566 +2735 +2660 +6402 +2515 +3439 + +3772 +12809 +5790 +10817 +9729 + +1788 +2476 +4333 +2940 +7232 +3582 +8604 +3742 +7644 +4823 + +6128 +6737 +5712 +4168 +4465 +2183 +1217 +2257 +6326 +5966 +5699 +2985 +2290 + +5190 +4155 +10754 +9830 +4507 +2165 +7208 +4409 + +4256 +4158 +1941 +3124 +5852 +3376 +6411 +5508 +5072 +2374 +4189 +4032 +4957 +2050 + +2737 +6772 +4468 +2072 +1214 +6231 +7077 +6100 +6990 +5676 + +7474 +4524 +8778 +7129 +8151 +7975 +7277 +8672 +8097 +3847 + +29172 +7125 + +50466 + +14169 +22037 + +4483 +1801 +4863 +1920 +2776 +1407 +3998 +1142 +2664 +3920 +5708 +5481 +5701 +5159 +3654 + +1960 +4419 +5933 +4941 +4511 +1120 +5179 +3447 +5285 +3675 +1333 +1418 +4595 +4140 +2353 + +1995 +5588 +1443 +5681 +3439 +6178 +5795 +5144 +2788 +6232 +2620 +4665 +5624 + +37076 +7362 + +3885 +8268 +10947 +9084 +3016 +10383 +9594 + +16223 +16281 +5352 +11764 +10192 + +5248 +3151 +7341 +8125 +10671 +13510 + +1759 +7635 +7021 +3620 +7640 +4967 +3662 +3840 +7613 +3948 +1820 + +4010 +3936 +7375 +8060 +12396 +8236 + +5033 +2739 +4296 +5564 +6502 +3501 +5637 +5826 +5862 +5579 +6416 +2277 +2471 + +4325 +2621 +1600 +4290 +15494 + +6834 +15459 +10418 +12560 +6382 + +8598 +1089 +11386 +9538 +6933 +3943 + +14268 +7172 +7680 +15936 + +5695 + +69893 + +7293 +4968 +7862 +3210 +1924 +4742 +3519 +1095 +5922 +6335 +2405 + +4233 +3819 +7282 +10018 +3820 +5380 +9377 +8889 + +13183 +9964 +4473 +12469 +9862 +6567 + +4903 +1198 +5238 +1582 +1820 +6080 +10178 + +4554 +6315 +2818 +5956 +2491 +5291 +5056 +3699 +2843 +1243 +5430 +2175 + +31126 + +5392 +1590 +3738 +2507 +2817 +6963 +6140 +4606 +2814 +7044 +1282 + +12230 +1430 +4283 +11385 +2557 +6868 + +3871 +2558 +1957 +4058 +1935 +5738 +1513 +5744 +4130 +3321 +3760 +4155 +1063 +3694 + +67474 + +2215 +8962 +5442 +7390 +6223 +3648 +2390 +5464 + +5125 +3842 +2131 +3414 +6085 +3353 +3660 +1593 +4395 +2664 +4987 +2352 +5693 +1391 +1661 + +6017 +5013 +5243 +3734 +3116 +1541 +6456 +2806 +6143 +1344 +5977 +5422 +2326 +2164 + +33377 + +5950 +5168 +1348 +3144 +5760 +6974 +8537 +6664 +1248 + +9040 +4510 +5642 +8547 +9407 +5058 +6158 +7726 +6731 + +4182 +5246 +2686 +4196 +3809 +3253 +3260 +3162 +2703 +6373 +6136 +2904 +6356 +5038 + +3368 +11040 +5793 +13747 +10478 +5166 + +11697 +10072 +8434 +5732 + +5244 +6374 +1877 +2902 +4263 +4835 +2283 +2086 +6044 +6343 +6232 +2849 +3694 +6392 + +13062 +12405 +12869 +16366 +1652 + +8036 +1720 +7759 +13661 +1370 +3505 + +5885 +1708 +10440 +7438 +11480 +4904 +8387 + +4788 +10326 +6143 +11063 +9649 +9384 + +5849 +1935 +7411 +4694 +4435 +1321 +5301 +2096 +2422 +5484 +6560 +3070 + +5008 +5405 +9444 +6355 +13150 +12686 + +1618 +5831 +1957 +1288 +1382 +2946 +1926 +1828 +1646 +4823 +3637 +1309 +5727 +4450 + +66234 + +4016 +4075 +4551 +5869 +2866 +2461 +1055 +4712 +7220 +1973 +4650 + +2387 +1102 +4558 +2290 +1969 +3624 +2383 +4059 +4877 +3439 +6891 +3819 +5824 + +3440 +11447 +5522 +16700 + +8373 +1356 +7249 +8484 +6767 +7990 +8778 +2235 +5995 +8511 + +5466 +6088 +2583 +5235 +6093 +3089 +1570 +6112 +3668 +2187 +5582 +1380 +4800 +5529 +2987 + +12323 +4775 +11105 +3805 +7671 +6737 + +3197 +1256 +4539 +6383 +1476 +2134 +6785 +7337 +1229 +4965 +5055 + +13128 +8149 +4426 +8087 +2873 + +1695 +8214 +7147 +7216 +1070 +2801 +5033 +8103 +7750 +5749 + +4082 +1630 +1958 +6113 +4486 +2111 +1486 +6451 +5630 +5682 +4558 + +10475 +30751 + +5397 +5067 +1178 +2106 +5557 +4847 +1661 +3469 +4908 +2590 +4686 +2118 +4495 +2994 + +12590 +12902 +8742 +15433 +2777 + +7451 +8722 +7302 +2269 +9592 +6162 + +1907 +6741 +4442 +6526 +8289 +7479 +9481 +1642 +4982 + +2001 +12803 +7255 +13545 +10713 + +14621 +25826 +20771 + +1055 +8331 +7422 +8400 +2208 +6688 +8425 +6883 +4192 +4725 + +9970 +10127 +2777 +3933 +9274 +3791 +9906 + +2779 +1118 +2823 +1563 +9917 +6065 +1863 +1885 + +5053 +1922 +5356 +8266 +7335 +4260 +8730 +7307 +3140 + +4340 +1080 +2120 +5498 +6769 +6170 +2944 +4677 +1155 +4676 +2420 +4945 +5941 + +14838 +3844 +19712 +16283 + +1906 +3672 +4274 +6550 +6072 +5299 +4493 +3348 +2804 +3747 +3208 +5359 + +59156 + +6128 +5959 +12262 +12166 +4945 +9635 + +8095 +1757 +13656 +7215 +2545 +6298 + +7873 +6515 +6407 +4182 +4911 +3966 +1942 +7281 +5082 +7446 +5545 + +3876 +5618 +4226 +4445 +3915 +1250 +5469 +3872 +5157 +5539 +2894 +1357 +1419 +4214 +3009 + +3364 +6618 +1432 +7583 +6192 +2529 +2076 +6231 +7333 + +5028 +2989 +3801 +1895 +2367 +5747 +2724 +1304 +3085 +2524 +1295 +1022 +2613 +4239 +4413 + +3418 +23885 + +3895 +3302 +1097 +3150 +1379 +1815 +1484 +2074 +2616 +3395 +6100 +5488 +1006 +3046 + +1951 +8238 +6010 +3652 +6640 +7990 +3900 +8191 + +6978 +3025 +1966 +7152 +5963 +7326 +1866 +2507 +4764 +1434 +3194 +3326 + +3081 +6828 +6495 +3257 +4236 +4938 +7090 +2826 +6614 +1341 +7098 +6771 + +18885 +3826 +13103 +1193 + +16653 +13676 +19294 +13878 + +7460 +4327 +7335 +4898 +6259 +7941 +5940 +5603 +3095 +5674 +2886 + +8680 +4662 +8044 +8210 +5886 +9057 +7917 +3258 +5995 + +2800 + +31568 + +5423 +33845 + +8988 +13780 +9059 +14650 +9824 + +10327 +13747 +10937 +4594 +6882 + +2696 +2860 +1070 +1662 +6098 +2369 +2821 +3603 +3593 +5347 +4871 +4967 +2211 +3330 +5226 + +8520 +1896 +1906 +5172 +6845 +1565 +6366 +5509 +5068 + +4047 +4661 +2384 +1904 +4705 +5402 +6650 +6120 +4834 +5897 +6492 +6200 +5868 + +4130 +1530 +9654 +9306 +8529 +2508 +8918 +4040 +3717 + +3245 +4195 +11698 +10053 +6635 +4749 + +12283 +11894 +8168 + +6418 +7151 +4827 +2391 +8643 +2315 +8664 +3547 +6900 +4418 + +65083 + +5164 +1748 +8739 +4945 +2604 +5493 +5310 +3763 +6011 +4091 + +4342 +2566 +6021 +4067 +1385 +2486 +4444 +1713 +3380 +4253 +4720 +1602 +4814 +3234 +1739 + +13603 +22634 + +7845 + +4139 +8832 +7441 +1967 +6707 +7981 +3416 +10633 + +4808 +9206 +6975 +4257 +6024 +5176 + +10639 +9329 +5010 +7215 +4522 +8168 +9936 +9792 + +4326 +4601 +6508 +2501 +3866 +3060 +2188 +6429 +1014 +1383 +6937 +5313 +4392 + +8977 +2420 +1013 +4267 +9689 +2955 +5583 +2947 +5570 + +5067 +1139 +4531 +3001 +7698 +2028 +8025 +1424 +6837 +1357 +2186 + +5433 +19739 +5321 + +5019 +4714 +2688 +5290 +1593 +1320 +1703 +2978 +1476 +3034 +3538 +2825 +5819 +5547 + +6441 +1104 +3128 +7940 +5977 +8434 +4493 +2139 +1617 +4933 + +3219 +3273 +12383 +6076 +11222 +5523 + +8875 +1005 +4958 +3234 +3196 +5527 +9875 +7306 + +1591 +6031 +2124 +1224 +7008 +2559 +4540 +7670 + +5927 +4323 +5630 +1067 +5482 +1939 +5428 +3416 +3494 +2469 +4287 +5538 +1190 +4234 +1734 + +10808 +15234 +14069 +10497 +7697 diff --git a/2022/cpp/01/result b/2022/cpp/01/result new file mode 100755 index 0000000..95484ad Binary files /dev/null and b/2022/cpp/01/result differ diff --git a/2022/cpp/01/result.cpp b/2022/cpp/01/result.cpp new file mode 100644 index 0000000..bd1722a --- /dev/null +++ b/2022/cpp/01/result.cpp @@ -0,0 +1,28 @@ +#include "answer.hpp" +#include + + +void error(const char * msg){ + std::cerr << msg << std::endl; + exit(1); +} + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + std::vector input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/01/sample1 b/2022/cpp/01/sample1 new file mode 100644 index 0000000..444e241 --- /dev/null +++ b/2022/cpp/01/sample1 @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 \ No newline at end of file diff --git a/2022/cpp/01/sample2 b/2022/cpp/01/sample2 new file mode 100644 index 0000000..444e241 --- /dev/null +++ b/2022/cpp/01/sample2 @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 \ No newline at end of file diff --git a/2022/cpp/01/test b/2022/cpp/01/test new file mode 100755 index 0000000..6d57062 Binary files /dev/null and b/2022/cpp/01/test differ diff --git a/2022/cpp/01/test.cpp b/2022/cpp/01/test.cpp new file mode 100644 index 0000000..8dcfb25 --- /dev/null +++ b/2022/cpp/01/test.cpp @@ -0,0 +1,65 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "answer.hpp" +#include "../doctest/doctest/doctest.h" + +#ifdef SAMPLE1 + +TEST_CASE("Part 1"){ + std::vector expected_input; + + Input elf1; + elf1.calories.insert(elf1.calories.end(), { + 1000, + 2000, + 3000, + }); + + Input elf2; + elf2.calories.insert(elf2.calories.end(), { + 4000, + }); + + Input elf3; + elf3.calories.insert(elf3.calories.end(), { + 5000, + 6000, + }); + + Input elf4; + elf4.calories.insert(elf4.calories.end(), { + 7000, + 8000, + 9000, + }); + + Input elf5; + elf5.calories.insert(elf5.calories.end(), { + 10000 + }); + + expected_input.insert(expected_input.end(), { + elf1, elf2, elf3, elf4, elf5 + }); + + + std::vector actual_input = get_input("sample1"); + + SUBCASE("Testing input is parsed correctly"){ + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "24000"); + } +} +#endif + +#ifdef SAMPLE2 +TEST_CASE("Part 2"){ + std::vector actual_input = get_input("sample2"); + + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "45000"); + } +} +#endif diff --git a/2022/cpp/02/Makefile b/2022/cpp/02/Makefile new file mode 100644 index 0000000..a8467e3 --- /dev/null +++ b/2022/cpp/02/Makefile @@ -0,0 +1,36 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +# sample1 sample2: $(OBJ_DIR)/$(OBJ) + # $(eval CXXFLAGS += -DSAMPLE$(@:sample%=%)) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/02/answer.cpp b/2022/cpp/02/answer.cpp new file mode 100644 index 0000000..1d94fb9 --- /dev/null +++ b/2022/cpp/02/answer.cpp @@ -0,0 +1,74 @@ +// AOC - 2022 - 02 +#include "answer.hpp" +#include +#include + +Move get_move(const char & move){ + if (move == 'A' || move == 'X') + return Move::ROCK; + if (move == 'B' || move == 'Y') + return Move::PAPER; + if (move == 'C' || move == 'Z') + return Move::SCISSORS; + error("Move not valid"); +} + + +std::vector get_input(const char * filename){ + std::ifstream stream(filename); + std::string str; + std::vector ret; + + while (getline(stream, str)){ +#if PART == 1 + ret.push_back({ + .my = get_move(str[2]), + .opponent = get_move(str[0]) + }); +#else + Move opponent = get_move(str[0]); + Move my; + switch (str[2]) { + case 'X': // lose + my = static_cast((opponent - 1 + 3) % 3); + break; + case 'Y': // draw + my = opponent; + break; + case 'Z': // win + my = static_cast((opponent + 1) % 3); + break; + default: + error("Part 2 move not valid"); + } + ret.push_back({ + .my = my, + .opponent = opponent + }); +#endif + } + return ret; +} + +std::string get_result(std::vector input){ + std::string ret; + uint score = 0; + + for (auto round : input){ + std::cout << "before: " << score << std::endl; + score += round.my + 1; + if (round.my == round.opponent){ // draw + score += 3; + continue; + } + std::cout << round.my << " " << round.opponent << std::endl; + if (round.my == (round.opponent + 1) % 3){ // win + std::cout << "win" << std::endl; + score += 6; + } + std::cout << "after: " << score << std::endl; + } + ret = std::to_string(score); + + return ret; +} diff --git a/2022/cpp/02/answer.hpp b/2022/cpp/02/answer.hpp new file mode 100644 index 0000000..84184d1 --- /dev/null +++ b/2022/cpp/02/answer.hpp @@ -0,0 +1,36 @@ +#include +#include +#include "lib.hpp" + +enum Move { + ROCK, + PAPER, + SCISSORS +}; + + +struct Input{ + Move my, opponent; + + bool operator ==(const Input & rhs) const{ + return this->my == rhs.my && this->opponent == rhs.opponent; + } +}; + +inline std::ostream& operator<<(std::ostream &os, Input & inp){ + os << "my: " << inp.my << ", opponent: " << inp.opponent; + return os; +} + + +#ifndef VERBOSE +#define VERBOSE 0 +#endif +std::vector get_input(const char *); + +std::string get_result(std::vector); + +inline void error(const char * msg){ + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/02/build/answer.o b/2022/cpp/02/build/answer.o new file mode 100644 index 0000000..be29c6c Binary files /dev/null and b/2022/cpp/02/build/answer.o differ diff --git a/2022/cpp/02/input b/2022/cpp/02/input new file mode 100644 index 0000000..e49a6c4 --- /dev/null +++ b/2022/cpp/02/input @@ -0,0 +1,2500 @@ +A Z +A Z +C Y +A X +A X +A Z +C X +A X +C Y +A Z +B Y +B Y +C X +C Y +C X +B Y +A Y +C Y +B Y +B Z +B Y +C X +A Z +C Y +B Y +B Y +A Z +B Y +B Y +B Y +B Z +B Y +A Z +B Y +C Y +B Y +B Z +C X +B Z +A X +B Y +B Z +C Y +A Z +C Y +A Z +B Y +C Y +A Z +A Z +B Y +C Y +C X +A Z +B Z +A Z +B Y +C X +B Y +B Y +B Y +A Y +B Z +C Y +C X +A Z +B Y +B Y +B X +A Z +C X +B Y +B Y +A Z +A Z +A X +A X +B Y +C X +A X +A Z +A Z +B Y +C Y +B Z +A X +B Z +A Z +B Y +A Z +C Y +A Z +A X +B Y +C Y +B Y +A X +B Z +C Y +B Y +B Y +A Z +A X +A X +B Y +C Y +B X +B Z +C X +B Y +A Y +B Y +B Y +B Y +A X +C Y +A Z +C Y +C Y +B Z +B Y +A Z +A Z +A Z +B Y +C Y +B Z +C X +C Y +B Z +A X +A Z +C Y +B Y +C Y +C Y +C Y +B Y +B X +B Y +B Y +C Y +A X +B Z +B X +C X +B X +C Y +B Z +B Y +A X +C Y +C Y +C X +B Z +B Y +A Y +B Z +A Z +B Y +B Y +B Z +B Z +C Y +A Z +A X +A X +C Y +B Y +A Z +B Z +A X +A Z +B Z +C Y +C Z +A X +A Y +B Y +C Y +B Y +C Y +B Z +B Y +A Z +C Y +B Z +B Y +A Z +A Y +B Y +B Y +C Y +A Z +C Y +B Y +B Y +A X +A Z +A Z +B Y +B Y +B Z +C Y +C X +B Z +A Y +B Z +A X +C X +A Y +B Z +B Z +A X +C X +C X +A Z +C Y +A X +A X +C X +A X +B Z +C Z +B Z +B Y +B Y +A Z +A Z +B Y +A Z +C Y +C X +B Y +A X +A X +B Z +C Y +C Y +A X +B Z +A X +C X +C X +B Z +C X +B Z +A X +A Y +A X +A Z +A X +B Y +C Y +A X +C Y +A Z +B Z +A Z +C Y +A X +A Z +B Y +A Z +B Z +B Z +A Z +B Z +A X +B Y +B Z +C X +B Y +B Y +B Y +B Y +A X +B Z +B Z +A Z +A Z +C Y +B Y +C X +A X +B Y +B Y +C X +B Y +A X +C Y +A Z +B Y +A Y +B Z +B Y +A Z +A X +B Z +C Y +A Z +C Y +C X +B Y +B Z +C X +A Y +B Z +A Y +B Z +B Z +B Y +B Z +C X +B Y +A X +B Z +A Z +B Y +A Z +C Y +C Y +C X +C X +A X +C X +A X +A X +B Z +B Y +B Y +B Y +B Y +C X +C X +A Z +A Z +C X +C X +C Y +C Y +C Y +B Y +A Z +C Y +B Z +B Z +B Y +C X +C X +C Y +C Y +C X +B Z +B Z +B Y +C Y +B Y +A Z +C Y +A X +B Y +C Y +C Y +C Y +B Y +A Z +B Y +A X +B Z +C X +A Z +C X +A Z +B Z +C Y +C Y +C Y +C X +B Z +C X +B Y +B Z +C X +B X +A Z +B Z +B Y +C Y +B Z +C Y +A X +B Z +C Y +C Y +B Z +B Y +C Y +B Z +C Y +A Z +C X +C X +C Y +A Z +C Z +C Y +B Y +C Y +B Y +B Y +B Z +C X +C X +B Z +B Z +B Z +A Z +B Y +B Y +A X +B Y +C Y +B Y +C X +C X +B Y +B Y +A Y +B Z +B Y +B Y +C Y +A Z +C X +A Y +B Y +B Z +B Y +A Y +C Y +A Y +A Z +B Y +A Z +C Y +C Y +B Y +A Z +B Y +B Y +A Z +B Z +B Y +C X +C Y +B Z +B Y +B Y +A Z +C Z +A Z +A X +C X +B Z +B Z +C X +C Y +B Y +B X +A X +C Y +B Z +B Y +C X +B Y +A Z +A Z +A X +C Y +B Z +B Y +C X +C X +B Y +A Z +C Z +B Y +A Z +C Y +B Y +C X +A Z +A Z +A X +C X +A X +C X +C X +A Z +C X +B Y +A Z +B Y +B Y +C Y +C X +A Z +B Z +B Y +B Y +A Y +A Z +B Y +A Z +B Z +B Y +C Y +A Z +B Y +B Y +B Y +C Y +C X +A X +A Z +B Z +B Y +C Y +B Z +C X +B Y +C X +A Z +B Y +B X +B Y +A Y +A X +A X +C X +C X +B Y +B X +C Y +A Z +B Z +C Y +C Y +B Y +A Z +C X +A Z +C Y +B Z +B X +C X +B X +C X +B Y +C Y +C Y +B Z +B Y +B Z +B Z +A Z +B Y +A Y +B Z +C X +C Y +C X +A Z +B Z +C Y +B Y +B Z +C X +B Y +A Y +A X +A X +C Y +B X +C X +B Z +A Z +A Y +B Z +C X +C X +B Y +A X +B Z +C Y +B X +B Z +C Y +B Y +C Y +B Z +B Y +B Z +B Y +C X +B Y +C X +B Z +B Z +C X +B Z +A X +B X +B Y +A X +B Z +C X +B Z +B Y +B Y +C X +A Y +B Z +B Z +B Y +B Y +B Z +C Y +B Z +C Y +C X +A Z +C X +A X +C Y +B Y +C X +A Z +B Y +A Z +A Z +B X +C Y +B Z +C Y +B Y +B Z +A X +B Z +A X +C X +B Z +B Y +C X +C Y +C Y +B X +C X +C X +B Z +C Y +B Z +B Y +B Z +C Y +B Y +C Y +C X +B Y +B Y +B Z +A Z +C X +A Z +B Y +B Z +A Z +C Y +A Z +B Y +A Z +A Z +C Y +A Y +B Y +A Y +C X +B Y +C X +B Z +C Y +A Z +C X +C X +B Z +C Z +C Z +B Y +B Y +C Y +C Y +A Z +C Y +C Y +C Y +A Z +B Z +C Y +B Y +C Y +C X +C X +B X +B Y +C Y +A Y +B Y +B Z +B Y +A Y +B Y +B X +C X +C Z +C Y +C Y +C Y +B Z +B X +C Y +B Z +C X +C Y +A Z +A Y +B Y +C X +A Z +B Y +C Y +B Z +A Z +A Z +B Y +A Z +C Y +A X +C Y +B Z +B Z +B Z +B Y +B Y +A Z +A Z +B Y +C X +A Z +C Y +B Y +A Z +C Y +A Z +B Z +B Z +A Y +B Y +B Y +B Z +C Z +B Y +B X +B Y +C X +B Z +C Y +B Z +A Y +B Z +A Z +A X +A Z +C X +C X +B Y +C X +C Y +A Z +B Z +B Y +A Z +B Z +B Z +B Z +C Y +B Y +A X +C Y +C Y +C X +A X +C Y +B Y +C X +B Z +A Y +B X +B Y +A Z +B Y +B Y +B Y +B Z +C X +B Z +B Z +C X +B Y +A Y +B Y +C Y +A Z +C Z +B Y +B Z +B Z +C Y +A Z +C Y +C X +C Y +B Y +A X +A X +B Z +C Y +B Z +B Y +A Y +C X +A Z +B Z +C Y +C Z +C X +B Y +C X +A Y +A Y +B Z +C X +B X +C X +B Y +B Z +B Z +B Y +C Y +B Y +B Y +A Z +B Z +B Y +C X +B Z +B Y +B Y +C Y +C Z +C X +B Y +A X +C Y +C X +B Z +C Y +A X +B Y +A Y +B Y +C X +C Y +C X +A X +B Z +C X +B Y +A Z +C X +B Y +A Z +B Y +B Y +B Y +A Z +A X +C X +C Y +C Y +B Y +B Y +B Z +A Y +C X +C X +B Y +B Y +B Z +A X +C X +C X +B X +A X +B Z +B Y +C X +A X +B Y +C Y +A X +B Y +C X +B Z +C Z +B X +C Y +B Y +C Y +A Z +C Y +B Z +C X +B Z +C X +C Y +C X +C Y +C Z +C X +C X +B Y +C Y +C X +C X +B Z +B Z +A X +C Y +C Z +B Y +C Z +C Y +C X +C X +B Y +C Y +A Y +B Y +A Y +B Y +B Y +B Y +C Y +C Z +B Y +C X +C X +B Z +B Y +B Z +C Y +B Z +C X +B Z +B Y +B Y +A Z +A X +B X +A X +C X +C Y +B Y +A X +B Z +B Y +B Y +A X +C X +C X +B Y +B Y +C Y +C Y +B Z +B Y +B Y +B Y +C Y +A X +B Y +C Y +B Z +B Y +B Y +B Z +B Y +B Y +B Z +B Y +B Y +C Y +A Z +B Z +A Y +B Y +C Y +B Z +B Y +B Y +C X +A X +C Z +C X +B Y +B Z +C Y +C X +B Z +A Z +A Z +C Y +A Z +B Y +B Y +B Z +B Y +C Y +B Z +C Y +B Z +B Z +B Y +C X +B Y +B Z +A X +C X +C X +B Z +B Z +C X +B Y +A Z +B Y +B Z +C Y +B Y +A Z +B Z +B Y +B Y +B Y +C X +C Y +A Z +B Y +B Y +A Z +A Z +A Z +A X +B X +A Z +C Y +C X +A X +B Z +A X +C X +B Z +B Y +B Y +C X +A Z +C Y +C X +A Y +C X +A Z +B Z +B Z +B Y +C Y +B Z +A Z +A Z +C Y +B Y +B Z +A Z +A Z +B Z +B Y +A Z +C Y +A Z +A Z +B Y +B Z +B Z +A X +A X +B Y +B Z +B Z +C X +B Z +C Z +B Y +B Y +B Y +B Z +A X +A Z +B Y +A X +B Y +B Y +C X +C X +C Y +C Y +A Z +A X +B Y +A Z +B Z +B Y +C X +B Y +B X +C X +A X +A Z +C Y +C X +C Y +B Y +B Y +A X +A Y +B Y +A Y +C Y +C X +B Y +B Y +B Z +C X +C X +B Y +C X +B Z +C Y +A X +A Z +A Z +B Y +A Y +C Y +C X +C Y +C Y +B Y +B Y +A Z +C X +A X +B Y +B Y +B Y +B Z +A Z +B Y +B Y +A Z +B Y +A Z +C X +B Z +C Y +B Y +B Y +A X +B Y +A X +B Y +B Z +C X +B Z +B Y +C X +C Y +C Y +A X +A X +B Z +B Z +B Y +C X +A Z +C Y +C Y +C Y +B Z +B Z +B Y +A Z +C Y +A X +A Z +C Y +B Z +B Y +B Y +A Z +B Y +A Z +C X +A X +B Z +B Y +B Y +B Z +A Z +C X +B Y +C X +B Z +C X +A Z +B Y +A Z +A Z +B Y +C Y +C X +C Y +A X +C Y +B Y +B Z +C X +C Y +A X +B Z +B Y +C Y +A X +B Z +A Y +B Z +B Y +B Z +A X +A Z +A X +B Z +C Y +B Y +A Y +B Z +A Z +C Y +C X +B Z +B Y +C Y +B Z +C X +C Y +A X +B Z +B Z +B Z +B Z +B Y +C X +B Y +C X +A Z +A Z +C Y +A Y +B Y +C X +A Z +B Z +C X +C X +A X +B Y +C X +A Z +B Y +B Y +B Y +B Y +C X +C Y +C Y +B Y +A Z +B Y +B Z +B Z +B Y +A X +B Z +B Z +C X +C X +C Y +B Y +C Y +B Y +B Y +B Z +B X +C X +C Y +A Z +B Z +B Y +A X +B Z +A Z +B Z +B Y +B Y +A Z +C Y +B Y +C Y +B Z +B Y +C X +B Z +B Z +C Y +C Y +B Y +C Y +C X +B X +B Y +C Y +B Y +B X +C X +B Y +A Y +A Z +A Y +B Z +A Z +A Z +A Z +B Y +A Z +A Z +C Y +B Z +A Z +A X +B Y +C Y +C X +C X +A Z +B Z +B X +B Z +C X +B Z +C Y +C X +C Z +C X +B Z +B Z +B Z +B Z +A Z +A Z +C Y +C Z +C Y +A X +B Y +B Y +B Y +A Z +A Z +B Z +C Y +C X +B Y +C X +B Z +B Y +B Y +B Y +B Y +B Y +B X +A Z +A X +A Y +C X +B Z +B Y +B Z +C Y +A Z +B Z +B Z +C Y +B Z +B Z +C X +B Y +C X +B Z +C X +C Y +C Y +C X +A Z +A Z +B Z +B Y +C X +C X +A Y +A Z +C X +A X +A X +A Z +C Y +B Z +B Y +A Z +B Y +B Y +B Z +B Y +C Y +B Z +A X +B Y +A X +B Z +A Z +C X +A X +C Z +A Z +C Y +A X +B Y +B Z +B Z +A X +C Y +A Z +C X +A Y +C X +B Y +B Z +A X +B Y +B Y +A Y +A X +B Y +C X +A Y +B Y +B Y +C Y +A Z +B Z +A Z +B Y +A Z +C Y +C X +B Z +B Y +B Z +A X +C Y +C Z +A X +A X +C Y +C Y +A Z +C Y +C X +A Y +C Y +B Y +B X +B Y +A Z +C X +C X +B Y +A Z +A X +B Z +C X +B Y +B Y +C Z +A X +A X +B Z +C X +C Y +B Z +C Z +B Y +A Y +A Z +A Y +C Y +C Y +C Y +B Z +B Z +B Y +B Y +B Y +B Z +B Z +C X +A X +C X +B Y +B Y +B Y +C Y +B Y +B Y +C X +A Y +B Z +C X +B Z +B Y +A X +C Y +B Y +C Y +B Z +B Y +C Y +C Z +C Y +A Y +B Z +A X +B Z +A Z +A Z +A Z +B Y +A X +C Y +B Y +B Y +B Y +C X +B Y +A X +C X +B Y +C X +A Z +B Z +B Z +B Y +C Y +B Y +A Z +B Y +C Y +B Z +C X +B Y +B Y +B Z +C X +C Y +B Z +B Z +C Y +C X +C X +B Y +B Z +A X +C X +A Z +A Y +B Z +B Y +B Y +C Y +C X +B Y +B Z +C Y +C Y +C X +C X +B Z +A Z +B Z +B Y +C X +A X +C X +A Z +C X +B Z +C Z +B Y +C Y +B Y +C Z +A Z +B Y +C Z +A X +B Z +B X +C Y +C X +B Z +A Z +B Y +A X +B X +A Z +C Y +A Y +A Z +A X +C X +A Y +C X +B X +B Y +C Z +A Z +C X +B Y +C X +C X +B X +B Y +A Y +B Y +B Y +A Z +B Y +C Y +C X +B Y +B Y +B Z +C Y +B Z +A Z +A Z +A X +B Y +C Y +B Y +C Y +C Y +B Z +A X +C Y +B Z +C Y +A Y +B Y +B Y +B Y +C Y +A Z +A Z +B Z +A Z +C Y +C Y +B Y +C X +B Y +A Z +B Z +A X +B Z +B Y +B Z +A X +A X +C Y +A Z +B Z +C Y +B Z +C X +B Z +A X +B Z +B Z +C Y +B Y +B Y +C Y +B Y +A X +B Y +C Y +A Z +B Y +C X +A Z +B Z +A Z +B Z +C Y +B Y +B Y +B Y +C Y +B Y +B Y +A Z +B Y +B Z +C X +B Z +C X +C X +A X +B Z +C Z +B Z +A Y +C X +C X +A Z +A X +A Y +A X +B Y +B Y +C X +C Z +A Y +C Y +B Y +B Y +C X +C X +B Z +B Z +B Y +C Y +A Z +B Y +C X +C X +A X +A X +A Y +B Y +C X +A Z +C Z +C Y +C Y +C Y +B Y +C Y +A Y +A X +B X +B Y +B Y +B Y +B Y +A Z +B Z +B Y +B Y +C Y +A X +A Z +A Z +A Z +B Y +A X +C Y +C Y +B Y +B Y +C Y +A Z +C Y +A X +C X +B Z +B Y +C X +B Y +A X +A Z +C X +C Y +C Y +C X +C X +B Z +B Y +A Z +B Y +B Z +B Z +B Y +B Y +A X +C X +C Y +C X +A Z +B Z +B Y +B Y +C Y +A X +A X +C Y +C X +B Z +A Y +B Y +B Z +B Y +A X +C Y +B Y +A X +C X +B Y +B Y +A Z +A Z +A Z +B Y +B Y +C X +B Z +A Z +B Z +A Z +C X +B Y +C X +B Y +C X +C X +B Y +B Y +A Y +C Y +C Y +B Z +C X +B Y +B Y +A X +A Z +B Y +C Y +B Y +C Y +A X +C Y +C Y +B Z +C Y +A X +C X +A Z +C Y +A Z +C X +B Z +B Y +B Z +C Y +B X +A Z +C Y +C X +B Y +C X +B Z +A X +A Z +C X +B Z +C X +B Y +A Z +B Y +C Y +B Y +C X +C Y +B X +B X +B Y +B Y +B Y +B Z +B Y +C X +A Z +A Z +C X +B Y +B Y +B Y +A Y +C Y +B Z +C X +C Y +A X +C Y +B Z +C Y +B Y +B Y +B Z +A Z +A Z +B Y +A Y +B Z +A X +C Y +B Y +A X +C X +B Y +C Y +B Z +B Y +C X +A Z +C Y +A Z +B Y +C Y +B Y +C X +A Z +C Y +C Y +A Y +B Z +C X +C X +A Z +C Z +C X +B Y +C X +A X +C Y +C X +C X +A X +A Y +A X +C X +A X +A X +B X +B Y +C Y +A X +B Z +C X +C Y +B Y +B Z +C Y +B Z +B Y +C X +A X +B Y +B Z +C Y +A Z +C X +C Z +A X +A Z +A Z +B Z +A Z +B Y +C Y +C Y +B Z +C Y +C Y +C Y +C X +A X +B Y +A X +C X +C Z +B Y +A Z +B Y +C Y +C Y +B Z +C Y +C X +B Z +C Y +B Y +B Z +A Z +B Z +B Z +B Y +A Z +A Y +C X +B Y +A Z +C Y +B Z +A Z +B Y +B Y +C Y +B Y +B Y +B Y +C Y +B X +B Y +A X +A X +B Z +A X +C Y +A Z +B Y +B Y +B Z +A Z +C Y +C X +B Y +A Z +A X +B Y +C X +B Z +A X +C X +B Z +B Z +B Z +B X +A X +C Z +C X +C Z +B X +A Z +C X +B Y +C X +C Y +C Y +B Y +B Z +B Y +C Y +C X +A Z +C Y +C Y +C Y +B Z +A Z +A Y +C X +C Y +B Y +C Y +C Y +B Z +C Y +B Y +B Y +C X +B Z +B Y +A X +B Y +A Z +B Y +C X +B Y +C X +B Y +A Z +C X +B Y +C X +B Z +C X +B Y +A X +C X +C X +A X +C Y +A Z +C Y +B Z +A Z +A X +C X +A X +C Y +A X +A X +B Z +C X +C Y +A Y +B Z +B Y +C Y +B Y +A Y +C Y +C Y +B Y +B Y +B Z +B Z +C X +B Y +C X +A X +A Z +C Y +B Z +C Y +B Y +C X +C X +A Z +B Z +A Z +C X +B Y +B Z +C Y +B Y +A X +A X +B Y +B Y +B Y +A X +B Y +B Z +A Z +A X +A X +B Z +B Y +A X +B Z +A Z +B Y +B Z +B Z +B Z +C Y +C Y +A Z +B Y +C Y +A Z +C X +B Z +A X +B X +B Y +B Y +C Y +C Y +B Z +B Y +A Z +B Y +A X +B Z +B Y +C Z +B Y +B Y +C Y +B Y +B Y +A Y +C Y +A Z +A Y +B Z +C X +B Z +A X +A Z +A X +C Y +A Z +B Y +B Z +B Z +C Y +C Y +C Y +B X +B Y +B Z +A X +B Y +C Y +A X +A X +B Y +C Y +A Y +B Z +B Z +B Z +A Z +B Y +C X +A Z +C Y +C Y +B Y +C Y +C Y +C Y +C Y +B Y +B Z +A Z +B X +C Y +C X +C X +C Y +A Z +C Y +C X +B Z +B Y +A Y +C Z +B Y +B Y +B Y +B Y +C Y +B Y +C Y +A Z +B Y +C X +C Y +B Z +A X +C Y +B Y +B Y +B Y +A Z +A X +A X +C X +C Z +B Z +A X +A Z +A X +A Y +C Y +C Y +A Z +C X +C X +B X +B Y +A X +A Z +A X +C Y +B X +A X +A X +C X +A Y +B Z +A Z +B Y +A X +A Z +B Y +A Z +C Y +B Z +A Z +C Y +C X +B Z +C Y +B Y +C X +A Z +B Z +A X +A X +C Z +A X +C X +B Y +B Z +A X +A X +C Y +A Z +C Y +C X +C Y +B Z +B Y +C X +C X +B Z +B Z +C Y +A X +B X +C X +C Y +A X +A Z +B Y +C X +A Y +C X +C Y +A Z +B Y +B Z +B Y +C X +C Y +C X +C X +B Y +C X +C X +B Y +C Z +C Y +C Y +B Z +B Z +B Y +A X +B Y +A Z +A Y +B Y +C Y +B Z +B Z +A X +B Y +A X +B Y +A Z +B Y +C X +A Z +B Z +C Y +B Y +A Y +A X +C X +A X +B Y +B Z +B Z +C Y +A X +B Y +B Z +B Y +B X +C Z +B Z +C X +C Y +C X +B Z +C Y +A X +A X +C X +B Y +B X +B Y +A X +C Y +A Z +B Y +B Y +B Y +B Z +B Y +C X +A Z +C Y +A Z +B Z +C X +C Y +C X +C Y +A Y +B Z +B Y +B Z +C Y +A X +B Y +A X +C X +B X +C X +A Z +B Z +B Z +C Y +B Z diff --git a/2022/cpp/02/lib.hpp b/2022/cpp/02/lib.hpp new file mode 100644 index 0000000..769aaaa --- /dev/null +++ b/2022/cpp/02/lib.hpp @@ -0,0 +1,18 @@ +#include +#include + +template +std::ostream &operator<<(std::ostream &os, const std::vector &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} diff --git a/2022/cpp/02/result b/2022/cpp/02/result new file mode 100755 index 0000000..9243995 Binary files /dev/null and b/2022/cpp/02/result differ diff --git a/2022/cpp/02/result.cpp b/2022/cpp/02/result.cpp new file mode 100644 index 0000000..db0428d --- /dev/null +++ b/2022/cpp/02/result.cpp @@ -0,0 +1,22 @@ +#include "answer.hpp" +#include + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + std::vector input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/02/sample1 b/2022/cpp/02/sample1 new file mode 100644 index 0000000..25097e8 --- /dev/null +++ b/2022/cpp/02/sample1 @@ -0,0 +1,3 @@ +A Y +B X +C Z \ No newline at end of file diff --git a/2022/cpp/02/sample2 b/2022/cpp/02/sample2 new file mode 100644 index 0000000..25097e8 --- /dev/null +++ b/2022/cpp/02/sample2 @@ -0,0 +1,3 @@ +A Y +B X +C Z \ No newline at end of file diff --git a/2022/cpp/02/test b/2022/cpp/02/test new file mode 100755 index 0000000..97dda3a Binary files /dev/null and b/2022/cpp/02/test differ diff --git a/2022/cpp/02/test.cpp b/2022/cpp/02/test.cpp new file mode 100644 index 0000000..b09c4be --- /dev/null +++ b/2022/cpp/02/test.cpp @@ -0,0 +1,41 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1"){ + std::vector expected_input; + expected_input.push_back({.my = Move::PAPER, .opponent = Move::ROCK}); + expected_input.push_back({.my = Move::ROCK, .opponent = Move::PAPER}); + expected_input.push_back({.my = Move::SCISSORS, .opponent = Move::SCISSORS}); + + std::vector actual_input = get_input("sample1"); + + SUBCASE("Testing input is parsed correctly"){ + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "15"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2"){ + std::vector expected_input; + expected_input.push_back({.my = Move::ROCK, .opponent = Move::ROCK}); + expected_input.push_back({.my = Move::ROCK, .opponent = Move::PAPER}); + expected_input.push_back({.my = Move::ROCK, .opponent = Move::SCISSORS}); + + std::vector actual_input = get_input("sample2"); + + SUBCASE("Testing input is parsed correctly"){ + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "12"); + } +} +#endif diff --git a/2022/cpp/03/Makefile b/2022/cpp/03/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/03/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/03/answer.cpp b/2022/cpp/03/answer.cpp new file mode 100644 index 0000000..7496300 --- /dev/null +++ b/2022/cpp/03/answer.cpp @@ -0,0 +1,77 @@ +// AOC - 2022 - 03 +#include "answer.hpp" + +#include +#include +#include +#include +#include +#include + +std::vector get_input(const char *filename) { + std::ifstream stream(filename); + std::string str; + std::vector ret; + + while (getline(stream, str)) + ret.push_back({.content = str}); + return ret; +} + +int get_priority(const char &c) { + return std::islower(c) ? 1 + (c - 'a') : 27 + (c - 'A'); +} + +std::string get_result(std::vector input) { + std::string ret; + + int score = 0; +#if PART == 1 + // filter through the first half to find common elements + for (auto &sacks : input) { + size_t size = sacks.content.size(); + if (size % 2 == 1) + error("sack does not have even number of elements"); + + std::string sack1 = sacks.content.substr(0, size / 2); + std::string sack2 = sacks.content.substr(size / 2); + std::unordered_set common_items; + + std::copy_if( + sack1.begin(), sack1.end(), + std::inserter(common_items, common_items.end()), + [sack2](char c) { return sack2.find(c) != std::string::npos; }); + + std::cout << sacks << std::endl; + std::cout << common_items << std::endl; + + // get priority of items found + for (auto &item : common_items) + score += get_priority(item); + std::cout << "score: " << score << std::endl; + } +#else + std::vector> groups; + for (size_t i = 0; i < input.size(); i += 3) { + groups.insert(groups.end(), {input[i + 0].content, input[i + 1].content, + input[i + 2].content}); + } + for (auto &group : groups) { + std::cout << group << std::endl; + std::unordered_set common_items; + std::copy_if(group[0].begin(), group[0].end(), + std::inserter(common_items, common_items.end()), + [group](char c) { + return group[1].find(c) != std::string::npos && + group[2].find(c) != std::string::npos; + }); + + // get priority of items found + for (auto &item : common_items) + score += get_priority(item); + std::cout << "score: " << score << std::endl; + } +#endif + ret = std::to_string(score); + return ret; +} diff --git a/2022/cpp/03/answer.hpp b/2022/cpp/03/answer.hpp new file mode 100644 index 0000000..731dfe7 --- /dev/null +++ b/2022/cpp/03/answer.hpp @@ -0,0 +1,33 @@ +#include +#include +#include "lib.hpp" + +#ifndef PART +#define PART 2 +#endif + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +struct Input{ + std::string content; + bool operator ==(const Input & rhs) const{ + return this->content == rhs.content; + } +}; + +inline std::ostream& operator<<(std::ostream &os, Input & inp){ + os << inp.content; + return os; +} + +std::vector get_input(const char *); + +std::string get_result(std::vector); + +inline void error(const char * msg){ + std::cerr << msg << std::endl; + exit(1); +} + diff --git a/2022/cpp/03/build/answer.o b/2022/cpp/03/build/answer.o new file mode 100644 index 0000000..e139662 Binary files /dev/null and b/2022/cpp/03/build/answer.o differ diff --git a/2022/cpp/03/input b/2022/cpp/03/input new file mode 100644 index 0000000..fa87bf9 --- /dev/null +++ b/2022/cpp/03/input @@ -0,0 +1,300 @@ +wgqJtbJMqZVTwWPZZT +LHcTGHQhzrTzBsZFPHFZWFFs +RnLRClzGzRGLGLGCNRjTMjJfgmffSffMqNgp +WPLgsfLmLgqZvZgSRR +RbwHdbDdQFFFMvvMjbhqhZZS +lzTdldBDszfGcRsr +ZjnhJjMjnbdnbHdFLmmfFLmnCCWFFl +PpNwtRsNsZSsRwCfzQQBfQszCBsC +PpwcqqVZRtbggggjcgJJ +ntczBcVcgnHzgBHnVntcBBFhgsmmmssqWNWNWqLvNhsqTN +bSSGdSDZbGSGdDmLmGTvTGmLFFhm +PlFbDpJDPbPdPbZQZDZlSCDBfMVRwBzBtBQzfzRHVMVRtH +fDVrmmrvcmCcVpfcfGlswpPwsttMpjJMPPjjtP +RgSTdndFLbJqqPssWWjPWjPjHS +FqgzQnTqJRRQqLLhTCDhDCDmcmlvvlhcVm +nnqVtHbfVHZVmtlvmHtZtrFSFTRRFhRccTbrLsLSGr +cCNJQJPJQgjjMQdDrGGsRhTFGFRFSpMS +gzdCwWdjNPgzcJgjwdZtVlHHmvvmZlvffHnz +FFgsgwNwWvggQsMWDwvQQvQcccdcJZDtJGBtVGGGtcVlzt +rjfTrbjpjRSRTbTpzldjjHBtJGBdltJG +RbrPTfpCfmbpmnfRRCvMvQWNBwFFgMsgBL +zzLHgjjjdFHWbGBjjzcbgQRmSvqsSpmRsRSQSmRMWv +ZfJVrwPhZhZlhQQqBSQSNSqM +tnCfrDCltfPzHFFLBgngHz +DCpwrrMhwCrCMVCpGFqpVDnWWTWBtnTWvWfvbbTdFWRv +lmhhcsQPmTtTnnPBTB +QmcjNJsJzHNljZsNqDCGGhwqCqhgDDZV +hLfRnSLfhcndCCPfJJjzJfzt +pHNWwDpGGNJBZjjNNj +gmgwwHpWTpmGDmDTggqHmmDSsnnhvcqScdVVSVcjLrRcnq +CdlTJgnQJVCllNVWTPZBmPPGhGRmghPRGs +wHDrSwtHbmhRvHVZ +DzzwrtVFjLNnMTCTLCWW +SppdsnGpNVnZZZLPMlMPGq +mdcfvTTbBddLJgZJLlcFqJ +fzTvfwjjfzzCbvvjvQjWvNHVNNVWrRtdnVNppNprDp +lmlCGTmNbZlbSFlbNGfnzWfWzCdWWfVdwRCf +jDHtHHvLjQtqrsqpjBBLprRzwfwJfzzhRnczhWQVwzVR +rqpPtpPjDqpqDLtLrPGGFSFgPlNZZSGMPnMZ +pSHShqgSMzVpphFnJMFMBtssdjRJ +PZDbZfmCDgDfDNQPwCflCQNJsjRBRBFsdBWBsJRjsbsFJF +DQvCwwZrPPlffDmQCDwZQPmPhqVTcGhSHSLTpSGhLHzpGghr +gPqgqqmmmPgsqvGmsMCCnfZZfvBpWZhVrrZdHBvH +TSlcttTjRTDlDDTRhZVdrHHpWVnfVrtd +SFJzFzcJjcRJwGGqJMMCwW +gzWNLSjRLzlNqqQMLhvQccGGmcQm +fFrttPdTFTrpVwGpbdVQQp +HnTBGfHTGzWWqCqngn +SddrLdVpjjVSgRBszFswzwlV +mtPMbMqPMvqHHHDTTglBvsFshFFg +bmBHbtPctMtbMNMtbPtPqHmMjpdZdcJGrjSWWZGjJZnjndWj +nljWJHRHGrDcMBbDLZHV +wdvwmhghhbtBMcLtwZ +gvQvvghTfPSmpmQljGFRjQbFGCsJbC +BmphBWmDBBQfpVgQZpjg +rqqGrrrqlnqqHqjNnVDSSSgQQffj +FqLrbsLFsbLbLqHlMrmwwPDcCmMMmJBwDJcC +wZccfslqZPFFjrFbFfQQ +vvTvVWCJJZVBWCSvnVJJrhjQVMjVjzbgMQbjpphh +BBmNWBvZRvSqwsGwssPcmc +LGpnfcnzfzQdNFNHqHJptq +RBNNvZSBRbRCCDJqHrDZqHFZtw +SRNhRsNhWSNWsRRvgjngQnnMTnTgQjGMff +twtZmwqBHtmqnnmlGLfcfvQQ +dgMSPSMdQGclRRdF +pVMrDgThDDlPWPWbBZtZqqttBqjqjT +PPSWCGSzpCCQwNsNPFhTNVbB +vqcgJngqLLcZLvBhNTVlbsvdFF +DDmHgRjHZhHtGfWpQH +sBLbwWWBvsBsqLqStRjcGGRnggjGcntJDn +NCMQPMQPMQNzGGRRgRJRGzcG +dHQNfPCFTQfFfVVNvwwJvSWSqWqrSqdS +prDBnnDpFDprnDPBDQBvpBZttcSqSZSZcScFJSHcZJtz +VhMVdLsjdqVWJSWZZZcHst +hLVdGLfqjGjlfhCfCLjTTmrlpQBPBmvnrgDgpp +SSSTJmmgbGwtmRZHCCZRCH +FWcPQrrWqflzSWpRHZCZHRSt +PSQzzdcQTghdndDJ +cLlrNPvljRhRgTlM +VmDBGnVdmJDnDBndnnVwDRvMgRsTbTzMMsgZghzzsB +SpHvmGnSDJnwvDQqfrCPLpPLCfpF +PppbRCCgpzzQCgCSgZTlNNTWnNNDNlRnGl +hwMhLtBcBdjjNzWzlclcNGTs +FjJFHJwhfwLHMLJLwPmqfQPgVCmQgCmCzC +jTtMqFjMBqBmTntTztBTnTZBRZRLpLJgDgJNhghJrNNhhLJh +DCDdvdGsVDVsflVdQSsfSwhRJlgppgpRpbWLgJbJpgbR +wfSHSsdSVvQSwfwQPQPqHtFDcMzmtjBntqMTqn +HQhQWLCSHCSCjnjQdSJdCSQgTTmZPTVZmqnTpPtnpmRmpp +vDrzhvGzfchvlGvMFMrqVqPgZVVtZtVRgZgm +bcvMfwvDsDfbvfwDbbdhCShWCBLLJWjHsHjj +zLSsJNCjsjLCNLCgGcwBPPdwBwqwqz +WMFZprZDbrddWRVRRDvlPPBcPhhlBqqHPGPhhffg +VdbFvZWWWZZDFTLtCmNntjTnLSnn +JLVhhwRbhVwcLFJFhhJcccqwsvpRlllvpWvZSBSSSRsNWpzl +jgzfPffgZNspgZQS +fjCmDCGnfmTfFqrFnhqbFzqt +SfMRRNHSNNLfRfHcRRsqwdCCsssTqBCvgBLv +llDDWQnFGtQnmtGQDWQFsgdHVBddndHsgqTsCTsg +GzGjpWmWbmQmbpGGmGjHSrZcMfZSRPJPfcMRcb +PDdMdRTRrLDSwzJvfSvJ +FnjQnsqsFTnStvplhhzzFS +TBHHCsgVRRcMHbLR +GcLdGBJvBvLJHccJBvqHpGzDFfzwfzjwhDwrSFpfpDSn +mZZrTTQVmQmlsMPVblZQVZmfCwjzzjChzCCbDSzhFjfnSb +gZlRlZNPlmlgTTPmNRvJWcqrNLdvHWLBcHtH +jWWbBwgwWwwtvvSCtHvgWsMFmscHzTGMmcssGFTTGz +ZrLtpLnlfQJqnfJtpLnZlrqdNNGqcDNNFFTNDzzMMTMsMNMs +LJQrnZnfLZnlrZflJJRVRQbbwBgCtCVjWgjBjjgbPjBB +fqQVfRqSqmpnlLnm +jFcjMJTjhwwggjFtgzCHmCzCmGzGlzpn +wstMFFjWDfQRvmDPSB +PgTFGPgcBZPcHPFBZRjGPgwCnmwCsmSdQdThmMMMQCQS +JbpvWtvfHblWDHJDzmndSdMQnSwCdhMdQD +rfvWlLlbtfJvvLJpqWbbqZRRGPVFNHVFgZNVFBgH +TRMrrGBLMLPtbssTGtBHwZmdQQbdNzzZZNZZdwjd +CVlVhCnclvhWSFFfQrWNrjmpNfwmjZ +rlqFlclChhCvnlDvgVvRRtPtqTGJHRMBRTPPqM +gZzCrQGQdrQvZHPTHWDbTgWPJM +nSpLlcnnVjsSVLLnLSnhLSsJPTTWFsqfbPMFMqJDbfqM +VwwnpwLnlPdQCwPPCC +lRlrnlrsrMlhVsRnVhGPvCFNcPBDBvccrCGr +RZQTzWTRdDNvBDdNcC +TqjZbWRHmlMJgnmsng +scQmLfQBQQvvZfLsmmvDJwpgSNSDDdcJSSwTGD +PHlMbtzCCnlbztMRzlPNNdNwGpDpwgwptNLGpw +rRHnLbhCzbbCHnHjMbzzjzFZmfQqWZQqvmhmfVZmqFBW +fLTQWTMQtjcCGCJCbf +gGsmsVSzmjCFHJCJgg +SPRsSwSvBsPRPsqzwSVqzmhVWtLWhTDNLlTDtLTWGpNMtDLt +mbzRbchRRQzzssLdhLggLddJ +DCqDNNNWvDvjcPLsJcLLdv +cpFCVNnVBHtbfFRtMRFf +PFRcCCPtsDDDtjVspgwmgTNpTgTpspsw +BqqqdJdHdMgSfMmZpZND +vDHJGdLbLzBJdGnDdrBqVtCzWPPhthtPFzzPCFtV +HvhvHdFdvJDfHdZdpfhrmGPljPRrGPPVDGrWWC +NMMsRBMzcRRMMBSzcnbmNGrCVCWrCqPClmPqlG +zLBbwMzQnRSQMThtZFLvpdgHtJfF +DpcJcJPmMcLSHHZCfpnH +BsBFvvqTFlbhgdbBBblfZLCLzfHWfjnjLCnCrh +dTsNgqFvNgsGlZJRtVtMPmtDmG +LdGQqzPGCCjJTJdTLJQJtFcFRSctcrFNFltPFtcc +HphMMbbMdBMHbBhhgHMnhvwFFvtrlSNRNgcRllcvcc +spHMhBnHnnsmWdnsnMBMdVGGmzjzLmZLDQCCCCZjqjTD +DDZMzcTRgDMLzqCffhfWfcWnfj +NsHVVJmswwSSwNPPNjnhqhnCCnhNvjfTnv +rSSddrGSGrlMrpTpQT +bbbfCfrLHMMMWVWC +SqsvNZqQvvqcjNvqZsMMwgFgFplTHQVRFgWH +BSZWWqBZBjmPGJGLbBtf +RNCNfzfRHmzHwSdRdGfzRJPqFcFcDFGccZZqtLtGLtgl +pjhVMhvhbjvPcDJvcZqt +bsQMTsjppmSdTnHSJH +PtLwpSwdSJwQnGvvqtvMhZ +TlFcHlTjVjsDTQnCQhbZGCVVnb +cjljTslTrlzzHDNRfNgLSNBJfBwNfG +HvsZZqqqwWZswWHTmHsvvfhSfBfDffjchfBbhD +MCpnCVpQClRNnlNQVQClfDhScmjBfLhmLDGbBNbS +gmtpJpQQllJnWdZWwJWFwJJT +TzBvBwwdhgRPGHlRHh +NLWttJsrLWttppLpsGlsmVbVGRljGDRgjV +MlllnNFnnQqCdzqq +vptzrJhMMGGMptJPhJGJPvdFTFcSsTBVsczBScTSFFfn +gbRjWgRjCqjZnfHCHnTSVBHF +mRwqNbmqlbbjqRNlLbNrDJntNDGtDhNpGMrpvJ +GPWZLgWqLHHGbgbbGPPmqHqfcjjRHJJBDRBRjBBjMHjwvwQc +dhpFSpzVSSMSlDBvMQ +TndsVNztVTspnsdpshtmZLPCGGNPfgqbWWfDGb +CbqDjjCdClqgrfJvrv +NGNPtGGzzHztPWWnlgJvfBnWBFgp +hGzNHhsmGccwHPHZHcwdCCdbTVRTvSmTCjbLCb +sqnqsHGpJbqnrbshpshHmmmCWZZmWwfTjTjHmfLZ +dPggRgSDDttMFgctgdDtDcDcRWJBmjTWwmRLBLfmwBBjZWTB +PFcVPlJPglbqhhrnnlNz +wZdDNDdPPfhqwWqbsF +VTngRzpnzMLvzTCLlhvDfltqqDttqFqs +mggTVpCDDSNjBmPZrd +cSdqJSTTTJcSJpCdQbqTCPPdjdDtGzwzjDwjwwwwzD +rVvsBBVgsVBhHhfljtgbPgGtWjPtwt +fvHsVZHVnRHpSJJRmbbSLT +lDDPRRjwLGlvVRDRPlwwwPvmpSfhVWSzhqfzqpHpVpVHfqSH +qBnqBNsBBChhCSfZ +nTQbQnNNQJTLvlmTPLqqmG +TTCJhDrmDpRVhvhHfffwzwfz +dmmdmglWcqvHvWsHzB +gdQMZbtlgQlZcMSttCNmVVrLSTTJ +PLZLqhZZzZLBjjjGrrPjMH +CcQcCcfRlWDjdrMrBrHC +WlWFMcFpcRFmsWFcmflqSJzTqzwLvshggsZJwz +LgqRDDDHHGTpgpJrQrQhhhCqrwPw +ZSBWjjFshCFlQDrJ +SjWnnbWtWnsztgGDDbDTGgHHGp +llfvMlvzjzGzGRfvMSGRfSdStrCtQNCZrrFdJJLnNtLZ +shhhshPHsTTqsBHTVTwTwZZnCtQrQnJtQCCJBCCZdZ +TPHTPTHmDnljplfpGfGm +qcNTmvvSvTNrWhRrTdthzW +bDVJphpMMJwJpMHtrrttWsgwtzRW +bFpGJbllPfplVQmnhvvcSmCFqq +GTPJGMQTPQMqZjHTBmnndBVddHrrzNrz +bbcRFgDpptRbffwmzmrvLmcZvmLmLv +WCwgWbpgwtgfpfMlQGPhSPZWTZPl +DsPCswsMPBMwPDCVJPnTPPWFGJNJmbJW +RvvddfvftdtvNzghGSbFnWTntJSttGbG +LNgRddgRlgzcgCDjjjHjcBCwcM +gnVtgBnpwBgShBgcwhJJhjCMMMDmLRjDRMjrDMMMDMqMRF +slsblHPNHlbTNbsPvszHQWbzqrZLMRmFMFmdFmrtDFdMLNZD +bPzfvbfsvvlHtlzPHllHGTlTppCJpgcngcpwnwCGGJnnShwV +cgQRgtzDbHPcgHzQWpTjTLdjjNNpNLsDss +nwccZBmwcJqmJnjsTvmlTSsdlTNs +CCGFCBVrBwwGBhqVnZBrqWMMQzHfQcHzzzPtfztGfg +lhnwnhlbgbngbcfDgJLJQqDdVd +FSrvtMFZVJJJVtcq +jZNNNNjmjSPjFTJmGGzswwzHwHpBsbPblhhW +tnDWHntzDtzQBZLMLzNLDDcRFFjhJBmcFRCTjRchcRvT +sqwsPlbGfSbPGSVbJfpjjhcTFmCRjjvmTTvRdw +lJqSqPVbgGSGrVSqJqflbWZQNDMLHnrQQWNDMtQMQz +lpltwwJqsWVLPtVt +DGHsDdZQzHLSLZcFRrFS +BGsGCnHmMlMwCfwT +nrRNzRMPrrPnNwNzTSFSTNtqZdtMttvQqQmjdjvZpgjZ +GWVhGcGhHhGcffbZGDmmtttQvpdtbpppdj +HGlHBhHGJfJJhCfZzLTTNnNrTnCNwT +jBpCZStjBwWrQCMrhw +TvcHBzHdPPzdvFTzzJlvzdQfThrhhrhfQTWMQfWMqfwf +bzGJJBJcJvdvBPFzddGgjZSbZZngRZNNnnsjRs +dqPqbpPFJfsFfMcNQNNtNmzrNQJn +VVBDWvwZWDLwGlDhLGWWVcmQtSNmLmtSdSSTmrcQQm +lZlhwDCdhhHllvWvjMHbgMpgffMggpPb +SWSFLLFWDSWDNFzmmLMfGlfsdfnJMBfwMGVnBf +vPtgZcctcTQQZRRcgCtZwRfqBVGqnVTBGVnqlsdBJnqV +RcjgwbbgNSbFbhDb +JrRZLrHvjQFPLnnBPQ +DhwbtHbzpcpFTgtQ +zlDwlHlzWSwDqhMMbSJVZVvrCrCZJZNZdJ +fgNCZSDtDfDZTrTfqWghQGzGQshgpGGFQg +RFvLnvFjnVjmLQGPQWLmWh +MdwwVMFbMdRHFbccbCZJtbDJrqqJZNJZCZ +fdZVBMMdfdfBCzhTzMdMCgCrGGrpQJmSmGJGmpJQVpLmqV +RbFnhNsvlDsFHttllGmqGpPLvJpmPJSqLL +tjNsDnNwbNjttNNZTzhMWzcZcMTwMd +DjSSMShjRjPCbDFCdCSDbpBBswfNWZBZZrBVBPNfVmVf +zltLjLqqGlzQntqqGztqcgncZrVrmNfNwWBVrVmrgwrfswsW +qcTqHLlnJzGznLJtHGhMbhjFhMMpFbpbThpp +ZVFZcctFQzsCtbZFnPPHqmqpwmvPmp +NrjGfMgcLLcfdLqpmRwRRqJJmdPw +LMNDgMBGlgGDLMNDGljctVbVWZTTCWChhTttsl +RMGRRhhgzgZMtHdGTtvDwDJFCDvvwdvwqFFv +rfrrjLNmmSnSjVSmNNPPbJVbqvqsqvhvFqCq +flrpnSlrSNfjrNjSphNSWlHRGzTgtQGHQtttBTRBRQHW +tplDDprhbvprvrJDprCpbsvHRfzSzTtzmRqSTznRRBRnSfFF +MVwWjVNVQGfcMnTmRnBm +VQwGLNLjWNWPGjZbsDBppBZhhDvBlZ +RVVrGVVchRZsnzRzBWZb +FQHWWCHwQmWmlqfCHSwJnsbNJnNsvttntBtb +QQSFgqgqLMLPPdWdMVhWDT +cZrMjncTdfJpPJbr +WHNqnQwwCwvlqHtCtHNslNlvLLPDfSVdVPVDVSfVSbftffVf +NCwwwQwllwnvgsvZzgzFZzBzjGGGMM +MvHpfzcTcZzpphhbsDSTStsltqSDtS +PRmnwCrWnWQrmNMRNnlNGbqlbDltdlbDtNtD +CRJnmRVWJfgMLvcz +HPFbHrrwLdVdgbDZqcphCqSZBhLZ +tQRfRRGtvTNNSGTMjjmDCRhmqpBChqhsRDZh +vjSfMzGQNQQnMtNTTWNNjgblFdFHwgdJJHHPwddgnr +BggPRVBPPgfCBmJTjTTqpTNpZBwMbr +lclLLllsQLFlsbMqNrMwTpwpcM +SvbDzSDbWFJfWPPgdnfR +GbpSSbGDNbSSJbDZNZbDppGtMntHLHvHCTLCJMHnRCMLTT +cdwddjBfPsmPPQqQqscnHgRMtngvtjjgCCTMzM +WvwPvWvflBwdQPlNVVhbDGpFhNGhbl +WZRGmRvpCRFTZMQQQMCdddDDcD +lqgqsgvjVtbMDzzbtcDQ +NsNNgjNNjsNhnSvRmnpGRmSTSG +nTgFtDTDDLrFBStdGdcHcbvGSc +QPzfPCMzWCjfMPJhWGlRbRWRWrRRdVVH +zhCrCQCjPrpNNBsNspNnwq +zTJpqFzbTzsWsVbbfLGfSSCDNSBCHfMLHG +rZcvtmhctrvmlPPmmmrhhmBLCHDCCLLDlqMlGMNDMwDC +rRhRhnnQPZhtZcZtdttZgqFWWVjssqQpppWpFpJW +NWPhdWJPWVzVqQrqmSsPbrPP +cZDRjGsffGsCDfffgjGgRQSrTcTmSlTrbnqmSSrlln +fFGCjGCjLDLFRgfDHZvzLVWtvsWWBtzJNWMB +qMVbtnmMMTpCppsR +NffHGrWzWWgDBfTRhChCnSCWcnjT +QrlQBPBrlPHrrQlrHFLqPnLvVvbVmVVJtq +MVMpHMZLVCpMrfWjvWnfrJ +hlblzDDzwlSlGtRhRlSdrfGGWnWWfFPjJjnfqWGF +lmlhBRlDhhhDRRhwDmBpHJmsNCHmCgNHJCJLsc +jvsLgmqLgHvbPPVbNjSCjC +pwTcRpRWLRMLJJFwBBGWcFWNVlDDCSTVttNPblZZCVVDlP +GcdhccpcpRpGRhGmfsHHzLQQHrmsnh +FMmSRgtMltMnVgnmNvlrsJrsZWjspvsZJp +QbdhqwqbNqdHbTdcbcpsrpvjfWfLJLfwJrWp +DQBBQqQGccdTPGqqBNtFGRSMRSFGtnVSnnmM +fPjGrfFrrprprdrbQPZwlcZwZmlJwH +qvNnvWnvWDvSvqNtWSLWStqbcJBQwQJwQZHLBZbcmJbblb +DMtvqSvvDtntCRfwzGCgdzzFjG +TfdZgtmfDgqgvlLFFsFHvcvZ +pphWQMVjQVVBWWjRlHlHnlcLDDhcnF +JQwwWVPBwMJpJwpWwGBWNzrDzSSzfgTPqTSTTtSPgt diff --git a/2022/cpp/03/lib.hpp b/2022/cpp/03/lib.hpp new file mode 100644 index 0000000..764ebb5 --- /dev/null +++ b/2022/cpp/03/lib.hpp @@ -0,0 +1,35 @@ +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &os, const std::unordered_set &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} + +template +std::ostream &operator<<(std::ostream &os, const std::vector &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} diff --git a/2022/cpp/03/result b/2022/cpp/03/result new file mode 100755 index 0000000..89e04f4 Binary files /dev/null and b/2022/cpp/03/result differ diff --git a/2022/cpp/03/result.cpp b/2022/cpp/03/result.cpp new file mode 100644 index 0000000..97db539 --- /dev/null +++ b/2022/cpp/03/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" +#include + + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + std::vector input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/03/sample1 b/2022/cpp/03/sample1 new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/2022/cpp/03/sample1 @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file diff --git a/2022/cpp/03/sample2 b/2022/cpp/03/sample2 new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/2022/cpp/03/sample2 @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file diff --git a/2022/cpp/03/test b/2022/cpp/03/test new file mode 100755 index 0000000..23289bf Binary files /dev/null and b/2022/cpp/03/test differ diff --git a/2022/cpp/03/test.cpp b/2022/cpp/03/test.cpp new file mode 100644 index 0000000..da6b52c --- /dev/null +++ b/2022/cpp/03/test.cpp @@ -0,0 +1,34 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1") { + std::vector expected_input; + expected_input.insert(expected_input.end(), { + {.content = "vJrwpWtwJgWrhcsFMMfFFhFp"}, + {.content = "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"}, + {.content = "PmmdzqPrVvPwwTWBwg"}, + {.content = "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"}, + {.content = "ttgJtRGJQctTZtZT"}, + {.content = "CrZsJsPPZsGzwwsLwLmpwMDw"}, + }); + std::vector actual_input = get_input("sample1"); + + SUBCASE("Testing input is parsed correctly") { CHECK(actual_input == expected_input); } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "157"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + std::vector actual_input = get_input("sample2"); + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "70"); + } +} +#endif diff --git a/2022/cpp/04/Makefile b/2022/cpp/04/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/04/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/04/answer.cpp b/2022/cpp/04/answer.cpp new file mode 100644 index 0000000..1462470 --- /dev/null +++ b/2022/cpp/04/answer.cpp @@ -0,0 +1,52 @@ +// AOC - 2022 - 04 +#include "answer.hpp" +#include +#include +#include + +Range get_range(std::stringstream & ss) { + uint start, end; + char _; + ss >> start; + ss >> _; + ss >> end; + ss >> _; + return {start, end}; +} + +std::vector get_input(const char * filename) { + std::ifstream stream(filename); + std::string str; + std::vector ret; + + while (getline(stream, str)) { + std::stringstream ss(str); + ret.push_back({get_range(ss), get_range(ss)}); + } + return ret; +} + +bool Range::fully_contains(const Range & rhs) const { + return this->start <= rhs.start && this->end >= rhs.end; +} + +bool Range::is_disjoint_from(const Range & rhs) const { + return this->start < rhs.start && this->end < rhs.start; +} + +bool Range::overlaps(const Range & rhs) const { + return !this->is_disjoint_from(rhs) && !rhs.is_disjoint_from(*this); +} + +std::string get_result(std::vector input) { + std::string ret; + uint count = 0; + for (auto & ranges : input) +#if PART == 1 + count += ranges.r1.fully_contains(ranges.r2) || ranges.r2.fully_contains(ranges.r1); +#else + count += ranges.r1.overlaps(ranges.r2); +#endif + ret = std::to_string(count); + return ret; +} diff --git a/2022/cpp/04/answer.hpp b/2022/cpp/04/answer.hpp new file mode 100644 index 0000000..68a27d2 --- /dev/null +++ b/2022/cpp/04/answer.hpp @@ -0,0 +1,45 @@ +#include +#include "lib.hpp" + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 1 +#endif + +struct Range{ + uint start, end; + bool operator ==(const Range & rhs) const{ + return this->start == rhs.start && this->end == rhs.end; + } + bool fully_contains(const Range & rhs) const; + bool overlaps(const Range & rhs) const; + bool is_disjoint_from(const Range & rhs) const; +}; + +struct Input{ + Range r1, r2; + + bool operator ==(const Input & rhs) const{ + return this->r1 == rhs.r1 && this->r2 == rhs.r2; + } +}; + +inline std::ostream& operator<<(std::ostream &os, Range & range){ + os << "[" << range.start << ", " << range.end << "]"; + return os; +} + +inline std::ostream& operator<<(std::ostream &os, Input & inp){ + os << inp.r1 << " & " << inp.r2; + return os; +} + + +std::vector get_input(const char *); + +std::string get_result(std::vector); + + diff --git a/2022/cpp/04/build/answer.o b/2022/cpp/04/build/answer.o new file mode 100644 index 0000000..1f996c8 Binary files /dev/null and b/2022/cpp/04/build/answer.o differ diff --git a/2022/cpp/04/input b/2022/cpp/04/input new file mode 100644 index 0000000..c7fb3fc --- /dev/null +++ b/2022/cpp/04/input @@ -0,0 +1,1000 @@ +48-50,48-49 +5-89,5-5 +17-57,55-96 +24-67,23-25 +89-99,11-89 +34-71,35-71 +13-17,16-95 +13-93,5-12 +11-91,1-11 +4-81,2-11 +53-82,52-53 +5-98,4-97 +4-78,3-3 +20-81,21-80 +79-83,77-82 +76-87,43-87 +14-93,12-62 +13-94,15-95 +22-75,14-53 +67-90,67-79 +1-82,1-82 +5-80,83-96 +28-57,56-56 +53-91,52-54 +21-97,2-36 +8-18,10-98 +7-91,12-91 +11-88,19-95 +22-88,21-88 +31-69,9-48 +2-3,3-43 +5-95,2-6 +5-85,5-79 +51-91,51-69 +41-91,41-64 +23-37,24-95 +1-28,3-94 +6-93,6-98 +8-65,34-66 +2-8,9-38 +3-75,2-4 +40-77,37-76 +51-51,11-51 +95-95,3-94 +90-90,81-90 +26-61,26-61 +1-96,97-98 +41-84,90-91 +94-94,1-94 +52-89,74-90 +9-11,10-74 +57-67,56-62 +29-98,30-78 +20-88,18-54 +11-81,10-82 +85-87,26-86 +8-45,46-78 +16-33,20-49 +73-93,16-93 +35-36,29-36 +4-98,98-98 +24-30,30-80 +64-99,72-97 +8-33,8-47 +88-96,93-99 +23-46,35-49 +41-60,40-41 +5-83,4-6 +76-76,15-76 +37-70,69-71 +11-77,11-12 +57-65,65-97 +20-46,46-62 +76-84,8-77 +10-29,29-53 +16-50,6-17 +20-99,19-19 +11-12,12-72 +24-91,77-92 +37-73,72-73 +50-52,73-89 +37-99,25-76 +1-88,1-2 +18-64,17-63 +19-52,17-52 +5-86,6-87 +13-87,14-87 +23-82,19-92 +56-79,57-79 +25-68,26-67 +5-95,94-96 +38-50,38-39 +32-75,32-75 +23-69,23-69 +25-54,18-25 +33-79,34-67 +38-62,38-65 +62-75,69-78 +2-17,3-17 +12-20,19-96 +7-7,4-8 +1-92,2-93 +29-70,69-77 +7-54,2-54 +9-34,16-34 +4-20,7-31 +4-89,4-47 +12-91,9-96 +8-79,9-80 +32-91,31-91 +1-4,5-55 +85-94,84-95 +73-74,6-74 +51-70,52-70 +21-75,7-27 +13-38,37-39 +42-97,42-98 +33-45,28-45 +12-14,13-98 +91-92,90-91 +66-83,57-83 +8-79,9-67 +59-72,71-72 +33-71,3-33 +13-38,12-38 +18-93,18-52 +28-80,12-26 +87-88,2-88 +7-89,7-85 +26-27,27-27 +12-97,12-97 +36-95,32-91 +25-33,25-32 +4-39,5-75 +40-58,41-58 +8-60,8-93 +9-69,70-70 +19-34,35-68 +47-47,38-47 +1-70,1-70 +39-82,38-83 +17-98,17-99 +56-86,37-56 +2-2,4-96 +5-97,97-97 +52-93,52-77 +39-66,38-66 +9-99,4-99 +25-37,24-37 +2-99,1-98 +13-15,15-15 +10-51,5-9 +38-99,19-96 +6-38,37-78 +94-98,95-99 +8-89,29-89 +2-98,1-98 +13-99,99-99 +1-76,1-25 +8-97,8-97 +9-90,89-93 +24-52,7-49 +30-97,45-99 +26-62,26-59 +15-27,21-30 +80-81,64-80 +65-76,2-76 +9-9,10-99 +3-6,4-82 +78-79,20-79 +11-50,49-49 +10-12,11-68 +1-5,4-85 +8-10,9-95 +16-86,16-87 +11-81,10-11 +36-87,75-87 +55-92,54-99 +3-18,18-84 +4-73,1-2 +16-66,5-76 +29-73,29-79 +60-91,67-91 +82-82,47-82 +31-38,31-37 +44-44,21-44 +93-94,65-94 +13-79,2-12 +4-97,5-97 +40-78,40-41 +69-99,1-69 +8-97,8-96 +27-27,26-79 +60-96,71-88 +24-57,18-25 +1-26,2-27 +53-81,69-81 +3-96,2-96 +9-99,9-97 +45-48,46-56 +6-36,5-30 +45-73,46-73 +28-41,27-42 +4-91,90-94 +6-39,20-44 +3-3,5-65 +17-98,18-65 +15-98,15-96 +29-88,65-90 +10-61,60-62 +26-91,27-91 +78-96,95-95 +6-22,6-17 +40-56,40-57 +4-5,6-98 +78-83,51-83 +8-78,8-78 +26-87,26-86 +4-94,3-4 +5-55,2-6 +40-50,19-56 +55-68,2-94 +39-41,9-40 +68-76,68-69 +51-60,50-59 +9-48,2-10 +13-96,13-63 +1-90,4-91 +1-7,6-99 +25-67,25-66 +13-94,14-93 +27-28,28-70 +39-41,25-40 +8-53,9-53 +56-89,57-88 +4-56,50-56 +22-76,15-17 +7-98,69-76 +12-46,11-46 +1-41,3-72 +43-55,44-76 +9-81,9-81 +45-95,45-94 +12-68,12-69 +90-96,74-85 +67-68,68-68 +50-73,2-73 +6-17,6-97 +70-76,71-99 +9-43,9-43 +9-76,8-75 +66-90,65-66 +13-99,12-14 +1-2,3-91 +53-86,52-53 +3-57,3-96 +81-92,80-92 +6-18,17-88 +70-94,69-69 +17-17,18-88 +4-92,4-93 +41-61,47-61 +25-52,25-73 +78-97,12-97 +39-54,38-46 +39-52,39-58 +8-99,8-51 +52-58,51-52 +5-45,6-46 +3-99,98-98 +84-87,84-98 +58-87,86-88 +6-72,71-73 +41-42,41-86 +27-42,27-77 +14-15,14-74 +43-43,40-43 +17-95,17-93 +44-66,65-70 +4-99,5-99 +43-43,44-86 +64-75,63-71 +4-99,5-99 +6-29,6-99 +19-84,18-83 +72-72,10-72 +9-14,13-88 +3-98,98-98 +9-81,10-81 +13-69,13-70 +3-87,1-87 +22-61,10-62 +75-90,75-90 +69-99,68-87 +94-94,91-95 +2-3,3-96 +5-93,48-93 +43-45,33-49 +85-87,2-86 +51-52,52-62 +50-91,40-49 +1-81,21-81 +51-56,51-56 +22-23,23-37 +25-27,26-67 +58-59,23-58 +37-85,84-85 +12-97,12-96 +14-38,2-13 +33-81,32-44 +34-65,46-60 +3-28,27-28 +11-54,11-55 +2-93,1-93 +28-28,28-50 +10-82,81-83 +77-82,77-82 +33-63,49-63 +2-84,89-91 +37-90,36-91 +46-85,45-85 +97-97,14-97 +5-18,6-19 +7-94,6-95 +17-87,12-86 +11-85,39-85 +7-94,7-93 +9-98,9-96 +18-18,19-96 +62-62,34-62 +24-36,24-91 +72-91,4-91 +7-84,7-83 +9-93,9-87 +22-99,21-99 +32-46,31-32 +32-70,69-97 +13-89,12-88 +10-10,11-89 +46-46,47-64 +51-51,24-52 +5-51,3-3 +46-90,99-99 +6-96,5-95 +50-96,50-97 +96-96,4-70 +13-36,12-35 +19-90,13-20 +12-43,42-95 +1-83,1-84 +3-5,4-70 +24-97,25-98 +27-40,1-50 +37-82,36-83 +47-95,47-94 +2-80,2-58 +64-83,63-87 +42-52,41-44 +26-97,1-97 +35-45,35-78 +6-45,28-82 +11-17,11-16 +67-67,44-67 +2-95,2-94 +22-95,22-94 +20-43,21-92 +33-60,59-76 +6-75,7-75 +3-90,96-99 +38-76,41-76 +18-98,6-19 +2-65,64-77 +3-88,2-88 +14-94,13-93 +40-89,71-99 +44-93,44-72 +48-62,48-61 +6-27,6-26 +43-53,43-54 +4-97,36-98 +8-13,12-83 +15-96,7-96 +18-91,17-18 +11-13,12-55 +31-92,19-94 +44-87,14-45 +18-51,17-18 +23-24,27-27 +23-46,24-88 +12-90,13-90 +37-70,34-70 +1-98,88-98 +2-93,6-95 +73-99,74-98 +4-46,2-46 +50-52,51-75 +45-91,46-90 +11-85,58-94 +26-43,26-44 +56-95,55-94 +10-93,11-92 +1-99,1-99 +8-95,7-7 +19-69,9-18 +18-90,9-60 +74-93,13-73 +62-90,61-72 +7-53,52-95 +44-82,4-44 +17-88,17-88 +26-27,26-89 +1-9,1-62 +13-40,39-62 +2-93,1-92 +91-91,17-91 +57-58,58-85 +23-98,97-99 +16-48,16-48 +25-61,61-87 +14-47,13-84 +46-96,47-94 +8-98,7-99 +3-5,4-97 +38-83,28-82 +7-99,6-34 +23-91,38-84 +33-35,34-63 +14-55,55-55 +34-50,11-35 +3-69,4-69 +67-99,99-99 +38-94,2-59 +2-98,2-97 +77-79,42-78 +6-45,44-45 +39-79,61-93 +5-92,5-97 +4-61,3-64 +47-48,48-67 +11-23,10-23 +2-99,9-99 +31-99,30-81 +28-87,29-88 +8-31,2-13 +3-17,16-83 +29-58,57-59 +4-75,15-35 +49-94,48-93 +23-85,84-84 +7-92,7-97 +53-91,91-97 +26-95,1-95 +9-22,7-9 +13-63,62-95 +54-55,54-97 +80-88,79-88 +1-44,2-44 +3-83,1-4 +95-95,26-95 +23-26,23-26 +19-35,19-34 +17-34,34-34 +39-89,75-97 +42-84,84-84 +60-95,61-94 +6-88,5-88 +2-68,1-68 +16-76,15-16 +30-93,9-96 +10-91,11-91 +36-88,35-36 +24-67,45-66 +41-58,16-96 +84-85,83-85 +10-59,18-59 +33-70,69-69 +58-69,57-58 +4-6,68-85 +3-91,91-91 +99-99,2-98 +33-94,93-95 +68-69,27-68 +4-4,5-14 +98-98,2-97 +5-91,90-92 +2-91,2-90 +37-81,80-80 +9-51,54-69 +45-59,46-59 +8-69,7-69 +20-21,21-29 +45-83,88-91 +22-87,20-21 +4-77,3-76 +50-81,49-49 +47-59,58-78 +22-85,21-84 +3-86,7-91 +63-83,62-63 +10-58,7-57 +14-88,14-88 +18-67,16-28 +39-50,39-42 +9-85,8-85 +7-28,7-8 +72-72,19-73 +25-27,25-56 +40-43,40-43 +10-26,25-25 +29-99,30-99 +9-14,13-92 +9-17,10-56 +93-94,1-94 +31-67,30-68 +15-84,84-84 +45-46,8-46 +10-93,10-94 +31-48,5-94 +49-93,92-92 +45-80,40-67 +63-94,7-93 +2-94,1-66 +26-44,41-53 +51-94,1-94 +5-93,4-92 +14-86,85-86 +9-52,8-10 +24-61,23-62 +1-78,77-79 +6-97,17-96 +43-65,19-43 +45-99,35-99 +58-58,20-57 +22-89,64-85 +12-22,21-80 +6-36,36-36 +1-92,87-92 +26-99,26-97 +7-85,1-85 +20-98,20-98 +2-51,2-54 +73-95,74-95 +16-54,15-17 +4-95,4-96 +11-98,11-98 +67-79,66-80 +61-61,62-63 +50-62,12-50 +9-80,9-81 +1-1,3-87 +16-61,15-23 +31-35,34-40 +13-89,12-90 +89-91,90-95 +20-60,12-81 +2-65,1-66 +38-96,56-96 +23-23,12-22 +96-96,11-95 +8-37,36-97 +2-26,1-26 +30-71,37-73 +71-87,70-70 +31-99,31-82 +6-39,2-7 +1-99,3-99 +29-73,32-81 +33-37,33-36 +15-41,14-40 +32-33,33-90 +23-85,22-24 +26-93,26-86 +3-97,2-98 +10-92,92-94 +29-55,30-55 +9-75,29-96 +6-94,93-97 +63-63,52-63 +24-78,5-5 +38-67,37-67 +14-32,13-32 +35-94,36-94 +23-92,24-92 +9-36,8-36 +63-95,62-62 +37-78,3-78 +34-51,17-37 +84-85,13-84 +1-95,1-98 +18-56,18-19 +17-78,13-16 +6-52,15-44 +13-33,14-33 +10-88,11-89 +61-80,61-79 +6-98,98-98 +1-91,1-85 +60-86,10-87 +3-96,95-98 +34-99,33-98 +19-21,20-91 +7-96,8-96 +2-90,2-90 +36-96,36-96 +76-96,60-75 +8-44,7-44 +9-28,10-28 +10-95,7-95 +56-68,29-82 +10-97,18-99 +68-97,45-97 +19-92,2-92 +6-97,5-97 +74-74,73-73 +20-70,20-70 +18-66,19-67 +91-94,7-92 +4-47,4-47 +11-93,12-93 +33-54,32-53 +54-64,55-63 +20-99,20-21 +39-82,56-68 +4-36,36-47 +30-41,25-31 +65-69,69-69 +20-71,71-74 +16-36,24-54 +2-96,95-98 +11-70,10-12 +5-36,35-95 +36-92,36-37 +37-78,32-87 +28-89,15-88 +9-63,62-62 +2-94,4-94 +85-85,72-85 +30-73,7-31 +10-36,10-36 +75-97,47-95 +23-23,4-24 +49-88,36-87 +25-25,26-42 +33-86,26-86 +13-76,76-76 +60-60,60-60 +3-23,24-24 +4-85,10-85 +7-38,8-37 +40-85,41-84 +46-86,47-87 +8-38,7-38 +76-78,9-77 +80-94,57-94 +86-96,86-97 +35-89,10-67 +45-94,35-81 +8-93,7-8 +22-48,23-47 +21-94,20-94 +63-72,60-73 +89-89,57-90 +34-41,41-78 +5-5,2-6 +1-79,1-80 +79-82,80-83 +37-37,38-53 +65-93,64-96 +33-85,33-33 +7-98,44-57 +2-5,4-69 +19-69,18-68 +80-81,7-81 +19-99,20-99 +5-34,4-14 +4-88,88-89 +51-91,59-93 +33-60,33-58 +32-67,27-94 +8-98,7-8 +34-43,44-69 +10-80,10-11 +7-73,11-73 +23-69,24-69 +68-83,6-68 +2-35,5-35 +1-4,3-90 +88-89,30-89 +57-99,14-63 +16-29,17-28 +67-82,62-77 +20-65,19-66 +37-69,61-63 +58-94,94-94 +1-72,72-72 +7-11,10-47 +18-59,59-59 +57-57,37-58 +60-81,54-72 +2-86,1-87 +7-78,7-77 +59-69,59-65 +15-16,15-73 +15-82,16-92 +8-88,57-88 +36-92,36-88 +59-60,24-59 +4-22,1-3 +44-60,45-60 +90-95,35-89 +21-30,20-30 +4-83,83-83 +16-99,7-17 +11-18,19-95 +45-94,44-94 +3-11,3-12 +9-70,8-10 +1-8,8-78 +1-5,4-97 +30-55,29-31 +7-89,8-89 +47-47,46-48 +66-96,38-84 +33-34,33-73 +2-23,1-22 +52-64,53-91 +9-34,19-61 +25-57,25-57 +1-98,97-98 +79-89,76-80 +28-80,28-80 +13-77,76-78 +3-88,2-88 +69-72,70-72 +91-91,22-91 +77-95,76-94 +84-92,84-91 +55-99,56-99 +1-91,2-91 +33-36,27-36 +79-84,62-74 +52-54,18-53 +26-37,25-27 +55-85,40-85 +58-74,10-59 +6-53,6-53 +42-49,42-47 +47-79,80-97 +11-24,25-91 +83-87,86-87 +1-88,1-96 +55-63,54-62 +76-76,8-76 +3-78,78-99 +1-86,8-96 +44-70,43-70 +25-27,26-91 +65-98,97-97 +14-64,15-65 +9-36,8-35 +78-84,77-85 +42-58,41-59 +60-92,59-60 +95-97,14-86 +8-82,7-83 +5-58,5-59 +5-99,6-99 +4-13,1-4 +57-86,43-58 +11-82,11-11 +4-87,1-5 +6-7,7-11 +6-22,2-7 +15-49,14-49 +39-97,99-99 +12-49,13-90 +70-80,71-80 +46-95,66-94 +94-98,13-94 +3-11,11-11 +13-23,12-23 +11-98,11-98 +20-99,8-20 +28-87,27-87 +16-21,21-61 +2-91,7-56 +7-7,8-71 +74-74,67-74 +23-88,1-74 +1-99,2-98 +34-85,40-86 +6-85,6-68 +28-31,31-49 +13-40,13-40 +55-56,56-56 +70-72,26-71 +93-94,34-93 +45-75,44-75 +4-10,10-50 +65-74,65-77 +39-87,6-59 +11-85,3-85 +8-46,12-46 +9-9,10-95 +30-41,27-92 +13-97,9-96 +21-62,22-63 +54-69,52-53 +17-99,18-99 +75-92,7-76 +38-39,38-90 +50-69,49-50 +65-87,65-99 +6-98,6-96 +34-62,40-90 +33-47,34-48 +33-95,34-95 +42-99,43-98 +70-85,1-71 +37-63,38-63 +12-17,17-72 +28-31,29-92 +18-69,8-17 +41-96,40-95 +7-86,7-86 +68-78,67-73 +4-6,7-99 +8-86,86-87 +5-6,6-37 +88-94,89-95 +33-74,33-74 +86-87,86-87 +29-55,62-78 +23-38,13-38 +9-44,10-89 +18-67,19-97 +3-83,1-66 +55-81,55-81 +6-72,7-71 +5-55,6-55 +14-99,14-97 +39-83,16-40 +11-73,55-73 +4-68,5-67 +66-73,26-73 +3-92,3-91 +36-59,33-35 +72-73,4-73 +1-83,2-83 +25-88,25-43 +45-71,45-73 +9-96,95-95 +4-5,7-23 +48-50,4-49 +45-96,44-96 +50-87,49-50 +98-99,27-96 +4-40,40-78 +3-94,94-94 +7-34,25-26 +72-79,23-72 +2-84,3-83 +78-96,25-78 +77-94,73-94 +63-66,62-65 +25-80,24-80 +10-33,78-90 +25-55,24-54 +58-62,23-64 +61-62,62-84 +83-83,46-84 +23-30,31-92 +8-95,94-99 +35-65,34-64 +12-19,12-18 +21-25,24-43 +54-79,54-84 +37-56,37-57 +77-77,15-77 +57-88,55-88 +7-93,8-93 +5-89,3-66 +33-63,32-33 +9-52,51-96 +7-97,6-66 +3-58,1-3 +26-85,25-84 +11-63,10-12 +52-54,53-54 +23-68,22-22 +14-96,14-95 +4-41,3-21 +4-6,5-83 +97-98,1-97 +13-90,73-88 +11-15,7-10 +65-80,24-81 +48-82,48-83 +81-87,4-81 +40-47,40-47 +15-87,14-75 +30-50,29-30 +26-65,64-64 +37-37,7-37 +51-51,49-51 +5-71,7-70 +18-54,53-54 +47-48,48-60 +26-98,25-97 +5-6,5-81 +3-98,3-99 +9-22,21-22 +4-97,1-1 +36-36,37-77 +22-35,4-23 +18-95,19-96 +14-94,14-96 +43-46,47-50 +80-91,81-91 +20-67,5-81 +8-87,9-86 +30-69,23-30 +96-99,2-96 +59-75,74-76 +20-66,19-66 +10-85,10-65 +9-82,8-38 +32-66,54-66 +23-64,63-65 +1-89,1-90 +1-95,1-94 +12-82,7-64 +55-66,60-66 +3-93,3-42 +45-87,15-88 +3-50,3-76 +7-86,8-79 +39-39,40-85 +57-86,52-52 +17-84,16-18 +63-99,64-99 +83-94,83-94 +12-85,11-86 +18-89,17-17 +12-25,13-87 +9-12,10-15 +62-92,85-88 +26-97,26-94 +16-94,94-94 +47-83,47-83 +25-86,85-88 +1-99,1-98 +47-87,87-99 +9-99,9-97 +8-73,2-73 +45-47,46-85 +18-95,18-96 +41-41,40-41 +1-4,4-61 +4-95,5-96 +8-79,78-80 +6-81,9-81 +7-90,1-7 +6-22,6-22 +32-82,15-59 +19-93,13-93 +43-48,12-99 +45-45,46-59 +27-69,1-28 +78-78,59-79 +12-57,57-57 +4-39,4-77 +55-91,91-91 +27-87,92-95 +83-87,87-98 +22-84,22-31 +3-91,3-22 +45-86,57-62 +68-82,55-82 +96-98,3-97 +12-32,1-32 +37-71,37-71 +31-59,31-59 +86-88,79-88 +4-92,3-3 +40-91,17-91 +51-52,52-74 +2-97,1-98 +50-83,82-83 +34-34,35-41 +6-47,5-5 +5-65,5-47 +32-77,33-85 +21-68,22-67 +12-45,46-95 +45-48,38-46 +39-84,35-40 +4-60,3-4 +67-83,68-84 +24-92,25-93 diff --git a/2022/cpp/04/lib.hpp b/2022/cpp/04/lib.hpp new file mode 100644 index 0000000..0e53a4c --- /dev/null +++ b/2022/cpp/04/lib.hpp @@ -0,0 +1,24 @@ +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &os, const std::vector &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} + +inline void error(const char * msg){ + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/04/result b/2022/cpp/04/result new file mode 100755 index 0000000..598c735 Binary files /dev/null and b/2022/cpp/04/result differ diff --git a/2022/cpp/04/result.cpp b/2022/cpp/04/result.cpp new file mode 100644 index 0000000..97db539 --- /dev/null +++ b/2022/cpp/04/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" +#include + + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + std::vector input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/04/sample1 b/2022/cpp/04/sample1 new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/2022/cpp/04/sample1 @@ -0,0 +1,6 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8 \ No newline at end of file diff --git a/2022/cpp/04/test b/2022/cpp/04/test new file mode 100755 index 0000000..235eb90 Binary files /dev/null and b/2022/cpp/04/test differ diff --git a/2022/cpp/04/test.cpp b/2022/cpp/04/test.cpp new file mode 100644 index 0000000..b9d1c0b --- /dev/null +++ b/2022/cpp/04/test.cpp @@ -0,0 +1,35 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1"){ + std::vector expected_input; + expected_input.insert(expected_input.end(), { + {{2, 4}, {6, 8}}, + {{2, 3}, {4, 5}}, + {{5, 7}, {7, 9}}, + {{2, 8}, {3, 7}}, + {{6, 6}, {4, 6}}, + {{2, 6}, {4, 8}}, + }); + std::vector actual_input = get_input("sample1"); + + SUBCASE("Testing input is parsed correctly"){ + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "2"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2"){ + std::vector actual_input = get_input("sample1"); + SUBCASE("Testing output is the one expected from AOC"){ + CHECK(get_result(actual_input) == "4"); + } +} +#endif diff --git a/2022/cpp/05/Makefile b/2022/cpp/05/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/05/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/05/answer.cpp b/2022/cpp/05/answer.cpp new file mode 100644 index 0000000..3893fea --- /dev/null +++ b/2022/cpp/05/answer.cpp @@ -0,0 +1,91 @@ +// AOC - 2022 - 05 +#include "answer.hpp" +#include +#include +#include +#include + +#define STACK_WIDTH 4 + +stacks_t get_stacks(std::vector stack_lines, uint n_stacks) { + // reverse so that stack_lines[0] is the bottom of the stacks + std::reverse(stack_lines.begin(), stack_lines.end()); + + std::vector> stacks(n_stacks); + uint max_height = stack_lines.size(); + + for (uint h = 0; h < max_height; h++) { + for (uint i = 0; i < n_stacks; i++) { + char item = stack_lines[h][i * STACK_WIDTH + 1]; + if (item != ' ') + stacks[i].push_back(item); + } + } + return stacks; +} + +Move get_move(std::string line) { + std::stringstream ss(line); + std::string _; + uint item, from, to; + ss >> _; // move + ss >> item; + ss >> _; // from + ss >> from; + ss >> _; // to + ss >> to; + return {--item, --from, --to}; +} + +Input get_input(const char * filename) { + std::ifstream stream(filename); + std::string str; + Input ret; + + std::vector stack_lines; + stacks_t stacks; + + while (getline(stream, str) && !std::isdigit(str[1])) { + stack_lines.push_back(str); + } + std::reverse(str.begin(), str.end() - 1); + std::stringstream ss(str); + uint n_stacks; + ss >> n_stacks; + stacks = get_stacks(stack_lines, n_stacks); + + getline(stream, str); // get rid of the empty line between the stacks and + // the move instructions + + std::vector moves; + while (getline(stream, str)) + moves.push_back(get_move(str)); + + return {stacks, moves}; +} + +template std::vector pop(std::vector & v, uint i) { + std::vector ret(v.begin() + i, v.end()); + v.erase(v.begin() + i, v.end()); + return ret; +} + +std::string get_result(Input input) { + std::string ret; + stacks_t stacks = input.stacks; + std::cout << stacks << std::endl; + for (auto & move : input.moves) { + std::cout << move << std::endl; + std::vector tbi = pop(stacks[move.from], stacks[move.from].size() - 1 - move.item); +#if PART == 1 + std::reverse(tbi.begin(), tbi.end()); +#endif + stacks[move.to].insert(stacks[move.to].end(), + tbi.begin(), tbi.end()); + std::cout << stacks << std::endl << std::endl; + } + + for (auto & stack : stacks) + ret += stack.back(); + return ret; +} diff --git a/2022/cpp/05/answer.hpp b/2022/cpp/05/answer.hpp new file mode 100644 index 0000000..e7f5367 --- /dev/null +++ b/2022/cpp/05/answer.hpp @@ -0,0 +1,61 @@ +#include +#include +#include "lib.hpp" + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 2 +#endif + +struct Move{ + uint item, from, to; + + bool operator ==(const Move & rhs) const{ + return this->item == rhs.item && this->from == rhs.from && this->to == rhs.to; + } +}; + +typedef std::vector> stacks_t; + +struct Input{ + stacks_t stacks; + std::vector moves; + bool operator ==(const Input & rhs) const{ + return this->moves == rhs.moves && this->stacks == rhs.stacks; + } +}; + +inline std::ostream& operator<<(std::ostream &os, stacks_t & stacks){ + uint i = 0; + for (auto & stack: stacks){ + os << i++ << " "; + for (auto & item : stack) + os << item << " "; + os << std::endl; + } + return os; +} + +inline std::ostream& operator<<(std::ostream &os, Move & move){ + os << "move " << move.item << " from " << move.from << " to " << move.to; + return os; +} + +inline std::ostream& operator<<(std::ostream &os, Input & inp){ + os << "---------------------------------" << std::endl; + os << inp.stacks << std::endl; + for (auto & move: inp.moves) + os << move << std::endl; + os << "---------------------------------" << std::endl; + return os; +} + + +Input get_input(const char *); + +std::string get_result(Input); + + diff --git a/2022/cpp/05/build/answer.o b/2022/cpp/05/build/answer.o new file mode 100644 index 0000000..3fda9ee Binary files /dev/null and b/2022/cpp/05/build/answer.o differ diff --git a/2022/cpp/05/input b/2022/cpp/05/input new file mode 100644 index 0000000..33b0fd6 --- /dev/null +++ b/2022/cpp/05/input @@ -0,0 +1,512 @@ +[F] [L] [M] +[T] [H] [V] [G] [V] +[N] [T] [D] [R] [N] [D] +[Z] [B] [C] [P] [B] [R] [Z] +[M] [J] [N] [M] [F] [M] [V] [H] +[G] [J] [L] [J] [S] [C] [G] [M] [F] +[H] [W] [V] [P] [W] [H] [H] [N] [N] +[J] [V] [G] [B] [F] [G] [D] [H] [G] + 1 2 3 4 5 6 7 8 9 + +move 6 from 4 to 3 +move 5 from 8 to 9 +move 1 from 4 to 5 +move 1 from 4 to 5 +move 2 from 2 to 7 +move 2 from 1 to 6 +move 9 from 6 to 1 +move 12 from 3 to 5 +move 1 from 8 to 4 +move 3 from 1 to 5 +move 1 from 6 to 7 +move 10 from 5 to 2 +move 14 from 5 to 1 +move 8 from 7 to 9 +move 11 from 2 to 9 +move 1 from 3 to 9 +move 11 from 1 to 5 +move 2 from 1 to 9 +move 1 from 4 to 8 +move 6 from 1 to 5 +move 1 from 8 to 3 +move 16 from 5 to 1 +move 4 from 1 to 3 +move 1 from 5 to 6 +move 4 from 3 to 4 +move 1 from 6 to 7 +move 21 from 9 to 6 +move 2 from 1 to 9 +move 2 from 4 to 9 +move 5 from 9 to 4 +move 9 from 1 to 6 +move 6 from 4 to 6 +move 1 from 6 to 2 +move 1 from 7 to 6 +move 1 from 3 to 2 +move 8 from 6 to 9 +move 3 from 1 to 8 +move 1 from 2 to 1 +move 13 from 6 to 3 +move 1 from 1 to 9 +move 2 from 1 to 6 +move 3 from 8 to 4 +move 4 from 4 to 9 +move 3 from 1 to 3 +move 22 from 9 to 8 +move 1 from 2 to 9 +move 6 from 8 to 9 +move 15 from 6 to 5 +move 5 from 8 to 9 +move 11 from 9 to 8 +move 13 from 5 to 1 +move 1 from 6 to 5 +move 1 from 9 to 3 +move 21 from 8 to 3 +move 3 from 5 to 3 +move 11 from 1 to 2 +move 25 from 3 to 1 +move 5 from 1 to 7 +move 20 from 1 to 7 +move 1 from 6 to 7 +move 16 from 3 to 9 +move 8 from 9 to 6 +move 1 from 1 to 5 +move 5 from 9 to 4 +move 2 from 2 to 1 +move 2 from 9 to 4 +move 1 from 9 to 4 +move 1 from 8 to 4 +move 1 from 5 to 2 +move 3 from 4 to 6 +move 1 from 4 to 7 +move 9 from 7 to 6 +move 5 from 4 to 6 +move 7 from 7 to 2 +move 1 from 1 to 6 +move 11 from 2 to 5 +move 10 from 5 to 1 +move 1 from 6 to 8 +move 1 from 5 to 7 +move 24 from 6 to 1 +move 12 from 1 to 4 +move 12 from 4 to 8 +move 2 from 2 to 7 +move 3 from 7 to 2 +move 5 from 2 to 8 +move 9 from 8 to 9 +move 9 from 8 to 5 +move 1 from 9 to 1 +move 14 from 1 to 8 +move 11 from 7 to 9 +move 4 from 1 to 3 +move 7 from 1 to 2 +move 3 from 3 to 7 +move 12 from 9 to 7 +move 8 from 7 to 2 +move 4 from 9 to 2 +move 1 from 3 to 6 +move 5 from 5 to 9 +move 14 from 2 to 1 +move 8 from 9 to 4 +move 6 from 4 to 5 +move 5 from 5 to 7 +move 1 from 8 to 2 +move 2 from 4 to 6 +move 4 from 7 to 3 +move 10 from 8 to 4 +move 2 from 3 to 6 +move 7 from 7 to 6 +move 10 from 4 to 8 +move 5 from 1 to 6 +move 8 from 2 to 1 +move 7 from 6 to 8 +move 9 from 6 to 5 +move 16 from 1 to 6 +move 2 from 3 to 9 +move 1 from 7 to 4 +move 2 from 9 to 1 +move 14 from 6 to 7 +move 1 from 6 to 3 +move 2 from 6 to 3 +move 9 from 5 to 7 +move 3 from 1 to 6 +move 3 from 3 to 7 +move 5 from 5 to 9 +move 3 from 6 to 2 +move 1 from 6 to 2 +move 12 from 8 to 2 +move 5 from 2 to 1 +move 2 from 1 to 3 +move 25 from 7 to 1 +move 1 from 4 to 6 +move 2 from 3 to 9 +move 26 from 1 to 9 +move 2 from 1 to 8 +move 1 from 6 to 8 +move 1 from 7 to 1 +move 7 from 8 to 1 +move 7 from 1 to 5 +move 1 from 1 to 2 +move 2 from 8 to 6 +move 32 from 9 to 8 +move 1 from 6 to 5 +move 5 from 2 to 9 +move 1 from 9 to 7 +move 24 from 8 to 3 +move 1 from 6 to 9 +move 3 from 2 to 5 +move 1 from 7 to 9 +move 4 from 9 to 3 +move 8 from 8 to 7 +move 18 from 3 to 7 +move 20 from 7 to 8 +move 6 from 8 to 9 +move 6 from 5 to 1 +move 8 from 9 to 4 +move 3 from 5 to 4 +move 8 from 8 to 4 +move 2 from 5 to 2 +move 3 from 1 to 5 +move 4 from 3 to 7 +move 6 from 2 to 9 +move 3 from 3 to 6 +move 6 from 4 to 5 +move 2 from 6 to 3 +move 1 from 3 to 1 +move 4 from 3 to 8 +move 8 from 4 to 3 +move 4 from 3 to 7 +move 4 from 4 to 5 +move 4 from 9 to 5 +move 3 from 3 to 4 +move 3 from 4 to 9 +move 1 from 1 to 4 +move 2 from 1 to 5 +move 7 from 7 to 8 +move 4 from 7 to 4 +move 1 from 6 to 7 +move 1 from 1 to 5 +move 1 from 3 to 8 +move 11 from 5 to 9 +move 17 from 9 to 8 +move 13 from 8 to 4 +move 1 from 4 to 8 +move 4 from 7 to 1 +move 4 from 8 to 3 +move 6 from 5 to 4 +move 3 from 3 to 6 +move 2 from 1 to 9 +move 1 from 9 to 5 +move 1 from 3 to 5 +move 5 from 5 to 9 +move 2 from 1 to 8 +move 21 from 8 to 6 +move 2 from 8 to 4 +move 4 from 9 to 6 +move 1 from 9 to 7 +move 19 from 4 to 1 +move 28 from 6 to 5 +move 7 from 4 to 2 +move 28 from 5 to 3 +move 1 from 9 to 4 +move 1 from 4 to 2 +move 1 from 7 to 8 +move 1 from 8 to 9 +move 13 from 1 to 3 +move 8 from 2 to 8 +move 3 from 1 to 2 +move 5 from 8 to 5 +move 1 from 2 to 7 +move 1 from 9 to 7 +move 1 from 2 to 3 +move 2 from 7 to 9 +move 1 from 2 to 6 +move 1 from 9 to 1 +move 9 from 3 to 9 +move 3 from 9 to 1 +move 1 from 6 to 8 +move 21 from 3 to 7 +move 7 from 9 to 4 +move 2 from 4 to 2 +move 1 from 8 to 6 +move 7 from 1 to 4 +move 7 from 7 to 8 +move 4 from 5 to 9 +move 10 from 7 to 1 +move 7 from 3 to 9 +move 1 from 7 to 9 +move 1 from 5 to 3 +move 3 from 3 to 5 +move 10 from 4 to 2 +move 1 from 3 to 7 +move 2 from 4 to 9 +move 3 from 9 to 1 +move 3 from 7 to 1 +move 1 from 6 to 4 +move 1 from 1 to 2 +move 1 from 3 to 4 +move 2 from 4 to 3 +move 1 from 7 to 4 +move 4 from 8 to 9 +move 1 from 4 to 9 +move 3 from 1 to 9 +move 12 from 1 to 7 +move 2 from 9 to 5 +move 12 from 9 to 7 +move 5 from 5 to 1 +move 1 from 8 to 5 +move 4 from 1 to 4 +move 1 from 9 to 6 +move 1 from 3 to 4 +move 3 from 8 to 3 +move 1 from 1 to 7 +move 8 from 2 to 5 +move 2 from 8 to 1 +move 10 from 7 to 1 +move 4 from 9 to 5 +move 2 from 5 to 8 +move 11 from 5 to 4 +move 6 from 7 to 2 +move 2 from 2 to 1 +move 1 from 7 to 5 +move 1 from 5 to 1 +move 2 from 4 to 8 +move 1 from 6 to 9 +move 8 from 4 to 3 +move 8 from 1 to 7 +move 7 from 1 to 2 +move 4 from 3 to 9 +move 1 from 9 to 6 +move 7 from 2 to 1 +move 5 from 2 to 3 +move 2 from 7 to 8 +move 5 from 8 to 4 +move 2 from 9 to 3 +move 1 from 8 to 1 +move 6 from 3 to 5 +move 10 from 3 to 1 +move 3 from 5 to 3 +move 3 from 2 to 1 +move 1 from 5 to 4 +move 6 from 4 to 5 +move 1 from 6 to 2 +move 3 from 4 to 7 +move 1 from 9 to 4 +move 2 from 3 to 1 +move 1 from 9 to 8 +move 1 from 3 to 7 +move 4 from 4 to 8 +move 2 from 7 to 4 +move 8 from 5 to 9 +move 2 from 8 to 6 +move 2 from 4 to 3 +move 2 from 3 to 4 +move 4 from 9 to 7 +move 1 from 8 to 7 +move 2 from 6 to 9 +move 2 from 8 to 9 +move 1 from 2 to 9 +move 1 from 7 to 8 +move 1 from 2 to 7 +move 19 from 7 to 6 +move 1 from 8 to 1 +move 2 from 4 to 8 +move 5 from 6 to 1 +move 2 from 7 to 2 +move 2 from 2 to 8 +move 2 from 1 to 8 +move 4 from 8 to 2 +move 3 from 2 to 8 +move 6 from 9 to 5 +move 8 from 6 to 3 +move 26 from 1 to 6 +move 1 from 5 to 3 +move 1 from 1 to 5 +move 8 from 3 to 1 +move 1 from 3 to 7 +move 3 from 9 to 2 +move 4 from 2 to 6 +move 26 from 6 to 1 +move 1 from 7 to 5 +move 3 from 8 to 4 +move 2 from 8 to 2 +move 7 from 1 to 2 +move 1 from 5 to 9 +move 2 from 4 to 6 +move 9 from 6 to 2 +move 18 from 1 to 7 +move 6 from 7 to 1 +move 6 from 5 to 6 +move 1 from 1 to 2 +move 19 from 2 to 7 +move 1 from 4 to 2 +move 9 from 7 to 1 +move 3 from 6 to 7 +move 1 from 9 to 4 +move 1 from 2 to 3 +move 8 from 7 to 8 +move 4 from 6 to 5 +move 2 from 6 to 3 +move 1 from 4 to 2 +move 4 from 5 to 1 +move 8 from 8 to 7 +move 17 from 7 to 8 +move 3 from 3 to 1 +move 1 from 2 to 8 +move 8 from 8 to 4 +move 8 from 8 to 7 +move 1 from 8 to 2 +move 7 from 7 to 6 +move 1 from 2 to 7 +move 5 from 7 to 8 +move 7 from 1 to 6 +move 10 from 6 to 1 +move 4 from 7 to 9 +move 3 from 9 to 7 +move 1 from 7 to 2 +move 6 from 4 to 2 +move 7 from 1 to 5 +move 4 from 2 to 5 +move 16 from 1 to 9 +move 3 from 2 to 7 +move 2 from 4 to 9 +move 4 from 1 to 6 +move 5 from 7 to 4 +move 4 from 6 to 3 +move 1 from 7 to 4 +move 1 from 6 to 9 +move 1 from 8 to 5 +move 4 from 3 to 2 +move 2 from 5 to 3 +move 3 from 6 to 2 +move 3 from 2 to 1 +move 9 from 5 to 8 +move 1 from 3 to 1 +move 10 from 8 to 1 +move 1 from 8 to 5 +move 16 from 9 to 2 +move 1 from 3 to 2 +move 12 from 1 to 9 +move 1 from 9 to 2 +move 3 from 1 to 6 +move 2 from 1 to 9 +move 3 from 6 to 8 +move 20 from 2 to 7 +move 16 from 9 to 7 +move 1 from 7 to 5 +move 2 from 5 to 9 +move 2 from 2 to 3 +move 2 from 8 to 5 +move 3 from 9 to 7 +move 2 from 5 to 2 +move 1 from 4 to 6 +move 2 from 1 to 4 +move 23 from 7 to 5 +move 4 from 8 to 5 +move 7 from 7 to 1 +move 16 from 5 to 7 +move 1 from 6 to 5 +move 1 from 2 to 4 +move 2 from 3 to 9 +move 1 from 2 to 3 +move 13 from 5 to 1 +move 1 from 3 to 8 +move 1 from 9 to 4 +move 19 from 1 to 9 +move 2 from 1 to 9 +move 22 from 9 to 8 +move 14 from 8 to 5 +move 12 from 5 to 3 +move 21 from 7 to 9 +move 14 from 9 to 7 +move 1 from 8 to 6 +move 9 from 3 to 7 +move 1 from 3 to 2 +move 4 from 4 to 1 +move 1 from 2 to 4 +move 1 from 3 to 9 +move 6 from 8 to 9 +move 4 from 1 to 7 +move 2 from 5 to 9 +move 6 from 4 to 5 +move 4 from 7 to 4 +move 1 from 5 to 3 +move 5 from 9 to 7 +move 2 from 3 to 1 +move 6 from 9 to 6 +move 1 from 1 to 6 +move 2 from 4 to 2 +move 8 from 7 to 5 +move 20 from 7 to 5 +move 2 from 5 to 6 +move 4 from 9 to 5 +move 1 from 1 to 3 +move 1 from 3 to 4 +move 1 from 2 to 7 +move 1 from 4 to 9 +move 9 from 6 to 3 +move 2 from 4 to 3 +move 28 from 5 to 3 +move 1 from 8 to 3 +move 1 from 8 to 1 +move 1 from 2 to 8 +move 1 from 6 to 2 +move 1 from 8 to 1 +move 6 from 5 to 7 +move 1 from 5 to 1 +move 1 from 9 to 2 +move 1 from 1 to 3 +move 1 from 9 to 7 +move 2 from 1 to 2 +move 11 from 3 to 8 +move 3 from 8 to 6 +move 3 from 6 to 9 +move 25 from 3 to 7 +move 4 from 3 to 8 +move 4 from 2 to 3 +move 9 from 8 to 9 +move 2 from 3 to 7 +move 3 from 8 to 2 +move 11 from 9 to 7 +move 1 from 9 to 1 +move 4 from 7 to 3 +move 1 from 1 to 5 +move 23 from 7 to 2 +move 12 from 2 to 3 +move 2 from 3 to 9 +move 12 from 2 to 1 +move 2 from 3 to 9 +move 1 from 5 to 4 +move 1 from 2 to 5 +move 1 from 9 to 4 +move 1 from 5 to 9 +move 2 from 4 to 2 +move 3 from 1 to 4 +move 1 from 2 to 1 +move 10 from 3 to 2 +move 7 from 7 to 3 +move 11 from 7 to 9 +move 5 from 3 to 1 +move 1 from 4 to 5 +move 11 from 2 to 3 +move 9 from 9 to 3 +move 3 from 9 to 4 +move 2 from 4 to 8 +move 1 from 5 to 6 +move 13 from 1 to 5 +move 3 from 3 to 8 +move 3 from 7 to 2 +move 1 from 7 to 4 +move 3 from 8 to 3 +move 8 from 3 to 8 +move 4 from 4 to 5 +move 2 from 8 to 2 +move 8 from 8 to 3 +move 1 from 6 to 3 +move 2 from 2 to 8 +move 6 from 5 to 2 +move 3 from 2 to 8 +move 1 from 1 to 7 +move 2 from 9 to 3 +move 3 from 5 to 4 +move 2 from 8 to 6 diff --git a/2022/cpp/05/lib.hpp b/2022/cpp/05/lib.hpp new file mode 100644 index 0000000..0e53a4c --- /dev/null +++ b/2022/cpp/05/lib.hpp @@ -0,0 +1,24 @@ +#include +#include +#include + +template +std::ostream &operator<<(std::ostream &os, const std::vector &list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) { + os << ", "; + } + i++; + } + os << "]"; + + return os; +} + +inline void error(const char * msg){ + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/05/result b/2022/cpp/05/result new file mode 100755 index 0000000..93a70b6 Binary files /dev/null and b/2022/cpp/05/result differ diff --git a/2022/cpp/05/result.cpp b/2022/cpp/05/result.cpp new file mode 100644 index 0000000..d922b3f --- /dev/null +++ b/2022/cpp/05/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" +#include + + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + Input input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/05/sample1 b/2022/cpp/05/sample1 new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/cpp/05/sample1 @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 diff --git a/2022/cpp/05/sample2 b/2022/cpp/05/sample2 new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/cpp/05/sample2 @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 diff --git a/2022/cpp/05/test b/2022/cpp/05/test new file mode 100755 index 0000000..b1ceb13 Binary files /dev/null and b/2022/cpp/05/test differ diff --git a/2022/cpp/05/test.cpp b/2022/cpp/05/test.cpp new file mode 100644 index 0000000..8c9d53c --- /dev/null +++ b/2022/cpp/05/test.cpp @@ -0,0 +1,40 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1") { + Input expected_input{.stacks = + { + {'Z', 'N'}, + {'M', 'C', 'D'}, + {'P'}, + }, + .moves = { + {0, 1, 0}, + {2, 0, 2}, + {1, 1, 0}, + {0, 0, 1}, + }}; + + Input actual_input = get_input("sample1"); + std::cout << expected_input << std::endl; + std::cout << actual_input << std::endl; + + SUBCASE("Testing input is parsed correctly") { CHECK(actual_input == expected_input); } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "CMZ"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + Input actual_input = get_input("sample2"); + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "MCD"); + } +} +#endif diff --git a/2022/cpp/06/Makefile b/2022/cpp/06/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/06/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/06/answer.cpp b/2022/cpp/06/answer.cpp new file mode 100644 index 0000000..86cb384 --- /dev/null +++ b/2022/cpp/06/answer.cpp @@ -0,0 +1,58 @@ +// AOC - 2022 - 06 +#include "answer.hpp" + +#include +#include +#include +#include +#include +#include + +Input get_input(const char* filename) { + std::ifstream stream(filename); + std::string str; + getline(stream, str); + return {str}; +} + +template +bool contains_duplicates(std::deque q) { + std::unordered_set set; + for (auto& elem : q) { + if (set.find(elem) != set.end()) + return true; + set.insert(elem); + } + return false; +} + +std::string get_result(Input input) { + std::string ret; + std::stringstream stream(input.str); + std::deque q; + char c; +#if PART == 1 +#define Q_SIZE 4 +#else +#define Q_SIZE 14 +#endif + for (int i = 0; i < Q_SIZE - 1; i++) { + stream >> c; + q.push_back(c); + } + + int i = Q_SIZE - 1; + while (stream.rdbuf()->in_avail()) { + i++; + stream >> c; + q.push_back(c); + if (!contains_duplicates(q)) { + std::cout << q << "is duplicate free" << std::endl; + break; + } + std::cout << q << " DOES contain duplicates" << std::endl; + q.pop_front(); + } + ret = std::to_string(i); + return ret; +} diff --git a/2022/cpp/06/answer.hpp b/2022/cpp/06/answer.hpp new file mode 100644 index 0000000..cece290 --- /dev/null +++ b/2022/cpp/06/answer.hpp @@ -0,0 +1,29 @@ +#include "lib.hpp" + +#include +#include + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 2 +#endif + +struct Input { + std::string str; + + bool operator==(const Input& rhs) const { + return this->str == rhs.str; + } +}; + +inline std::ostream& operator<<(std::ostream& os, Input& inp) { + os << inp.str; + return os; +} + +Input get_input(const char*); + +std::string get_result(Input); diff --git a/2022/cpp/06/build/answer.o b/2022/cpp/06/build/answer.o new file mode 100644 index 0000000..8f305a0 Binary files /dev/null and b/2022/cpp/06/build/answer.o differ diff --git a/2022/cpp/06/input b/2022/cpp/06/input new file mode 100644 index 0000000..1daaf7d --- /dev/null +++ b/2022/cpp/06/input @@ -0,0 +1 @@ +hlfhfzffqnnrlnnvnmmgbgwgttbppcrcnnmdmfdmmgwwrrqnrrscrctcbttvcvtvvhchjhccjgjttmddplplqplqlbqlblrrbrvvprpffpmmzpmpcczjzzbwwfssvrrvggncgncgcwczzswwqqjjflffpwfpwpbwpwpdpbpvvqffcfcjffjllncczfzzmhzzmddgdrgrwwjzzdjjsnjsjfsjsjhhcchlccchqchhzzpnngdgndnpnppsdsggbvgvgpprqrqmmlzmzllvrrcvclcwczcqqcdcfcqqmmzbzdzdjzdjdmjdjzdjjcvjvcjvcvssltstttfbtftrfrlrdllrqqfssslccjdcjdjfdjfjqjnqjnjnrnddtnndtnnztzqztqztqzzpmzmggzrgrwwdqwdwcdwdnnmlmgmtmtstwssbffcnclclnclcjjcjpcpqcpqcpqpmqqfccpcjppnspsnnzggnpntndtdqtthwhnhwnwllzhlzhlzzghzghhlhvhwhjhfjjcnjnvjnjvvqccdmmgddllnmnrrdtdnncggfhgfglfgfmfnnpvvggznnwvnwwfgghrrfwrwzwszzzldzdldhlhblhblhbbbgjgsjggmqqmrrzggrhhwpwdpwdpwplpgpbggtssqffbqfbqbnnsqnqfnngcnnmwnmnbmmmslsjllbtbbpllltzzhgzztllsdllrvvhvjvbbhcbhchmchcctbcttvccgwcwpcchrcrdrdggcrrntrrfllcffbdfflrrrgbgrbbbdqbqjbbgbgrrqwqtwqwhwghwhzwwcswsnwnqqjhjhwhfwhffdfgddgjgsjgjhgglhlwhlhssfqfhhdmdnmnppdcddfzzhmhqqntqnnjvnjvjddcvcgcbgbbpjbjtbjjfgftgffplljfjrrhqqpddlssrvsrvrpppsllsdsqqqzzfttqsqzssjbbrnbnnrbbsrshsrrshrhwhbwbrrsrfrttfqtfqqfddvrvjrjvjsjhjsjdjqdjjlqjjjgcjcmcncfcrcwrwsrsslffzszmsszrsssrnrjjvbvpvcppptbbhhrddbcbggbqbmmsqqwggfpfbblmldmmpmwpwfwjfjsjnjmmpllccjzcjcwwpswshhpthhzchctcbcrrrrmvrvrdvvjmmvgmgwglghllvmllzlzzsvzzrmmhnnsjnnpvpwvpwwmvwwdqqdffhhhmccfgfvggchcctrrmdrrhrhnhnzzgpzzgttnhthvhzzqvvvwpwqpqdppsnnrgnnhphphmhcmcrmrvvqlvqqsccqhchzhwwmvmzmczzgsgdsggthgglrlnrlllbdllhwlwltwwcswsgssbhbsbvsbsbwbhbnncrcllttbrbppjccfpfhhgshschsccmrcmmcrrrzvvrcrggmwgwjwnwjjbffjddjnjgngqgdgnndznndvvfqfgfvvrvqrvvpllnsszbsbdbbdzbdbzbqbzznrznzjzpptcptccvwccfscffrftrrsnsvvswvvhbhzzfbzffncchhcnngzzcpcmmfttsntnjjsccqbcqqmzzgppdhppdtppmffgtgvvlzlpptdtttdppqjqtqctcrrzsswwtnwtnwnqqvbbdgjhvmmzpnhfvsbddzhgdwcnfdstvhhbzlzcfjwhlptbhmbmblprtsdmrdhbbbwpplnzgdnrzjmgzgpqbggnqvwwtntzgfwqrztqtdrsnhpfzswptggnvbszdcrmrhhtlrrfnpqrnpwrbmhlfwmdqqdbqrwbzqjbzwrgmbgrtzrhdclqfgsrtsgfwqrnnqgwsncmpgffggssrqvwjlhpsghbqdtzwmvzzvcmzsjqvprvcqwqjbcqcqrhpwwcsrscgmfdppbgvmnrdfrppblznbstnjzwwgstjvtprjbhtpdfgrhdjnjmnlbfwggzhcngvcwvcfpcwdtdppwjrdzsnjlnrzbfqqshlnzvwsmscgpfwjzhtwgfwgzdhbdwwzbsmfwwbmvrlrpswnjlmfbfzhwvcmgwfzssmmtjlwtrpwpwgnspbgchdncbfcpjsvtzjqtwqwjwgbhrbwvhqbcstsgsnwsjmhrlrvzgqhqfrmnrjdrhdjwcwctpdrzctlvnfzmzwhsnfprlzgzjpqvzchlmvbhffhpfjtvsdbvbdmwgvmqpflhwwndbqthmmwshdtspsrvqdflmmzwbqbqmpfdwjmvpbzdnqzfmhzdgldqjjvgpfcqftvjzwnzmfqdggrwlfzdhjnhmtrjbnllgqpntwmhnwtglnqdwbqdblpwnnrdwzpsqzfwqcmhqhnpsdcwvdldphgnrtqzdbnnzdzfttldrqcztlvlrgpdqzrcthslmtqhfvbzrfgnlrprcpbsctqhspbhnjtzrzhqjzszbzdthttqmbznzssftztwlggmdqqdtfllqjzjtvpgjfhtbwtbmtjplqnbdmsvlnqcwtdbdvfjnzgsmpnhbvvwwfbrgffjqfsccdjdwvbsdhqwfzvcpjzjbdjgrdctjplhwbdhhnbnwstvndnnwtsgbhzbvwdshvmnbwsthlrggtmddvjbfzfrnrdrqfjpslrccctzpjbwpdbhlbzfmwbltcqfngdprvfhgcszdtpnrcpdmllfnlspgrdrpwqmqbmrglvlrsmrfqrtzzgjcvqtqzpmghjrvmdmvvqztrjzbzjwdqsmrwpqnswbzhjbzzhdvmnfdsztzdzrjssgnnfqvbtsqrrmcppjgrmnstrnrlwjvvcczqlcbmwqzdfpssfwdfrvwtstwchdgwtrhhcmppcqlmrqlnwqccfphsdhbsbmtjvpcwwjrmrllbpnrmpbvwgwbftpdpphccwqcblcnvvbbppscmnjqgddllbnbvmmqzdffrrjtqwllzgpqrmnlfqrptzqmdmnrfnjpvqvjbsqrhljslgqqcqqtmtbwjrpphtjgjbqpzmzrzjrfjwcdcnbsjfljclffjplnrrcfbmhphtcjrzlrvvjcznpgpnrdwwqvgnbnzqnlcghhgwvhqbvjzfbdvhrzlqfbtlqhpltfjlfpbnjbphmmpntzqgjmwjtchwmlwvfjmfflqzpqnvvrgnbddzlfpdpdjghfgbsfddjspnfdwvqppncmdgfrnvrpcrflhgjgbwdsbwblfcwbtlrrnjjdhbvmrzgsvjwgfnnhqfbvhprlmwwqgclzlbqbrdspcbhftmdscsmpwrggrmnsvjphjmzmmrlrhnmdhwjlbmjchtvsrcplfspsssjznmzcrqnsjjtwjzvlhshbptqwwvjhjvzrhphphsbphpnzpfbwcdnqrhrrvlrwrztlpqnrcfzrncsvpzqzgslrlrwhvtgjmfncldqmvshlmnlpqbgvnwqfcthgrgllmqrjqmfgznspgltpptglpdcvhtzsprprbldbzhbmjsqzwvjggwhsczltcvgwqhspzpzvljwqjgrgtwswjdswlzjzslrsslvqzncjwhbbjpbdthqpgmhfglggmlrgwdsplgscrwstntvrhjzjjlshtgmnnhvsjwfmcjbpzjcstmnpvtbgrfcfdwjljsrfhdphrdcslwhgvlnwltwchplvfzntfgcnlsvzrvnnczhhqdlwjvqprhmtjdtwmppffmszzzqtfrgnhnzqgqzhrjzgntcszstrfhhtptgvswvzvjcgcntmhzzmdgsmtgzhpfvqfnwmsjdhtfgmmbrrfsdlptchqqzqdqjncmtpznfssrcnmcdnthglmfzsfgltrndqsfmdftmfgchbwmzgrtjvgqtshlltthnnpqnzfrchzhdzrrnpzvfzblrmhwdwjnqdptlbvndmmlhzhvsfdlmlhqrgqqzsdqtpczwcrwcbsftvvphfbwjrvrnrcqbbcsqgnhltwzvllljcvpwjgslbmngcdmpdvjlgcnrzwqjdgrblncpqmrgjmpqjzvdmcmwfnwqlszdgwqdfznhsnpsjrfwrqpqmpvhstmzgqblfmcfvwljbhdfhdmqcvrwnqcstwtzgmng diff --git a/2022/cpp/06/lib.hpp b/2022/cpp/06/lib.hpp new file mode 100644 index 0000000..5ef5e31 --- /dev/null +++ b/2022/cpp/06/lib.hpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +template +std::ostream& operator<<(std::ostream& os, const std::vector& list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) + os << ", "; + i++; + } + os << "]"; + + return os; +} + +template +std::ostream& operator<<(std::ostream& os, const std::deque& list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) + os << ", "; + i++; + } + os << "]"; + + return os; +} + +inline void error(const char* msg) { + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/06/result b/2022/cpp/06/result new file mode 100755 index 0000000..6937200 Binary files /dev/null and b/2022/cpp/06/result differ diff --git a/2022/cpp/06/result.cpp b/2022/cpp/06/result.cpp new file mode 100644 index 0000000..d922b3f --- /dev/null +++ b/2022/cpp/06/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" +#include + + +int main(int argc, char *argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + Input input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/06/sample1 b/2022/cpp/06/sample1 new file mode 100644 index 0000000..5a2b0a7 --- /dev/null +++ b/2022/cpp/06/sample1 @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb \ No newline at end of file diff --git a/2022/cpp/06/sample2 b/2022/cpp/06/sample2 new file mode 100644 index 0000000..9e3311c --- /dev/null +++ b/2022/cpp/06/sample2 @@ -0,0 +1 @@ +bvwbjplbgvbhsrlpgdmjqwftvncz \ No newline at end of file diff --git a/2022/cpp/06/sample3 b/2022/cpp/06/sample3 new file mode 100644 index 0000000..cca46f5 --- /dev/null +++ b/2022/cpp/06/sample3 @@ -0,0 +1 @@ +nppdvjthqldpwncqszvftbrmjlhg \ No newline at end of file diff --git a/2022/cpp/06/sample4 b/2022/cpp/06/sample4 new file mode 100644 index 0000000..848af1c --- /dev/null +++ b/2022/cpp/06/sample4 @@ -0,0 +1 @@ +nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg \ No newline at end of file diff --git a/2022/cpp/06/sample5 b/2022/cpp/06/sample5 new file mode 100644 index 0000000..a4fc5ba --- /dev/null +++ b/2022/cpp/06/sample5 @@ -0,0 +1 @@ +zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw \ No newline at end of file diff --git a/2022/cpp/06/test b/2022/cpp/06/test new file mode 100755 index 0000000..1ce51f0 Binary files /dev/null and b/2022/cpp/06/test differ diff --git a/2022/cpp/06/test.cpp b/2022/cpp/06/test.cpp new file mode 100644 index 0000000..36249e7 --- /dev/null +++ b/2022/cpp/06/test.cpp @@ -0,0 +1,67 @@ +#include +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 + +TEST_CASE("Part 1") { + std::vector expected_inputs; + expected_inputs.insert(expected_inputs.end(), + { + {"mjqjpqmgbljsphdztnvjfqwrcgsmlb"}, + {"bvwbjplbgvbhsrlpgdmjqwftvncz"}, + {"nppdvjthqldpwncqszvftbrmjlhg"}, + {"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"}, + {"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"}, + }); + + std::vector expected_outputs; + expected_outputs.insert(expected_outputs.end(), + { + "7", + "5", + "6", + "10", + "11", + }); + + for (int i : {0, 1, 2, 3, 4}) { + Input expected_input = expected_inputs[i]; + + std::string sample_name = "sample" + std::to_string(i + 1); + Input actual_input = get_input(sample_name.c_str()); + + SUBCASE("Testing input is parsed correctly") { + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == expected_outputs[i]); + } + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + std::vector expected_outputs; + expected_outputs.insert(expected_outputs.end(), + { + "19", + "23", + "23", + "29", + "26", + }); + + for (int i : {0, 1, 2, 3, 4}) { + std::string sample_name = "sample" + std::to_string(i + 1); + Input actual_input = get_input(sample_name.c_str()); + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == expected_outputs[i]); + } + } +} +#endif diff --git a/2022/cpp/07/Makefile b/2022/cpp/07/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/07/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/07/answer.cpp b/2022/cpp/07/answer.cpp new file mode 100644 index 0000000..cbe41d2 --- /dev/null +++ b/2022/cpp/07/answer.cpp @@ -0,0 +1,145 @@ +// AOC - 2022 - 07 +#include "answer.hpp" + +#include +#include +#include +#include + +bool is_dir(std::string line) { + return line.substr(0, 3) == "dir"; +} + +bool is_command(std::string line) { + return line[0] == '$'; +} + +Dir* get_subdir(Dir* current_dir, const std::string& subdir_name) { + for (auto& dir : current_dir->dirs) + if (dir->name == subdir_name) + return dir; + + std::cerr << "'" << subdir_name << "' not in " << current_dir->name + << std::endl; + error("couldn't find subdir"); +} + +Dir* handle_command( + Dir* current_dir, std::string& cmd_line, std::ifstream& input) { + std::cout << "CWD: " << current_dir->name << std::endl; + std::cout << "Handling cmd: '" << cmd_line << "'" << std::endl; + std::stringstream cmd_stream(cmd_line); + std::string cmd, _; + cmd_stream >> _; // $ + cmd_stream >> cmd; + std::string line; + + Dir* reading_dir = current_dir; + if (cmd == "ls") { + if (!cmd_stream.eof()) { + std::string dir_name; + cmd_stream >> dir_name; + reading_dir = get_subdir(current_dir, dir_name); + } + + while (std::getline(input, line) && !is_command(line)) { + if (is_dir(line)) { + std::string dir_name = line.substr(4); + std::cout << "Adding dir '" << dir_name << "' to " + << current_dir->name << std::endl; + reading_dir->dirs.push_back(new Dir{reading_dir, dir_name}); + } else { + uint size; + std::string file_name; + std::stringstream file_line_stream(line); + file_line_stream >> size; + file_line_stream >> file_name; + reading_dir->files.push_back({size, file_name}); + } + } + std::cout << "After reading, the cwd looks like" << std::endl; + std::cout << *reading_dir << std::endl; + if (is_command(line)) + return handle_command(current_dir, line, input); + else + return current_dir; + } else if (cmd == "cd") { + std::string new_dirname; + cmd_stream >> new_dirname; + std::cout << "cd-ing to " << new_dirname << std::endl; + if (new_dirname == "..") { + std::cout << "\t thus to " << current_dir->parent->name + << std::endl; + return current_dir->parent; + } else + return get_subdir(current_dir, new_dirname); + } else { + std::cerr << "Invalid cmd: " << cmd << std::endl; + error("Invalid command"); + } +} + +Input get_input(const char* filename) { + std::ifstream stream(filename); + std::string str; + + Dir* root = new Dir{nullptr, "/"}; + getline(stream, str); // cd / + Dir* current_dir = root; + + while (getline(stream, str)) + current_dir = handle_command(current_dir, str, stream); + + std::cout << "current" << std::endl; + std::cout << *current_dir << std::endl; + std::cout << "root" << std::endl; + std::cout << *root << std::endl; + + return {*root}; +} + +#define CUTOFF_SIZE 100000 +#define FILESYSTEM_SIZE 70000000 +#define NEEDED_SIZE 30000000 + +std::string get_result(Input input) { + std::string ret; +#if PART == 1 + std::vector queue; + queue.push_back(&input.root); + int i = 0; + uint res = 0; + while (i < queue.size()) { + std::cout << "ehhlo?" << std::endl; + Dir d = *queue[i++]; + if (d.get_size() < CUTOFF_SIZE) + res += d.get_size(); + for (auto& d : d.dirs) + queue.push_back(d); + for (auto& d : queue) + std::cout << *d << std::endl; + } + ret = std::to_string(res); +#else + uint space_to_free = input.root.get_size() - NEEDED_SIZE; + std::cout << space_to_free << std::endl; + std::vector queue; + queue.push_back(&input.root); + int i = 0; + uint min_above_needed = -1; + while (i < queue.size()) { + Dir d = *queue[i++]; + if (d.get_size() < space_to_free) + continue; + else if (d.get_size() < min_above_needed) + min_above_needed = d.get_size(); + + for (auto& sub_dir : d.dirs) + queue.push_back(sub_dir); + for (auto& d : queue) + std::cout << d->name << std::endl; + } + ret = std::to_string(min_above_needed); +#endif + return ret; +} diff --git a/2022/cpp/07/answer.hpp b/2022/cpp/07/answer.hpp new file mode 100644 index 0000000..996fb22 --- /dev/null +++ b/2022/cpp/07/answer.hpp @@ -0,0 +1,80 @@ +#include "lib.hpp" + +#include + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 2 +#endif + +struct File { + uint size; + std::string name; + + bool operator==(const File& rhs) const { + return this->name == rhs.name && this->size == rhs.size; + } +}; + +struct Dir { + Dir* parent; + std::string name; + std::vector dirs; + std::vector files; + uint size = 0; + + uint get_size() { + if (this->size > 0) + return this->size; + uint res = 0; + for (auto& f : this->files) + res += f.size; + + for (auto& d : this->dirs) + res += d->get_size(); + this->size = res; + return res; + } + + bool operator==(const Dir& rhs) const { + return this->name == rhs.name && this->dirs == rhs.dirs && + this->files == rhs.files; + } +}; + +struct Input { + Dir root; + + bool operator==(const Input& rhs) const { + return this->root == rhs.root; + } +}; + +inline std::ostream& operator<<(std::ostream& os, File& file) { + os << "- " << file.name << " (file, size=" << file.size << ")" << std::endl; + return os; +} + +inline std::ostream& operator<<(std::ostream& os, Dir& dir) { + os << "- " << dir.name << " (dir"; + if (dir.parent) + os << ", parent=" << dir.parent->name; + os << ")" << std::endl; + for (auto& d : dir.dirs) + os << '\t' << *d; + for (auto& f : dir.files) + os << '\t' << f; + return os; +} + +inline std::ostream& operator<<(std::ostream& os, Input& inp) { + os << inp.root; + return os; +} + +Input get_input(const char*); + +std::string get_result(Input); diff --git a/2022/cpp/07/build/answer.o b/2022/cpp/07/build/answer.o new file mode 100644 index 0000000..00635ec Binary files /dev/null and b/2022/cpp/07/build/answer.o differ diff --git a/2022/cpp/07/input b/2022/cpp/07/input new file mode 100644 index 0000000..b9cccb9 --- /dev/null +++ b/2022/cpp/07/input @@ -0,0 +1,1101 @@ +$ cd / +$ ls +dir grdd +270251 hjlvwtph.jzv +230026 jzmgcj.gmd +dir nns +dir rrfflbql +$ cd grdd +$ ls +233044 mqbz.fcp +dir nnch +82939 rgtvsqsh.psq +150253 srvg.dth +$ cd nnch +$ ls +4014 mqbz.fcp +$ cd .. +$ cd .. +$ cd nns +$ ls +dir cgbdghtd +dir dnh +dir gjhp +dir jwjm +dir mrpfzd +dir ncvv +dir pfnglqgw +dir tlh +dir vnrhpc +$ cd cgbdghtd +$ ls +276941 jrrcdgz.szm +$ cd .. +$ cd dnh +$ ls +269539 nnch +220637 sjzmpwwb +$ cd .. +$ cd gjhp +$ ls +dir czclvmwc +dir jgtzfsm +$ cd czclvmwc +$ ls +179729 fzqvvlg +67916 pmsdthr.prv +$ cd .. +$ cd jgtzfsm +$ ls +151591 rcggj.nwm +$ cd .. +$ cd .. +$ cd jwjm +$ ls +203559 nnch +$ cd .. +$ cd mrpfzd +$ ls +dir pfnglqgw +204978 qscs.vpq +16184 tbfwpmp.hvl +$ cd pfnglqgw +$ ls +dir wvzw +dir ztl +$ cd wvzw +$ ls +195912 jpn.ndh +143238 nnch.djz +2239 pmsdthr.prv +dir sfqq +$ cd sfqq +$ ls +134913 pthmqd +dir vcdhz +$ cd vcdhz +$ ls +13800 ffhv.jnq +$ cd .. +$ cd .. +$ cd .. +$ cd ztl +$ ls +181007 mqbz.fcp +266517 zbpjz.gbr +$ cd .. +$ cd .. +$ cd .. +$ cd ncvv +$ ls +296494 ccmvjm.bjb +20801 hjr +32494 mqbz.fcp +dir nnch +dir pfnglqgw +dir qpq +dir rftzzmq +dir wgblcl +294511 wrdmgdb.fmh +$ cd nnch +$ ls +dir hjzpfvm +dir ncvv +dir pffr +dir pfnglqgw +dir ssdffgsq +$ cd hjzpfvm +$ ls +304078 mqbz.fcp +dir nnch +146425 nnch.lrw +dir vcfglm +dir zhscvh +$ cd nnch +$ ls +311625 gqmpvplj.vjg +248744 ndfcj +$ cd .. +$ cd vcfglm +$ ls +dir btptvs +dir ghpvzvzp +146354 nnch +$ cd btptvs +$ ls +127157 nqnnwq.rtz +16115 pmsdthr.prv +$ cd .. +$ cd ghpvzvzp +$ ls +dir ncvv +$ cd ncvv +$ ls +236869 pmsdthr.prv +$ cd .. +$ cd .. +$ cd .. +$ cd zhscvh +$ ls +225337 glpz +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir nljtcssr +dir svjhsvjh +$ cd nljtcssr +$ ls +258085 gqmpvplj.vjg +dir ncvv +dir qmzfgcr +249049 wznr.gbs +dir zvvpqlmq +$ cd ncvv +$ ls +147686 ndfcj.sfr +$ cd .. +$ cd qmzfgcr +$ ls +162245 ndfcj.nlj +$ cd .. +$ cd zvvpqlmq +$ ls +160481 ggnc +$ cd .. +$ cd .. +$ cd svjhsvjh +$ ls +dir bjqbmt +58138 gqmpvplj.vjg +dir nsvf +154398 rrhjs.gch +dir wmvhmlr +$ cd bjqbmt +$ ls +252614 ndfcj.wzg +153886 nnch +214625 zhmdvb +$ cd .. +$ cd nsvf +$ ls +274932 nnch.jfg +$ cd .. +$ cd wmvhmlr +$ ls +137205 nnch +$ cd .. +$ cd .. +$ cd .. +$ cd pffr +$ ls +89895 cwpnzngf.swg +197833 jgv +184768 jjhzddp.fbb +31033 tpngfdsg.brv +$ cd .. +$ cd pfnglqgw +$ ls +228015 ccmvjm.bjb +$ cd .. +$ cd ssdffgsq +$ ls +165887 gqmpvplj.vjg +dir lfq +dir nnch +170828 qjb.mnp +dir ttj +$ cd lfq +$ ls +257411 gqmpvplj.vjg +137375 jzmgcj.gmd +267329 nsbsgd.zvq +$ cd .. +$ cd nnch +$ ls +dir mrfwfq +171186 ndhqthf.jlp +$ cd mrfwfq +$ ls +100447 lsfsh.mvr +$ cd .. +$ cd .. +$ cd ttj +$ ls +49634 ndfcj +dir tqvld +$ cd tqvld +$ ls +dir ndfcj +$ cd ndfcj +$ ls +dir gnzj +80690 pmlnbvj +12843 zbrfmfgj.lzr +$ cd gnzj +$ ls +dir wtpzrpn +$ cd wtpzrpn +$ ls +33840 gqmpvplj.vjg +18122 rrfflbql.vws +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +289092 fdjfvb +$ cd .. +$ cd qpq +$ ls +126281 ccmvjm.bjb +dir crtzrd +231988 jzmgcj.gmd +203061 mgmmp +272098 mqbz.fcp +dir pfnglqgw +dir qzvcsn +105003 rrfflbql.mcm +$ cd crtzrd +$ ls +162336 fsps +100215 mctd.wsz +67983 mqbz.fcp +281538 mqq.cgz +dir ndfcj +dir rrfflbql +dir wbmtvr +$ cd ndfcj +$ ls +290647 pfjczr.wjc +$ cd .. +$ cd rrfflbql +$ ls +dir mpr +dir ncvv +dir rrfflbql +301818 zml.qfj +$ cd mpr +$ ls +237300 gqmpvplj.vjg +dir pfnglqgw +$ cd pfnglqgw +$ ls +dir brnwdjtg +dir dvqlmzw +248787 pmsdthr.prv +$ cd brnwdjtg +$ ls +256129 ncvv +dir ptztp +$ cd ptztp +$ ls +104775 nnch.wlc +$ cd .. +$ cd .. +$ cd dvqlmzw +$ ls +25407 twgqbrtl +$ cd .. +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir jvwvsm +62293 jzmgcj.gmd +261836 mqbz.fcp +dir vvvrf +$ cd jvwvsm +$ ls +222978 ccmvjm.bjb +207799 jzmgcj.gmd +dir pcvsvcn +248569 pmsdthr.prv +dir wmd +$ cd pcvsvcn +$ ls +39803 pmsdthr.prv +$ cd .. +$ cd wmd +$ ls +9516 rfstbvj.nhz +$ cd .. +$ cd .. +$ cd vvvrf +$ ls +dir ncvv +dir znwc +$ cd ncvv +$ ls +110667 jzmgcj.gmd +$ cd .. +$ cd znwc +$ ls +182248 ndfcj.crv +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +231013 ccmvjm.bjb +dir jlc +dir rrfflbql +79210 ttm.zmw +$ cd jlc +$ ls +28096 ccmvjm.bjb +113156 pmsdthr.prv +$ cd .. +$ cd rrfflbql +$ ls +234558 lbg.bpn +$ cd .. +$ cd .. +$ cd .. +$ cd wbmtvr +$ ls +285832 fqhs +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +110965 nnch +195414 pmsdthr.prv +243812 thcpw.jfw +$ cd .. +$ cd qzvcsn +$ ls +279179 gqmpvplj.vjg +191705 mhmlfc.czv +146298 pfnglqgw.ppm +2775 pmsdthr.prv +$ cd .. +$ cd .. +$ cd rftzzmq +$ ls +310418 bddhlvs.rwm +152681 cdznrjl +278447 rrfflbql +dir tmcltf +$ cd tmcltf +$ ls +dir mzr +$ cd mzr +$ ls +24154 ccmvjm.bjb +dir nnch +dir rqsbw +$ cd nnch +$ ls +100523 mqbz.fcp +$ cd .. +$ cd rqsbw +$ ls +64033 czzqg.pcz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wgblcl +$ ls +235768 dvdzbgv.vwl +186757 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +129407 zfphqcsf.cfn +$ cd .. +$ cd tlh +$ ls +89310 jzmgcj.gmd +21486 nwnbbmr.lsq +40023 rdmtp.zsf +$ cd .. +$ cd vnrhpc +$ ls +104731 gqmpvplj.vjg +176015 grn +3646 jzmgcj.gmd +dir ncvv +45414 nfrj.lvq +233767 pfnglqgw.bvf +$ cd ncvv +$ ls +252691 ccmvjm.bjb +dir ncvv +dir vwv +dir wwmwfbf +$ cd ncvv +$ ls +306441 qfhhnmqz.snc +$ cd .. +$ cd vwv +$ ls +56033 rrfflbql +$ cd .. +$ cd wwmwfbf +$ ls +45964 gqmpvplj.vjg +118391 mvwl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +175540 bcw.sqp +154750 ggncvs.nvn +dir jqzm +dir mgbglnr +192820 ndfcj +dir pfnglqgw +217147 qjng.svz +dir rrfflbql +$ cd jqzm +$ ls +dir ncvv +dir nfcvcddz +242934 rjwlgm +dir wzj +$ cd ncvv +$ ls +298067 pfnglqgw.jdv +$ cd .. +$ cd nfcvcddz +$ ls +261264 gqmpvplj.vjg +19464 mqbz.fcp +121507 ncqhrf +dir ndfcj +58485 ndfcj.vhh +dir rcrzjm +228359 wnftnshq +$ cd ndfcj +$ ls +309008 bwn +$ cd .. +$ cd rcrzjm +$ ls +48178 fgzpwhvt +129342 qns.lnj +$ cd .. +$ cd .. +$ cd wzj +$ ls +91384 gqmpvplj.vjg +dir nnch +dir rzm +$ cd nnch +$ ls +dir sgbwrl +$ cd sgbwrl +$ ls +dir ndfcj +$ cd ndfcj +$ ls +295624 cbmdr +$ cd .. +$ cd .. +$ cd .. +$ cd rzm +$ ls +dir cprj +86746 dfwsj.hqq +dir dljnvq +dir ndfcj +159465 nsglq +202670 pfnglqgw.wbh +29700 rrfflbql.wln +dir vgtftq +$ cd cprj +$ ls +148192 gqmpvplj.vjg +165473 hwp.ltc +$ cd .. +$ cd dljnvq +$ ls +91675 pmsdthr.prv +$ cd .. +$ cd ndfcj +$ ls +83124 rrfflbql.ghs +$ cd .. +$ cd vgtftq +$ ls +186744 mqbz.fcp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mgbglnr +$ ls +dir ddtcz +dir ncvv +dir nhpwf +dir rrfflbql +203683 ttbc +dir tvbjgv +dir wvzcq +$ cd ddtcz +$ ls +61553 wfzj +$ cd .. +$ cd ncvv +$ ls +37589 ccmvjm.bjb +87987 dnct +196537 ndfcj.cqg +40448 pmsdthr.prv +$ cd .. +$ cd nhpwf +$ ls +243345 gqmpvplj.vjg +53165 nnch.gfc +dir pfnglqgw +dir vdnnf +$ cd pfnglqgw +$ ls +131411 mhvzv.scz +142119 nnch.gnt +$ cd .. +$ cd vdnnf +$ ls +83904 ccmvjm.bjb +dir czfqdtd +dir dgblftbz +dir jnftbbtm +dir pfbnl +dir pfnglqgw +$ cd czfqdtd +$ ls +dir nnch +$ cd nnch +$ ls +255744 ndfcj.ldv +$ cd .. +$ cd .. +$ cd dgblftbz +$ ls +278883 ncvv.zph +dir pfnglqgw +133315 phns.cmq +130316 sftj +$ cd pfnglqgw +$ ls +174155 vnwtv +$ cd .. +$ cd .. +$ cd jnftbbtm +$ ls +255828 nmln +30605 pfnglqgw +$ cd .. +$ cd pfbnl +$ ls +8603 ndfcj +$ cd .. +$ cd pfnglqgw +$ ls +235364 pmsdthr.prv +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +dir gfgbj +$ cd gfgbj +$ ls +215226 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd tvbjgv +$ ls +dir nnch +$ cd nnch +$ ls +dir gmsb +$ cd gmsb +$ ls +dir tlqdvpr +$ cd tlqdvpr +$ ls +130013 hzrq.zrg +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wvzcq +$ ls +dir ncvv +$ cd ncvv +$ ls +103168 ccmvjm.bjb +18537 ncvv +dir nnch +dir rrfflbql +$ cd nnch +$ ls +20928 ndfcj.lln +$ cd .. +$ cd rrfflbql +$ ls +dir lgfwf +$ cd lgfwf +$ ls +dir fmzqt +dir rrfflbql +$ cd fmzqt +$ ls +301419 gqmpvplj.vjg +$ cd .. +$ cd rrfflbql +$ ls +dir nbvqch +$ cd nbvqch +$ ls +298966 csqvdql.cwr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +dir bhmndjpq +203077 fssbcjcm.hvt +dir lfslp +dir ncvv +dir nftdcrl +dir nnch +dir pfnglqgw +dir qfbbnr +dir wttc +$ cd bhmndjpq +$ ls +299744 cmgwwccb.tvv +173562 fpwv +dir hmnbfdtr +dir jqpcs +73425 mqbz.fcp +dir ncvv +dir ndfcj +95707 pmsdthr.prv +dir ptzdv +dir qzhrsnqh +dir sbqg +$ cd hmnbfdtr +$ ls +dir pfnglqgw +dir qmpplbtv +77228 tvpdstcn.zbb +$ cd pfnglqgw +$ ls +dir dwr +dir jmgp +102634 mqbz.fcp +148654 ncvv +257637 ncvv.nzn +286938 rrfflbql +$ cd dwr +$ ls +141669 ndfcj +9012 ptrlq.stq +$ cd .. +$ cd jmgp +$ ls +78473 pfnglqgw +$ cd .. +$ cd .. +$ cd qmpplbtv +$ ls +202948 wjp.rgt +$ cd .. +$ cd .. +$ cd jqpcs +$ ls +290654 fmmcph +8123 zrr.vqm +$ cd .. +$ cd ncvv +$ ls +dir hbrttp +$ cd hbrttp +$ ls +128004 nffzj +$ cd .. +$ cd .. +$ cd ndfcj +$ ls +dir vscwfsl +$ cd vscwfsl +$ ls +251706 snww.dzb +$ cd .. +$ cd .. +$ cd ptzdv +$ ls +113702 nnch +$ cd .. +$ cd qzhrsnqh +$ ls +118758 gqmpvplj.vjg +75504 vcnn.stz +102737 zvv +$ cd .. +$ cd sbqg +$ ls +287663 bhcpslm.wwt +dir bqr +dir czhfphh +39170 fqn +dir gqnts +267314 hlv.ljc +8701 jqpdpg.prz +dir ncvv +211749 psln.pdq +$ cd bqr +$ ls +dir chjfw +dir dgccfvtl +219440 fvfsfz +262276 jzmgcj.gmd +dir ndfcj +294287 pfnglqgw.lwh +163881 rrfflbql +278231 vgjm.rrh +$ cd chjfw +$ ls +134800 hvmvqbz.bqj +$ cd .. +$ cd dgccfvtl +$ ls +155579 lwmqrd.wvp +$ cd .. +$ cd ndfcj +$ ls +144255 ncvv.hrn +236730 ndfcj +dir pfz +$ cd pfz +$ ls +297448 rrfflbql.fdt +$ cd .. +$ cd .. +$ cd .. +$ cd czhfphh +$ ls +dir wmws +$ cd wmws +$ ls +14499 ncvv +$ cd .. +$ cd .. +$ cd gqnts +$ ls +165940 ccmvjm.bjb +dir gjcm +dir hldzdlrl +dir jtnpgg +dir nnch +287896 pmsdthr.prv +$ cd gjcm +$ ls +125716 gqmpvplj.vjg +dir mhjm +197155 msspbg +176407 trtdggnf +$ cd mhjm +$ ls +18749 jzmgcj.gmd +252999 nnch +76392 rrfflbql.mzh +$ cd .. +$ cd .. +$ cd hldzdlrl +$ ls +dir gzpcgj +dir rvvgn +$ cd gzpcgj +$ ls +dir vfz +$ cd vfz +$ ls +183852 pmsdthr.prv +$ cd .. +$ cd .. +$ cd rvvgn +$ ls +143443 ndfcj +$ cd .. +$ cd .. +$ cd jtnpgg +$ ls +dir dhvd +dir hlmgslbs +dir rrfflbql +dir vmqpcm +$ cd dhvd +$ ls +131598 ltr.rph +$ cd .. +$ cd hlmgslbs +$ ls +173562 rrfflbql +$ cd .. +$ cd rrfflbql +$ ls +dir ghvmc +$ cd ghvmc +$ ls +311611 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd vmqpcm +$ ls +192032 mqbz.fcp +$ cd .. +$ cd .. +$ cd nnch +$ ls +112995 ccmvjm.bjb +$ cd .. +$ cd .. +$ cd ncvv +$ ls +106711 dswpw.wgr +46614 jzmgcj.gmd +115391 mqbz.fcp +dir nnch +61970 pmsdthr.prv +dir rrfflbql +$ cd nnch +$ ls +50060 gqjtv.gcs +dir lnmmd +73078 ncvv +49129 tfb +dir vgwpcjrl +dir wnqlrqlf +$ cd lnmmd +$ ls +71780 gqmpvplj.vjg +$ cd .. +$ cd vgwpcjrl +$ ls +8269 zcspgw +$ cd .. +$ cd wnqlrqlf +$ ls +dir wzsvhssb +$ cd wzsvhssb +$ ls +187249 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +dir bpzmvds +24889 hfnbzcn +dir lqffwfr +274793 nnch.svh +dir rhwm +$ cd bpzmvds +$ ls +298048 tfnqwpj +$ cd .. +$ cd lqffwfr +$ ls +dir mzfpbjtl +$ cd mzfpbjtl +$ ls +217469 jzmgcj.gmd +$ cd .. +$ cd .. +$ cd rhwm +$ ls +54198 gqmpvplj.vjg +dir tlnmwhdt +$ cd tlnmwhdt +$ ls +94380 ndfcj.bvv +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd lfslp +$ ls +dir cmrwvp +dir cnh +311549 jgmf.fbs +dir jtgrbqvj +166298 pbwsqpcg.whf +151437 pfnglqgw.mcj +dir sqlwn +$ cd cmrwvp +$ ls +217666 pmsdthr.prv +172469 vzw.rqw +$ cd .. +$ cd cnh +$ ls +84011 jfb.mpt +$ cd .. +$ cd jtgrbqvj +$ ls +dir dcfpfq +dir ghhs +dir ntbmh +dir pfnglqgw +$ cd dcfpfq +$ ls +159731 pfnglqgw +$ cd .. +$ cd ghhs +$ ls +104713 blwnhcn +$ cd .. +$ cd ntbmh +$ ls +174007 jzmgcj.gmd +dir pfnglqgw +60549 rrfflbql.scj +dir zwcdggd +$ cd pfnglqgw +$ ls +49844 rfdw.pqh +$ cd .. +$ cd zwcdggd +$ ls +135925 ccmvjm.bjb +1135 gqmpvplj.vjg +120968 hmgpcj.nbb +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +184937 ccmvjm.bjb +128621 llsjsmg.vtv +dir lsbf +42834 ndfcj.fwq +85391 trrchml.sgp +$ cd lsbf +$ ls +192593 cfdtsfq.sln +289191 nnch.qzj +$ cd .. +$ cd .. +$ cd .. +$ cd sqlwn +$ ls +77962 ccmvjm.bjb +114288 ndfcj +$ cd .. +$ cd .. +$ cd ncvv +$ ls +dir gsd +$ cd gsd +$ ls +14949 jwcp.lmq +$ cd .. +$ cd .. +$ cd nftdcrl +$ ls +185409 jzmgcj.gmd +$ cd .. +$ cd nnch +$ ls +280827 djftt +dir ljt +dir ncvv +dir ndfcj +33396 qvhndl.pwn +136204 qvlc.cbr +14063 rrfflbql.mrq +130666 vscjncbm.sls +$ cd ljt +$ ls +166514 cgrgbpvw +55138 jzmgcj.gmd +$ cd .. +$ cd ncvv +$ ls +dir fhnw +56003 gqtcgszl.vnf +145831 jzmgcj.gmd +dir mwcbd +dir rclnhb +$ cd fhnw +$ ls +28050 gqmpvplj.vjg +$ cd .. +$ cd mwcbd +$ ls +288468 ncvv +$ cd .. +$ cd rclnhb +$ ls +190004 ndjmjbp +$ cd .. +$ cd .. +$ cd ndfcj +$ ls +287125 gqmpvplj.vjg +dir ndfcj +247237 nnch +138902 pfnglqgw +$ cd ndfcj +$ ls +143400 ssvsvffz +$ cd .. +$ cd .. +$ cd .. +$ cd pfnglqgw +$ ls +dir cwwb +dir dtf +97867 mqbz.fcp +$ cd cwwb +$ ls +dir wtz +$ cd wtz +$ ls +300949 zcq +$ cd .. +$ cd .. +$ cd dtf +$ ls +28018 gqmpvplj.vjg +$ cd .. +$ cd .. +$ cd qfbbnr +$ ls +dir bqmdfjp +89207 gjfzv +176709 pmsdthr.prv +246390 rrfflbql.vdl +$ cd bqmdfjp +$ ls +137504 cwz.jdg +9191 ncvv +$ cd .. +$ cd .. +$ cd wttc +$ ls +279516 ccmvjm.bjb +115478 lwnpwdqt.jpj +$ cd .. +$ cd .. +$ cd rrfflbql +$ ls +162371 nnch.pnm diff --git a/2022/cpp/07/lib.hpp b/2022/cpp/07/lib.hpp new file mode 100644 index 0000000..b28afd7 --- /dev/null +++ b/2022/cpp/07/lib.hpp @@ -0,0 +1,23 @@ +#include +#include +#include + +template +std::ostream& operator<<(std::ostream& os, const std::vector& list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) + os << ", "; + i++; + } + os << "]"; + + return os; +} + +inline void error(const char* msg) { + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/07/result b/2022/cpp/07/result new file mode 100755 index 0000000..3b8f770 Binary files /dev/null and b/2022/cpp/07/result differ diff --git a/2022/cpp/07/result.cpp b/2022/cpp/07/result.cpp new file mode 100644 index 0000000..8a81bf4 --- /dev/null +++ b/2022/cpp/07/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" + +#include + +int main(int argc, char* argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + Input input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/07/sample1 b/2022/cpp/07/sample1 new file mode 100644 index 0000000..bcbb513 --- /dev/null +++ b/2022/cpp/07/sample1 @@ -0,0 +1,23 @@ +$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k \ No newline at end of file diff --git a/2022/cpp/07/sample2 b/2022/cpp/07/sample2 new file mode 100644 index 0000000..bcbb513 --- /dev/null +++ b/2022/cpp/07/sample2 @@ -0,0 +1,23 @@ +$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k \ No newline at end of file diff --git a/2022/cpp/07/test b/2022/cpp/07/test new file mode 100755 index 0000000..7c6a6ed Binary files /dev/null and b/2022/cpp/07/test differ diff --git a/2022/cpp/07/test.cpp b/2022/cpp/07/test.cpp new file mode 100644 index 0000000..83acf0c --- /dev/null +++ b/2022/cpp/07/test.cpp @@ -0,0 +1,25 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1") { + Input actual_input = get_input("sample1"); + std::cout << "actual_input" << std::endl; + std::cout << actual_input << std::endl; + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "95437"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + Input actual_input = get_input("sample2"); + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "24933642"); + } +} +#endif diff --git a/2022/cpp/08/Makefile b/2022/cpp/08/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/08/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/08/answer.cpp b/2022/cpp/08/answer.cpp new file mode 100644 index 0000000..14bd2be --- /dev/null +++ b/2022/cpp/08/answer.cpp @@ -0,0 +1,130 @@ +// AOC - 2022 - 08 +#include "answer.hpp" + +#include +#include +#include + +Input get_input(const char* filename) { + std::ifstream stream(filename); + std::string str; + Input ret; + + getline(stream, str); + ret.width = str.size(); + for (auto c : str) + ret.trees.push_back(c); + + while (getline(stream, str)) + for (char c : str) + ret.trees.push_back(c); + ret.height = ret.trees.size() / ret.width; + return ret; +} + +bool is_tree_seen(uint x, uint y, Forest& forest) { + Tree current_tree = forest.tree_at(x, y); + bool is_seen = true; + // north + for (uint i = 0; i < y; i++) + if (forest.tree_at(x, i) >= current_tree) + is_seen = false; + if (is_seen) + return true; + + is_seen = true; + // east + for (uint i = x + 1; i < forest.width; i++) + if (forest.tree_at(i, y) >= current_tree) + is_seen = false; + if (is_seen) + return true; + + is_seen = true; + // south + for (uint i = y + 1; i < forest.height; i++) + if (forest.tree_at(x, i) >= current_tree) + is_seen = false; + if (is_seen) + return true; + + is_seen = true; + // west + for (uint i = 0; i < x; i++) + if (forest.tree_at(i, y) >= current_tree) + is_seen = false; + + if (!is_seen) + std::cout << "couldn't see tree " << current_tree << " at " << x << ", " + << y << std::endl; + + return is_seen; +} + +uint scenic_score(uint x, uint y, Forest& forest) { + Tree current_tree = forest.tree_at(x, y); + uint total_score = 1; + uint score = 0; + + // north + for (int i = y - 1; i >= 0; i--) { + score++; + if (forest.tree_at(x, i) >= current_tree) + break; + } + total_score *= score; + + score = 0; + // east + for (uint i = x + 1; i < forest.width; i++) { + score++; + if (forest.tree_at(i, y) >= current_tree) + break; + } + total_score *= score; + + score = 0; + // south + for (uint i = y + 1; i < forest.height; i++) { + score++; + if (forest.tree_at(x, i) >= current_tree) + break; + } + total_score *= score; + + score = 0; + // west + for (int i = x - 1; i >= 0; i--) { + score++; + if (forest.tree_at(i, y) >= current_tree) + break; + } + + total_score *= score; + + return total_score; +} + +std::string get_result(Input& forest) { + std::string ret; +#if PART == 1 + uint count = 2 * forest.width + 2 * forest.height - 4; + for (uint y = 1; y < forest.height - 1; y++) { + for (uint x = 1; x < forest.width - 1; x++) + if (is_tree_seen(x, y, forest)) + count++; + } + ret = std::to_string(count); +#else + uint max_scenic_score = 0; + for (uint y = 1; y < forest.height - 1; y++) { + for (uint x = 1; x < forest.width - 1; x++) { + uint score = scenic_score(x, y, forest); + if (max_scenic_score < score) + max_scenic_score = score; + } + } + ret = std::to_string(max_scenic_score); +#endif + return ret; +} diff --git a/2022/cpp/08/answer.hpp b/2022/cpp/08/answer.hpp new file mode 100644 index 0000000..c11d96b --- /dev/null +++ b/2022/cpp/08/answer.hpp @@ -0,0 +1,48 @@ +#include "lib.hpp" + +#include +#include + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 2 +#endif + + +typedef uint8_t Tree; + +struct Forest { + uint width, height; + std::vector trees; + + bool operator==(const Forest& rhs) const { + return this->trees == rhs.trees; + } + + uint get_idx(uint x, uint y) { + return y * width + x; + } + + Tree tree_at(uint x, uint y) { + return trees[get_idx(x, y)]; + } +}; + +typedef Forest Input; + +inline std::ostream& operator<<(std::ostream& os, Input& inp) { + uint col = 0; + for (auto& tree : inp.trees) { + os << tree; + if (++col % inp.width == 0) + os << '\n'; + } + return os; +} + +Input get_input(const char*); + +std::string get_result(Input&); diff --git a/2022/cpp/08/build/answer.o b/2022/cpp/08/build/answer.o new file mode 100644 index 0000000..6944caf Binary files /dev/null and b/2022/cpp/08/build/answer.o differ diff --git a/2022/cpp/08/input b/2022/cpp/08/input new file mode 100644 index 0000000..e67c61c --- /dev/null +++ b/2022/cpp/08/input @@ -0,0 +1,99 @@ +200120010031113332213034140102430141241124321111512323442304040044220141342121012210022220211111200 +111021120230012003124233312003010303204555454332453445353125225214221214442214301113012111020220220 +010000112330232223210123331341111021455213441355415452453433542411022110033014112420103200010200112 +210220131023203033023421012411405531115123145115445215311422135252541100222231431331003220231121021 +020112210133112114444340100410411231114131452544155151415233415312511222041221113442132231003203100 +121210122332301142321211440441113455552434332511455325315545354524142541534411112013143013120333022 +002022223332143323320030004111431434153352153321414154453442552321332341355132444030442110103203212 +212130023213100232130230411132244242133251435525323326611211534441513532233340420321144212221332012 +112201210213131241203033341411115513233426453426433653356253533133113511115222441140433214102332200 +212012000101044144022321521321444445566546646625625665232323663631331412343532242340023204432003233 +101233132121222100241254211542133266356326564552445466322626353633223221135151311240430411433022011 +200132020222314334333551152431232424524623645555522366665322366466545513112135535223102441334310202 +023113000020114221524122422222662645435544455234636242322656464245462233555511334411233402112203303 +323212301014434412315252233452332225554646452654545433445553336343544664455435323243510303023142200 +310022200424121333144123222262332322343322244646667564465544426245466232342143412141451122001041322 +212301111322001335441132334525635254352425553354565445643472236236266635446421522132452121404230333 +222212444300345211222512256444456624535343536756355746664436374344346652444335121144422222340114403 +133103420101433344253425324366444265345745457464444766674564476573243345254266312555221132304022243 +104024111323421413115464433455553477333456354575666765646757774776752425462423244113225443242031103 +102001244212344453145353232652647556546465453563544776667356374453675332254663444121345213131024201 +224444241243433331136646422564367344447534333336573546336576477746463642222264422255444411444213332 +111102440221143134255523244546456346745346547453543465664363545634575433436342252431313443241200434 +134231321331555233423635635366465473563535535586445888474374535544636375425246452265522242444124430 +234112414314111344422243544643454775656784865866884848444877466476364476356322553554245224411411221 +034400114351525345222546546744637535635657575787587675474664764764736443646462322222525151425423322 +200214241324215322354446653764777674587466544685754678446567884767437556756324542252661541311422130 +343144113253113264625454456777333344687566876677476786864558645875776364454562645333232242321313142 +343024121143435322432236376444777668467756646886688564457885758774655565674476525265433235351133020 +403402124545255442342535445764457654765745674556784877787445455686478773563733543423236235255444144 +103322531152132266562676355566367676848756788886766967585484486646776553753344443262466623351341241 +310355244513624552353465735465555764667556575665597778969594858667646774744555337236256442514154340 +023311451112523462333545544537465688645559988688956997665697964748587858337347363225646321434554144 +320135522136342345436443367666564847488558589757575579796688785767645678467555334323622425311134113 +302355525243323546577736555686874886486985688686888898966978978668487864787645754566534465541511251 +334125312126266532347576536548547584696886997768569758698995755875465674754333433642553345425533431 +341111335132225635365534457665748575977867585959997886979957965558787465664637533766462255534332423 +332515314534343636736373478687676678865555895657977886779567677978977887466835476754353523355412552 +223554423243256435734467677444784886576856669787898778968956989875566754875777467667635224535114135 +155522522343322534364735656868875656687568869667769697877887796866975856457447636733764232364431215 +242345244424435673345476658556448689589759768689899966877768987857579874745884637474453634352133231 +415255446645436567674735664645455855996887768879976678686967768687868985856885736776326622253351232 +345511155426334655353457846578475998667966669787979667866969678557875668566464436456435236255355314 +142554534636264637437346664558867877978687978689679869967766679569789677485678377367342526223134543 +445152112563265336347774464854776998568899676687677797996796877777879786686845577654534235322155423 +333311452256452445537757558677965658888896678979799998788899698785896978877575444547572432544335251 +153342523322435374455644657476596955598987666997999878876686677879688676464687476363732553363413554 +144335145664565566437454557675966795686769969779977988878679878778787789465465637576634655354532332 +152415343652336664567566647557776598677998979889877998977867798897785759685544636646555455665423232 +233452324356364643433768554555595977968766779897887998898666688775789556775758843355374525244243434 +415514136342636535455644448567857957878867697988999887897878896698589669784466744746433345345355353 +232342145445335734443368474877857768999979889789879797978786868766697957448687837436437246435352325 +423142134652644455355368647749855698998886897798977997799867788779868756865564445675347463653623132 +221215125542657466763755887857659858677696867997877899797779899996787779754784846775376254442211254 +154531463524644747547747455647685566769977767978799879788898879798999996758665537564563322242224125 +114543323332546776763457675777969696978788779877889799879998997975759878686666834757774555334611241 +353441154256436766664748544445957885788777978878979877989798876799786999466647747557553324526153313 +433233236244364537664567776764977668967888876787887977898978679686668778764647857553662664333221513 +112335142543334473565677774456986769698869989679787999966877889796668798648474667745633463364123315 +113555236654542657737447854474695576987788777767987976776779677896568655666568777374452626625515123 +335535115334436455537766485588496998559679678997888767666867796988758985466776445675326242423551222 +113534446446435634464566878474597579966586699697988866966997787776788976778774466655756463456235323 +432234422656545375357656547448457656589888789768979867898878979959955468888683644466555336562315155 +313344415452422533675445775845665999956589788887986877769867786689888565577764433373633363624515141 +222432351346446453465663784584875977688789959977876966969658866777886544775874577535665532224311252 +225114233145352533344653757488874695566977756698769768695668575699668757584555454357642544414312222 +033415322244643242337557674678486867575857998965787859688788778777887745544746656765646262354135222 +244444135424622245536477356767676647596876888859989597677576756885858547856577434533266366252341111 +043422441154553466473376764757644888596669857975585975898957778585855575867574554436566563342243533 +331142132342425642446677456357664865566895568659588999997598897847485545576757377726563624233332331 +023252542543632422666654576368776445864568889675685656595757555564576478356355573264462564525315531 +401013153355343645357335364463874854866687865999886679877556446867474565563474773455256232432344241 +021145311524153334266573666344586785645787666877957988698886547444446737764473344255436645355133043 +343022114235145422524664767447348848745548778464684486768568675765556575337775543626443412212441440 +111402455441135334663624565477563468586767564584754776865658486876757446535673333342623334312243002 +300312451335434322222245676775746358685854756547745674447587647487533473336654232544332431554454412 +433111155454541556355524273356653577688858857767884847858548544877576457444762553325524214215402141 +131233333423335563265232427676636343767667757678865557657665545337553674363435662333422331253432142 +424110224332255444554444426563344537343587775787775766858448446366677553432226234426335534235001312 +333412002215124441543424366273747763767633456777658554454464446357566564765333524264253113145112300 +101110030515255434456252656567734365435643756543777575374763565634563366532624443621453215143234300 +303322412424455315323623364345353365376676743654344676735433644466637676342646445331232331222232310 +323112211415542144152522545435246756534467755554567676637543357465766366552324534423444455001240332 +104010010231221552145233456226456635564775656444567465773637433733444632663325561451234351230241312 +121411232340543435321316435636242655564355535365574763633547465475523553325562122152444444042300041 +131012210332331125512334254246234345354543573744464635755354544666345534262435114123351541032131413 +320204021134412321515435566234224352423426577577364457356445435455454244243342521425351312003010221 +323114214132034241313542544555435465334424654666346364554655422236635456436354122354410044244241323 +013302120424301452153353441245652435266525344656664634425626224355262362255351231351513443132401213 +320030122021334215313244242334523262333453263233364243362363226452453421344311524253311414201210001 +232000301112410134252124412414346224626463252363536262245565624425334453135542241232142142113023001 +023101113421202000305251552134255163452646652324334242436543224522235515444445314014323100240020120 +320223110031202012323335553432231212252653563665545464422324656333443431535442453303001103202231020 +102033222010100142203435155413125314311166645446465322244546532441244154143433143443342400201012012 +103310002120123402241302221241151441435355114335344522434421453552354154135223134443030043002000021 +112323110033212002113322411233332355223342424233213123445224135331545115152043042400020200022133330 +022111301200101044211214231433231311322554214331413543414521424255241531111410022413003020200231110 +012022111212202121003233141343155411314255551134343533133453252153433344441013011221420230021203220 +221101122202110034040022441212441254112211113355531534335143125434435312222200311411322330203331100 +210122210200133032213341213111433213432513121525124133544144433541201142230241123430213303001122221 diff --git a/2022/cpp/08/lib.hpp b/2022/cpp/08/lib.hpp new file mode 100644 index 0000000..b28afd7 --- /dev/null +++ b/2022/cpp/08/lib.hpp @@ -0,0 +1,23 @@ +#include +#include +#include + +template +std::ostream& operator<<(std::ostream& os, const std::vector& list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) + os << ", "; + i++; + } + os << "]"; + + return os; +} + +inline void error(const char* msg) { + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/08/result b/2022/cpp/08/result new file mode 100755 index 0000000..bf6bb4e Binary files /dev/null and b/2022/cpp/08/result differ diff --git a/2022/cpp/08/result.cpp b/2022/cpp/08/result.cpp new file mode 100644 index 0000000..8a81bf4 --- /dev/null +++ b/2022/cpp/08/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" + +#include + +int main(int argc, char* argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + Input input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/08/sample1 b/2022/cpp/08/sample1 new file mode 100644 index 0000000..6557024 --- /dev/null +++ b/2022/cpp/08/sample1 @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 \ No newline at end of file diff --git a/2022/cpp/08/sample2 b/2022/cpp/08/sample2 new file mode 100644 index 0000000..6557024 --- /dev/null +++ b/2022/cpp/08/sample2 @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 \ No newline at end of file diff --git a/2022/cpp/08/test b/2022/cpp/08/test new file mode 100755 index 0000000..fba139b Binary files /dev/null and b/2022/cpp/08/test differ diff --git a/2022/cpp/08/test.cpp b/2022/cpp/08/test.cpp new file mode 100644 index 0000000..2e1685c --- /dev/null +++ b/2022/cpp/08/test.cpp @@ -0,0 +1,58 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1") { + Input actual_input = get_input("sample1"); + + Input expected_input; + expected_input.width = 5; + for (auto t : { + '3', + '0', + '3', + '7', + '3', + '2', + '5', + '5', + '1', + '2', + '6', + '5', + '3', + '3', + '2', + '3', + '3', + '5', + '4', + '9', + '3', + '5', + '3', + '9', + '0', + }) { + expected_input.trees.push_back(t); + }; + SUBCASE("Testing input is parsed correctly") { + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "21"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + Input actual_input = get_input("sample2"); + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "8"); + } +} +#endif diff --git a/2022/cpp/Makefile b/2022/cpp/Makefile new file mode 100644 index 0000000..f15ee71 --- /dev/null +++ b/2022/cpp/Makefile @@ -0,0 +1,16 @@ +COOKIE = 53616c7465645f5f49dff2a6091a00e67a9678384d5d9867c24d2c2024c8524adb2391b31b45118c978505c7ea27ddecf0cc047ce8d51dc32203dffde652c690 + +.PHONY = new +NUMBER := $$(($(shell ls -D | grep "^[0-2][0-9]$$" | wc -l) + 1)) +NUMBER_DIR := $(shell printf "%02d" $(NUMBER)) +# NUMBER := $(shell ls -D | wc -l) + +# https://adventofcode.com/2022/day/1/input + +new: + @echo "Creating $(NUMBER)..." + cp -r template/ $(NUMBER_DIR)/ + @echo "Getting input" + @wget -q -O $(NUMBER_DIR)/input --header "Cookie: session=$(COOKIE)" https://adventofcode.com/2022/day/$(NUMBER)/input + sed -i "s/DATE/$(NUMBER_DIR)/" $(NUMBER_DIR)/answer.cpp + xclip -o > $(NUMBER_DIR)/sample1 diff --git a/2022/cpp/doctest b/2022/cpp/doctest new file mode 160000 index 0000000..86892fc --- /dev/null +++ b/2022/cpp/doctest @@ -0,0 +1 @@ +Subproject commit 86892fc480f80fb57d9a3926cb506c0e974489d8 diff --git a/2022/cpp/template/Makefile b/2022/cpp/template/Makefile new file mode 100644 index 0000000..8033b50 --- /dev/null +++ b/2022/cpp/template/Makefile @@ -0,0 +1,33 @@ +PART := 1 +CXXFLAGS := -Wall -O3 -DPART=$(PART) +OBJ_DIR = build +OBJ = answer.o + +.PHONY = clean asdf sample1 sample2 + +asdf: result + @echo "------- RESULT -------" + @./result "input" | xclip -selection c + @xclip -selection c -o + +check: test + ./test + +test: $(OBJ_DIR)/$(OBJ) + +verbose: clean + $(eval CXXFLAGS += -DVERBOSE) + +$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: %.hpp + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $<..." + @mkdir -p $(@D) + gcc $(CXXFLAGS) -c -o $@ $< + +result: $(OBJ_DIR)/$(OBJ) + +clean: + -rm -f *.o + -rm -rf $(OBJ_DIR)/ + diff --git a/2022/cpp/template/answer.cpp b/2022/cpp/template/answer.cpp new file mode 100644 index 0000000..e050fd0 --- /dev/null +++ b/2022/cpp/template/answer.cpp @@ -0,0 +1,22 @@ +// AOC - 2022 - DATE +#include "answer.hpp" + +#include +#include + +Input get_input(const char* filename) { + std::ifstream stream(filename); + std::string str; + Input ret; + + while (getline(stream, str)) {} + return ret; +} + +std::string get_result(Input& input) { + std::string ret; +#if PART == 1 +#else +#endif + return ret; +} diff --git a/2022/cpp/template/answer.hpp b/2022/cpp/template/answer.hpp new file mode 100644 index 0000000..e22edbd --- /dev/null +++ b/2022/cpp/template/answer.hpp @@ -0,0 +1,28 @@ +#include "lib.hpp" + +#include + +#ifndef VERBOSE +#define VERBOSE 0 +#endif + +#ifndef PART +#define PART 1 +#endif + +struct A { + bool operator==(const A& rhs) const { + return this->== rhs.; + } +}; + +typedef A Input; + +inline std::ostream& operator<<(std::ostream& os, Input& inp) { + os << inp.; + return os; +} + +Input get_input(const char*); + +std::string get_result(Input&); diff --git a/2022/cpp/template/lib.hpp b/2022/cpp/template/lib.hpp new file mode 100644 index 0000000..b28afd7 --- /dev/null +++ b/2022/cpp/template/lib.hpp @@ -0,0 +1,23 @@ +#include +#include +#include + +template +std::ostream& operator<<(std::ostream& os, const std::vector& list) { + unsigned long i = 0; + os << "["; + for (auto el : list) { + os << el; + if (i < list.size() - 1) + os << ", "; + i++; + } + os << "]"; + + return os; +} + +inline void error(const char* msg) { + std::cerr << msg << std::endl; + exit(1); +} diff --git a/2022/cpp/template/result.cpp b/2022/cpp/template/result.cpp new file mode 100644 index 0000000..8a81bf4 --- /dev/null +++ b/2022/cpp/template/result.cpp @@ -0,0 +1,23 @@ +#include "answer.hpp" + +#include + +int main(int argc, char* argv[]) { + if (argc < 2) + error("Please specify the input file as first argument."); + +#if !VERBOSE + std::cout.setstate(std::ios_base::failbit); +#endif + + Input input = get_input(argv[1]); + std::string result = get_result(input); + +#if !VERBOSE + std::cout.clear(); +#endif + + std::cout << result << std::endl; + + return 0; +} diff --git a/2022/cpp/template/test.cpp b/2022/cpp/template/test.cpp new file mode 100644 index 0000000..58eb898 --- /dev/null +++ b/2022/cpp/template/test.cpp @@ -0,0 +1,33 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../doctest/doctest/doctest.h" +#include "answer.hpp" + +#if PART == 1 +TEST_CASE("Part 1") { + Input actual_input = get_input("sample1"); + + Input expected_input; + SUBCASE("Testing input is parsed correctly") { + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "SAMPLE1_OUT"); + } +} +#endif + +#if PART == 2 +TEST_CASE("Part 2") { + Input actual_input = get_input("sample2"); + + Input expected_input; + SUBCASE("Testing input is parsed correctly") { + CHECK(actual_input == expected_input); + } + + SUBCASE("Testing output is the one expected from AOC") { + CHECK(get_result(actual_input) == "SAMPLE2_OUT"); + } +} +#endif