From faabb2f188e432be9cf4748b36eac1bec12fb4c3 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Tue, 14 Dec 2021 02:07:16 +0100 Subject: [PATCH] Add solution for 2021 day 13 --- 2021/README.md | 3 +- 2021/day-13/Justfile | 6 + 2021/day-13/common.cpp | 164 ++++++ 2021/day-13/common.hpp | 42 ++ 2021/day-13/inputs/puzzle.txt | 941 ++++++++++++++++++++++++++++++++++ 2021/day-13/inputs/sample.txt | 21 + 2021/day-13/part_one.cpp | 15 + 2021/day-13/part_two.cpp | 14 + README.md | 2 +- 9 files changed, 1206 insertions(+), 2 deletions(-) create mode 100644 2021/day-13/Justfile create mode 100644 2021/day-13/common.cpp create mode 100644 2021/day-13/common.hpp create mode 100644 2021/day-13/inputs/puzzle.txt create mode 100644 2021/day-13/inputs/sample.txt create mode 100644 2021/day-13/part_one.cpp create mode 100644 2021/day-13/part_two.cpp diff --git a/2021/README.md b/2021/README.md index 967d073..6ff48e7 100644 --- a/2021/README.md +++ b/2021/README.md @@ -16,7 +16,7 @@ | 10 | ✓ | ✓ | [OCaml] | | 11 | ✓ | ✓ | [Crystal] | | 12 | ✓ | ✓ | [Python] | -| 13 | | | | +| 13 | ✓ | ✓ | [C++] | | 14 | | | | | 15 | | | | | 16 | | | | @@ -48,3 +48,4 @@ [ocaml]: https://ocaml.org [crystal]: https://crystal-lang.org [python]: https://www.python.org +[c++]: https://isocpp.org diff --git a/2021/day-13/Justfile b/2021/day-13/Justfile new file mode 100644 index 0000000..eeb96ef --- /dev/null +++ b/2021/day-13/Justfile @@ -0,0 +1,6 @@ +@part PART INPUT_FILE="inputs/puzzle.txt": + clang -std=c++20 -lstdc++ -Wall -Wextra -Werror -pedantic -o part_{{PART}} common.cpp part_{{PART}}.cpp + ./part_{{PART}} {{INPUT_FILE}} + +clean: + rm -f part_one part_two diff --git a/2021/day-13/common.cpp b/2021/day-13/common.cpp new file mode 100644 index 0000000..265771e --- /dev/null +++ b/2021/day-13/common.cpp @@ -0,0 +1,164 @@ +#include +#include +#include "common.hpp" + +using std::ifstream; +using std::getline; + +enum class ParseState { ReadCoordinates, ReadInstruction }; + +Sheet::Sheet(string path) { + auto parsed = parse_file(path); + auto coordinates = std::get<0>(parsed); + auto instructions = std::get<1>(parsed); + + for (size_t i = 0; i < 2; i++) { + auto instruction = instructions.at(i); + switch (std::get<0>(instruction)) { + case Axis::X: + this->width = std::get<1>(instruction) * 2 + 1; + break; + case Axis::Y: + this->height = std::get<1>(instruction) * 2 + 1; + } + } + + this->instructions = instructions; + + this->map.resize(width * height, false); + + for (auto coord : coordinates) { + auto x = std::get<0>(coord); + auto y = std::get<1>(coord); + this->map[x + (y * this->width)] = true; + } +} + +void Sheet::fold(Axis axis, size_t pos) { + switch (axis) { + case Axis::X: + this->fold_left(pos); + break; + case Axis::Y: + this->fold_up(pos); + break; + } +} + +void Sheet::fold_up(size_t pos) { + vector top(this->width * pos, false); + + for (size_t row = 0; row < pos; ++row) { + for (size_t col = 0; col < this->width; ++col) { + size_t top_pos = col + (row * this->width); + size_t bottom_pos = col + (this->map.size() - ((row + 1) * this->width)); + + top[top_pos] = this->map[top_pos] || this->map[bottom_pos]; + } + } + + this->height = pos; + this->map = top; +} + +void Sheet::fold_left(size_t pos) { + vector left(this->height * pos, false); + + for (size_t row = 0; row < this->height; ++row) { + for (size_t col = 0; col < pos; ++col) { + size_t left_pos = col + (row * this->width); + size_t right_pos = (this->width - 1 - col) + (row * this->width); + + left[col + (row * pos)] = this->map[left_pos] || this->map[right_pos]; + } + } + + this->width = pos; + this->map = left; +} + +void Sheet::fold_once() { + auto instruction = this->instructions.at(0); + this->fold(std::get<0>(instruction), std::get<1>(instruction)); +} + +void Sheet::fold_all() { + for (auto instruction : this->instructions) { + this->fold(std::get<0>(instruction), std::get<1>(instruction)); + } +} + +int Sheet::count_dots() { + int dots = 0; + + for (auto dot : this->map) { + if (dot) dots++; + } + + return dots; +} + +void Sheet::print() { + for (size_t i = 0; i < this->map.size(); ++i) { + auto chr = this->map[i] ? "#" : "."; + std::cout << chr << " "; + if ((i + 1) % this->width == 0) { + std::cout << std::endl; + } + } +} + +Coordinate parse_coordinates(string str) { + const string delim = ","; + + auto x = std::stoi(str.substr(0, str.find(delim))); + str.erase(0, str.find(delim) + delim.length()); + auto y = std::stoi(str); + + return std::make_tuple(x, y); +} + +Instruction parse_instruction(string str) { + const string delim = "="; + + str.erase(0, 11); + auto axis = str.substr(0, str.find(delim)) == "x" ? Axis::X : Axis::Y; + str.erase(0, str.find(delim) + delim.length()); + auto where = std::stoi(str); + + return std::make_tuple(axis, where); +} + +tuple, vector> parse_file(string path) { + vector coordinates; + vector instructions; + + ifstream file; + file.open(path); + + auto state = ParseState::ReadCoordinates; + string line; + while (getline(file, line)) { + switch(state) { + case ParseState::ReadCoordinates: { + if (line.empty()) { + state = ParseState::ReadInstruction; + continue; + } + auto coord = parse_coordinates(line); + coordinates.push_back(coord); + break; + } + case ParseState::ReadInstruction: { + auto instr = parse_instruction(line); + instructions.push_back(instr); + break; + } + } + + } + + file.close(); + + return std::make_tuple(coordinates, instructions); +} diff --git a/2021/day-13/common.hpp b/2021/day-13/common.hpp new file mode 100644 index 0000000..8fa90c8 --- /dev/null +++ b/2021/day-13/common.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +using std::vector; +using std::tuple; +using std::string; + +enum class Axis { X, Y }; + +using Coordinate = tuple; +using Instruction = tuple; + +class Sheet { +private: + vector map; + size_t width = 0; + size_t height = 0; + vector instructions; + +public: + Sheet(string path); + + void fold_once(); + + void fold_all(); + + int count_dots(); + + void print(); + +private: + void fold(Axis axis, size_t pos); + + void fold_up(size_t pos); + + void fold_left(size_t pos); +}; + +tuple, vector> parse_file(string path); diff --git a/2021/day-13/inputs/puzzle.txt b/2021/day-13/inputs/puzzle.txt new file mode 100644 index 0000000..23f9b23 --- /dev/null +++ b/2021/day-13/inputs/puzzle.txt @@ -0,0 +1,941 @@ +92,632 +795,9 +1034,368 +1230,233 +969,816 +1218,526 +971,780 +139,417 +892,313 +1196,196 +1091,70 +728,747 +632,10 +1089,247 +70,159 +492,375 +919,581 +304,469 +736,431 +7,630 +457,879 +174,567 +328,773 +207,714 +361,452 +77,151 +907,298 +325,838 +956,644 +272,52 +259,794 +254,847 +517,851 +669,516 +746,275 +1146,801 +1019,297 +679,43 +517,416 +703,814 +90,201 +206,74 +239,794 +1120,114 +197,711 +1247,131 +239,71 +418,805 +129,757 +1195,56 +895,598 +975,712 +57,171 +656,746 +984,177 +1,182 +785,457 +810,107 +703,278 +699,593 +795,477 +67,750 +190,616 +1,712 +162,438 +1079,432 +1010,868 +431,30 +293,9 +957,518 +452,634 +1245,452 +631,87 +395,200 +721,284 +1143,198 +669,378 +1205,827 +974,483 +33,588 +894,7 +1064,245 +1099,616 +1307,248 +63,234 +249,71 +919,182 +589,284 +730,152 +251,298 +644,78 +731,148 +669,638 +663,712 +356,824 +960,873 +1139,602 +902,404 +785,728 +830,434 +691,207 +118,567 +927,742 +436,556 +946,866 +1235,746 +433,647 +1266,103 +267,775 +796,581 +474,219 +686,582 +982,354 +897,263 +398,626 +689,687 +1184,294 +403,374 +303,135 +1168,670 +1275,562 +1000,812 +974,411 +785,309 +1019,108 +189,551 +969,857 +401,549 +574,873 +73,885 +417,406 +1029,584 +345,626 +238,887 +103,847 +1006,21 +919,712 +256,268 +13,773 +1093,644 +1230,890 +858,260 +1010,196 +1002,341 +761,807 +1034,526 +661,318 +381,390 +1111,597 +1161,65 +1258,152 +1031,56 +393,890 +683,752 +22,621 +671,49 +972,31 +581,574 +1163,145 +1051,694 +805,513 +1119,28 +1051,794 +1288,884 +1092,226 +152,270 +174,327 +266,151 +763,863 +733,151 +1292,105 +55,47 +663,630 +190,780 +1170,537 +177,381 +550,390 +251,150 +23,43 +1007,135 +386,726 +431,847 +1079,24 +171,574 +997,212 +1056,89 +1238,257 +74,775 +152,119 +344,107 +833,793 +1146,737 +972,150 +107,600 +505,605 +1124,273 +474,194 +17,380 +492,823 +1309,712 +719,131 +830,460 +1237,574 +1180,28 +808,176 +507,98 +339,114 +155,261 +318,859 +335,824 +919,548 +920,497 +1074,413 +1221,233 +118,103 +1079,14 +673,518 +761,197 +289,96 +602,294 +1064,218 +549,197 +1091,600 +375,80 +691,625 +965,268 +574,469 +890,191 +796,89 +547,415 +3,248 +858,95 +162,71 +748,526 +222,392 +994,526 +803,796 +868,565 +218,42 +293,718 +763,31 +1222,742 +279,56 +966,107 +74,791 +455,177 +507,796 +1071,71 +875,518 +499,875 +177,45 +1258,70 +659,248 +467,750 +1044,151 +244,714 +1260,480 +576,476 +212,26 +181,156 +1071,346 +239,598 +893,406 +666,658 +1086,401 +114,565 +171,633 +103,495 +290,476 +1098,420 +1268,148 +582,257 +408,452 +310,787 +435,376 +991,824 +1130,518 +333,514 +1148,413 +90,649 +649,457 +326,298 +1155,875 +607,814 +868,329 +1302,859 +1048,532 +259,884 +1006,469 +1155,712 +587,122 +868,250 +1155,261 +328,705 +604,414 +271,175 +974,247 +621,687 +1054,211 +298,70 +647,793 +857,432 +293,207 +190,838 +842,173 +238,439 +113,383 +938,530 +11,114 +1074,295 +736,661 +1193,666 +364,642 +525,728 +405,590 +1089,199 +199,877 +1206,726 +835,301 +621,400 +1111,297 +1238,637 +580,551 +969,37 +945,768 +189,343 +164,737 +890,703 +661,514 +820,317 +13,170 +219,175 +551,847 +512,858 +1031,814 +1031,260 +637,376 +1130,667 +105,255 +1097,735 +1305,828 +619,625 +1126,367 +1087,30 +13,121 +47,726 +416,679 +338,31 +647,630 +313,343 +729,320 +82,571 +1139,574 +465,491 +1017,207 +234,847 +137,478 +875,527 +956,26 +591,660 +514,581 +326,189 +1303,152 +483,857 +869,831 +483,37 +739,863 +383,600 +502,723 +919,96 +169,437 +33,157 +7,712 +1250,665 +1098,644 +792,280 +236,413 +813,437 +107,863 +1001,474 +1001,626 +1092,194 +666,638 +202,95 +223,210 +907,596 +380,600 +1087,847 +475,301 +114,196 +977,206 +666,82 +119,266 +716,823 +689,437 +303,9 +10,658 +371,637 +773,241 +1000,586 +935,56 +1202,220 +699,628 +518,280 +1076,210 +273,838 +259,880 +972,66 +798,450 +236,581 +763,600 +816,227 +1250,229 +2,250 +1120,126 +1002,789 +731,772 +222,56 +1057,345 +268,726 +802,425 +335,182 +364,252 +949,885 +464,780 +344,630 +1130,443 +1049,162 +206,820 +1168,858 +982,298 +10,275 +363,786 +1161,605 +42,298 +1158,492 +782,742 +171,709 +350,682 +1240,159 +985,504 +734,418 +364,866 +341,485 +1203,751 +902,732 +80,213 +338,150 +55,847 +726,235 +536,775 +647,661 +621,588 +445,666 +1235,74 +954,824 +610,791 +1000,406 +949,562 +82,91 +1133,45 +436,42 +1285,638 +631,291 +416,663 +1017,114 +874,194 +736,233 +441,831 +902,452 +480,434 +949,780 +272,500 +666,12 +1099,479 +57,311 +130,866 +631,43 +925,499 +868,278 +42,148 +1266,551 +657,630 +300,226 +118,327 +1089,647 +47,504 +1184,742 +966,140 +556,700 +1088,392 +239,548 +146,376 +895,151 +549,697 +897,631 +644,414 +657,182 +641,638 +146,667 +652,644 +811,709 +88,152 +1300,598 +336,411 +1054,716 +105,827 +1309,630 +304,21 +1287,851 +736,540 +883,182 +734,194 +201,462 +1228,120 +783,381 +1230,213 +574,233 +1017,885 +528,742 +905,304 +774,551 +343,621 +836,219 +420,703 +748,859 +500,18 +647,600 +415,548 +300,26 +1196,747 +152,402 +734,866 +965,514 +364,194 +154,787 +7,518 +25,638 +802,441 +972,828 +171,320 +383,495 +457,687 +355,812 +17,868 +212,868 +1181,885 +190,56 +793,739 +263,182 +181,738 +1303,712 +505,513 +565,406 +1205,67 +508,425 +982,637 +75,74 +795,190 +1236,152 +1164,376 +915,78 +293,687 +36,630 +80,354 +1019,597 +537,534 +1202,642 +310,308 +92,526 +437,644 +836,194 +155,409 +413,631 +1193,86 +7,264 +1017,306 +1120,768 +1011,304 +966,586 +728,537 +527,65 +1193,205 +1173,2 +1146,295 +63,682 +1287,535 +1253,311 +551,47 +652,698 +443,479 +63,548 +584,683 +256,211 +798,892 +1268,298 +691,114 +1220,201 +1091,724 +1201,868 +246,666 +1148,800 +78,642 +1043,775 +1161,180 +1091,518 +1027,628 +649,128 +1069,183 +164,157 +417,488 +679,87 +223,864 +1300,236 +1130,70 +492,325 +267,152 +935,814 +766,182 +80,890 +219,70 +436,338 +949,452 +939,705 +664,859 +180,21 +517,43 +947,513 +1146,463 +764,252 +811,427 +1133,849 +303,134 +736,354 +82,452 +1007,759 +164,352 +1017,9 +611,628 +1238,17 +44,103 +892,581 +1143,646 +152,624 +846,838 +1139,320 +890,236 +351,424 +845,674 +1310,11 +551,399 +1228,263 +326,717 +1308,698 +1072,439 +679,291 +754,700 +331,198 +731,820 +982,596 +80,233 +1136,684 +1277,306 +13,704 +1280,582 +103,460 +647,101 +1124,425 +319,824 +733,375 +654,33 +1283,306 +783,65 +201,432 +574,4 +1038,394 +1310,861 +938,364 +95,656 +997,551 +200,376 +313,78 +564,201 +902,325 +164,381 +1201,646 +678,10 +231,813 +361,9 +977,514 +117,86 +646,859 +427,182 +984,637 +52,70 +560,142 +1101,666 +574,764 +1143,696 +246,228 +823,646 +127,269 +164,463 +773,808 +408,442 +1230,354 +151,444 +1148,438 +146,443 +239,738 +256,716 +656,861 +290,264 +303,457 +249,823 +353,518 +540,196 +338,66 +1288,234 +482,413 +331,696 +759,460 +197,191 +114,147 +780,410 +811,633 +1007,155 +313,485 +1120,56 +1087,658 +129,137 +345,268 +846,278 +300,698 +508,3 +847,28 +822,667 +487,646 +244,442 +574,540 +689,530 +211,616 +78,252 +159,646 +894,663 +1098,868 +730,551 +436,700 +1031,437 +946,488 +11,780 +366,858 +452,351 +73,9 +1192,103 +1021,798 +508,586 +1235,148 +1232,642 +5,437 +547,143 +663,569 +1299,114 +10,816 +1300,812 +684,824 +745,406 +503,674 +1247,96 +744,742 +115,838 +621,207 +1183,65 +1299,668 +474,667 +1292,519 +361,754 +545,248 +974,859 +579,820 +117,205 +512,892 +509,518 +808,12 +8,859 +1274,264 +145,152 +836,70 +836,451 +761,639 +1104,820 +610,103 +811,37 +82,442 +793,851 +241,183 +180,451 +1062,700 +212,278 +592,852 +1087,864 +418,357 +743,828 +527,829 +199,597 +483,602 +490,705 +743,598 +80,793 +517,739 +611,593 +1156,787 +765,644 +864,432 +1146,431 +763,415 +581,885 +654,11 +1148,879 +1044,677 +499,467 +746,299 +180,443 +1133,101 +33,400 +381,196 +774,775 +847,476 +162,456 +145,44 +171,516 +442,250 +995,114 +1058,600 +957,70 +43,266 +792,320 +1297,773 +721,452 +246,676 +649,318 +180,667 +582,357 +892,357 +160,219 +304,660 +746,270 +408,121 +719,630 +579,263 +60,677 +720,329 +431,47 +344,219 +231,880 +299,530 +579,122 +293,158 +1287,43 +564,59 +845,491 +820,766 +433,247 +1059,744 +644,236 +949,392 +335,712 +802,675 +734,252 +442,565 +770,565 +171,602 +1048,362 +502,775 +1303,264 +1051,365 +1293,26 +853,879 +333,688 +644,816 +136,574 +719,234 +392,362 +774,119 +1087,684 +699,266 +736,764 +1133,381 +371,257 +793,43 +1191,266 +341,346 +607,373 +147,145 +905,590 +102,849 +505,381 +1113,703 +748,816 +782,152 +197,183 +431,684 +139,29 +746,624 +1247,630 +1300,12 +1155,19 +631,359 +72,877 +1083,467 +382,170 +298,824 +730,600 +946,194 +162,15 +512,450 +1011,28 +423,141 +488,667 +1236,775 +793,70 +2,644 +919,122 +856,518 +728,257 +723,325 +833,773 +211,415 + +fold along x=655 +fold along y=447 +fold along x=327 +fold along y=223 +fold along x=163 +fold along y=111 +fold along x=81 +fold along y=55 +fold along x=40 +fold along y=27 +fold along y=13 +fold along y=6 diff --git a/2021/day-13/inputs/sample.txt b/2021/day-13/inputs/sample.txt new file mode 100644 index 0000000..282114c --- /dev/null +++ b/2021/day-13/inputs/sample.txt @@ -0,0 +1,21 @@ +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5 diff --git a/2021/day-13/part_one.cpp b/2021/day-13/part_one.cpp new file mode 100644 index 0000000..f1af93b --- /dev/null +++ b/2021/day-13/part_one.cpp @@ -0,0 +1,15 @@ +#include +#include +#include "common.hpp" + +using std::string; + +int main([[maybe_unused]] int argc, char *argv[]) { + string path = argv[1]; + + Sheet sheet {path}; + + sheet.fold_once(); + + std::cout << sheet.count_dots() << std::endl; +} diff --git a/2021/day-13/part_two.cpp b/2021/day-13/part_two.cpp new file mode 100644 index 0000000..c6aa8f8 --- /dev/null +++ b/2021/day-13/part_two.cpp @@ -0,0 +1,14 @@ +#include +#include "common.hpp" + +using std::string; + +int main([[maybe_unused]] int argc, char *argv[]) { + string path = argv[1]; + + Sheet sheet {path}; + + sheet.fold_all(); + + sheet.print(); +} diff --git a/README.md b/README.md index 31d6223..8d6de97 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,4 @@ - [2018](2018/README.md) (0% completed) - [2019](2019/README.md) (0% completed) - [2020](2020/README.md) (20% completed) -- [2021](2021/README.md) (48% completed) +- [2021](2021/README.md) (52% completed)