Solve day 7 of AoC 2024

This commit is contained in:
Ivan R. 2024-12-12 22:52:19 +05:00
parent 5affe5b40e
commit f6da5e2e12
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
9 changed files with 1049 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
advent-of-code/2024/day_07/.gitignore vendored Normal file
View file

@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
day_07-*.tar
# Temporary files, for example, from tests.
/tmp/

View file

@ -0,0 +1,64 @@
# Day 7: Bridge Repair
The Historians take you to a familiar rope bridge over a river in the middle of a jungle.
The Chief isn't on this side of the bridge, though; maybe he's on the other side?
When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.)
You won't be able to cross until it's fixed.
You ask how long it'll take; the engineers tell you that it only needs final calibrations,
but some young elephants were playing nearby and stole all the operators from their calibration equations!
They could finish the calibrations if only someone could determine which test values could possibly be produced
by placing any combination of operators into their calibration equations (your puzzle input).
For example:
```
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
```
Each line represents a single equation. The test value appears before the colon on each line;
it is your job to determine whether the remaining numbers can be combined with operators to produce the test value.
Operators are always evaluated left-to-right, not according to precedence rules.
Furthermore, numbers in the equations cannot be rearranged. Glancing into the jungle, you can see elephants holding
two different types of operators: add (+) and multiply (*).
Only three of the above equations can be made true by inserting operators:
- 190: 10 19 has only one position that accepts an operator: between 10 and 19. Choosing + would give 29, but choosing * would give the test value (10 * 19 = 190).
- 3267: 81 40 27 has two positions for operators. Of the four possible configurations of the operators, two cause the right side to match the test value: 81 + 40 * 27 and 81 * 40 + 27 both equal 3267 (when evaluated left-to-right)!
- 292: 11 6 16 20 can be solved in exactly one way: 11 + 6 * 16 + 20.
The engineers just need the total calibration result, which is the sum of the test values from just the equations that could possibly be true.
In the above example, the sum of the test values for the three equations listed above is 3749.
Determine which equations could possibly be true. What is their total calibration result?
## Part Two
The engineers seem concerned; the total calibration result you gave them is nowhere close to being within safety tolerances.
Just then, you spot your mistake: some well-hidden elephants are holding a third type of operator.
The concatenation operator (||) combines the digits from its left and right inputs into a single number.
For example, 12 || 345 would become 12345. All operators are still evaluated left-to-right.
Now, apart from the three equations that could be made true using only addition and multiplication,
the above example has three more equations that can be made true by inserting operators:
- 156: 15 6 can be made true through a single concatenation: 15 || 6 = 156.
- 7290: 6 8 6 15 can be made true using 6 * 8 || 6 * 15.
- 192: 17 8 14 can be made true using 17 || 8 + 14.
Adding up all six test values (the three that could be made before using only + and * plus the new three that can now be made by also using ||)
produces the new total calibration result of 11387.
Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result?

View file

