diff options
Diffstat (limited to '2023')
-rw-r--r-- | 2023/05/Makefile | 19 | ||||
-rw-r--r-- | 2023/05/resources/input.txt | 207 | ||||
-rw-r--r-- | 2023/05/resources/input_small.txt | 33 | ||||
-rw-r--r-- | 2023/05/src/part1.cpp | 84 | ||||
-rw-r--r-- | 2023/05/src/part2.cpp | 142 |
5 files changed, 485 insertions, 0 deletions
diff --git a/2023/05/Makefile b/2023/05/Makefile new file mode 100644 index 0000000..f15fa16 --- /dev/null +++ b/2023/05/Makefile | |||
@@ -0,0 +1,19 @@ | |||
1 | CPPFLAGS := -I../include | ||
2 | CXXFLAGS := -std=c++17 | ||
3 | EXE := part1 part2 | ||
4 | |||
5 | .PHONY: all clean configure | ||
6 | |||
7 | all: $(EXE) | ||
8 | |||
9 | configure: | ||
10 | bear -- $(MAKE) all | ||
11 | |||
12 | %.o: %.cpp | ||
13 | $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@ | ||
14 | |||
15 | clean: | ||
16 | rm -rf $(EXE) src/*.o compile_commands.json | ||
17 | |||
18 | %: src/%.o | ||
19 | $(CXX) $^ -o $@ | ||
diff --git a/2023/05/resources/input.txt b/2023/05/resources/input.txt new file mode 100644 index 0000000..8e9d0ba --- /dev/null +++ b/2023/05/resources/input.txt | |||
@@ -0,0 +1,207 @@ | |||
1 | seeds: 104847962 3583832 1212568077 114894281 3890048781 333451605 1520059863 217361990 310308287 12785610 3492562455 292968049 1901414562 516150861 2474299950 152867148 3394639029 59690410 862612782 176128197 | ||
2 | |||
3 | seed-to-soil map: | ||
4 | 2023441036 2044296880 396074363 | ||
5 | 2419515399 3839972576 454994720 | ||
6 | 274688417 699823315 258919718 | ||
7 | 533608135 0 431744151 | ||
8 | 965352286 431744151 161125324 | ||
9 | 3391658630 2936663910 903308666 | ||
10 | 200749950 1177785526 73938467 | ||
11 | 2874510119 1440389999 315892137 | ||
12 | 1916089471 2440371243 20593195 | ||
13 | 0 977035576 200749950 | ||
14 | 1936682666 1957538510 86758370 | ||
15 | 1440389999 2902130623 34533287 | ||
16 | 1126477610 592869475 106953840 | ||
17 | 3190402256 1756282136 201256374 | ||
18 | 1474923286 2460964438 441166185 | ||
19 | 1233431450 958743033 18292543 | ||
20 | |||
21 | soil-to-fertilizer map: | ||
22 | 1479837493 1486696129 480988794 | ||
23 | 3637384566 3730606485 267472485 | ||
24 | 70483107 174821741 921411492 | ||
25 | 3586173142 3071290434 51211424 | ||
26 | 1960826287 1166716340 319979789 | ||
27 | 3952751562 3398939385 283772589 | ||
28 | 0 1096233233 70483107 | ||
29 | 1166716340 1967684923 313121153 | ||
30 | 3904857051 3682711974 47894511 | ||
31 | 2902018973 3122501858 276437527 | ||
32 | 991894599 0 174821741 | ||
33 | 3416901681 2902018973 169271461 | ||
34 | 3178456500 3998078970 238445181 | ||
35 | |||
36 | fertilizer-to-water map: | ||
37 | 4274676882 2765984054 20290414 | ||
38 | 3642266392 2324011621 382224743 | ||
39 | 3159410287 4157769177 137198119 | ||
40 | 3437898965 2786274468 204367427 | ||
41 | 2136710407 1497332580 94233249 | ||
42 | 4121681656 2706236364 59747690 | ||
43 | 2362529584 1912615374 411396247 | ||
44 | 4181429346 3961317447 93247536 | ||
45 | 270199336 152334204 473273308 | ||
46 | 1977000445 3853228854 108088593 | ||
47 | 2085089038 1304420652 51621369 | ||
48 | 1407624846 2990641895 345516575 | ||
49 | 908901118 638701782 307729932 | ||
50 | 3296608406 1356042021 141290559 | ||
51 | 743472644 625607512 13094270 | ||
52 | 2230943656 3721642926 131585928 | ||
53 | 0 946431714 270199336 | ||
54 | 2773925831 3336158470 385484456 | ||
55 | 4024491135 1815424853 97190521 | ||
56 | 756566914 0 152334204 | ||
57 | 1304420652 4054564983 103204194 | ||
58 | 1753141421 1591565829 223859024 | ||
59 | |||
60 | water-to-light map: | ||
61 | 139728365 0 27290780 | ||
62 | 4161521920 2345099742 65970280 | ||
63 | 3549264451 2411070022 15588060 | ||
64 | 846553766 4012820620 62155872 | ||
65 | 3276913175 3215861697 30588309 | ||
66 | 7256118 139495191 27523954 | ||
67 | 3653026602 3908255344 104565276 | ||
68 | 2007806695 3246450006 21131889 | ||
69 | 426542603 2677155019 292403006 | ||
70 | 3265259672 4167732422 11653503 | ||
71 | 2959802238 3267581895 77570040 | ||
72 | 2766290955 2562319792 114835227 | ||
73 | 779480399 3352881787 3460872 | ||
74 | 718945609 995514267 33385697 | ||
75 | 908709638 1675494216 164534584 | ||
76 | 1547970680 1643941067 31553149 | ||
77 | 1579523829 2009572806 335526936 | ||
78 | 782941271 2969558025 63612495 | ||
79 | 752331306 465171569 27149093 | ||
80 | 237284175 1454682639 189258428 | ||
81 | 3757591878 3724399189 59699158 | ||
82 | 2881126182 3784098347 78676056 | ||
83 | 1520219770 2426658082 27750910 | ||
84 | 3564852511 1366508548 88174091 | ||
85 | 2383999106 3681454004 42945185 | ||
86 | 4287237444 3345151935 7729852 | ||
87 | 2242562254 3649815914 31638090 | ||
88 | 3307501484 753751300 241762967 | ||
89 | 34780072 27290780 104948293 | ||
90 | 2028938584 3033170520 182691177 | ||
91 | 3037372278 237284175 227887394 | ||
92 | 3817291036 3356342659 5821939 | ||
93 | 0 132239073 7256118 | ||
94 | 1360895538 1266240382 51601463 | ||
95 | 3823112975 492320662 230498145 | ||
96 | 2274200344 1840028800 109798762 | ||
97 | 4053611120 2454408992 107910800 | ||
98 | 2426944291 1317841845 48666703 | ||
99 | 1073244222 3362164598 287651316 | ||
100 | 1457977942 1203998554 62241828 | ||
101 | 2591192365 1028899964 175098590 | ||
102 | 1412497001 3862774403 45480941 | ||
103 | 2475610994 4179385925 115581371 | ||
104 | 4227492200 1949827562 59745244 | ||
105 | 1915050765 4074976492 92755930 | ||
106 | 2211629761 722818807 30932493 | ||
107 | |||
108 | light-to-temperature map: | ||
109 | 3741602262 2758947303 142653736 | ||
110 | 628739598 2901601039 50811783 | ||
111 | 1842260329 1084521599 145122645 | ||
112 | 2990409993 3493390513 390865485 | ||
113 | 4190333929 4159289690 83510514 | ||
114 | 984282519 2952412822 202948629 | ||
115 | 1826968660 3155361451 15291669 | ||
116 | 1329953386 2288178120 328046945 | ||
117 | 830513455 3304469747 74190775 | ||
118 | 1187231148 2616225065 142722238 | ||
119 | 2705943734 836690664 247830935 | ||
120 | 3381275478 1332397059 281233458 | ||
121 | 679551381 605080109 150962074 | ||
122 | 1987382974 756042183 44013157 | ||
123 | 4159289690 4263923057 31044239 | ||
124 | 1772730322 3250231409 54238338 | ||
125 | 2549609472 2131843858 156334262 | ||
126 | 1658000331 3378660522 114729991 | ||
127 | 298584178 195831363 330155420 | ||
128 | 3662508936 525986783 79093326 | ||
129 | 4273844443 4242800204 21122853 | ||
130 | 2953774669 800055340 36635324 | ||
131 | 2031396131 1613630517 518213341 | ||
132 | 265644560 162891745 32939618 | ||
133 | 162891745 1229644244 102752815 | ||
134 | 904704230 3170653120 79578289 | ||
135 | |||
136 | temperature-to-humidity map: | ||
137 | 671484955 1144907174 532089323 | ||
138 | 1414132335 1960778188 125717021 | ||
139 | 2631474761 2586973888 1058655511 | ||
140 | 1991131055 744338927 221864400 | ||
141 | 2212995455 192320896 28611061 | ||
142 | 192320896 2186552402 224875394 | ||
143 | 2241895588 966203327 169532208 | ||
144 | 1744764149 431490014 67442815 | ||
145 | 1812206964 2106058626 80493776 | ||
146 | 516655377 1135735535 9171639 | ||
147 | 1559412773 1775426812 185351376 | ||
148 | 2622156499 2577655626 9318262 | ||
149 | 1203574278 220931957 210558057 | ||
150 | 3690130272 4094120212 156346211 | ||
151 | 3946017132 3645629399 348950164 | ||
152 | 525827016 598680988 145657939 | ||
153 | 1892700740 1676996497 98430315 | ||
154 | 3846476483 3994579563 99540649 | ||
155 | 2577655626 4250466423 44500873 | ||
156 | 1539849356 2086495209 19563417 | ||
157 | 2241606516 598391916 289072 | ||
158 | 417196290 498932829 99459087 | ||
159 | |||
160 | humidity-to-location map: | ||
161 | 547577859 2546258172 54451455 | ||
162 | 2564186976 3913248498 28610653 | ||
163 | 2460249359 129990669 103937617 | ||
164 | 257798579 3257354132 21143365 | ||
165 | 511274864 3365252234 24536388 | ||
166 | 412475023 3389788622 98799841 | ||
167 | 2843712442 3615348771 251219053 | ||
168 | 0 2984989380 24111266 | ||
169 | 1074541266 4051128852 126947592 | ||
170 | 3109497668 265707418 9056683 | ||
171 | 3268495430 1450483293 18654108 | ||
172 | 1298820860 954974424 392849235 | ||
173 | 1691670095 1469137401 93027519 | ||
174 | 3886041222 2131196811 39611681 | ||
175 | 2797706800 1347823659 46005642 | ||
176 | 3767841776 233928286 19835810 | ||
177 | 743957513 253764096 11943322 | ||
178 | 4068806743 3941859151 109269701 | ||
179 | 2754107115 1910801576 16288912 | ||
180 | 1223331059 2244121482 75489801 | ||
181 | 3118554351 1393829301 56653992 | ||
182 | 3094931495 3009100646 14566173 | ||
183 | 3991994741 893524279 61450145 | ||
184 | 692486724 2192650693 51470789 | ||
185 | 1008564686 2952439802 32549578 | ||
186 | 535811252 3603582164 11766607 | ||
187 | 1784697614 2698363640 44335967 | ||
188 | 755900835 2353038285 193219887 | ||
189 | 278941944 1777268497 133533079 | ||
190 | 3925652903 3488588463 66341838 | ||
191 | 3558101581 2742699607 209740195 | ||
192 | 949120722 3305808270 59443964 | ||
193 | 2624116446 0 129990669 | ||
194 | 1939852817 274764101 520396542 | ||
195 | 3175208343 2037909724 93287087 | ||
196 | 3287149538 2600709627 97654013 | ||
197 | 650681177 1562164920 41805547 | ||
198 | 3787677586 795160643 98363636 | ||
199 | 1829033581 1927090488 110819236 | ||
200 | 4053444886 3897886641 15361857 | ||
201 | 1201488858 2170808492 21842201 | ||
202 | 3384803551 1603970467 173298030 | ||
203 | 2770396027 3278497497 27310773 | ||
204 | 1041114264 2319611283 33427002 | ||
205 | 602029314 3554930301 48651863 | ||
206 | 2592797629 3866567824 31318817 | ||
207 | 24111266 3023666819 233687313 | ||
diff --git a/2023/05/resources/input_small.txt b/2023/05/resources/input_small.txt new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/2023/05/resources/input_small.txt | |||
@@ -0,0 +1,33 @@ | |||
1 | seeds: 79 14 55 13 | ||
2 | |||
3 | seed-to-soil map: | ||
4 | 50 98 2 | ||
5 | 52 50 48 | ||
6 | |||
7 | soil-to-fertilizer map: | ||
8 | 0 15 37 | ||
9 | 37 52 2 | ||
10 | 39 0 15 | ||
11 | |||
12 | fertilizer-to-water map: | ||
13 | 49 53 8 | ||
14 | 0 11 42 | ||
15 | 42 0 7 | ||
16 | 57 7 4 | ||
17 | |||
18 | water-to-light map: | ||
19 | 88 18 7 | ||
20 | 18 25 70 | ||
21 | |||
22 | light-to-temperature map: | ||
23 | 45 77 23 | ||
24 | 81 45 19 | ||
25 | 68 64 13 | ||
26 | |||
27 | temperature-to-humidity map: | ||
28 | 0 69 1 | ||
29 | 1 0 69 | ||
30 | |||
31 | humidity-to-location map: | ||
32 | 60 56 37 | ||
33 | 56 93 4 | ||
diff --git a/2023/05/src/part1.cpp b/2023/05/src/part1.cpp new file mode 100644 index 0000000..d58c0cb --- /dev/null +++ b/2023/05/src/part1.cpp | |||
@@ -0,0 +1,84 @@ | |||
1 | #include <iostream> | ||
2 | #include <fstream> | ||
3 | #include <sstream> | ||
4 | #include <tuple> | ||
5 | #include <vector> | ||
6 | |||
7 | #include "util.h" | ||
8 | |||
9 | using Seeds = std::vector<long>; | ||
10 | using Step = std::vector<std::tuple<long,long,long>>; | ||
11 | using Almanac = std::vector<Step>; | ||
12 | |||
13 | constexpr char SEEDS[] = "seeds:"; | ||
14 | |||
15 | std::pair<Seeds,Almanac> parse_input() | ||
16 | { | ||
17 | Seeds seeds; | ||
18 | Almanac almanac; | ||
19 | |||
20 | std::ifstream input{ "resources/input.txt" }; | ||
21 | if (input.is_open()) | ||
22 | { | ||
23 | std::string line; | ||
24 | |||
25 | /* Seeds */ | ||
26 | long seed; | ||
27 | std::getline(input,line); | ||
28 | std::istringstream sline{ line }; | ||
29 | sline >> util::skip<SEEDS>; | ||
30 | while (sline >> seed) | ||
31 | { | ||
32 | seeds.push_back(seed); | ||
33 | } | ||
34 | |||
35 | /* Mappings */ | ||
36 | Step step; | ||
37 | long from, to, size; | ||
38 | while (not std::getline(input,line).eof()) | ||
39 | { | ||
40 | if (line.empty()) continue; | ||
41 | |||
42 | if (std::isdigit(line[0])) | ||
43 | { | ||
44 | std::istringstream sline{ line }; | ||
45 | sline >> to >> from >> size; | ||
46 | step.push_back(std::make_tuple(from, from + size, to - from)); | ||
47 | } | ||
48 | else if (not step.empty()) | ||
49 | { | ||
50 | almanac.push_back(std::move(step)); | ||
51 | step = {}; | ||
52 | } | ||
53 | } | ||
54 | almanac.push_back(std::move(step)); | ||
55 | } | ||
56 | input.close(); | ||
57 | |||
58 | return std::make_pair(std::move(seeds), std::move(almanac)); | ||
59 | } | ||
60 | |||
61 | int main(void) | ||
62 | { | ||
63 | Seeds seeds; | ||
64 | Almanac almanac; | ||
65 | std::tie(seeds, almanac) = parse_input(); | ||
66 | |||
67 | for (const auto& step : almanac) | ||
68 | { | ||
69 | for (auto& seed : seeds) | ||
70 | { | ||
71 | for (const auto& map : step) | ||
72 | { | ||
73 | if (std::get<0>(map) <= seed and seed < std::get<1>(map)) | ||
74 | { | ||
75 | seed += std::get<2>(map); | ||
76 | break; // mappings in a step don't overlap | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | std::cout << *std::min_element(seeds.begin(), seeds.end()) << std::endl; | ||
83 | return 0; | ||
84 | } | ||
diff --git a/2023/05/src/part2.cpp b/2023/05/src/part2.cpp new file mode 100644 index 0000000..89f887e --- /dev/null +++ b/2023/05/src/part2.cpp | |||
@@ -0,0 +1,142 @@ | |||
1 | #include <algorithm> | ||
2 | #include <iostream> | ||
3 | #include <fstream> | ||
4 | #include <sstream> | ||
5 | #include <set> | ||
6 | #include <tuple> | ||
7 | #include <vector> | ||
8 | |||
9 | #include "util.h" | ||
10 | |||
11 | using SeedRange = std::tuple<int,long,long>; | ||
12 | using Seeds = std::set<SeedRange>; | ||
13 | |||
14 | using Map = std::tuple<long,long,long>; | ||
15 | using Step = std::vector<Map>; | ||
16 | using Almanac = std::vector<Step>; | ||
17 | |||
18 | constexpr char SEEDS[] = "seeds:"; | ||
19 | |||
20 | std::pair<Seeds,Almanac> parse_input() | ||
21 | { | ||
22 | Seeds seeds; | ||
23 | Almanac almanac; | ||
24 | |||
25 | std::ifstream input{ "resources/input.txt" }; | ||
26 | if (input.is_open()) | ||
27 | { | ||
28 | std::string line; | ||
29 | long from, to, size; | ||
30 | |||
31 | /* Seeds */ | ||
32 | std::getline(input,line); | ||
33 | std::istringstream sline{ line }; | ||
34 | sline >> util::skip<SEEDS>; | ||
35 | while (sline >> from >> size) | ||
36 | { | ||
37 | seeds.insert(std::make_tuple(0, from, from + size)); | ||
38 | } | ||
39 | |||
40 | /* Mappings */ | ||
41 | Step step; | ||
42 | while (not std::getline(input,line).eof()) | ||
43 | { | ||
44 | if (line.empty()) continue; | ||
45 | |||
46 | if (std::isdigit(line[0])) | ||
47 | { | ||
48 | std::istringstream{ line } >> to >> from >> size; | ||
49 | step.push_back(std::make_tuple(from, from + size, to - from)); | ||
50 | } | ||
51 | else if (not step.empty()) | ||
52 | { | ||
53 | almanac.push_back(std::move(step)); | ||
54 | step = {}; | ||
55 | } | ||
56 | } | ||
57 | almanac.push_back(std::move(step)); | ||
58 | } | ||
59 | input.close(); | ||
60 | |||
61 | return std::make_pair(std::move(seeds), std::move(almanac)); | ||
62 | } | ||
63 | |||
64 | Seeds map_to_range(int step, const SeedRange& seed_range, const Map& map) | ||
65 | { | ||
66 | int s; | ||
67 | long rbegin, rend; | ||
68 | long mbegin, mend, diff; | ||
69 | std::tie(s, rbegin, rend) = seed_range; | ||
70 | std::tie(mbegin, mend, diff) = map; | ||
71 | |||
72 | if (mbegin <= rbegin) | ||
73 | { | ||
74 | if (mend >= rend) | ||
75 | { | ||
76 | return { | ||
77 | std::make_tuple(step + 1, rbegin + diff, rend + diff) | ||
78 | }; | ||
79 | } | ||
80 | else if (mend > rbegin) | ||
81 | { | ||
82 | return { | ||
83 | std::make_tuple(step + 1, rbegin + diff, mend + diff), | ||
84 | std::make_tuple(step, mend, rend), | ||
85 | }; | ||
86 | } | ||
87 | } | ||
88 | else if (mbegin < rend) | ||
89 | { | ||
90 | if (mend >= rend) | ||
91 | { | ||
92 | return { | ||
93 | std::make_tuple(step, rbegin, mbegin), | ||
94 | std::make_tuple(step + 1, mbegin + diff, rend + diff), | ||
95 | }; | ||
96 | } | ||
97 | else | ||
98 | { | ||
99 | return { | ||
100 | std::make_tuple(step, rbegin, mbegin), | ||
101 | std::make_tuple(step + 1, mbegin + diff, mend + diff), | ||
102 | std::make_tuple(step, mend, rend), | ||
103 | }; | ||
104 | } | ||
105 | } | ||
106 | return { seed_range }; | ||
107 | } | ||
108 | |||
109 | int main(void) | ||
110 | { | ||
111 | Seeds seeds; | ||
112 | Almanac almanac; | ||
113 | std::tie(seeds, almanac) = parse_input(); | ||
114 | |||
115 | for (int s = 0; s < almanac.size(); ++s) | ||
116 | { | ||
117 | const auto& step = almanac[s]; | ||
118 | for (const auto& map : step) | ||
119 | { | ||
120 | std::set<SeedRange> new_seeds; | ||
121 | for(auto& seed : seeds) | ||
122 | { | ||
123 | if (std::get<0>(seed) <= s) | ||
124 | { | ||
125 | auto res = map_to_range(s, seed, map); | ||
126 | new_seeds.insert(res.begin(), res.end()); | ||
127 | } | ||
128 | else | ||
129 | { | ||
130 | new_seeds.insert(seed); | ||
131 | } | ||
132 | } | ||
133 | seeds = std::move(new_seeds); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | auto min = std::min_element(seeds.cbegin(), seeds.cend(), | ||
138 | [](auto s1, auto s2) { return std::get<1>(s1) < std::get<1>(s2); }); | ||
139 | std::cout << std::get<1>(*min) << std::endl; | ||
140 | |||
141 | return 0; | ||
142 | } | ||