@ -0,0 +1,850 @@
503516274925: 9 314 8 83 6 45 997 25
999: 45 25 5 645 4
15456974992821: 22 96 792 149 491 23
139006: 6 238 288 81 1 9
755796: 1 82 53 8 311 7 3 49 6 4
384013872: 79 4 1 2 9 16 902 12 6 9
465691: 98 365 44 8 13 5
34397: 68 12 6 7 75 50
54741: 88 85 63 1 7 4 9 255 7 8
1632542804: 5 6 2 6 5 5 358 4 8 688 4
217037623: 40 72 533 2 6
1187802343: 15 8 371 58 4 15 460 3
1008262: 3 734 3 27
548005: 4 91 47 9 7 32 37 1
14828: 14 99 95 4 11
3783474: 587 7 39 3 8 2 19 498
2667160: 14 9 3 67 162
1263787: 59 51 6 7 4
651186: 3 775 1 837
473524929: 8 4 66 9 436 11 27 9
2271915476: 699 8 330 759 6 31
25852199: 309 670 3 73 2 88
37608760: 625 2 6 387 1 7 6 7 680
812737: 34 42 90 437 3 7
9298739: 481 537 36 47
9382757: 85 9 79 24 2 5 1 3 4 6 87
22652160: 573 468 80 8 34
109201: 5 8 50 24 2 827 99
7246295424: 723 80 5 4 4 72 87
2303: 64 1 2 18
5341031: 7 12 792 1 3 6 820 6 3 1
415842953: 670 58 672 7 4 10 85 3
741: 94 9 2 6 9 490 2 8 4 2 8 6
365816301: 233 157 1 629 9
5125227: 46 97 832 968 2 633
9657162: 105 4 9 88 465 7 1 8 6 2
152416090: 1 980 9 6 4 9 15 8 6 2 1 8
1851971: 346 79 11 396 672
135144: 335 6 6 3 281 4 9
1792474: 86 5 234 9 7 677 474
51979269: 816 91 1 2 5 7
48627: 8 4 5 302 5 232 55 15
565772597: 9 962 4 470 580
66684: 64 2 8 1 7 2 179 1 8 5 2 4
2077169184: 4 1 564 3 5 4 1 8 71 6 9 2
688: 5 91 592
2684: 3 859 7 64 30 6
888: 5 866 4 6 7
54607217737: 925 546 59 3 2 4 4 9 7
17356045750: 4 6 9 6 4 243 299 83 6 6
7706910726435: 58 50 8 9 2 211 6 82 36
1455501866546: 6 5 4 8 9 3 2 883 8 7 5 43
295831040: 5 241 7 99 735 57 9 6
1167390723558: 8 6 9 6 7 8 8 16 9 3 55 8
308095772: 81 9 7 9 9 38 1 2 68 3 2 6
6184: 600 5 80 9 19
6982: 403 4 2 4 5 515 6
155288752: 77 5 89 55 375 2
199604: 99 794 8 1 2 1
46905372: 71 8 5 9 35 9 7 426 207
288012: 4 720 1 2
32643417: 660 634 64 78 105
5022593: 3 7 43 2 6 1 71 524 53
386670: 53 333 62 4 7
15883: 84 93 96 2 67
172339200: 8 46 6 80 6 96 1
784679: 3 44 2 96 677
166196: 683 27 25 9 2
23280: 52 467 45 5 2 4 703 8 9
595: 7 9 8 86 5
327764222884327: 667 9 78 6 7 2 8 843 25
4411: 2 22 7 8 5 80 5 3 4 6 873
2538232: 533 33 3 556 915 8
696764362: 7 82 54 165 21 89 171
31675: 2 4 7 5 2 54 1 7 2 355 2 7
49007459: 7 93 309 2 59
5783913: 865 4 5 12 8 816 97 1 8
277: 45 136 93
1788313636: 1 8 9 9 2 86 3 6 9 1 3 636
59740: 530 8 24 35 40
35328: 5 4 2 8 48
1525221525: 8 247 7 383 769
7533999509: 93 9 9 999 50 9
274501597440: 4 8 6 62 645 376 56
2610116: 1 96 9 184 57 67
134153: 7 63 6 3 2 7 26
424868: 9 9 236 33 35
411933: 292 6 5 23 6
223647426: 3 5 2 6 159 70 457 1 1 6
41194: 1 5 8 73 3 19 14 6
56917069: 1 942 480 235 58 4 8
15353604: 1 825 56 1 621 28
1430: 252 3 590 3 9 72
122411407: 53 25 975 1 31 5
1097569287928: 7 58 505 3 6 431 9 30
4461: 539 608 3 6 301 713
25394: 7 64 167 8 4 764
825126652: 89 5 849 6 2 2 26 24 7
154944: 9 753 843 9 96
2653891262: 4 48 87 344 5 8 1 8
2409350403: 20 4 30 1 955 876 4 3
133212294: 430 1 809 1 8 61 88 2 2
1014612833: 5 2 4 78 5 698 8 79 6 5 3
63663: 79 4 8 14 1
167573: 9 8 5 64 5 70
100912: 5 67 1 5 3 22 3 40 1
932526: 1 4 4 6 39 4 6 8 68 570 6
2194325: 8 3 5 6 6 3 7 16 9 37 35
1961026895296: 50 92 11 609 328 7
2055368: 9 5 1 1 68 1 8 5 289 7 8 1
7236: 780 1 9 6 5 4 4 17 1 170
846: 4 1 6 77 1 75
131382: 1 3 1 6 936 4 3 1 5 96 3 9
2583527660: 3 7 5 29 2 6 1 927 4 6 74
255304: 205 22 49 14 4
773591: 72 38 486 1 20 6 7
6666: 8 990 6 614 59 5
180134027548: 2 551 6 7 9 965 26 893
14953953098: 149 539 5 30 99
1960777: 5 1 7 56 8 789 9 9 8 3 8 4
1913224: 86 33 272 3 1 1 2 614
338070744: 8 5 7 5 7 3 692 9 3 23 8 3
46: 4 1 3 6 4
36851060: 756 31 496 9 62 7 460
23209760: 18 608 4 8 265
264112: 7 5 6 60 4 2 971
18162: 863 4 9 2 480 321
2802485: 6 969 24 4 3 40 7 229 9
739889196: 4 4 369 23 452
83354254274: 4 54 377 63 3 487
122067100: 2 328 805 369 56
6797737: 35 7 185 813 792
2830: 4 71 9 271 5
2436394611081: 993 1 44 678 5 49 1 8 1
982687: 491 282 2 62 61 2
4192127701923: 3 9 8 8 6 7 4 738 3 97 5
91146: 434 3 7 1 3
70528: 9 67 3 892 60
26604091381: 2 7 7 1 9 4 8 89 199 2 81
6150240: 4 3 4 3 9 2 8 1 91 75 915
59058234918: 84 79 8 7 647 7 4 918
201855402: 24 6 17 1 9 826 9
29483: 1 3 5 4 35 8 3
4629: 7 92 8 5 30 5 494
2749: 621 4 59 204 2
54250753: 6 22 6 546 5 1 6 4 7 1 6 7
118435: 2 1 2 4 9 9 6 807 8 2 9 9
68271336: 4 911 1 79 4 23 72 27 7
212592: 202 4 3 43 8
12947040: 23 800 8 7 62 1 7 9 216
9078083: 605 996 978 64 55 3
5355287: 86 822 9 584 3 4
12652: 4 81 39 9 7
2035: 3 453 5 548 517 509
5611918160: 269 7 72 289 5 59
26932269: 949 18 7 197 8 5
240011: 5 8 1 61 159 86 9 62
3411271: 261 4 29 45
24525043726: 77 700 456 739 455
94519353: 222 692 66 615 3
358029672: 2 8 45 802 8 9 4 248 8
1275858: 7 9 28 339 9 4 6 80 1 3 6
1791397506: 5 5 5 625 9 873 5 1 30 6
3365: 8 25 9 79 48
261: 3 9 6 60 97 32
222995252: 9 5 7 6 61 1 6 2 462 8 4 9
261898: 19 50 920 7 1 91
30184: 2 5 5 259 645 1 16 4 8
3847: 2 70 31 7 4 9 22 5 37 30
13080858: 215 2 11 5 4 79 9 7 8 4 7
717: 63 2 5 9 8
80971966678876: 5 153 74 3 218 759 69
480732: 8 7 29 39 6 1 963 195
450048608: 6 329 1 3 2 79 31 30 8
6093024751: 4 767 62 2 3 6 71 3 3
226564332: 472 48 426 7 2
48982: 237 420 7 299 2
4440: 7 6 4 7 3 5 7 5 6 618 1 3
580189477005: 1 755 9 413 33 4 35 23
646758: 947 65 5 421 118
93690470793: 6 3 289 3 5 12 1 4 3 9 9 6
25976449: 129 88 2 3 2 9 1 6 36
6870096: 6 830 1 76 9 12
1164172: 9 467 96 9 4 3 2 7 6 8 1 4
111844711261: 3 11 587 740 25 7 20 9
572421521: 20 24 13 364 57 520
94739474: 1 1 81 637 9 543 3 9 92
48296772777: 727 142 82 2 81 974
3228386: 4 10 1 2 5 4 8 8
2735400: 8 88 2 33 37 29 8 47 25
6845918: 95 5 5 1 5 9 31 2 9 6 926
5284: 9 86 532 15 4
766: 7 8 45 66 1 24
22616142: 6 707 9 8 55 1 32 39 1 2
3951879: 35 7 8 7 457 1 8 688 2 7
41179: 402 3 3 946
7867122361: 2 841 147 563 34 1 43
1366266: 245 7 7 6 897 119 27
16647867: 7 900 757 78 67
105950: 9 56 899 95 50 1
195377030370: 4 33 37 42 57 5 93 4 1
140132793856: 5 1 901 1 9 3 9 30 1 8 5 6
27881717: 2 788 17 1 7
56485800: 150 59 26 6 5 79 700 9
90644: 50 42 2 6 43
2845403: 59 82 1 196 3 71
1980594: 58 488 883 231 6
572298805: 2 9 1 4 49 5 82 8 7 9 11
3274949796144: 74 155 6 32 23 6 6 1 47
677711: 66 9 1 8 712
85322326: 462 342 36 3 8 5 126
6385506: 6 68 6 8 3 7 60 5 9 936 7
1864498: 1 4 401 1 82 8 7 1 66 80
4519368: 8 7 427 3 63
24: 5 5 7 5 2
8093: 4 4 18 5 3
361285303: 715 934 541 85 8
39897000: 590 812 309 5 930 25
208599160: 192 28 917 11 94
69387137: 248 7 80 69 53 39 16
59784: 7 8 558 94
1014910171872: 1 4 83 2 905 5 8 7 3 4 3 2
4973794: 680 83 931 189 7
164008: 279 7 22 1 83
426007: 4 76 772 500 7
1264772: 94 437 88 97 9 3 497
777423: 492 340 6 2 61 42 437
3549229385: 6 472 21 7 15 7 7 505 2
2027404: 452 46 512 731 2 92
988526: 6 5 66 98 77
4281018631074: 6 7 632 28 4 8 4 3 9 2 6 9
2159708869: 306 9 4 2 6 60 392 549
916: 230 8 678
183600: 583 17 17 9 2
443520010: 110 6 840 8 13
1475473384457: 59 25 473 384 460
160741089: 50 8 4 6 1 410 92
2035920165: 427 83 8 499 16 5
60345625: 75 449 448 4 28
29720: 12 208 6 470 10
7165534: 642 186 2 27 30 4
150039126: 44 54 46 43 5 86 9
378059: 434 1 15 842 1
250361457: 4 287 80 9 73
6138: 43 8 8 96 1
520: 64 1 8
420328: 211 498 4 4
9678: 6 8 8 683 136 7
162317: 29 2 5 557 787
326910: 5 7 2 4 6 623 3 5 31 6 9 1
10966656356: 78 33 324 94 103 14
1621452509: 7 9 89 8 6 611 2 9 2 5 9 2
672831369: 8 3 1 9 564 76 6 4 7 370
177892638: 417 158 27 3 1 4 1 34 8
59318052778: 1 9 7 5 892 52 766 4 6 2
9394135: 2 19 125 19 19 187 4
35557288: 83 199 2 3 42 86 5
432902: 322 5 3 34 89 6 1
1891059124: 2 3 67 8 4 11 5 68 6 5 68
3321927963623: 6 641 1 27 5 592 725 5
4102092: 2 8 4 2 8 2 3 819 16 6 4 3
5739: 633 186 976 3 354
231062629: 794 8 2 6 24 38 6
1402640: 9 962 81 2 20 2
114056: 44 232 59 9 7 5
148014: 96 256 75 9 2 1 3 47 6 1
35590816: 541 286 742 58 44
41690470: 758 110 38 56 5
24812752: 37 2 3 6 78 3 1 3 4 3 7 52
3138: 4 33 21 893 3
4022973: 804 58 2 5 9 5 9 39
186733343: 9 9 673 33 45
22788366272: 45 9 69 809 9 8 7 21 8 2
48666984: 77 8 58 4 681 2
60071291360: 4 2 9 7 7 47 2 616 13 9 9
10886433370: 6 2 840 9 1 8 6 9 912 4 3
60741361249: 65 567 25 549 41 7 3
7749672: 926 8 4 62 2 328 978
5412196: 44 615 98 1 2
8436139761: 674 19 43 1 291
1799720919: 6 1 2 72 17 1 8 6 73 303
22059: 1 54 13 4 9
2761: 6 52 2 6 7 80 7 770 6 5 2
289388465: 30 1 442 2 4 4 646 6 77
1028: 5 5 2 5 4
582074: 3 6 4 5 41 5 2 766 6 8 3 2
72626799: 38 22 4 2 5 18 482 4
2242287: 88 136 22 8 7
234: 9 52 93 47 33
6296935814: 13 9 84 753 41 23 26
122472761: 40 22 66 1 36 6 9 1 139
21168: 1 3 2 2 12 2 108 2
294: 41 4 54 1 7 69
66716829922: 4 7 9 1 7 9 1 17 8 2 992 4
2039681: 251 8 3 313 8 1
811841690: 8 2 99 41 690
120714155: 8 57 8 637 96 3 7 99
404736: 75 55 6 32 93
27832168: 691 959 6 9 1 7
1651202: 2 692 34 97 2 6 617 33
79796285: 51 701 558 4 53
790308: 3 52 281 70 18
162662144: 783 57 23 834 2 226
82139624: 5 596 5 689 889 464 8
1522241: 15 1 359 858 6 1 8
131133: 80 6 99 708 87 66
662903: 507 3 4 8 6 394 2 94 6 9
7568405081: 85 742 12 50 7 8
39526830: 5 168 32 9 49 30 1
4337378891: 481 23 3 6 45 9 7 4 671
25949169: 463 30 731 1 5 6 9 8 9 9
59860555220478: 532 8 6 8 4 602 40 584
540788639: 54 10 788 63 8
2043084: 7 59 8 53 8 9 7 8 9 1 6 89
13630963923465: 7 47 93 495 47 9 465
18083187849305: 30 3 817 31 192 930 3
5748924927: 66 2 3 78 8 4 61 1 2 926
1358017933: 7 353 726 757 811
2246226: 26 919 2 94 1
5868735036: 8 1 3 154 3 7 8 7 7 9 9
45783834: 643 6 99 8 410 7 89 9
10308: 1 7 8 3 727 3 7 655 4
12233357: 27 175 9 1 45 1 10 4
104528159: 81 163 91 87 8
613974: 9 6 56 203 94 2 6
38690663936: 8 4 9 1 7 84 3 77 70 3 4 2
114677: 3 46 3 4 44 87 1 33 5 2
9826905: 341 88 50 287 597
829657: 452 38 6 651 8
424815: 94 4 5 223 7 139 4 30 5
59389041475: 3 5 7 7 6 9 9 7 791 6 69 4
172: 9 8 1 26 4 73
16358: 768 666 53 11 1
330997: 979 4 1 8 7 1 9 8 7 36 6 7
5417685: 3 9 9 63 5 49
8618: 3 1 7 3 1 9 1 1 7 3 5 3
877726: 3 315 928 1 765
1601327616: 4 4 6 26 468 1 1 94 13
2197638149907: 4 8 7 1 67 3 81 49 83 7 6
268380: 2 38 3 7 869 65 3 1 9 5 4
2119834: 5 48 1 84 1 8 815 834
58660: 1 5 549 3 84 28 56 32 4
35571484: 8 13 28 460 85 81 6
181504804: 4 19 148 96 16 7 9 2 9 5
955: 7 90 3 7 84 5
51337: 1 5 439 33 1 8 2 353 6
9532789: 362 6 8 7 580 2 78 92 5
28459225: 1 171 446 1 176 25
620276408: 392 58 25 79 8 9
816480498239: 240 9 675 8 89 7 56 7
217: 11 71 135
231322296303: 933 92 68 6 11 6 75 68
192956202: 1 245 911 861 936
80650542: 3 566 588 7 10 72 66 7
968607515: 7 5 897 210 739 6 15 7
1752813: 93 8 890 950 3 3 57 9
5440597: 2 37 3 465 99
1159489: 8 12 4 94 4 135 2 3 58 9
2649588029: 6 87 499 77 228 1 5 5 4
51782216: 6 5 7 3 172 4 56 9 8 8 8 1
31519206: 78 30 6 1 8 6 8 80 6 48 9
3057038372: 288 17 703 8 37 1 2
8648058: 118 741 559 265 3 5 6
556: 456 16 1 4 8 2 3 58 5 4
215155: 7 14 43 8 53
1622634: 2 4 9 28 1 3 18 4 49 8 8 3
24381: 8 398 2 1 8 2 2 3 7 188 9
1480032: 9 62 5 1 4 4 494 7 3 571
952035798: 400 8 47 6 633
147750925: 1 460 641 85 5
190: 2 57 76
57878584: 5 7 260 1 8 706 9
3547396263: 66 932 53 21 2 9 24
135214298: 73 900 42 3 8 7 450 7 7
14944568: 4 9 4 3 4 4 4 4 56 5 2 9
412856: 483 681 6 59 800
9508: 328 9 5 45 1 8 3 2 1 4
141354: 4 2 8 13 54
5328026674: 9 84 638 4 3 569 7
11500910: 45 2 3 6 597 4 2 7 43 1
104905: 6 99 525 10 9 84 494
201182: 459 499 210
207002978: 115 90 1 2 978
18576316: 3 17 586 41 78
910278: 232 447 2 515 486
765230: 8 39 9 955 4 622 41 4 6
7262: 82 76 11 633 1 86 299
19858280: 8 76 4 1 810 4 2 731 8 5
5515699: 359 4 89 75 9 4 857 47
620162191: 337 3 629 64 172 47 2
118078596: 7 229 15 7 5 93
19685081: 44 16 1 2 4 2 10 55 8 5 8
81562: 4 4 66 7 7 9 9 9 2 4 3 5
210651: 9 6 7 4 81 53
549095: 8 52 6 3 56 448 9 9 662
19726578: 36 21 6 7 7 955 1 67 4 9
187405929: 9 35 515 192 4 611 3 9
290136: 6 176 111 77 112 8 7
4709255004: 58 8 513 1 2 7 59 7 3 4
485146080: 75 23 54 78 3 398
329901573640: 5 9 3 4 70 4 944 7 8 3 7
1477985: 6 192 33 914 7 4 43
2020375: 6 22 5 1 8 9 4 4 84 1 7 1
847224518112: 807 8 51 84 7 89 622 4
28701386: 336 3 91 67 261
741813594: 38 382 9 511 4
65525811: 16 6 4 9 6 2 6 3 96 81 1
942200280: 36 700 6 89 7 60
57133481663: 57 1 33 481 663
1345469: 206 8 5 72 182 9 527
4764600: 7 77 1 1 1 7 567 2 1 2 3 8
1175505: 8 172 4 983 34 808 8 7
334768: 1 438 1 14 739
303910: 9 1 1 8 6 7 9 3 629 9 8 2
68537: 6 7 90 11 8 9 6 3 5 29 6 4
3953: 3 9 56
3726320: 90 40 999 9 2 9 90 4 8
259479360: 424 2 2 3 3 9 3 6 5 844 8
51764994: 951 9 7 2 36 2 27 6
11506: 7 8 6 24 7 6 9 45 5 4 86
1408246243: 4 3 1 3 19 5 1 60 611 2 1
831958: 45 9 549 872 43 27
75609177: 14 1 4 135 88 82 294
11196208: 1 302 57 7 6 54 3 29 2
10486268688: 4 1 5 3 5 645 69 85 7 84
783700: 6 863 901 561 170
10233: 215 39 734 149 9
99267640: 456 511 4 6 8 7 71 55
6154106425: 1 2 834 4 6 9 7 6 562 31
11898774723439: 82 701 90 31 454 23
3476: 8 7 491 322 4 1
7394399: 79 312 30 1 1
50681: 5 6 22 12 9 40 3 70 1
3886740: 190 6 37 370 2
7757958: 36 2 5 579 1 55 2
42278: 49 859 150 37 1
27700: 481 8 3 3 7 17 620 80 5
501630: 66 76 28
2353086: 9 310 43 318 5 6 8 6 93
115593: 46 83 9 830 954 99
662402: 77 97 1 6 10 471 404
7433496: 482 262 628 86 63
26718687652: 1 3 483 745 95 3 8 24 4
2622329: 8 9 2 1 291 5 6 830 1 4 5
21822622642: 483 97 3 88 9 8 470
226201508: 61 5 4 4 52 98 9 5 52
14556015: 7 4 183 209 359 1
4507340: 1 9 1 7 30 7 33 72 3 65
493978: 25 97 73 6 5 3 567 4 61
2299920307: 9 8 95 5 85 611 7 113 6
6664: 4 3 61 490 3 8 1 2 19 2 7
528034: 11 8 9 3 7 49 1 19 3 100
883072: 30 43 14 46 84 72
217158636592: 88 628 7 1 5 89 7 7 7 4
456622173: 4 14 99 8 7 76 9 8 18 3
50397: 1 8 5 3 856 2 9 19 3 9
74139: 7 3 95 9 71 7 8
19347: 1 70 76 9 4 7 8 427 8 2 9
36183472: 7 284 395 52 292
2670660: 664 4 5 69 4 9 6 2 5 8 2
1243274157218: 9 145 33 7 2 8 6 91 336
2001719392: 208 8 94 1 9 917 92
7711836990966: 986 2 6 970 5 78 9 61 5
6735456: 7 2 8 633 63 1 3 39 4 56
2690092: 6 787 4 4 2 670 5 5 5 12
15724: 1 4 63 7 85 8 932
1407379: 438 9 357 1 84
2556820932: 4 854 828 61 84 59
2136243: 710 3 6 243
12226491: 96 579 683 499 9
1240218850: 7 3 4 16 7 3 8 5 5 9 5 170
2730: 6 49 9 4 76 4
123055884: 3 101 9 4 5 82 5 855 3 9
10108067: 266 7 65 726 95 66
492474: 2 785 46 6 3 150 5 5
241011: 709 8 42 8 99
116965045: 5 10 460 3 6 8 6 3 4 7 95
17782497: 8 75 5 88 51 8 9 6 1 8 81
1697329: 3 166 73 24 5
221441: 1 7 370 71 900 1 2 9 8 9
266044: 665 4 4 25 44
952177: 413 93 6 23 1
7586258: 4 1 3 3 3 6 3 47 286 5 8
54577938: 9 225 8 9 31 7 1 1 3 334
13583: 3 796 17
81111: 1 51 2 1 656 3 6 11 57 7
265381: 188 64 3 2 8 14 79 83
19589339: 7 89 387 810 6 86 947
8554: 6 75 5 4 399
978443926: 3 9 38 8 98 4 345 1 7 4 9
294479514: 1 9 2 975 60 51 95 411
7244048: 1 3 1 769 471 5 67 1
1977857: 56 5 82 2 8 6 3 9 496 1 5
2524389840: 179 4 595 552 6 7
609: 3 4 86 7
244611: 20 3 1 461 1
468375308835: 58 841 11 92 796
1796950306: 19 9 27 4 838 50 306
435875: 163 28 3 19 7 8 7 4 4 8 5
945: 8 60 82
8575159: 98 1 22 873 553
45725: 4 6 7 2 13 9 9 971 1 5 8 5
314609430: 42 861 29 300 30
187488: 6 282 9 20 2 4 2 7 2 9 6
713462: 9 79 494 979 989
42672794724: 6 35 8 5 473 4 7 6 8 4
19129110: 29 4 6 1 2 143 65 49
17263540: 44 636 1 4 3 66 7 883 7
3050879: 3 4 33 3 8 823 7 8
92525: 36 7 61 6 1 61 14 212 5
44576633: 402 7 40 5 396 893
18465: 6 11 6 181 3
198011778: 2 8 2 788 5 6 4 83 7 9 3 3
1315167: 3 3 7 151 67
88616232: 9 5 3 6 441 49 16 7 1 89
18180: 6 68 8 2 38 2 59 60
29757054381: 9 70 1 17 426 2 72
12704125: 30 7 8 9 354 6
2887873: 8 8 850 5 3 2 1 446 3 7 2
1366446: 6 83 2 3 8 6 6
5765335843: 88 479 9 6 724 76 3
277868: 29 6 9 3 7 96 3 6 3 5
9914: 509 518 9 582 90
71231084: 585 898 98 8 7 6
137766489769: 34 3 6 21 8 551 45 4 5
6147470: 76 843 8 2 9
2678328244: 9 4 91 99 6 60 4 964 1 6
244: 8 4 95 137
20610048: 1 92 97 44 27 2 220 4 1
3736617: 92 6 364 4 253 104 1
270279903: 741 6 89 2 775 36
1902000: 6 304 7 2 40 5 3 5
4712267: 8 7 92 6 569 9 47 891
5204: 4 8 2 3 45 9 9 27 759 6 8
759015: 6 3 5 87 7 2 90 39 7 848
896813617: 97 3 3 9 9 9 2 6 6 7 547 6
7822: 8 22 44 78
39939244086: 5 8 335 4 2 638 97 9 4
1230: 78 37 1 8 10
1096017489: 72 6 3 3 942 7 4 91 171
116678: 5 233 1 73 4
160388: 1 313 512 1 131
164448789574: 65 8 5 47 2 6 3 9 3 459 3
1063936: 37 2 3 1 9 6 5 1 9 76 775
53132559: 90 12 7 52 940
755254858: 7 5 8 59 545 76 281
2234356919011: 3 2 2 43 8 761 556 7 9 4
2121417: 61 373 181 3 51 9
381591: 67 8 25 6 673
8819132976: 54 3 68 1 6 9 159 3 2 8
2556: 5 8 128 143 9
129948: 180 5 449 3 9 68
31267993680: 75 358 7 4 495 588
1495697: 80 41 6 31 71
3791: 5 3 2 40 511
89977578: 62 533 714 164 151
321811265: 4 90 694 5 906 6 3 5 30
2202264: 18 745 1 6 61 7 9 292
228040: 30 116 13 368 5
441916: 4 9 9 4 9 9 16
34478: 677 8 5 570 2 18
479918517002: 16 722 327 9 18 650 3
1111395: 2 708 47 6 9 791 72 3
1051590893: 8 32 657 5 4 760 4 49 1
403654115: 162 640 440 65 823 5
551939995: 511 12 6 666 9
48125762: 4 2 8 5 911 78 32 1
155118270: 328 30 9 7 553 25 2 2 5
195929415: 1 399 99 124 8 9 882 5
93312359129: 3 6 662 3 9 8 1 8 5 5 5 75
8317764: 6 782 1 9 7 14 79 9 2 9 7
70199168: 9 59 17 2 862 64
199: 91 11 97
40713835: 7 70 94 9 125 17 5
4804067610: 788 6 41 3 4 254
443923336: 463 91 90 4 8 93 43
29206033927: 5 9 6 648 2 706 38 7
253866: 33 3 68 3 416 51 87
6694800: 750 47 56 150
3202: 8 4 9 3 103 730
132656121360: 7 85 19 1 8 5 5 97 9 42 8
24352278: 3 431 561 7 31 6 4 75
2396712: 91 45 476 40 37
402942728: 402 94 27 2 8
562: 18 2 42 475 9
3978788000125: 1 9 8 9 39 39 8 2 5 4 1 24
2946: 894 541 7 31 2
11019730827: 4 9 6 362 7 4 9 32 7 5 3 8
51625: 3 75 228 259 56 7
2616000: 8 8 1 2 16 4 89 17 4 9 3 8
238476846386: 2 7 1 6 710 3 29 622 5
696570: 27 8 215 13 21 2 749
4244053945: 659 4 4 8 4 268 9 6 2 2 6
11856: 8 13 507 3 6 760 7 86 5
6286: 8 1 50 5 80 5 1 177 7
745206: 281 6 21 76 4 3 7
179170: 2 943 19 1 5
93741: 54 544 60 675 6 70 6 5
1541535: 2 8 55 1 535
7784184: 767 1 96 945 87
527408412903: 9 71 977 80 39 9 167
14445336: 351 3 731 176 45 46 2
487272188: 9 938 6 3 7 5 7 7 99 6 1 8
3280685: 7 1 5 9 99 6 4 3 1 28 8 5
176745622: 96 57 646 2 2 1 5
30125: 60 6 5 91 68 4 8 1 7 8
85298610476: 94 37 36 844 57 847 6
1307051: 233 1 1 3 8 5 25 7 9 14
137979: 1 31 7 62 79 1
47462976: 7 8 56 553 8 2 33 8 4 9 1
754844698: 752 5 14 1 4 5 18 6 698
72111: 3 7 78 44 28 1 3 7
4508256030: 449 1 824 88 72 30
364280231: 603 863 10 7 95 837
3965160: 8 8 4 328 511 4 9 35 3 1
7644726: 537 2 9 62 3 162 1 513
89236397251: 8 219 25 770 141
41473455: 70 45 6 60 1 73 456
8139142603: 2 84 1 2 468 9 4 92 7 5 3
289561268303: 59 4 6 2 1 732 9 3 77 48
316135053: 13 6 855 77 3 1
131160: 4 197 9 7 7 8 55 6 9 4 5
20264832: 22 988 67 42 448
1047875584: 8 1 56 7 736 454
31317451619: 1 933 6 6 2 555 5 42 2
9684: 83 6 19 192 573
159920: 3 5 7 2 39 4 9 8 504 3
28810503200: 7 107 9 2 56 9 2 4 53 4 5
1522842: 7 6 138 6 1 979 6
8678: 8 5 3 8 18 1 1 2 27 679 7
84382: 4 40 17 633 661 6 62
485993799469: 449 994 2 135 79 34 8
31962572: 249 8 21 3 33 96 947 4
3099348: 1 94 18 58 4 28 441
365212: 866 47 5 8 9
79695525: 8 31 547 1 249 869 15
4159339: 5 6 9 3 7 5 36 2 38 2 617
575: 8 49 7
6802299: 1 3 39 9 93 3 9 7
2641: 982 4 806 849
170124: 347 61 2 193 4 4
68381369840: 6 67 4 63 110 5 8 434 7
75088640: 335 6 311 1 220
213: 49 1 80 4 17 4 58
5871618032: 302 7 3 7 952 240 646
5000502164: 91 4 7 662 303 76 39 8
41648: 25 9 443 87 8 141
161622932: 654 233 257 709 1
154450181861: 6 715 2 8 36 6 4 1 8 5 3 8
1664: 1 3 6 1 8 9 795 805 15 8
206617: 8 1 258 38 177
61799810613: 2 7 6 29 442 6 14 8 7 3 3
307: 5 94 3 3 7
50722: 1 13 5 3 689 425
3462120: 6 82 8 6 3 6 8 748 1 8 8 8
12071655: 299 287 206 55
308113457677: 79 39 13 457 679
2806671008: 3 5 2 209 6 9 9 10 7 30 8
718569388: 7 7 646 157 5 6 69 6 7 4
309226: 2 65 19 910 292
45836: 3 6 3 3 45 8 3 21 2 2 2 6
10238605: 8 2 6 39 5 48 3 4 871 1
540929791: 657 1 98 9 3 4 4 7 8 5 1 7
1819180440: 8 5 21 6 812 5 2 803
4680361654: 371 6 5 5 3 97 867 7
496333577: 2 95 40 5 624 94 3 41
1224: 3 3 1 3 2 6 5 61 9 265 2 3
2359823: 831 4 2 5 1 3 3 47 2 3 47
43895: 14 1 39 75 20
1232405374023: 4 277 70 215 3 2 670 3
308211169: 4 69 3 2 6 2 7 1 8 6 493 7
966: 5 2 4 9 226 7 590 2 1 8 7
5606560759: 5 3 129 8 6 5 8 2 67 759
1925379094485: 90 5 129 9 758 9 8 627
54389794191: 2 406 1 669 941 9 1 1
70005469: 942 17 457 56 739
122958: 3 8 4 8 29 725 510 92
31351050: 4 258 45 6 675
48886: 288 2 84 502
10348: 41 2 22 923 361
199756: 74 8 5 42 914 32 4 972
20140882: 610 61 3 628 3
6885: 6 8 4 26 3 4 77 2 8 8 9 5
151119936: 253 4 2 488 17 9
32732547: 6 4 6 3 2 2 5 28 249 2 2
121176900: 374 8 4 5 3 75 97 3 9
3027367: 8 9 7 3 8 5 8 8 204 1 5 7
34496056: 1 1 96 9 8 984 6 50 856
65882880: 1 2 379 30 9 8 19 344 3
5615124: 4 7 3 453 927 3 4
8526656770: 698 26 839 8 56
43959805: 5 86 75 9 760 3 42
6122221444: 612 221 20 94 42 3
3800861: 4 557 78 2 82
6497422: 21 9 5 9 84 361 80 74
452465: 9 6 3 2 5 7 11 3 4 92 7 2
3580700: 3 1 34 10 540 61 25 4
1062440442057: 8 2 5 1 42 2 475 56 1 57
390236066076700: 92 88 1 83 94 350 482
5100: 4 9 78 6 5
2726331102: 791 2 966 8 4 223
143421: 72 3 664
10399: 73 1 3 7 2
618197: 4 3 574 4 266 7 6
5708930220: 120 1 1 147 786 5 81
1071766382: 8 931 6 7 8 7 1 10 8 1 4 5
39849259: 4 6 60 1 79 1 6 6 3 350 9
1725025: 7 70 69 51 715
8642774: 815 5 487 69 7 79
3853: 9 632 6 7
64571316: 56 8 571 316
616927080: 1 576 319 924 746
1438: 6 8 22 87 48 581 414
4616555876: 794 5 879 581 177
2275229: 31 9 629 56 5
82766: 2 7 3 7 4 15 8 14 2 839 2
2544570: 2 1 9 8 6 9 97 2 88 48 6 5
57285750: 47 487 25 30 25
5885217807300: 972 9 5 500 3 90 5 598
164897: 9 4 43 27 898
33678972736: 1 5 877 3 39 49 64
2359840: 45 73 6 2 86
106395: 94 3 9 39 8
41061279: 6 5 5 4 2 5 3 8 2 4 33 569
2359224198: 3 7 4 35 3 9 7 7 9 188 4 6
113433: 1 54 8 57 88
19804474: 7 8 3 9 9 11 7 3 373 4 1 3
1740960: 8 364 5 936
6107: 7 1 9 68 9 1 9 4 6 3 950
239324643: 974 1 230 83 6 8 4 6 4 3
50780262: 7 6 1 2 1 38 2 960 4 9 8 6
5647983: 5 57 50 31 93
63144: 420 443 2 12 9 8
91212709: 46 6 80 9 51 9 8 9 54 22
55561: 3 92 544 59 274 66
20184719: 40 826 39 9 6 60 32 61
3995: 242 6 253 2 53 8 9 515
923198470: 158 317 87 5 5 194
747: 400 17 316 6 8
48659373: 407 369 3 12 9 9
1953: 4 89 3 1 7
104266695: 91 24 115 4 2 7 443 51
78870: 7 2 2 84 38 33
5629698: 6 473 85 3 642
1539748: 298 735 934 7
2092818: 7 838 85 4 113 7 26 3
244301: 2 534 64 94 92
456120: 661 23 1 6 5
15278572: 891 2 5 181 94
968968364: 529 8 629 73 364
221377755: 6 356 20 8 7 434
305430: 4 44 580 404 6
1009175131: 91 6 463 175 132
1254388516: 2 5 793 9 584 76 5 9 1 8
5086841824: 770 7 33 61 66
1488169274: 8 8 36 7 8 6 52 6 35 3 71
2200: 62 7 6 1 5
111640120704: 974 415 2 2 274 8 9 7
6098049041: 8 6 54 6 2 56 8 276 765
1126667169: 72 326 231 48 78
426: 2 208 7 3 1
6038385312: 89 1 73 4 8 88 7 8 502 3
11543: 175 375 20 4 539
16453949: 39 25 9 455 56 4 32
2070421: 641 6 8 4 21
15782: 3 8 5 1 3 4 8 9 6 88 26
2346: 3 2 9 3 7 74 6 4 8
4426435: 6 816 226 2 4 84 359
127359829066: 2 16 304 736 2 421 3 8
33924376: 5 1 4 9 148 22 6 27 7 3 6
4878607725: 48 78 607 7 25
11704911754: 29 3 2 34 8 8 7 9 1 1 751
5781418: 973 3 594 14
1599411: 6 2 5 818 2 6 7 44 36 3 3
35080351: 676 62 31 27 3 3
63052996402: 5 474 4 38 5 7 640 5
21192: 686 5 1 5 96 6
3899: 469 41 16 7 92 125
6338195: 501 2 5 3 3 7 1 7 9 5 76 4
1989: 7 427 50 4 53
3583: 17 1 2 9 153
11496418: 1 7 74 9 69 54 9 15 5 5 8
6681: 9 1 3 73 17 2 646 352 8
163587610: 2 381 5 18 1 1 7 680 1 8
56802687708: 7 2 4 668 64 250 618 6
2764745: 6 3 1 7 7 71 1 1 6 69 7 53
61589110: 61 54 4 911 2
980929619: 846 970 53 50 6 9 719
12001: 421 238 5 833 4 9 1 2 5
11984293548: 3 7 6 4 9 492 9 8 9 4 102
2576400: 6 8 1 3 1 3 6 445 8 5 114
39247: 3 543 3 8 59 92
19888723: 2 994 8 720 3
208913040: 2 818 337 126 6
3658907: 2 70 95 890 9
13899: 71 4 3 5 1 3 89 4 9 65 8 2
4812: 6 9 72 920 5
39834: 25 20 224 11 3 5
16753825: 98 73 130 739 9 18 1
5025815: 5 7 5 8 6 52 7 9 7 8 5 516
57909860: 3 551 37 1 2 62 92 961
74133: 24 8 1 6 48 10 3
187412772600: 60 52 710 81 940 5 2 9
96688: 318 16 4 286 2 18
8342: 1 20 1 55 9 97
101549671: 3 936 76 496 71
547473881: 5 3 20 9 58 92 7 38 7 8
179229334: 1 698 53 931 256 662
441290: 57 98 3 949 5
1480836: 13 8 5 8 8 839 7 62 78 6
48544: 2 1 6 24 3 645 2 7 6 8 1 1
9538811346: 7 156 8 898 85 81 86
1680092: 50 400 84 91 1
9949559436: 57 2 501 850 3 6 22 36
15344423037: 1 5 8 9 5 665 262 2 9 8 1
48581: 29 5 17 534 82 609
1547: 34 51 2 3 72 74 85 3 71
1184465530: 1 4 8 2 6 6 89 3 1 5 2 8
536939381: 7 767 39 31 2 69
3216745: 72 85 81 94 511
2058711594: 6 8 7 5 457 79 3 8 2 9 39
1992211259: 158 2 549 7 9 72 2 5 49
6408597572: 6 1 634 85 975 75
1262184680: 7 6 1 683 4 75 1 8 5 1 4 2
3396: 3 8 9 34 563 323 3 576
40048: 9 9 8 7 88 6 1 2 9 7 69 4
6295300: 78 8 14 948 6 3 69 1 9 8
2570661181437: 8 1 14 46 11 88 576 5
11230549368122: 713 99 991 43 37 24
200254: 348 82 71 7 2
14205: 1 5 3 46 7 5 8 433 7 2 4 1
602616: 3 559 60 4 8 80 844
188713547: 7 43 255 90 4 37 230
1632052: 9 4 6 2 85 42 7
152869: 495 4 7 6 2 68 804 6 7 3
10354970912: 851 130 18 56 52
1175: 929 88 5 64 89
108231077: 299 13 4 2 1 6 8 867 1 1
15777018: 878 4 95 54 81
2918529: 4 143 26 51 6
1636355: 43 885 895 9 7 8 2 8 1 3
84277: 8 4 278
14448399: 3 4 294 48 402
7361631: 652 2 42 797 628
85232018241: 5 3 270 160 879 945 2
5012416: 7 716 402 7 6
11799346: 985 99 121 5 26
89285174: 89 28 1 5 17 4 2
2131137: 4 531 7 137
196850926: 7 75 254 898 26

View file

@ -0,0 +1,43 @@
defmodule Evaluator do
@doc """
Parameters:
- `equations` is a list of {test value, numbers} tuples.
- `operations` is a list of functions that may be applied to numbers.
iex> Evaluator.get_calibration_result([{4, [2, 2]}], [&Kernel.+/2])
4
"""
def get_calibration_result(equations, operations) do
equations
|> Enum.filter(fn {test_value, numbers} -> could_be_true?(test_value, numbers, operations) end)
|> Enum.map(fn {test_value, _} -> test_value end)
|> Enum.sum()
end
def could_be_true?(test_value, numbers, operations) do
res =
get_variants(numbers, operations)
|> Enum.find(fn x -> x == test_value end)
res != nil
end
# Very slow...
def get_variants([n1 | tail], operations) do
case tail do
[] ->
[n1]
[n2 | new_tail] ->
operations
|> Enum.map(fn op -> op.(n1, n2) end)
|> Enum.map(fn op_res -> get_variants([op_res | new_tail], operations) end)
|> Enum.concat()
end
end
def concat(n1, n2) do
n3 = Integer.to_string(n1) <> Integer.to_string(n2)
String.to_integer(n3)
end
end

View file

@ -0,0 +1,29 @@
defmodule Mix.Tasks.Solve do
use Mix.Task
@impl Mix.Task
def run(_) do
input =
File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(fn str -> String.split(str, ": ") end)
|> Stream.map(fn [test_value | numbers] ->
{String.to_integer(test_value), parse_numbers(hd(numbers))}
end)
|> Enum.to_list()
res1 = Evaluator.get_calibration_result(input, [&Kernel.+/2, &Kernel.*/2])
IO.puts("Part 1: #{res1}")
res2 =
Evaluator.get_calibration_result(input, [&Kernel.+/2, &Kernel.*/2, &Evaluator.concat/2])
IO.puts("Part 2: #{res2}")
end
def parse_numbers(nums) do
nums
|> String.split()
|> Enum.map(&String.to_integer/1)
end
end

View file

@ -0,0 +1,28 @@
defmodule Day07.MixProject do
use Mix.Project
def project do
[
app: :day_07,
version: "0.1.0",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

View file

@ -0,0 +1,4 @@
defmodule EvaluatorTest do
use ExUnit.Case
doctest Evaluator
end

View file

@ -0,0 +1 @@
ExUnit.start()