Compare commits

...

35 commits

Author SHA1 Message Date
Bruno BELANYI df66079060 2021: d13: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 8794333d64 2021: d13: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 4b19161d71 2021: d13: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 6dbdba8a46 2021: d13: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 64651f1be9 2021: d12: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI b2c5e732f5 2021: d12: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 3feee209e3 2021: d12: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 85fd510ec9 2021: d12: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 6434bbca1b 2021: d11: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI c7f1e0c57c 2021: d11: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 9c1b1bc4a5 2021: d11: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 3fb206b0e5 2021: d11: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI cef3c2fa8d 2021: d10: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 5e1b3b095c 2021: d10: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 97ce6eb8b5 2021: d10: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 00df99ac05 2021: d10: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 531bbda3d5 2021: d09: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI fc8af0d5ce 2021: d09: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 3b0f1c8882 2021: d09: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 4a99cd11bd 2021: d09: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI f535c7c129 2021: d08: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 2ab20607e1 2021: d08: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI ab86a2dce7 2021: d08: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI e05173e73b 2021: d08: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 6d778ff545 2021: d07: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI c341ad6eee 2021: d07: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI b8d6bc9b4f 2021: d07: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 7793f65af4 2021: d07: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 4794a6269a 2021: d06: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 7f9758eb0c 2021: d06: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI 10abc46e11 2021: d06: ex1: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 330a8d64d0 2021: d06: ex1: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI d474708fc8 2021: d05: ex2: add solution 2021-12-13 17:52:37 +01:00
Bruno BELANYI 55e08af4b1 2021: d05: ex2: add input 2021-12-13 17:52:37 +01:00
Bruno BELANYI ce04fb589b 2021: d05: ex1: add solution 2021-12-13 17:52:37 +01:00
35 changed files with 4154 additions and 0 deletions

63
2021/d05/ex1/ex1.py Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from typing import Iterable, List, NamedTuple, Tuple
class Point(NamedTuple):
x: int
y: int
Line = Tuple[Point, Point]
def solve(input: List[str]) -> int:
def parse_line(line: str) -> Line:
def parse_point(point: str) -> Point:
x, y = map(int, point.split(","))
return Point(x, y)
p1, p2 = map(parse_point, line.split(" -> "))
return (p1, p2)
def is_of_interest(line: Line) -> bool:
p1, p2 = line
return p1.x == p2.x or p1.y == p2.y
def line_to_points(line: Line) -> Iterable[Point]:
def inclusive_range_any_order(a: int, b: int) -> Iterable[int]:
if a < b:
yield from range(a, b + 1)
else:
yield from range(a, b - 1, -1)
p1, p2 = line
if p1.x == p2.x:
for y in inclusive_range_any_order(p1.y, p2.y):
yield Point(p1.x, y)
return
if p1.y == p2.y:
for x in inclusive_range_any_order(p1.x, p2.x):
yield Point(x, p1.y)
return
assert False # Sanity check
lines = list(filter(is_of_interest, map(parse_line, input)))
counts = Counter(itertools.chain.from_iterable(line_to_points(l) for l in lines))
return sum(counts[p] > 1 for p in counts)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

58
2021/d05/ex2/ex2.py Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from typing import Iterable, List, NamedTuple, Tuple
class Point(NamedTuple):
x: int
y: int
Line = Tuple[Point, Point]
def solve(input: List[str]) -> int:
def parse_line(line: str) -> Line:
def parse_point(point: str) -> Point:
x, y = map(int, point.split(","))
return Point(x, y)
p1, p2 = map(parse_point, line.split(" -> "))
return (p1, p2)
def line_to_points(line: Line) -> Iterable[Point]:
def inclusive_range_any_order(a: int, b: int) -> Iterable[int]:
if a < b:
yield from range(a, b + 1)
else:
yield from range(a, b - 1, -1)
p1, p2 = line
xs = inclusive_range_any_order(p1.x, p2.x)
ys = inclusive_range_any_order(p1.y, p2.y)
if p1.x == p2.x:
xs = itertools.repeat(p1.x)
if p1.y == p2.y:
ys = itertools.repeat(p1.y)
yield from map(Point._make, zip(xs, ys))
lines = list(map(parse_line, input))
counts = Counter(itertools.chain.from_iterable(line_to_points(l) for l in lines))
return sum(counts[p] > 1 for p in counts)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

500
2021/d05/ex2/input Normal file
View file

@ -0,0 +1,500 @@
284,294 -> 733,743
625,347 -> 653,375
561,848 -> 561,181
836,102 -> 836,339
946,941 -> 22,17
18,954 -> 956,16
370,142 -> 370,700
990,966 -> 677,966
366,603 -> 366,465
728,942 -> 57,271
615,493 -> 847,493
584,281 -> 301,281
125,356 -> 301,180
941,569 -> 555,183
151,116 -> 509,116
41,18 -> 841,818
627,670 -> 627,630
965,472 -> 965,100
93,404 -> 330,641
475,963 -> 475,514
389,389 -> 389,326
842,565 -> 842,576
454,700 -> 650,700
73,810 -> 73,319
450,212 -> 450,284
316,392 -> 316,697
915,592 -> 578,592
622,485 -> 434,485
109,853 -> 952,10
305,73 -> 305,222
27,489 -> 157,489
191,979 -> 867,979
527,329 -> 527,292
301,645 -> 301,162
639,730 -> 176,730
46,964 -> 46,458
727,422 -> 435,714
28,552 -> 404,552
33,108 -> 33,21
227,249 -> 327,249
414,903 -> 784,903
69,422 -> 888,422
422,924 -> 103,605
793,353 -> 450,10
714,682 -> 714,972
201,745 -> 410,745
408,713 -> 408,847
174,842 -> 818,198
863,353 -> 775,353
199,780 -> 670,780
877,947 -> 340,410
163,202 -> 163,91
955,919 -> 955,585
836,271 -> 533,271
258,366 -> 728,836
582,749 -> 582,12
80,40 -> 80,704
287,213 -> 287,635
390,546 -> 390,194
837,511 -> 538,810
473,281 -> 902,281
851,865 -> 731,745
918,59 -> 445,532
796,215 -> 796,248
875,111 -> 604,111
660,805 -> 538,805
507,850 -> 145,850
585,861 -> 585,52
426,74 -> 700,348
206,405 -> 529,405
418,333 -> 418,17
368,457 -> 33,792
186,81 -> 957,852
505,283 -> 113,283
20,878 -> 462,878
750,237 -> 69,918
15,280 -> 358,623
798,981 -> 500,683
965,970 -> 22,970
950,970 -> 148,970
660,392 -> 660,884
862,405 -> 862,527
801,283 -> 801,361
71,837 -> 136,837
651,438 -> 945,144
524,607 -> 614,517
348,955 -> 138,955
957,164 -> 404,717
531,581 -> 454,504
710,185 -> 710,271
822,86 -> 822,966
745,233 -> 490,488
350,823 -> 663,823
824,67 -> 447,444
846,667 -> 796,617
666,24 -> 666,906
640,39 -> 640,145
654,481 -> 985,481
581,894 -> 416,729
443,11 -> 697,11
318,627 -> 799,146
113,78 -> 891,856
181,149 -> 179,151
451,74 -> 451,262
458,726 -> 314,726
218,662 -> 533,662
965,108 -> 527,108
782,481 -> 896,367
557,927 -> 557,938
506,242 -> 941,677
948,778 -> 948,629
567,816 -> 567,956
323,773 -> 323,364
864,980 -> 864,12
611,699 -> 611,886
613,392 -> 901,104
528,905 -> 156,905
632,206 -> 798,40
338,237 -> 919,818
256,889 -> 11,644
835,52 -> 55,832
464,144 -> 322,144
254,747 -> 254,509
866,892 -> 866,916
827,946 -> 30,149
899,84 -> 177,806
134,634 -> 357,634
781,492 -> 244,492
817,762 -> 817,976
818,749 -> 818,860
262,480 -> 263,480
409,576 -> 409,698
242,151 -> 981,890
149,519 -> 149,557
42,990 -> 42,930
687,974 -> 50,337
758,382 -> 465,382
760,861 -> 760,934
17,835 -> 17,915
645,923 -> 645,648
702,116 -> 72,746
153,162 -> 955,964
185,101 -> 918,834
554,179 -> 554,353
879,673 -> 879,949
368,13 -> 368,512
582,105 -> 591,114
146,291 -> 600,745
609,538 -> 930,538
320,604 -> 320,146
566,698 -> 443,575
167,708 -> 844,31
712,630 -> 712,421
912,930 -> 64,82
980,931 -> 87,38
23,893 -> 888,28
640,435 -> 676,435
701,516 -> 190,516
684,145 -> 62,767
127,471 -> 91,435
685,197 -> 78,197
103,493 -> 103,522
309,986 -> 309,850
938,270 -> 938,300
295,72 -> 354,72
948,889 -> 948,455
254,733 -> 254,175
95,329 -> 942,329
19,672 -> 19,445
206,807 -> 206,934
886,961 -> 886,690
117,386 -> 117,292
199,59 -> 668,528
299,263 -> 299,878
28,295 -> 638,905
10,140 -> 276,406
279,526 -> 921,526
485,128 -> 856,499
418,398 -> 186,398
296,577 -> 296,521
514,261 -> 10,765
691,673 -> 776,758
131,430 -> 152,430
858,85 -> 62,85
394,846 -> 270,970
827,913 -> 827,376
634,669 -> 910,669
12,53 -> 945,986
782,467 -> 782,421
159,832 -> 109,832
793,807 -> 79,93
120,584 -> 356,584
645,16 -> 645,355
526,685 -> 217,376
296,305 -> 296,929
954,144 -> 954,839
748,88 -> 103,733
523,804 -> 473,754
524,316 -> 524,756
696,183 -> 912,183
288,564 -> 55,797
568,103 -> 568,348
468,626 -> 682,412
163,163 -> 961,961
762,824 -> 27,89
623,625 -> 32,34
865,343 -> 490,718
259,458 -> 259,33
944,660 -> 944,176
781,804 -> 826,759
15,702 -> 15,553
403,310 -> 918,825
438,734 -> 835,734
825,13 -> 825,245
129,611 -> 370,611
49,939 -> 172,939
687,906 -> 687,532
629,482 -> 273,126
727,218 -> 424,218
447,451 -> 233,451
142,779 -> 813,779
527,27 -> 527,804
482,55 -> 482,200
39,264 -> 806,264
884,636 -> 458,636
467,121 -> 199,389
856,925 -> 856,666
666,359 -> 378,359
11,946 -> 705,946
491,281 -> 940,730
86,112 -> 918,944
974,807 -> 974,707
445,67 -> 914,536
953,394 -> 953,822
468,398 -> 157,87
231,620 -> 231,646
979,869 -> 979,911
450,330 -> 450,79
675,659 -> 617,659
66,181 -> 66,723
181,406 -> 181,192
908,334 -> 908,526
254,891 -> 282,891
777,791 -> 127,141
469,58 -> 694,58
954,957 -> 566,569
957,957 -> 123,123
741,359 -> 741,986
763,526 -> 763,101
857,427 -> 600,170
527,756 -> 490,719
625,249 -> 397,249
798,702 -> 712,702
868,75 -> 868,853
332,296 -> 332,629
211,829 -> 100,940
12,139 -> 12,218
655,978 -> 655,242
99,852 -> 855,96
486,267 -> 486,855
474,90 -> 474,244
948,491 -> 186,491
896,59 -> 278,677
295,732 -> 629,732
860,936 -> 860,556
143,790 -> 143,26
371,847 -> 395,847
739,301 -> 739,44
384,716 -> 748,716
848,423 -> 848,923
855,23 -> 218,660
381,805 -> 381,438
451,610 -> 91,610
906,957 -> 191,957
118,675 -> 169,675
836,818 -> 95,818
368,945 -> 825,488
165,299 -> 899,299
392,327 -> 926,861
663,16 -> 131,548
630,302 -> 888,302
206,869 -> 206,331
979,413 -> 979,204
894,860 -> 62,28
444,897 -> 962,379
550,158 -> 550,885
845,736 -> 811,736
846,857 -> 12,857
981,730 -> 981,154
694,835 -> 88,835
21,101 -> 21,385
19,960 -> 964,15
283,721 -> 450,721
59,136 -> 758,835
287,313 -> 719,313
471,252 -> 849,630
682,189 -> 168,189
10,921 -> 774,157
884,598 -> 884,540
207,615 -> 207,443
627,408 -> 67,408
285,36 -> 285,792
116,585 -> 254,585
183,86 -> 183,702
220,138 -> 868,138
833,68 -> 286,615
367,534 -> 766,534
907,514 -> 621,228
133,593 -> 133,581
164,727 -> 768,123
566,227 -> 566,555
983,988 -> 105,110
620,177 -> 620,821
612,413 -> 612,176
168,889 -> 168,210
871,487 -> 559,175
399,870 -> 761,870
236,976 -> 582,630
699,216 -> 699,887
153,745 -> 790,745
444,749 -> 444,257
808,165 -> 939,165
546,525 -> 95,976
583,179 -> 373,389
235,816 -> 840,816
744,89 -> 832,89
425,317 -> 465,357
267,235 -> 114,82
887,59 -> 572,374
808,237 -> 808,626
431,352 -> 400,383
815,376 -> 815,905
249,218 -> 989,958
120,435 -> 357,198
807,551 -> 490,234
910,524 -> 910,725
802,304 -> 447,659
789,228 -> 678,339
229,322 -> 52,322
658,393 -> 506,393
378,438 -> 378,569
163,981 -> 473,671
537,984 -> 935,586
58,945 -> 966,37
132,696 -> 565,263
136,813 -> 136,284
606,656 -> 298,348
533,572 -> 673,712
872,912 -> 301,341
16,287 -> 16,613
571,541 -> 980,950
117,495 -> 35,495
85,79 -> 682,676
425,431 -> 117,739
982,984 -> 10,12
28,75 -> 431,478
259,529 -> 259,436
762,267 -> 170,859
323,135 -> 929,741
81,238 -> 561,718
128,213 -> 876,961
649,466 -> 649,540
715,863 -> 119,863
830,624 -> 794,660
123,968 -> 977,114
489,466 -> 489,811
27,10 -> 980,963
255,732 -> 255,484
574,829 -> 431,829
548,743 -> 22,217
903,297 -> 903,763
684,774 -> 64,154
260,823 -> 683,823
422,211 -> 422,826
10,196 -> 988,196
108,802 -> 15,802
104,70 -> 104,452
885,59 -> 885,36
68,854 -> 68,774
731,935 -> 731,718
657,986 -> 617,986
732,292 -> 732,32
841,56 -> 841,83
74,108 -> 862,896
654,895 -> 323,895
374,952 -> 374,217
90,723 -> 750,63
246,89 -> 911,754
453,301 -> 755,301
983,988 -> 23,28
81,705 -> 133,757
752,743 -> 752,397
53,243 -> 449,639
451,811 -> 451,187
26,672 -> 26,699
254,861 -> 943,861
643,740 -> 643,966
486,655 -> 149,318
375,146 -> 375,973
76,293 -> 103,293
246,398 -> 246,248
324,392 -> 595,121
130,577 -> 131,577
380,623 -> 549,454
224,181 -> 985,942
310,223 -> 310,594
23,982 -> 23,738
19,858 -> 832,858
726,531 -> 726,578
730,433 -> 196,433
606,599 -> 242,599
444,832 -> 444,238
198,870 -> 47,870
944,473 -> 795,473
737,386 -> 178,945
328,902 -> 328,644
422,851 -> 567,851
674,781 -> 215,781
920,757 -> 302,757
225,932 -> 640,517
359,337 -> 791,337
935,430 -> 935,262
772,850 -> 280,358
175,829 -> 175,451
938,204 -> 234,908
253,749 -> 308,749
704,458 -> 468,458
222,95 -> 743,616
968,840 -> 123,840
491,619 -> 491,889
979,580 -> 979,459
901,193 -> 171,923
246,155 -> 246,680
711,755 -> 247,755
671,734 -> 475,734
803,783 -> 129,109
145,890 -> 920,115
463,521 -> 463,700
782,99 -> 782,311
547,467 -> 630,467
14,88 -> 795,869
653,899 -> 653,90
488,874 -> 488,570
93,879 -> 645,327
320,658 -> 40,938
611,246 -> 611,22
258,935 -> 258,829
931,436 -> 931,263
252,460 -> 252,461
490,382 -> 965,382
242,89 -> 242,617
271,111 -> 595,435
462,706 -> 242,486
557,328 -> 747,328
486,99 -> 486,333
156,40 -> 488,372
323,482 -> 138,297
595,539 -> 812,756
923,861 -> 377,315
934,952 -> 256,274
314,777 -> 314,12
508,47 -> 508,144
888,807 -> 701,807
745,774 -> 878,907
740,716 -> 740,215
62,43 -> 62,12
571,196 -> 454,196
568,107 -> 408,107
549,676 -> 404,676
595,573 -> 595,970
148,168 -> 193,123
763,71 -> 759,71
797,64 -> 307,64
959,984 -> 32,57
457,562 -> 634,562
127,521 -> 601,47
112,296 -> 112,120
148,755 -> 451,755
636,494 -> 870,494
910,242 -> 945,277
912,911 -> 912,892
759,815 -> 759,314
391,285 -> 391,959
455,460 -> 182,460
112,78 -> 112,385
842,179 -> 842,592
236,424 -> 421,424
508,907 -> 30,907
637,219 -> 34,822
503,375 -> 503,205
570,533 -> 626,533
658,11 -> 658,94
179,286 -> 326,433
918,214 -> 200,932
339,887 -> 81,887
794,91 -> 50,835
225,356 -> 225,261
80,160 -> 80,335
148,64 -> 847,763
595,393 -> 941,393

45
2021/d06/ex1/ex1.py Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from dataclasses import dataclass
from typing import Iterator, List, TypeVar
T = TypeVar("T")
def nth(iterable: Iterator[T], n: int) -> T:
return next(itertools.islice(iterable, n, None))
def solve(input: List[str]) -> int:
fish = [0] * 9
for n, count in Counter(map(int, input[0].split(","))).items():
fish[n] = count
def step(fish: List[int]) -> List[int]:
# Count how many clones happen
new_fish = fish[0]
# Do the next cycle
fish[0:-1] = fish[1:]
fish[6] += new_fish
fish[8] = new_fish # Override number of new fish
return fish
def iter(fish: List[int]) -> Iterator[List[int]]:
while True:
yield (fish := step(fish))
return sum(nth(iter(fish), 80 - 1))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

1
2021/d06/ex1/input Normal file
View file

@ -0,0 +1 @@
3,1,4,2,1,1,1,1,1,1,1,4,1,4,1,2,1,1,2,1,3,4,5,1,1,4,1,3,3,1,1,1,1,3,3,1,3,3,1,5,5,1,1,3,1,1,2,1,1,1,3,1,4,3,2,1,4,3,3,1,1,1,1,5,1,4,1,1,1,4,1,4,4,1,5,1,1,4,5,1,1,2,1,1,1,4,1,2,1,1,1,1,1,1,5,1,3,1,1,4,4,1,1,5,1,2,1,1,1,1,5,1,3,1,1,1,2,2,1,4,1,3,1,4,1,2,1,1,1,1,1,3,2,5,4,4,1,3,2,1,4,1,3,1,1,1,2,1,1,5,1,2,1,1,1,2,1,4,3,1,1,1,4,1,1,1,1,1,2,2,1,1,5,1,1,3,1,2,5,5,1,4,1,1,1,1,1,2,1,1,1,1,4,5,1,1,1,1,1,1,1,1,1,3,4,4,1,1,4,1,3,4,1,5,4,2,5,1,2,1,1,1,1,1,1,4,3,2,1,1,3,2,5,2,5,5,1,3,1,2,1,1,1,1,1,1,1,1,1,3,1,1,1,3,1,4,1,4,2,1,3,4,1,1,1,2,3,1,1,1,4,1,2,5,1,2,1,5,1,1,2,1,2,1,1,1,1,4,3,4,1,5,5,4,1,1,5,2,1,3

45
2021/d06/ex2/ex2.py Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python
import itertools
import sys
from collections import Counter
from dataclasses import dataclass
from typing import Iterator, List, TypeVar
T = TypeVar("T")
def nth(iterable: Iterator[T], n: int) -> T:
return next(itertools.islice(iterable, n, None))
def solve(input: List[str]) -> int:
fish = [0] * 9
for n, count in Counter(map(int, input[0].split(","))).items():
fish[n] = count
def step(fish: List[int]) -> List[int]:
# Count how many clones happen
new_fish = fish[0]
# Do the next cycle
fish[0:-1] = fish[1:]
fish[6] += new_fish
fish[8] = new_fish # Override number of new fish
return fish
def iter(fish: List[int]) -> Iterator[List[int]]:
while True:
yield (fish := step(fish))
return sum(nth(iter(fish), 256 - 1))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

1
2021/d06/ex2/input Normal file
View file

@ -0,0 +1 @@
3,1,4,2,1,1,1,1,1,1,1,4,1,4,1,2,1,1,2,1,3,4,5,1,1,4,1,3,3,1,1,1,1,3,3,1,3,3,1,5,5,1,1,3,1,1,2,1,1,1,3,1,4,3,2,1,4,3,3,1,1,1,1,5,1,4,1,1,1,4,1,4,4,1,5,1,1,4,5,1,1,2,1,1,1,4,1,2,1,1,1,1,1,1,5,1,3,1,1,4,4,1,1,5,1,2,1,1,1,1,5,1,3,1,1,1,2,2,1,4,1,3,1,4,1,2,1,1,1,1,1,3,2,5,4,4,1,3,2,1,4,1,3,1,1,1,2,1,1,5,1,2,1,1,1,2,1,4,3,1,1,1,4,1,1,1,1,1,2,2,1,1,5,1,1,3,1,2,5,5,1,4,1,1,1,1,1,2,1,1,1,1,4,5,1,1,1,1,1,1,1,1,1,3,4,4,1,1,4,1,3,4,1,5,4,2,5,1,2,1,1,1,1,1,1,4,3,2,1,1,3,2,5,2,5,5,1,3,1,2,1,1,1,1,1,1,1,1,1,3,1,1,1,3,1,4,1,4,2,1,3,4,1,1,1,2,3,1,1,1,4,1,2,5,1,2,1,5,1,1,2,1,2,1,1,1,1,4,3,4,1,5,5,4,1,1,5,2,1,3

21
2021/d07/ex1/ex1.py Executable file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
import itertools
import sys
from typing import List
def solve(input: List[str]) -> int:
nums = sorted(int(n) for n in input[0].split(","))
median = nums[len(nums) // 2]
return sum(abs(n - median) for n in nums)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

1
2021/d07/ex1/input Normal file
View file

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,1133,1029,1446,1005,596,277,439,544,201,317,237,33,252,774,482,291,57,1434,153,134,385,993,677,1189,99,1082,351,80,1563,195,581,693,5,633,184,552,11,606,462,48,134,1451,850,221,336,661,276,156,932,23,98,76,327,212,489,230,819,998,1037,1561,206,159,757,207,1203,224,1459,294,39,1108,96,89,1664,353,41,83,942,771,362,193,900,676,851,277,558,368,1054,316,143,464,85,896,558,52,669,163,696,140,340,542,817,783,1436,786,81,21,669,184,430,312,1500,797,698,611,314,145,125,391,785,867,488,144,9,967,560,245,116,96,1769,314,533,25,0,1375,1307,1273,1178,332,59,26,234,487,480,615,313,629,28,707,96,637,30,350,1286,900,777,115,56,79,809,1101,13,150,741,940,20,403,384,479,622,108,325,276,609,654,535,539,62,187,414,535,140,222,845,357,55,17,8,1430,853,759,331,673,202,482,1280,714,911,409,429,604,278,478,301,408,590,329,1482,110,423,488,7,628,66,196,399,86,241,1058,501,1284,594,710,799,34,60,668,457,0,1209,20,50,1134,288,415,399,86,540,1356,285,1541,172,3,634,1387,146,669,195,403,601,758,160,970,760,43,213,68,805,50,350,292,78,252,54,1021,882,241,808,601,892,878,1401,48,191,74,1429,94,520,505,679,1133,120,485,183,362,684,520,1366,301,123,290,1248,140,44,230,572,57,1101,217,906,1012,668,85,230,32,100,472,275,78,91,19,61,732,518,189,85,1531,1255,134,489,637,792,42,36,392,603,976,651,127,486,340,534,185,890,346,906,1295,319,321,597,252,183,554,124,44,426,131,408,582,429,645,1227,574,14,1828,359,184,1830,10,288,194,949,1578,492,502,149,609,77,92,320,233,1026,123,26,188,1123,4,665,1793,331,858,481,197,424,133,933,338,7,163,1269,665,19,1,538,126,895,1751,345,895,203,175,66,305,1479,239,718,386,8,1148,203,636,1211,635,149,489,1156,1828,61,328,331,287,262,8,3,522,1427,931,1103,199,329,930,779,124,446,391,903,696,1764,16,595,522,143,296,19,872,29,546,566,256,63,1195,862,516,267,452,1287,11,547,760,77,117,988,185,293,421,118,767,70,169,456,600,755,740,1799,97,507,1165,265,1126,376,1250,408,22,674,774,361,54,558,1021,529,29,871,527,474,218,47,309,61,188,1365,725,267,235,359,1004,1771,765,8,17,103,89,967,65,302,759,1688,882,709,1469,123,548,834,355,646,404,76,257,101,627,2,66,1328,137,42,23,127,110,632,683,1163,843,1119,16,497,1756,347,958,981,57,185,204,1403,530,536,104,68,1152,95,320,959,144,281,350,622,717,112,631,150,19,938,296,1272,825,37,192,350,847,306,824,1042,320,480,857,589,137,687,93,21,1601,547,550,491,161,49,124,292,396,34,24,95,932,56,968,197,109,1300,1326,605,357,412,14,179,450,991,173,120,1402,478,77,933,88,96,49,95,596,1885,841,441,659,309,418,87,134,34,815,759,631,209,728,7,232,1304,698,140,304,1448,101,1,239,1321,362,1367,1411,279,111,633,1347,110,18,710,443,1676,64,531,386,64,518,619,893,140,301,426,1451,261,1083,115,61,943,60,891,217,475,48,436,586,31,287,196,430,357,78,161,1533,745,198,715,8,307,225,604,263,992,371,749,700,1573,573,546,480,425,47,88,66,799,388,60,736,111,81,856,29,695,141,4,271,143,939,423,3,1260,265,24,903,292,23,80,196,1245,399,123,3,532,283,366,1175,187,15,450,487,117,1041,669,59,579,159,461,598,915,666,1231,999,253,185,1016,135,1317,253,111,1261,833,26,851,120,11,63,718,682,233,76,0,50,1678,777,286,279,90,47,158,71,35,475,245,550,4,942,1816,1646,102,175,343,12,446,12,623,44,157,10,555,1173,651,361,745,1870,97,237,1628,90,233,126,1596,1146,746,383,209,55,209,318,904,144,90,21,86,4,970,262,8,210,249,871,1216,1003,172,405,821,225,2,143,915,129,598,397,583,1400,315,113,33,75,426,2,611,293,196,419,125,176,496,4,607,1425,27,48,172,972,626,227,184,1257,514,209,524,22,58,927,4,931,3,6,104,716,961,17,4,154,249,558,203,180,180,1194,935,263,535,380,1439,274,1566,873,1763,2,89,636,407,667,438,966,757,245,730,328,915,167,245,126,52,652,938,1183,320,1298,10,844,36,263,382,37,142

28
2021/d07/ex2/ex2.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env python
import itertools
import sys
from math import ceil, floor
from typing import List
def solve(input: List[str]) -> int:
def fuel(dist: int) -> int:
return dist * (dist + 1) // 2
nums = list(int(n) for n in input[0].split(","))
mean = sum(nums) / len(nums)
upper = sum(fuel(abs(n - ceil(mean))) for n in nums)
lower = sum(fuel(abs(n - floor(mean))) for n in nums)
return min(upper, lower)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

1
2021/d07/ex2/input Normal file
View file

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,1133,1029,1446,1005,596,277,439,544,201,317,237,33,252,774,482,291,57,1434,153,134,385,993,677,1189,99,1082,351,80,1563,195,581,693,5,633,184,552,11,606,462,48,134,1451,850,221,336,661,276,156,932,23,98,76,327,212,489,230,819,998,1037,1561,206,159,757,207,1203,224,1459,294,39,1108,96,89,1664,353,41,83,942,771,362,193,900,676,851,277,558,368,1054,316,143,464,85,896,558,52,669,163,696,140,340,542,817,783,1436,786,81,21,669,184,430,312,1500,797,698,611,314,145,125,391,785,867,488,144,9,967,560,245,116,96,1769,314,533,25,0,1375,1307,1273,1178,332,59,26,234,487,480,615,313,629,28,707,96,637,30,350,1286,900,777,115,56,79,809,1101,13,150,741,940,20,403,384,479,622,108,325,276,609,654,535,539,62,187,414,535,140,222,845,357,55,17,8,1430,853,759,331,673,202,482,1280,714,911,409,429,604,278,478,301,408,590,329,1482,110,423,488,7,628,66,196,399,86,241,1058,501,1284,594,710,799,34,60,668,457,0,1209,20,50,1134,288,415,399,86,540,1356,285,1541,172,3,634,1387,146,669,195,403,601,758,160,970,760,43,213,68,805,50,350,292,78,252,54,1021,882,241,808,601,892,878,1401,48,191,74,1429,94,520,505,679,1133,120,485,183,362,684,520,1366,301,123,290,1248,140,44,230,572,57,1101,217,906,1012,668,85,230,32,100,472,275,78,91,19,61,732,518,189,85,1531,1255,134,489,637,792,42,36,392,603,976,651,127,486,340,534,185,890,346,906,1295,319,321,597,252,183,554,124,44,426,131,408,582,429,645,1227,574,14,1828,359,184,1830,10,288,194,949,1578,492,502,149,609,77,92,320,233,1026,123,26,188,1123,4,665,1793,331,858,481,197,424,133,933,338,7,163,1269,665,19,1,538,126,895,1751,345,895,203,175,66,305,1479,239,718,386,8,1148,203,636,1211,635,149,489,1156,1828,61,328,331,287,262,8,3,522,1427,931,1103,199,329,930,779,124,446,391,903,696,1764,16,595,522,143,296,19,872,29,546,566,256,63,1195,862,516,267,452,1287,11,547,760,77,117,988,185,293,421,118,767,70,169,456,600,755,740,1799,97,507,1165,265,1126,376,1250,408,22,674,774,361,54,558,1021,529,29,871,527,474,218,47,309,61,188,1365,725,267,235,359,1004,1771,765,8,17,103,89,967,65,302,759,1688,882,709,1469,123,548,834,355,646,404,76,257,101,627,2,66,1328,137,42,23,127,110,632,683,1163,843,1119,16,497,1756,347,958,981,57,185,204,1403,530,536,104,68,1152,95,320,959,144,281,350,622,717,112,631,150,19,938,296,1272,825,37,192,350,847,306,824,1042,320,480,857,589,137,687,93,21,1601,547,550,491,161,49,124,292,396,34,24,95,932,56,968,197,109,1300,1326,605,357,412,14,179,450,991,173,120,1402,478,77,933,88,96,49,95,596,1885,841,441,659,309,418,87,134,34,815,759,631,209,728,7,232,1304,698,140,304,1448,101,1,239,1321,362,1367,1411,279,111,633,1347,110,18,710,443,1676,64,531,386,64,518,619,893,140,301,426,1451,261,1083,115,61,943,60,891,217,475,48,436,586,31,287,196,430,357,78,161,1533,745,198,715,8,307,225,604,263,992,371,749,700,1573,573,546,480,425,47,88,66,799,388,60,736,111,81,856,29,695,141,4,271,143,939,423,3,1260,265,24,903,292,23,80,196,1245,399,123,3,532,283,366,1175,187,15,450,487,117,1041,669,59,579,159,461,598,915,666,1231,999,253,185,1016,135,1317,253,111,1261,833,26,851,120,11,63,718,682,233,76,0,50,1678,777,286,279,90,47,158,71,35,475,245,550,4,942,1816,1646,102,175,343,12,446,12,623,44,157,10,555,1173,651,361,745,1870,97,237,1628,90,233,126,1596,1146,746,383,209,55,209,318,904,144,90,21,86,4,970,262,8,210,249,871,1216,1003,172,405,821,225,2,143,915,129,598,397,583,1400,315,113,33,75,426,2,611,293,196,419,125,176,496,4,607,1425,27,48,172,972,626,227,184,1257,514,209,524,22,58,927,4,931,3,6,104,716,961,17,4,154,249,558,203,180,180,1194,935,263,535,380,1439,274,1566,873,1763,2,89,636,407,667,438,966,757,245,730,328,915,167,245,126,52,652,938,1183,320,1298,10,844,36,263,382,37,142

38
2021/d08/ex1/ex1.py Executable file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
import itertools
import sys
from dataclasses import dataclass
from typing import List, Set
@dataclass
class Entry:
signals: List[Set[str]]
outputs: List[Set[str]]
def solve(input: List[str]) -> int:
def parse_entry(input: str) -> Entry:
signals, outputs = input.split(" | ")
return Entry(
[set(s) for s in signals.split()], [set(o) for o in outputs.split()]
)
entries = [parse_entry(line) for line in input]
return sum(
itertools.chain.from_iterable(
map(lambda __: 1, filter(lambda d: len(d) in (2, 3, 4, 7), entry.outputs))
for entry in entries
)
)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

200
2021/d08/ex1/input Normal file
View file

@ -0,0 +1,200 @@
badc bd dbeaf cfdbge dfb cfbdea efbag edcfgab dcafe degfca | eacfd acdfbe cbdegf fcbaedg
cd fdbac egcfab gbadcfe cfgdeb cbadfe deca cdf dfabg abefc | dcf cfbad gbafced fcd
cg agecfb cbg eabgfdc egdc fdgba bafecd cbdfe bfcdeg cfgdb | efdcb adcfeb fbdcg gbc
bfceg gfadb dbcfgea bgaef efad abe bcdgfa ea fbdgea agecbd | eadf ceadbg abfge fecdbga
gdcafe eacb adc gbfda afdceb edgbcf badfc ecgbafd ac fdbce | ebfcd cefdab bdfgeca egbdacf
aefbgd fbdc fbecg egdcb edcbag bf gcefa fbedcg bagfced fbg | afgce cfbd bdcf geafdbc
ebgacd edacg gcdeaf cabfg dbc bd cdbgaef ebad fecgbd acbgd | dgbac db caegbdf gcbfa
defbga gead ge efagcb gdafbc cegbdfa egf bfdce bgdef dgabf | gef eg bfcdag ge
bae adcgfbe cdfeag ab dcgeb bfdage agecb cagfe abcf bfcaeg | bae aeb agfceb afceg
ae adefcb gaceb gadceb edag efadcgb badgc cfgbe cadbfg cea | ceabgd fbeacdg aged aged
aegbdc dbegfac gcbaf df febd egdafc deabg gdf gdfeab adfbg | bcdegaf dfeb bcegad df
aegcdfb becdaf gdecf ag fag dcfgba badg fcbad cdgaf acefgb | abcdf cefdab ga ga
gbcfad cefadg cegad aefdb gedacb gcfe fc cfbgead cfade caf | degac egcf geacfbd afc
bcfad afcdbe fd ecfba cfd afde cfeabg gcbfed dacbg fdgabec | cafbged edfa fcd dabgc
afdc adbfe bgecfad ecagdb dbcafe dfe fd agbfe bdace gdbfec | fadbe efbdgc fadbe edcba
bdface geba ecbdag acegd afcdg ge eadbc bfecgda edg fgbdce | eg abge fadgceb abge
fagbde fcegbd gec efcdg fdgbe ebfcag gc adcfe cgdb cagbdfe | defcg cbgdfea gcbd ecfgd
acfbg debgf gfcdba edcbfa fbdag fgbeca cfbagde gadc da bda | dacg acbgf cedfgab caefdgb
ca dfegca edcbgf gfcde aedbg gfdeacb dcbfea facg dgcea ace | fedcg fdagec cea ac
acdf gcfde debcga ecbgaf cf ebgdf bagfdce cedag cef gedafc | dgcbea fc dcgeaf cdaeg
ebagf cabgfed ed cbgad afcbge afdbge edg eagbd cedgfb aefd | fcgdbe de cgbeaf ed
fbcdeag cafbd fgcbea fdgacb daecfb cg gca defga gbdc fdcga | abfcged agc acfgd cbfaeg
dacfe ed ebdcag gcadfe dfbcaeg dce dfeg fbcadg bfeca acdfg | fcbdga gcbade aegdcf fbcgda
cfdegb cbef fgdec gdfbca gdecbfa egfdca bc gbc cbegd aedgb | befc bc abegd ebcdfga
agcefdb fdbg bf bdafe gdebaf eafdg acdbe gcebaf bef edcfga | bf fdbg efdab gefda
gcb bdcgef dcbfg bg edbg facegb cbedf edgafbc dfgca edacbf | gb cdebfa deagfcb bg
eafbg gfad afegbd caefbg befdgc ebagd gd bdg cfbdage acdeb | gd geafb cabed dbg
bcedfg dc dgc aedfbcg acgbed bgfead gedbf bfdcg cedf facbg | gabfed cdebag dafegb cd
fa adbceg afecd cafdbge eacgfb daegcf aef bcdfe fdga daceg | acedf afe fae afdg
cdfaeg agcfb bcdgea dcafeb cef fabec ef dfgcbae defb eabcd | gdabce fdeb ebdf ecf
cdefg ceafg gebfd cdg dc efbcdg cdgbea cfedbga cbfd fdagbe | fbcd afdgbe fabedg dcfb
cdafgb egdb fecgabd cbfdg fcbedg feb acefg begcf be dacebf | be be ebfdca abgfcd
eafgcd facegb acb cdbfaeg fagec dgcabe baefd cgbf cb afecb | bcafeg fcgb gfdeac eagcf
ecgdfb deb edfgab eb faedcg agfed bfaed dbacf gbae efgdcba | fabde eb ecdgfb eabg
aefbg bdegfa gacdb gefc fbc fc bfdcae gefbac cbfag dbgefac | ecfg fc cfge efgcdab
gcfbed gcbae dcaef cbegad bdag bgeacf adefgcb deg adgec gd | agcbde bgad gdbfec gcfabe
dfacb egbcda dac agdcfb fecab adecfgb bgadf cd fgeabd cgfd | gabfdec acd dc dc
bfecg dcgeab af gadf aedbg caefdbg bgfade bafeg abf eabdcf | ebcfg gafd ebacfd fgbdace
ge fdeab gcebfda fadcg fgead abcdef eag ebdgfa acgbef dgeb | cdagf cfdgbae fdeab eag
dba dgfceb fbcegad bagde bcdeag bcedg bdafce agfbe ad cgad | bdcafe agdc ebdcg cdga
edfg fedac fbegcad acgfe fabcd ead bgefac de daecgf bcaged | bafcd ade bacefg fdbac
gdecbf edabc ef fcgead fcadg bdcfga fec ceadf dgaefbc agfe | faedgc ef gabfcd fe
deafg eb egbaf cadegf fcdeab degb bcgaf bfe bfcdega gefdba | abgfe eb gfdaeb efdcga
fabcd beadf efabgc fc cafdgb gcfd adfcgeb abgdce cfb dgcba | dcgf bdcgaef gdcf cfdg
ebac acedf ceadfb cfe dbcgaf ec efcgdab degaf abfcd edgbcf | ce edgaf dgbceaf fce
ecbag fg gacdfe edafgcb cgefa eacdfb deafc bdagcf gcf dgef | fged gedf cefdga cabge
dgebc bcg cg gdeabf cgfeba gdfc gefbdc adbec bdefg fgcdbae | begdf cfgd eadbc gbfde
aefdb ecgb dgaeb agdbce ega fgdacb bcadg cbdeafg ge edacfg | age fcbdaeg gea bcfegda
cdeabg fcgda bdagc acfb dfa af cdeabfg efdgab fbcgda decgf | bcagd cfba gedfc af
ceadb caebgfd bdfaeg cbgaf ge ega gadcbf gfce ebgcfa gebca | aecdb febgcad bgcaf fadcgbe
daegb gfdae ceagfd bgfade feagcb fbgd bg cbagdfe ebdac ebg | gefbacd dbgf geb adgbfe
ecadfg acdfb aefgc ecbfdag dce cfbega fcdae edga gbecdf ed | fcdba ced egfcba dega
defgbc cgdea efag fdbcgae acbdf gcdaeb fe dfcega efc defca | fage fec dcafeg ecagfdb
bg dbcafg gdafecb dgfcae defcg gebd cefgb bgfecd fbaec fbg | gbf dfgec bedg cfdgae
ebgda ebd adce cdgaeb edfcgb bcagd agdcebf ed agbef gcbfda | efagb gbefa dbcfgae eadc
ebcfdg dbe de cdabgf fgdab dgae aecbf abfgced edbaf eagdfb | bface cdafebg gadcefb fedab
agc dabcge agbcd cabe dbagf cfgeadb gecafd gbcdfe ac gbced | cbegd beac dgbce ac
fdagb bdeacf fga eagd gcfdb gecafb ga feabd abfgde dcbfega | bdafe abgcfe gbecfad abdgefc
fbecd dbaef bdgc debafgc daecgf ebfcgd ebc abegfc bc gfced | fadecbg cb cdgeaf bfade
fagbe aebfgcd gef bgadf cbagfd deafbg cfdeag ge begd beafc | bcfadeg edbgaf fcgaed ge
gabfc gabde fbdga bfegca agfbcd df cdfagbe dcfg caefbd dbf | decfagb bdf gfcd fd
bdafeg gf gbadf gefb cgefda daecbfg bacfed cdagb fdg fdaeb | daegcf eagdfb adgcb bagfed
dafegb efdac bda fgedbac egfcdb bgfa fbaed bgdef debagc ba | ba bgcead bad ab
cfged cbegdf cb ecgdb adefbcg fdcabg cdb cdagef efbc gedab | bc gdebcf gecdb bagdcf
fbegcd gcedba eba acegf dbfa efbgd ebfadg ab fgceadb bgfea | fgbae gbedcf degfb agdebc
ceadf fdacbe ef edgafb dfe bgaedc cadfg bcef cfebgad ecabd | cafde fabedc debac eacfd
cd dgafe cbgfae decfg cfebg cfd gdbc febgdc fbegacd fceadb | dc beagfc aebcfg gcfeabd
adcefbg caefgb bafd dface bdcfe cegbd fbe gdfcae dceabf fb | efb ebcgd gbdcaef fb
fbdg gebac degbfc gd dfeagbc decfb gcd dafgce faecbd bdgec | gfdcae gfdb dg afgdce
fb fgedca fbe gfcde gcbfde dbceaf bgeca eafbdcg dgfb fbegc | fbe cbdegf bfe gedfc
acfbe fcbaged bgdcf cabgfd ag cagd fcbga bcfegd gfa eadbfg | afgcb gbdecf efdbcg fag
fbegc dgca dfc fagbedc bfadcg dc ebdfca fadbg cgbfd gfdbae | dabefg egdbaf gaebdf dc
cdbgf bdfaeg ca fgcab dgacfbe feac facgbe ebgcad bca gfbea | dafbeg cab cdagbe debfga
gfbe ebcfag bcedaf be egfac fegdca gebca eab cbgad gfacdbe | dabcgef bcagfed gcfea bea
bdefacg fbgedc efdagb beg geabf eg dafbce dage gfabc eabdf | gead gcafb ecfdab egfbdac
gd gfecbd dacefb dgbf cdegf acfge agbfced becdf aedbgc gdc | gdcef cgbead gd gefacbd
cgbfea cafed dbgefac gacb bc afbge fdabge efbac bcfgde cbe | gbac fcdbge agcb aecgdfb
abgefc cfeba gcdba fgdcea gf fcbgaed eadfcb gbfe fga bacgf | efcba fabecd dgcbfae eagcfd
gcdbfe fdec gcfdb bdfeg de efgba cbgdafe afgcbd cgdbea ged | cedgbf fced agfcdeb cgfbed
dbacfge cafdb gbcfea aegc afbec ea bae bgcedf befgc edgafb | dfgeabc bfgcde faceb fbecag
bfcgad bfegd fg dcgeb egaf bfedcag efabdc dfegba gfb abefd | gf abdgcf dcbfag fgb
degab dgebac bfdeacg cadeb fbdeac bag debgf cgda abgecf ga | dceba fdbeg edbac dbacfge
abdef gcbead bgce cgdab deg dgcbfa aedbg fbacdge eadcfg ge | cgfdba gcafde agfcedb fdacbge
defabg cabdg gcfed cabe gbeacd fgadbc eb geb aedcfgb bdgce | eb dbegc dfecg dbecag
egdabcf edcgb bcedgf cgb bgfe fcgabd bg acgfed cbaed dfegc | cgb gbc egcfad degbc
fe agebdc gfbead fcdea abecgfd bafcde beacd cbef def cgfad | dabgfe fde adfcg bfgead
ba fcabed bad dgacf dfgecab dcgba gdecb bdgcfa abgf caegfd | gdecb dgcfba gcdafe afbedgc
bdgef cdbgfa acbed gedcfb efdgba cg abcgedf cgb cgdbe cfeg | edabc ebdca edbcg ecgf
edfba bcfgead cbged defbcg ag gfadce gcaebd edabg cabg dga | fedagc bcag bdegc gaebfdc
edab fed egcfb ecgbfad fegbd fagdcb degcaf afgebd gabdf ed | def gdefb fgedb edcagf
fc ebfgda ebcfd cabed gcadbf baecfdg cgfe fdc egdfb gdecbf | defbg aecbfdg cadbegf adgfbe
bgdfc fgc fdegb dgecfb fcabd egbafdc gc dgce gabefc gedbfa | fbdgce gc dbfegca cg
bgedc ged gdfcea dcefb cdabg bgfe defbgc bdecaf dcgbfae ge | badefc gbfe cbegd fdagbce
cdefga cgdafeb cbe becdg gdefc cfbg cfdbeg cb febdac edgba | edbgcf abecfd ebgfcd fgbc
de aegcf agedcb afegbdc edbf fbdga facgdb gdaef dbaegf dea | cdabgf ead afgbcd edbf
cfagde eagfbc cbegf cg fcaegdb ebgfd gcf dafebc efabc acbg | bacfe fgc gcf cg
gadfce fbcaeg bdag eba bedcgfa dcfbe ab ebdgca ceagd ecabd | eabdc badec cdaeg dagb
aedb ebcagf fdeac be bdgfc eafbdc abcfegd aefgcd feb ecdfb | fecbd adeb be bfe
dg fgdecb dfecb fdcgeba gedcb gceab fdge cgd cgdbfa baedcf | ecbgd cagfdeb dcbfeg gd
fgcead bfdcag gecdf dfeacb dce aged gcbfe dcfgeab ed fgcda | cfegd de gcafd gefdc
dbec eafbdg dafegc eac gbacf decagb bcgae bdgea gbcefad ce | becag edgab cbadgfe edbc
gefcab agbdc febd bcegd de ecbfg afgdce ebafgdc dfebgc egd | de gdecfba edbf cdegb
abdcefg fbdega dcfag bd fegba badgec fgadb edbf fgaceb bdg | acdfg fagbce adcfg abgfd
ag agdb bfeag fbgdace bcfeda dgafeb egafdc begfc dfeba eag | ecbfg agbd dabg bfeacd
dagc dae fdaec edcafg fbadeg cgefa abcgef ad dfecb fdgcbae | eda eagcbf agfec debfc
ead beafd dfagb faceb bfedca facegd aefcbg cbed ed gfcbaed | abgdf eda agbfce ead
afdgb eabcgf dfgbe fecdgba dagceb gfa cafgdb af cdfa abdcg | abdfg dceagb decfagb fagcedb
dbcfga cb acb gcbafde fcbg egbfda dceaf gbfda dcfba cdbgae | fcadgb cdefgba agbfd cgfb
gceb fcgba cefdba caebfdg egfadb eagfcb cb beafg acb gfacd | abcedf bca gfcda bcge
eabdc caebf ad dgabcef fcaedg cfegba deacbf dabf dca begdc | dbgce dbfa fgebadc acedb
fbgce ec gcea dafcgeb fgbed fec adgbcf abcfeg fcbga cfbade | cbdfag ebgfc fgcbe agbfdc
afcg afebcg becgf fgadeb bdgaefc fa afe acedb abecf gdfebc | fgecb eacdfgb ceabf cgfa
fb gadeb aegdfb abgfe fab dfgb adfecbg bcaegd ebdafc cafeg | becdfga bf fbadec bgfd
ab dgafe fdaeb adb agcedf ecbfd agfb caedgb bdfage egfabdc | fedbc dfecag afdcge becfd
bgedf dgecfb fae gbaec fa cedafg dcabfge gedfab gafbe fbda | fcgdea geadcfb efbdg fa
cgfaed fdebg fcgabd fcegb bd dfaeg gfbeda fgdceba edba dgb | db ebcgf fgcbe edgfa
fgc egcafb dcgb fagdb bedafgc cdbgaf fedac cg gacfd dfbage | febgad cgfaeb fadbg gc
fdaebcg deabgc bfdeg dfcaeg efcgd cdaf cfbeag fcg cf cgeda | bgefcad bgdfe dafc ebagfc
egcfad dbecf cbgf fbe aecbd bcfdge dfbgea aebgdcf ecfdg fb | cgfb gecfd gefacdb fbcg
cdb adcf gbcdae bdceaf dfaeb cdfeb febgc fgedba cd cgdafeb | eadfb fcgeb cbedga bdacge
gd dacg gbdafc gfbdc bdegaf fbgca cbgdefa cbafge edfcb dfg | gbcafd dgac dgcafb cbfgae
cbf dafeb fbceag bc egbdfc edbgafc fbced ecdfg cdgb gdafec | bcf dbfea ebagcf gadfcbe
dcebgf eagfc aebfcd edcbg baefgdc fd cdefg dgaebc fde bfgd | ebagfcd acgefdb fagec gbdaec
afgdc bagec bdfg fb ecgdbfa fab dfecga bcgadf dcafbe bgafc | afgced fb dcafg feabcd
gef febag ebagc egcfbd fg cafg abfde abcfge afcgebd ebcgda | egf gaebf dfeba gadbce
ag cdeafg gdbfce daceb acgf efgbda adg cegda gaedbfc efcgd | debac eadbc bgaecdf cgbaedf
acefdbg debgac ecdgf ead facebg eafgb ad fedbga bfda eafgd | adbf eagfd eabcfgd ecagfb
cd bafegc fgcaed fcaeg dgc fdcag agdbf fedc gbadce egdabcf | fedbcga bgafd fcde cfed
decg egf ecgbfa degfa edbagfc dgcabf cdfag eg fcaedg faebd | gcadef efadg egf eg
feacbg cgfadbe efb efbga dfagb eb aefgc bgec cgedaf dcebfa | eb be be fbe
begc eacfdb dfacg agcfe ce eagbf eac eafdgb aefbcg dfgaecb | ce ce ce gbce
dfeagb dgaeb dbagefc gbcfae degf decba cbgdfa bgfda eg gae | ge agfdcbe bdace fdeg
cgdab cegdbf cfbad bg aegb caedfg eacgbfd dgb dgaec gdbaec | gbea eacgd gb cgaebd
ag ecdgbaf edcba eagcd aeg gcfa dfcgae egcdf gfdeba fcgbde | fgeadb dacge fdagceb egafbd
fdbgec bge fdacbe bfdga edfgb cbgaed gefc cefdb ge baegdfc | ebfgd gbe begdf eg
cedafb gdab fgced cabdf fgbaecd dcbgf gb bgc gabfdc ebfgca | bcadgf cbfda cdgfab agdb
fdcbg bgface ecdabf ad fgcbaed gcefad dac efgac cfagd daeg | dcfabge edga ad gcfea
bce gaecdb aefc bedfgca acbfge aebgf fbceg defgba dgfcb ce | ceaf ce bfgce ce
gefadc gefadb agd ebfcdag dbeg dfagb abfed gafcb dg adbcef | fbgad bged gd cfebda
dcfge egdbf adcgf bafecgd ecd gdfeac bdecaf ec cfgdab cage | acge dacgf facdg cgea
bagdec gecdfba gdabe deb eabc be acdgfe afgdb deagc gdecfb | aebgd cgdea adbge eabc
gebca bedgaf ac gcbeaf bcagedf bafge egcfda bedgc cabf cea | ecbdg efcgab ebagc bgadfec
edacbg gcfad efdabg bc cgfdbae bdc cfedab efbc beadf fadbc | dbc fcbe fbedacg cabfd
adfgce acgfb bgfadc dgbca fc gfc fgacdbe fagbe bcdf ebagcd | cbagdfe gfc fgbac cgbaed
gdbfec bea acdb ba bcged edabgc gcfaebd afebcg gedba gdeaf | ba fgcebd efgda gadfecb
gd gcda bgd gafbcde bdcfag bcgdf cedfb bgefda eacfbg cfbga | fgcab fdgcba bgacf bfedga
fgcadeb abdgc gce bagec fcbea ge aebcgd gead cedfgb bagdfc | gbdace baefc fdcagb cdgab
fbca aedfcg eagcbd fadbec bdgef fec cf bgcfade dbcef cbade | fgedb bgefadc fc cf
dbgfa feca gfbced cf abfgce fcb cafbg eabgc acdbge edafcbg | abgdf dbcgef geabfc fcb
dbaf fag bdfgea ecbgfa dgbfe fa edacg agcfdbe cefdbg fdgea | fgbaed cdegbf dacfbge gdcfbe
abc acefbd gacedbf gbcde dgabf ca fgcedb cega dgbca cgeadb | ca bcged abgfd bca
ceabgd cgfeb ea aedf gcbdfa cadbf acdfeb abfec eac dgcefba | cgdbafe cea adfe aec
dbcfgae agebc ebgd cebdfa ebacdg deagc be fdecga gbacf bce | deagcb agecdf caefdg cegad
dcaeg gea gdaf befgac cefgd facbdeg dcgbfe ga dagfec adecb | gadf gaedc age cabedgf
bead dagfbe gda bagfec ad egfba ecgfad fdcgb ecfabdg dfgab | agefdc dga bgfea dafcgbe
abcgd gecbfda cegd badfge bcaed fcaeb ed adcgfb gcabed edb | acefb acbgde deb ed
cgefdab dgbcfe gbe beca be fdecga bgcafe bfgda abgfe fgace | bfgda eb gbe bdgaf
age cefbadg bcfea cdagbe begca ag gedfca cbfdeg ebcgd dbga | efbac ega fecab dgba
cgedf fc defbgc cdf fceb dgebf bafgdce eadbgf ecgad gcfadb | cf deabgfc efgdbc fgbcda
bdgeacf bagdef gb becg bcfage cafbg bga fdbac caefdg eafcg | bcedafg cbeg bg gb
dfceb gcdab ecbad gfbdea eab gcbeda bgcdeaf ea abfgdc cgea | baedc egca dgacbe cedfb
gdaeb dgfbc cefg fe cfbgaed gdfeb fadcbg defgcb abcdfe dfe | gdcfba eagdb fe cfeg
abfcedg afgcde acgfd ae bdecga efbcd dea fdgabc feacd gfae | afdcge gaef aefgcd geacfd
beadfg cfdgb fdb agfcbde db abcgfd ecdgf cbagf bafgce bdca | efgcd dcefg cdgfe bd
fagbe fedbcag afbgcd agdfbe age adgcbe ea fead egbcf abgdf | gae cbegf afgdb ea
badfcg fagdc gaf fedcgba dagb acefd dbgcf cgfdbe ag gbecfa | ag bagd bdfgca fbadgc
cfebg gc fcbea dcbafe gbc gcedafb cegdba cagf fbgde baecgf | cg agcf befgc bgc
fegb ecdaf ecb gacefdb eb gdacbf cdegba abgfc afceb acegfb | ebafgcd afdgceb ebc be
adgb cgbae bg abecdg gcefa ecbad dbgfec gbe cbfdega baecfd | badg badg dbag aegbc
ab bfa gbedcaf adbgfe bfecg acefb fgdcae bcad fceda ebafdc | gebdaf edcfag ab ba
ceadbg cd gfdeca ebgfd gdfec dce fegbca ecfag fadc gdeabfc | gfaec gface fdac ecadgf
fbagd egdafb bdg adcefg gbfac daegbcf eadgf bd bfde dgceab | dbfe bd agfde agdef
ebg bgdfe acebdg dcfage abfdge agefd dgbcf eb bdfegac afbe | cdfbg aefb dcagef edfga
bfcd efc bfdcge gbdec bagfe agdceb cf dacgbef bfgec eadgfc | cfe dcbf cgebdf dbecag
cda cefdbg fgeca fdgbeca acgdf bdcgf da deafcb fbgcad bdga | afgbcd cfega gbfcda gdebcf
dcaf cagfb gdacb bgdfca fgaecb ebdag cd fdgcbae dbc bcfegd | cfdegb dc abcgd cbfedag
gabedc dgfab ecdfbg adegf afbc dbagc bacedgf bf fdb fdcbag | cfedgb dgcfab gbfcead dcbga
bcagde efd bgfcd fdecb aefb fe dcfbae gacfebd caebd afecgd | ebcdf afeb ef befa
abfc gcdaef agecdfb cb gacedb cgb gcbdf bdgfac gedfb gcdaf | gfceda bc bdfgaec bdgef
geabc gcebad cgbeaf begdcf gfabe fe bfgda gfe afce ebcfagd | febgac fcea fbcged baecgd
aedfb gbfe acbgde fe fcedgba aef fcadge bacdf fegdab ebdga | fcgdeba afdeb ef fae
gbcfd adfgcb gba dgcbafe agbfde ag cfag fecdbg gbdac edcab | fgcbd bgcad agb bdefga
agfde efgcba gaefc bdcfeg cfe bafc bgcedaf egbac agcedb cf | gebacf ecgafb gfceab cefag
febdc begcd ecgbdf dbacfe bdfagc adgec gbd gb afbdceg gefb | dbg gacbfd egcdfb gfebcd
decfbg dagfb afcgbd gdcfb cbaedf caefgdb da gdca gafeb abd | adbgcef dgafb abd cfbgeda
begcfd bgeaf dcfaeb cadfeg gcdb cadbgfe cg ecfbd cfbge cgf | cg gfc gdcb gcf
ecagdf dfcebg fgbea bg cbeadgf egb ebcafg edfab gbca fegac | bafeg aebfd gcab fbecdag
dgacf abfecg fcgadeb cgd gdbace cgbfa egfda dbfc cdbfga dc | cfbd gabdce dbgcaf egfda
afedcgb afge bfead gf gdfbe fgabed dgcbe dfceab dgf fabgdc | feag cdabef bgdacf gfea
adebgc de bfgacde beafdg egabc egcabf deg cead cbdeg dcgfb | ed eabdgf eadc dbafge
fcdbgae gbacf ebgdfc bcadeg bdgea fagdbe gce aecgb acde ce | cge ecg cabeg efdcgba
aefgcb fedagcb cefbdg ef dcbeg bacdge fegd edbfc fabdc cef | ecbgfd gdef bacdeg bcdeg
geadc gecfad gfcadb eg agdcf bgafce gdef acdbe gea dabegcf | ecfbag gebcaf baecd dfeg
cdeba bd aegcd bfadec ecfab bafd agbfce bfdcge bdc cgebdfa | abcfe agdce db adecg
fbadg faedgc bgdac bcegd bcadge agc gdefcb cbae gdfbace ca | defgcb ceagbd bdgca afedcg
acgfd cbdage agcbd dfa edcfg fa dbcfeag fabc dbaefg gdbcfa | af gedabfc af ebgdfac
dafgbc dfegc dc cgfae cgeafd cdf cagefb ceda bfged fdegcba | aecd cade deac fbged
cbegda dgfeb gefcb aefbgc fdgceb agdfe dgb bd fdbc bdgfaec | dcgbeaf db gfbed efdag
aefbd cdbeagf gd fdgc fgcdab afbcg bdg egfabc bfgad aebgcd | gdfc gadcfeb cfgd fgacb
eb gcbaed aeb fgdecba begd eagdc cadfb eabdc cfgade gacfeb | egdca dcbea bdfcgea be

78
2021/d08/ex2/ex2.py Executable file
View file

@ -0,0 +1,78 @@
#!/usr/bin/env python
import itertools
import sys
from dataclasses import dataclass
from typing import Dict, List, Set
@dataclass
class Entry:
signals: List[Set[str]]
outputs: List[Set[str]]
def solve(input: List[str]) -> int:
def parse_entry(input: str) -> Entry:
signals, outputs = input.split(" | ")
return Entry(
[set(s) for s in signals.split()], [set(o) for o in outputs.split()]
)
def deduce_signals(entry: Entry) -> Dict[int, Set[str]]:
_1, _7, _4, *signals_to_deduce, _8 = sorted(entry.signals, key=len)
signals = {
1: _1,
4: _4,
7: _7,
8: _8,
}
for sig in signals_to_deduce:
match = len(sig), len(sig & _4), len(sig & _1)
if match == (6, 3, 2):
signals[0] = sig
elif match == (5, 2, 1):
signals[2] = sig
elif match == (5, 3, 2):
signals[3] = sig
elif match == (5, 3, 1):
signals[5] = sig
elif match == (6, 3, 1):
signals[6] = sig
elif match == (6, 4, 2):
signals[9] = sig
else:
assert False # Sanity check
assert len(signals) == 10 # Sanity check
return signals
def deduce_entry(entry: Entry) -> int:
decoded_signals = deduce_signals(entry)
res = 0
for output in entry.outputs:
assert output in decoded_signals.values() # Sanity check
for n, signal in decoded_signals.items():
if output != signal:
continue
res = res * 10 + n
return res
entries = [parse_entry(line) for line in input]
return sum(map(deduce_entry, entries))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

200
2021/d08/ex2/input Normal file
View file

@ -0,0 +1,200 @@
badc bd dbeaf cfdbge dfb cfbdea efbag edcfgab dcafe degfca | eacfd acdfbe cbdegf fcbaedg
cd fdbac egcfab gbadcfe cfgdeb cbadfe deca cdf dfabg abefc | dcf cfbad gbafced fcd
cg agecfb cbg eabgfdc egdc fdgba bafecd cbdfe bfcdeg cfgdb | efdcb adcfeb fbdcg gbc
bfceg gfadb dbcfgea bgaef efad abe bcdgfa ea fbdgea agecbd | eadf ceadbg abfge fecdbga
gdcafe eacb adc gbfda afdceb edgbcf badfc ecgbafd ac fdbce | ebfcd cefdab bdfgeca egbdacf
aefbgd fbdc fbecg egdcb edcbag bf gcefa fbedcg bagfced fbg | afgce cfbd bdcf geafdbc
ebgacd edacg gcdeaf cabfg dbc bd cdbgaef ebad fecgbd acbgd | dgbac db caegbdf gcbfa
defbga gead ge efagcb gdafbc cegbdfa egf bfdce bgdef dgabf | gef eg bfcdag ge
bae adcgfbe cdfeag ab dcgeb bfdage agecb cagfe abcf bfcaeg | bae aeb agfceb afceg
ae adefcb gaceb gadceb edag efadcgb badgc cfgbe cadbfg cea | ceabgd fbeacdg aged aged
aegbdc dbegfac gcbaf df febd egdafc deabg gdf gdfeab adfbg | bcdegaf dfeb bcegad df
aegcdfb becdaf gdecf ag fag dcfgba badg fcbad cdgaf acefgb | abcdf cefdab ga ga
gbcfad cefadg cegad aefdb gedacb gcfe fc cfbgead cfade caf | degac egcf geacfbd afc
bcfad afcdbe fd ecfba cfd afde cfeabg gcbfed dacbg fdgabec | cafbged edfa fcd dabgc
afdc adbfe bgecfad ecagdb dbcafe dfe fd agbfe bdace gdbfec | fadbe efbdgc fadbe edcba
bdface geba ecbdag acegd afcdg ge eadbc bfecgda edg fgbdce | eg abge fadgceb abge
fagbde fcegbd gec efcdg fdgbe ebfcag gc adcfe cgdb cagbdfe | defcg cbgdfea gcbd ecfgd
acfbg debgf gfcdba edcbfa fbdag fgbeca cfbagde gadc da bda | dacg acbgf cedfgab caefdgb
ca dfegca edcbgf gfcde aedbg gfdeacb dcbfea facg dgcea ace | fedcg fdagec cea ac
acdf gcfde debcga ecbgaf cf ebgdf bagfdce cedag cef gedafc | dgcbea fc dcgeaf cdaeg
ebagf cabgfed ed cbgad afcbge afdbge edg eagbd cedgfb aefd | fcgdbe de cgbeaf ed
fbcdeag cafbd fgcbea fdgacb daecfb cg gca defga gbdc fdcga | abfcged agc acfgd cbfaeg
dacfe ed ebdcag gcadfe dfbcaeg dce dfeg fbcadg bfeca acdfg | fcbdga gcbade aegdcf fbcgda
cfdegb cbef fgdec gdfbca gdecbfa egfdca bc gbc cbegd aedgb | befc bc abegd ebcdfga
agcefdb fdbg bf bdafe gdebaf eafdg acdbe gcebaf bef edcfga | bf fdbg efdab gefda
gcb bdcgef dcbfg bg edbg facegb cbedf edgafbc dfgca edacbf | gb cdebfa deagfcb bg
eafbg gfad afegbd caefbg befdgc ebagd gd bdg cfbdage acdeb | gd geafb cabed dbg
bcedfg dc dgc aedfbcg acgbed bgfead gedbf bfdcg cedf facbg | gabfed cdebag dafegb cd
fa adbceg afecd cafdbge eacgfb daegcf aef bcdfe fdga daceg | acedf afe fae afdg
cdfaeg agcfb bcdgea dcafeb cef fabec ef dfgcbae defb eabcd | gdabce fdeb ebdf ecf
cdefg ceafg gebfd cdg dc efbcdg cdgbea cfedbga cbfd fdagbe | fbcd afdgbe fabedg dcfb
cdafgb egdb fecgabd cbfdg fcbedg feb acefg begcf be dacebf | be be ebfdca abgfcd
eafgcd facegb acb cdbfaeg fagec dgcabe baefd cgbf cb afecb | bcafeg fcgb gfdeac eagcf
ecgdfb deb edfgab eb faedcg agfed bfaed dbacf gbae efgdcba | fabde eb ecdgfb eabg
aefbg bdegfa gacdb gefc fbc fc bfdcae gefbac cbfag dbgefac | ecfg fc cfge efgcdab
gcfbed gcbae dcaef cbegad bdag bgeacf adefgcb deg adgec gd | agcbde bgad gdbfec gcfabe
dfacb egbcda dac agdcfb fecab adecfgb bgadf cd fgeabd cgfd | gabfdec acd dc dc
bfecg dcgeab af gadf aedbg caefdbg bgfade bafeg abf eabdcf | ebcfg gafd ebacfd fgbdace
ge fdeab gcebfda fadcg fgead abcdef eag ebdgfa acgbef dgeb | cdagf cfdgbae fdeab eag
dba dgfceb fbcegad bagde bcdeag bcedg bdafce agfbe ad cgad | bdcafe agdc ebdcg cdga
edfg fedac fbegcad acgfe fabcd ead bgefac de daecgf bcaged | bafcd ade bacefg fdbac
gdecbf edabc ef fcgead fcadg bdcfga fec ceadf dgaefbc agfe | faedgc ef gabfcd fe
deafg eb egbaf cadegf fcdeab degb bcgaf bfe bfcdega gefdba | abgfe eb gfdaeb efdcga
fabcd beadf efabgc fc cafdgb gcfd adfcgeb abgdce cfb dgcba | dcgf bdcgaef gdcf cfdg
ebac acedf ceadfb cfe dbcgaf ec efcgdab degaf abfcd edgbcf | ce edgaf dgbceaf fce
ecbag fg gacdfe edafgcb cgefa eacdfb deafc bdagcf gcf dgef | fged gedf cefdga cabge
dgebc bcg cg gdeabf cgfeba gdfc gefbdc adbec bdefg fgcdbae | begdf cfgd eadbc gbfde
aefdb ecgb dgaeb agdbce ega fgdacb bcadg cbdeafg ge edacfg | age fcbdaeg gea bcfegda
cdeabg fcgda bdagc acfb dfa af cdeabfg efdgab fbcgda decgf | bcagd cfba gedfc af
ceadb caebgfd bdfaeg cbgaf ge ega gadcbf gfce ebgcfa gebca | aecdb febgcad bgcaf fadcgbe
daegb gfdae ceagfd bgfade feagcb fbgd bg cbagdfe ebdac ebg | gefbacd dbgf geb adgbfe
ecadfg acdfb aefgc ecbfdag dce cfbega fcdae edga gbecdf ed | fcdba ced egfcba dega
defgbc cgdea efag fdbcgae acbdf gcdaeb fe dfcega efc defca | fage fec dcafeg ecagfdb
bg dbcafg gdafecb dgfcae defcg gebd cefgb bgfecd fbaec fbg | gbf dfgec bedg cfdgae
ebgda ebd adce cdgaeb edfcgb bcagd agdcebf ed agbef gcbfda | efagb gbefa dbcfgae eadc
ebcfdg dbe de cdabgf fgdab dgae aecbf abfgced edbaf eagdfb | bface cdafebg gadcefb fedab
agc dabcge agbcd cabe dbagf cfgeadb gecafd gbcdfe ac gbced | cbegd beac dgbce ac
fdagb bdeacf fga eagd gcfdb gecafb ga feabd abfgde dcbfega | bdafe abgcfe gbecfad abdgefc
fbecd dbaef bdgc debafgc daecgf ebfcgd ebc abegfc bc gfced | fadecbg cb cdgeaf bfade
fagbe aebfgcd gef bgadf cbagfd deafbg cfdeag ge begd beafc | bcfadeg edbgaf fcgaed ge
gabfc gabde fbdga bfegca agfbcd df cdfagbe dcfg caefbd dbf | decfagb bdf gfcd fd
bdafeg gf gbadf gefb cgefda daecbfg bacfed cdagb fdg fdaeb | daegcf eagdfb adgcb bagfed
dafegb efdac bda fgedbac egfcdb bgfa fbaed bgdef debagc ba | ba bgcead bad ab
cfged cbegdf cb ecgdb adefbcg fdcabg cdb cdagef efbc gedab | bc gdebcf gecdb bagdcf
fbegcd gcedba eba acegf dbfa efbgd ebfadg ab fgceadb bgfea | fgbae gbedcf degfb agdebc
ceadf fdacbe ef edgafb dfe bgaedc cadfg bcef cfebgad ecabd | cafde fabedc debac eacfd
cd dgafe cbgfae decfg cfebg cfd gdbc febgdc fbegacd fceadb | dc beagfc aebcfg gcfeabd
adcefbg caefgb bafd dface bdcfe cegbd fbe gdfcae dceabf fb | efb ebcgd gbdcaef fb
fbdg gebac degbfc gd dfeagbc decfb gcd dafgce faecbd bdgec | gfdcae gfdb dg afgdce
fb fgedca fbe gfcde gcbfde dbceaf bgeca eafbdcg dgfb fbegc | fbe cbdegf bfe gedfc
acfbe fcbaged bgdcf cabgfd ag cagd fcbga bcfegd gfa eadbfg | afgcb gbdecf efdbcg fag
fbegc dgca dfc fagbedc bfadcg dc ebdfca fadbg cgbfd gfdbae | dabefg egdbaf gaebdf dc
cdbgf bdfaeg ca fgcab dgacfbe feac facgbe ebgcad bca gfbea | dafbeg cab cdagbe debfga
gfbe ebcfag bcedaf be egfac fegdca gebca eab cbgad gfacdbe | dabcgef bcagfed gcfea bea
bdefacg fbgedc efdagb beg geabf eg dafbce dage gfabc eabdf | gead gcafb ecfdab egfbdac
gd gfecbd dacefb dgbf cdegf acfge agbfced becdf aedbgc gdc | gdcef cgbead gd gefacbd
cgbfea cafed dbgefac gacb bc afbge fdabge efbac bcfgde cbe | gbac fcdbge agcb aecgdfb
abgefc cfeba gcdba fgdcea gf fcbgaed eadfcb gbfe fga bacgf | efcba fabecd dgcbfae eagcfd
gcdbfe fdec gcfdb bdfeg de efgba cbgdafe afgcbd cgdbea ged | cedgbf fced agfcdeb cgfbed
dbacfge cafdb gbcfea aegc afbec ea bae bgcedf befgc edgafb | dfgeabc bfgcde faceb fbecag
bfcgad bfegd fg dcgeb egaf bfedcag efabdc dfegba gfb abefd | gf abdgcf dcbfag fgb
degab dgebac bfdeacg cadeb fbdeac bag debgf cgda abgecf ga | dceba fdbeg edbac dbacfge
abdef gcbead bgce cgdab deg dgcbfa aedbg fbacdge eadcfg ge | cgfdba gcafde agfcedb fdacbge
defabg cabdg gcfed cabe gbeacd fgadbc eb geb aedcfgb bdgce | eb dbegc dfecg dbecag
egdabcf edcgb bcedgf cgb bgfe fcgabd bg acgfed cbaed dfegc | cgb gbc egcfad degbc
fe agebdc gfbead fcdea abecgfd bafcde beacd cbef def cgfad | dabgfe fde adfcg bfgead
ba fcabed bad dgacf dfgecab dcgba gdecb bdgcfa abgf caegfd | gdecb dgcfba gcdafe afbedgc
bdgef cdbgfa acbed gedcfb efdgba cg abcgedf cgb cgdbe cfeg | edabc ebdca edbcg ecgf
edfba bcfgead cbged defbcg ag gfadce gcaebd edabg cabg dga | fedagc bcag bdegc gaebfdc
edab fed egcfb ecgbfad fegbd fagdcb degcaf afgebd gabdf ed | def gdefb fgedb edcagf
fc ebfgda ebcfd cabed gcadbf baecfdg cgfe fdc egdfb gdecbf | defbg aecbfdg cadbegf adgfbe
bgdfc fgc fdegb dgecfb fcabd egbafdc gc dgce gabefc gedbfa | fbdgce gc dbfegca cg
bgedc ged gdfcea dcefb cdabg bgfe defbgc bdecaf dcgbfae ge | badefc gbfe cbegd fdagbce
cdefga cgdafeb cbe becdg gdefc cfbg cfdbeg cb febdac edgba | edbgcf abecfd ebgfcd fgbc
de aegcf agedcb afegbdc edbf fbdga facgdb gdaef dbaegf dea | cdabgf ead afgbcd edbf
cfagde eagfbc cbegf cg fcaegdb ebgfd gcf dafebc efabc acbg | bacfe fgc gcf cg
gadfce fbcaeg bdag eba bedcgfa dcfbe ab ebdgca ceagd ecabd | eabdc badec cdaeg dagb
aedb ebcagf fdeac be bdgfc eafbdc abcfegd aefgcd feb ecdfb | fecbd adeb be bfe
dg fgdecb dfecb fdcgeba gedcb gceab fdge cgd cgdbfa baedcf | ecbgd cagfdeb dcbfeg gd
fgcead bfdcag gecdf dfeacb dce aged gcbfe dcfgeab ed fgcda | cfegd de gcafd gefdc
dbec eafbdg dafegc eac gbacf decagb bcgae bdgea gbcefad ce | becag edgab cbadgfe edbc
gefcab agbdc febd bcegd de ecbfg afgdce ebafgdc dfebgc egd | de gdecfba edbf cdegb
abdcefg fbdega dcfag bd fegba badgec fgadb edbf fgaceb bdg | acdfg fagbce adcfg abgfd
ag agdb bfeag fbgdace bcfeda dgafeb egafdc begfc dfeba eag | ecbfg agbd dabg bfeacd
dagc dae fdaec edcafg fbadeg cgefa abcgef ad dfecb fdgcbae | eda eagcbf agfec debfc
ead beafd dfagb faceb bfedca facegd aefcbg cbed ed gfcbaed | abgdf eda agbfce ead
afdgb eabcgf dfgbe fecdgba dagceb gfa cafgdb af cdfa abdcg | abdfg dceagb decfagb fagcedb
dbcfga cb acb gcbafde fcbg egbfda dceaf gbfda dcfba cdbgae | fcadgb cdefgba agbfd cgfb
gceb fcgba cefdba caebfdg egfadb eagfcb cb beafg acb gfacd | abcedf bca gfcda bcge
eabdc caebf ad dgabcef fcaedg cfegba deacbf dabf dca begdc | dbgce dbfa fgebadc acedb
fbgce ec gcea dafcgeb fgbed fec adgbcf abcfeg fcbga cfbade | cbdfag ebgfc fgcbe agbfdc
afcg afebcg becgf fgadeb bdgaefc fa afe acedb abecf gdfebc | fgecb eacdfgb ceabf cgfa
fb gadeb aegdfb abgfe fab dfgb adfecbg bcaegd ebdafc cafeg | becdfga bf fbadec bgfd
ab dgafe fdaeb adb agcedf ecbfd agfb caedgb bdfage egfabdc | fedbc dfecag afdcge becfd
bgedf dgecfb fae gbaec fa cedafg dcabfge gedfab gafbe fbda | fcgdea geadcfb efbdg fa
cgfaed fdebg fcgabd fcegb bd dfaeg gfbeda fgdceba edba dgb | db ebcgf fgcbe edgfa
fgc egcafb dcgb fagdb bedafgc cdbgaf fedac cg gacfd dfbage | febgad cgfaeb fadbg gc
fdaebcg deabgc bfdeg dfcaeg efcgd cdaf cfbeag fcg cf cgeda | bgefcad bgdfe dafc ebagfc
egcfad dbecf cbgf fbe aecbd bcfdge dfbgea aebgdcf ecfdg fb | cgfb gecfd gefacdb fbcg
cdb adcf gbcdae bdceaf dfaeb cdfeb febgc fgedba cd cgdafeb | eadfb fcgeb cbedga bdacge
gd dacg gbdafc gfbdc bdegaf fbgca cbgdefa cbafge edfcb dfg | gbcafd dgac dgcafb cbfgae
cbf dafeb fbceag bc egbdfc edbgafc fbced ecdfg cdgb gdafec | bcf dbfea ebagcf gadfcbe
dcebgf eagfc aebfcd edcbg baefgdc fd cdefg dgaebc fde bfgd | ebagfcd acgefdb fagec gbdaec
afgdc bagec bdfg fb ecgdbfa fab dfecga bcgadf dcafbe bgafc | afgced fb dcafg feabcd
gef febag ebagc egcfbd fg cafg abfde abcfge afcgebd ebcgda | egf gaebf dfeba gadbce
ag cdeafg gdbfce daceb acgf efgbda adg cegda gaedbfc efcgd | debac eadbc bgaecdf cgbaedf
acefdbg debgac ecdgf ead facebg eafgb ad fedbga bfda eafgd | adbf eagfd eabcfgd ecagfb
cd bafegc fgcaed fcaeg dgc fdcag agdbf fedc gbadce egdabcf | fedbcga bgafd fcde cfed
decg egf ecgbfa degfa edbagfc dgcabf cdfag eg fcaedg faebd | gcadef efadg egf eg
feacbg cgfadbe efb efbga dfagb eb aefgc bgec cgedaf dcebfa | eb be be fbe
begc eacfdb dfacg agcfe ce eagbf eac eafdgb aefbcg dfgaecb | ce ce ce gbce
dfeagb dgaeb dbagefc gbcfae degf decba cbgdfa bgfda eg gae | ge agfdcbe bdace fdeg
cgdab cegdbf cfbad bg aegb caedfg eacgbfd dgb dgaec gdbaec | gbea eacgd gb cgaebd
ag ecdgbaf edcba eagcd aeg gcfa dfcgae egcdf gfdeba fcgbde | fgeadb dacge fdagceb egafbd
fdbgec bge fdacbe bfdga edfgb cbgaed gefc cefdb ge baegdfc | ebfgd gbe begdf eg
cedafb gdab fgced cabdf fgbaecd dcbgf gb bgc gabfdc ebfgca | bcadgf cbfda cdgfab agdb
fdcbg bgface ecdabf ad fgcbaed gcefad dac efgac cfagd daeg | dcfabge edga ad gcfea
bce gaecdb aefc bedfgca acbfge aebgf fbceg defgba dgfcb ce | ceaf ce bfgce ce
gefadc gefadb agd ebfcdag dbeg dfagb abfed gafcb dg adbcef | fbgad bged gd cfebda
dcfge egdbf adcgf bafecgd ecd gdfeac bdecaf ec cfgdab cage | acge dacgf facdg cgea
bagdec gecdfba gdabe deb eabc be acdgfe afgdb deagc gdecfb | aebgd cgdea adbge eabc
gebca bedgaf ac gcbeaf bcagedf bafge egcfda bedgc cabf cea | ecbdg efcgab ebagc bgadfec
edacbg gcfad efdabg bc cgfdbae bdc cfedab efbc beadf fadbc | dbc fcbe fbedacg cabfd
adfgce acgfb bgfadc dgbca fc gfc fgacdbe fagbe bcdf ebagcd | cbagdfe gfc fgbac cgbaed
gdbfec bea acdb ba bcged edabgc gcfaebd afebcg gedba gdeaf | ba fgcebd efgda gadfecb
gd gcda bgd gafbcde bdcfag bcgdf cedfb bgefda eacfbg cfbga | fgcab fdgcba bgacf bfedga
fgcadeb abdgc gce bagec fcbea ge aebcgd gead cedfgb bagdfc | gbdace baefc fdcagb cdgab
fbca aedfcg eagcbd fadbec bdgef fec cf bgcfade dbcef cbade | fgedb bgefadc fc cf
dbgfa feca gfbced cf abfgce fcb cafbg eabgc acdbge edafcbg | abgdf dbcgef geabfc fcb
dbaf fag bdfgea ecbgfa dgbfe fa edacg agcfdbe cefdbg fdgea | fgbaed cdegbf dacfbge gdcfbe
abc acefbd gacedbf gbcde dgabf ca fgcedb cega dgbca cgeadb | ca bcged abgfd bca
ceabgd cgfeb ea aedf gcbdfa cadbf acdfeb abfec eac dgcefba | cgdbafe cea adfe aec
dbcfgae agebc ebgd cebdfa ebacdg deagc be fdecga gbacf bce | deagcb agecdf caefdg cegad
dcaeg gea gdaf befgac cefgd facbdeg dcgbfe ga dagfec adecb | gadf gaedc age cabedgf
bead dagfbe gda bagfec ad egfba ecgfad fdcgb ecfabdg dfgab | agefdc dga bgfea dafcgbe
abcgd gecbfda cegd badfge bcaed fcaeb ed adcgfb gcabed edb | acefb acbgde deb ed
cgefdab dgbcfe gbe beca be fdecga bgcafe bfgda abgfe fgace | bfgda eb gbe bdgaf
age cefbadg bcfea cdagbe begca ag gedfca cbfdeg ebcgd dbga | efbac ega fecab dgba
cgedf fc defbgc cdf fceb dgebf bafgdce eadbgf ecgad gcfadb | cf deabgfc efgdbc fgbcda
bdgeacf bagdef gb becg bcfage cafbg bga fdbac caefdg eafcg | bcedafg cbeg bg gb
dfceb gcdab ecbad gfbdea eab gcbeda bgcdeaf ea abfgdc cgea | baedc egca dgacbe cedfb
gdaeb dgfbc cefg fe cfbgaed gdfeb fadcbg defgcb abcdfe dfe | gdcfba eagdb fe cfeg
abfcedg afgcde acgfd ae bdecga efbcd dea fdgabc feacd gfae | afdcge gaef aefgcd geacfd
beadfg cfdgb fdb agfcbde db abcgfd ecdgf cbagf bafgce bdca | efgcd dcefg cdgfe bd
fagbe fedbcag afbgcd agdfbe age adgcbe ea fead egbcf abgdf | gae cbegf afgdb ea
badfcg fagdc gaf fedcgba dagb acefd dbgcf cgfdbe ag gbecfa | ag bagd bdfgca fbadgc
cfebg gc fcbea dcbafe gbc gcedafb cegdba cagf fbgde baecgf | cg agcf befgc bgc
fegb ecdaf ecb gacefdb eb gdacbf cdegba abgfc afceb acegfb | ebafgcd afdgceb ebc be
adgb cgbae bg abecdg gcefa ecbad dbgfec gbe cbfdega baecfd | badg badg dbag aegbc
ab bfa gbedcaf adbgfe bfecg acefb fgdcae bcad fceda ebafdc | gebdaf edcfag ab ba
ceadbg cd gfdeca ebgfd gdfec dce fegbca ecfag fadc gdeabfc | gfaec gface fdac ecadgf
fbagd egdafb bdg adcefg gbfac daegbcf eadgf bd bfde dgceab | dbfe bd agfde agdef
ebg bgdfe acebdg dcfage abfdge agefd dgbcf eb bdfegac afbe | cdfbg aefb dcagef edfga
bfcd efc bfdcge gbdec bagfe agdceb cf dacgbef bfgec eadgfc | cfe dcbf cgebdf dbecag
cda cefdbg fgeca fdgbeca acgdf bdcgf da deafcb fbgcad bdga | afgbcd cfega gbfcda gdebcf
dcaf cagfb gdacb bgdfca fgaecb ebdag cd fdgcbae dbc bcfegd | cfdegb dc abcgd cbfedag
gabedc dgfab ecdfbg adegf afbc dbagc bacedgf bf fdb fdcbag | cfedgb dgcfab gbfcead dcbga
bcagde efd bgfcd fdecb aefb fe dcfbae gacfebd caebd afecgd | ebcdf afeb ef befa
abfc gcdaef agecdfb cb gacedb cgb gcbdf bdgfac gedfb gcdaf | gfceda bc bdfgaec bdgef
geabc gcebad cgbeaf begdcf gfabe fe bfgda gfe afce ebcfagd | febgac fcea fbcged baecgd
aedfb gbfe acbgde fe fcedgba aef fcadge bacdf fegdab ebdga | fcgdeba afdeb ef fae
gbcfd adfgcb gba dgcbafe agbfde ag cfag fecdbg gbdac edcab | fgcbd bgcad agb bdefga
agfde efgcba gaefc bdcfeg cfe bafc bgcedaf egbac agcedb cf | gebacf ecgafb gfceab cefag
febdc begcd ecgbdf dbacfe bdfagc adgec gbd gb afbdceg gefb | dbg gacbfd egcdfb gfebcd
decfbg dagfb afcgbd gdcfb cbaedf caefgdb da gdca gafeb abd | adbgcef dgafb abd cfbgeda
begcfd bgeaf dcfaeb cadfeg gcdb cadbgfe cg ecfbd cfbge cgf | cg gfc gdcb gcf
ecagdf dfcebg fgbea bg cbeadgf egb ebcafg edfab gbca fegac | bafeg aebfd gcab fbecdag
dgacf abfecg fcgadeb cgd gdbace cgbfa egfda dbfc cdbfga dc | cfbd gabdce dbgcaf egfda
afedcgb afge bfead gf gdfbe fgabed dgcbe dfceab dgf fabgdc | feag cdabef bgdacf gfea
adebgc de bfgacde beafdg egabc egcabf deg cead cbdeg dcgfb | ed eabdgf eadc dbafge
fcdbgae gbacf ebgdfc bcadeg bdgea fagdbe gce aecgb acde ce | cge ecg cabeg efdcgba
aefgcb fedagcb cefbdg ef dcbeg bacdge fegd edbfc fabdc cef | ecbgfd gdef bacdeg bcdeg
geadc gecfad gfcadb eg agdcf bgafce gdef acdbe gea dabegcf | ecfbag gebcaf baecd dfeg
cdeba bd aegcd bfadec ecfab bafd agbfce bfdcge bdc cgebdfa | abcfe agdce db adecg
fbadg faedgc bgdac bcegd bcadge agc gdefcb cbae gdfbace ca | defgcb ceagbd bdgca afedcg
acgfd cbdage agcbd dfa edcfg fa dbcfeag fabc dbaefg gdbcfa | af gedabfc af ebgdfac
dafgbc dfegc dc cgfae cgeafd cdf cagefb ceda bfged fdegcba | aecd cade deac fbged
cbegda dgfeb gefcb aefbgc fdgceb agdfe dgb bd fdbc bdgfaec | dcgbeaf db gfbed efdag
aefbd cdbeagf gd fdgc fgcdab afbcg bdg egfabc bfgad aebgcd | gdfc gadcfeb cfgd fgacb
eb gcbaed aeb fgdecba begd eagdc cadfb eabdc cfgade gacfeb | egdca dcbea bdfcgea be

42
2021/d09/ex1/ex1.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import itertools
import sys
from typing import Iterator, List, Tuple
HeightMap = List[List[int]]
Point = Tuple[int, int]
def solve(input: List[str]) -> int:
height_map = [[int(c) for c in line] for line in input]
def neighbours_of(point: Point) -> Iterator[Point]:
for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
x, y = point[0] + dx, point[1] + dy
if x < 0 or x >= len(height_map):
continue
if y < 0 or y >= len(height_map[0]):
continue
yield x, y
def is_low_point(point: Point) -> bool:
for neighbour in neighbours_of(point):
if height_map[neighbour[0]][neighbour[1]] <= height_map[point[0]][point[1]]:
return False
return True
def risk_level(point: Point) -> int:
return 1 + height_map[point[0]][point[1]]
points = [(x, y) for x in range(len(height_map)) for y in range(len(height_map[0]))]
return sum(risk_level(p) for p in points if is_low_point(p))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

100
2021/d09/ex1/input Normal file
View file

@ -0,0 +1,100 @@
9876543298765698956789545678992103976545976323678910997879876432123679765323567894679434987654596521
7998789129854987545697658789789212985439765454567899876568976569012489874212779943598929976653987432
6429898934943496434589769894658924976929898765679969965345698798923456976433567892456998765432196543
5210987899874987645678979943547895989899989898789357893234599987894678986547678901349899896563987656
4329896798765797657889989652126789998798967999899969954345989876789989797698789316498766987854598787
5498755679876798968997699643015678987676759899999898965959878965878997649899899997987654298976689898
7987834656997899878976578932123789998545745799998787899898767854567998798965969898999743109987789999
9896321345698998989965489543456999867623234567897676789789854343456799987654358799298654312398898989
8765430234999987697896789654567899954310123456998545785678963212567898992101245690129796453479987978
7654321345899986576997999876778978965421236567895431234789654345678957895212767789298987894567896669
9785532456799765434989643987989569877654345878976544345698765456789548954329878999987798979878975456
9876743767987654323679932198995476988765456989987655466899896787891239765434989989865679654989764367
0999899878998743212567893999674365699986567898998767877894949898962678986676799878654589943499876458
1989998989987654501457899876543234799897679987859878988943234999853467897897899867723457892398976567
9879867991099865212345789989656125789649899876545989999654345998765679998998987654012349910987987989
8768756689987654323456897298743239891234989765534597898798759879876789569989998632134598929876599096
8653344578999865784587976349994345910199878964523456789929898764987893498977987545245987999965432145
6542123469764986895678965499879966899989867893212345698939987653498902987856798767959876989996543234
7752056778953497898789878987769899998767656989101234567898765432459919876545689878998785678989654546
9854145789101298989899989876545768989654545679914348978997654321347898765434799989679654545678965687
7543236993212349878998998765432459876543234567895567889459865432456987654323988995498763234589896789
7654747894523498767897999654321345998769102678976988992398999545767899765499867894309982123456789999
8765678965654976654456899865210236799898993589987899101987978996988979876987658989219873256798999989
9876789998799865432345678994321346789987889998698978919876456789699767997996545678934965345789789778
4987899989891974321234789986545456899876777897569567898765347897432356989987439789549876956797674567
3298978978910985445445891297669767998785656789423456789653298976545469879876598999656989897896523456
3129469568899876656566990998798978989654345893214567898764989897656798767987987988978998679999314567
2012395436789987767678989899987899876543212379201879969889978789987987656799876877899976567898901238
2193989325679898978989579768976789965432101268912989456998965678999896547898765656999875456567895449
3989878934589769989793498959895689876743412347894592347987854567898786434569654547989765345478989567
9978968895699654997652987645784789987954523456789693469876432457897654321298763239876543217345678978
8867846789898969876543498732123678998766634787899989978994321367999865432598654567987982101234589989
7654437999987899989754989643034567899898745678998767899989993479899987543498765678999878212345678997
6542323899876789598769876542137878987999859899987656798767789598789897655679986799698765433456789876
4321016789765679349989987756756789876789878910199545987656698997655789796789997894549977567567898765
5633225899954598956799899867897897765689989429298769876845457986543456989899898943534988978678979987
8764334567893487897898789998998976454568997998999998765432356978532349878956769892123499989789459998
9875765789932346798997634569989432323467996987999989887845779865431234669545458789012943299892398989
5987876894321457989989429979874321012399875676889876998956889978560123458932345699129892109901987878
4598987899430349979678998899965456133987764545778965698768995989999294567890458798998789298319876567
3219399978921298767587897679876967845976543234667894539878954399878989698921239997987699987423965456
4301234567932987656456796567989878956965432133456793124989876498767878999432549876796589876539874347
5412345789743597542345895434593989869874321012349891013999999987654567898643498765987678987698765458
6523656897654987656556789323901299998765442123457999229878987896543456998789569979998789498789876769
5439787898765698787667893219892398999879543434568978998767986797855669999897689989879899349999987899
7656898919896789899878954398789987999989698545679567893457895498966798986998789394567988998789898989
8967999323987897976989765987679876889998787686893467892124789349997997895429893233459867999698769678
9698999934598956795499899876598765778999998797912349951015689123989876796210932012399756896569954599
6549387895999987989323902965439874355698789898909498932123478939876765989929993243987646789459893989
6321236789898999878919999876323943234987676989898987993234567899965434567898989349998789894398789878
5433345998797898767897889987213495349876565677797656789446889999876515698987678998999899965987698767
6985469899545989654665678999901989498765434545698768998658992398965106789976567987898999987976569656
7876798795439876543234889569899878999876323434569979569769321987654345678989456976447688999895498767
8987987654321989754015679499798767898983210128789989498999210398765656789654319895323567998789349878
9098999876510199863126789987652358997654321247899796987578931239976768899965498789212348987679257989
2129998995431239765257891099543768998765432656789545995467892946988979987896796568901236896578998996
3679976789542398954348999198959879999876558767893239876567899897899989896997987699322345985456789125
4599865679954987895459998987899989987998769879992123987678976789976799765989698789533959876348993234
5989234567899876799567897956789890196549878989789544998989765678965698943476569897659896987257899345
9976123456798765678989986844456789987698989395678959899895454567894567892565456998998765432125678976
8765277567987654569998765432349999898987693234569998789654323456923478921012345679539854321034567897
7654356679599987678999654321267898789965432145698987678943210147898569543235789794323969834245688998
8765467989459899789998874210158999679876541016987894599964521234997679659347898989019879765766899899
9876579894398789899987654332345696567987662929876793488895435349898989798959987879123989989877899787
3987698789298678967998965453456789349898799899985932376789655456789599987899976768934599999999989656
2498987679976543459019879867899892198789987789984321245698767567893498876789895457995678999878767938
1349876567895432398999989988957989987679876698765540356789878678932987545456789346889789998767656899
0198765456789321256789099899545678976598765549854321456899989799891095432347993275678992987654545789
1987674345699592345679129654334688965499994321965545567998999896789194321236892164568921298543237898
9876543234598989456989298743212567894389986539876667679567954965678987210235791013569432987654568956
5987621023987679867895497652101345789579876547988798789678942134567976521256789123456943498765678945
4997732654976598978996598764212345678989989656799899899989653655679876432345898235597894569876789534
9876543459865467899987987654325766789799999789899965957899879769989997543456797546678965778989899325
4988654598764348998998999766434589994598999898998654345678998998799987676567897667799796989999998934
3298797679974245697999429887847893212987898987987434234789897567688999789878998778945689998999987896
2129898989765134976878999998956789109876987656896510123498765434567899899989469899434567897898756789
3298959398754299865757889979767899912985436445689323234569854323776789998794357986523479956989434599
4987643239965987654345679869878999894976721336578935446798765612845899987654219875612396549876526989
9876543129876799765234889754999998789987530123489976557999976701234599998764329874323489932987789878
1995432035987999832123598932101989699998432236599987698999964312345678989876598765435567893499898767
3989654124599898641034567893219876568999543445679998899989899423456989879987699897576789965689989656
9878965435698765432155678965898765432987657656789999999875678954567999765698989998798999986789878543
8767896776789986749878789896799987621298767767899899989654579765678989854799878999939998997898765432
5456789987899987856989896789986798532349898898965778976543459879889875423999967894321987898969987643
2345678999998998767998945999965987643656929969434569198754678992994989567898656976439876749456898765
1234589923457899879987899889894398784789019654323691019867899921012398978997645897899985432345679876
2365699894578923989875987676789249895899998766513589129878987892124987899876536789999876521236789987
5456789689989012399954298545892134976999899895405678934989656789439876987654324679998787210123996798
7687893578899234569895987699976549899999789984312389545694545696598765699765312567899654323245695459
9798932345688965998769898987898799768987698765423578999943234789987674198653202469998765534367789568
9899321234567899899654679876789988657899579877567678987890125679876583019765313578929878946978897679
3976532345678945798753498765698976545986467988678789876789236798989432123984323489212989897889998989
2987645456789659987654579874987654324985358999799899765698945987998943439876534794343598789999999990
1298756569898798798765989989876543219876469999899988954567899876587894646987645679654987678999889891
2349869698999997659877996593987655424976567895999876543456998765456789656798789789769887567998768789
3567998987898989541988987432998966565987678934598765432567899854345699787899899899898765456789657679
7678987676487678932399998949899879677898989323459876554678998743234678998976989944999854346996545568
9899878554345568943567899898799998788969793212589988965689987664145699999765678932499710157895433477
1998765443123459956789998765678969899654579323679999878799876543234589987654567943987621238997312556
0197654321012367897991019834789456998765678954568954989899987654545678998765678954598534567898101245

61
2021/d09/ex2/ex2.py Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
import itertools
import sys
from typing import Iterator, List, Tuple
HeightMap = List[List[int]]
Point = Tuple[int, int]
def solve(input: List[str]) -> int:
height_map = [[int(c) for c in line] for line in input]
def neighbours_of(point: Point) -> Iterator[Point]:
for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
x, y = point[0] + dx, point[1] + dy
if x < 0 or x >= len(height_map):
continue
if y < 0 or y >= len(height_map[0]):
continue
yield x, y
def is_low_point(point: Point) -> bool:
for neighbour in neighbours_of(point):
if height_map[neighbour[0]][neighbour[1]] <= height_map[point[0]][point[1]]:
return False
return True
def explore_bassin(point: Point) -> int:
to_be_seen = set(neighbours_of(point))
explored = {point}
bassin = {point}
while len(to_be_seen) > 0:
new = to_be_seen.pop()
explored.add(new)
if height_map[new[0]][new[1]] == 9:
continue
bassin.add(new)
for neighbour in neighbours_of(new):
if neighbour in explored:
continue
to_be_seen.add(neighbour)
return len(bassin)
points = [(x, y) for x in range(len(height_map)) for y in range(len(height_map[0]))]
low_points = filter(is_low_point, points)
bassin_sizes = sorted(map(explore_bassin, low_points), reverse=True)
return bassin_sizes[0] * bassin_sizes[1] * bassin_sizes[2]
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

100
2021/d09/ex2/input Normal file
View file

@ -0,0 +1,100 @@
9876543298765698956789545678992103976545976323678910997879876432123679765323567894679434987654596521
7998789129854987545697658789789212985439765454567899876568976569012489874212779943598929976653987432
6429898934943496434589769894658924976929898765679969965345698798923456976433567892456998765432196543
5210987899874987645678979943547895989899989898789357893234599987894678986547678901349899896563987656
4329896798765797657889989652126789998798967999899969954345989876789989797698789316498766987854598787
5498755679876798968997699643015678987676759899999898965959878965878997649899899997987654298976689898
7987834656997899878976578932123789998545745799998787899898767854567998798965969898999743109987789999
9896321345698998989965489543456999867623234567897676789789854343456799987654358799298654312398898989
8765430234999987697896789654567899954310123456998545785678963212567898992101245690129796453479987978
7654321345899986576997999876778978965421236567895431234789654345678957895212767789298987894567896669
9785532456799765434989643987989569877654345878976544345698765456789548954329878999987798979878975456
9876743767987654323679932198995476988765456989987655466899896787891239765434989989865679654989764367
0999899878998743212567893999674365699986567898998767877894949898962678986676799878654589943499876458
1989998989987654501457899876543234799897679987859878988943234999853467897897899867723457892398976567
9879867991099865212345789989656125789649899876545989999654345998765679998998987654012349910987987989
8768756689987654323456897298743239891234989765534597898798759879876789569989998632134598929876599096
8653344578999865784587976349994345910199878964523456789929898764987893498977987545245987999965432145
6542123469764986895678965499879966899989867893212345698939987653498902987856798767959876989996543234
7752056778953497898789878987769899998767656989101234567898765432459919876545689878998785678989654546
9854145789101298989899989876545768989654545679914348978997654321347898765434799989679654545678965687
7543236993212349878998998765432459876543234567895567889459865432456987654323988995498763234589896789
7654747894523498767897999654321345998769102678976988992398999545767899765499867894309982123456789999
8765678965654976654456899865210236799898993589987899101987978996988979876987658989219873256798999989
9876789998799865432345678994321346789987889998698978919876456789699767997996545678934965345789789778
4987899989891974321234789986545456899876777897569567898765347897432356989987439789549876956797674567
3298978978910985445445891297669767998785656789423456789653298976545469879876598999656989897896523456
3129469568899876656566990998798978989654345893214567898764989897656798767987987988978998679999314567
2012395436789987767678989899987899876543212379201879969889978789987987656799876877899976567898901238
2193989325679898978989579768976789965432101268912989456998965678999896547898765656999875456567895449
3989878934589769989793498959895689876743412347894592347987854567898786434569654547989765345478989567
9978968895699654997652987645784789987954523456789693469876432457897654321298763239876543217345678978
8867846789898969876543498732123678998766634787899989978994321367999865432598654567987982101234589989
7654437999987899989754989643034567899898745678998767899989993479899987543498765678999878212345678997
6542323899876789598769876542137878987999859899987656798767789598789897655679986799698765433456789876
4321016789765679349989987756756789876789878910199545987656698997655789796789997894549977567567898765
5633225899954598956799899867897897765689989429298769876845457986543456989899898943534988978678979987
8764334567893487897898789998998976454568997998999998765432356978532349878956769892123499989789459998
9875765789932346798997634569989432323467996987999989887845779865431234669545458789012943299892398989
5987876894321457989989429979874321012399875676889876998956889978560123458932345699129892109901987878
4598987899430349979678998899965456133987764545778965698768995989999294567890458798998789298319876567
3219399978921298767587897679876967845976543234667894539878954399878989698921239997987699987423965456
4301234567932987656456796567989878956965432133456793124989876498767878999432549876796589876539874347
5412345789743597542345895434593989869874321012349891013999999987654567898643498765987678987698765458
6523656897654987656556789323901299998765442123457999229878987896543456998789569979998789498789876769
5439787898765698787667893219892398999879543434568978998767986797855669999897689989879899349999987899
7656898919896789899878954398789987999989698545679567893457895498966798986998789394567988998789898989
8967999323987897976989765987679876889998787686893467892124789349997997895429893233459867999698769678
9698999934598956795499899876598765778999998797912349951015689123989876796210932012399756896569954599
6549387895999987989323902965439874355698789898909498932123478939876765989929993243987646789459893989
6321236789898999878919999876323943234987676989898987993234567899965434567898989349998789894398789878
5433345998797898767897889987213495349876565677797656789446889999876515698987678998999899965987698767
6985469899545989654665678999901989498765434545698768998658992398965106789976567987898999987976569656
7876798795439876543234889569899878999876323434569979569769321987654345678989456976447688999895498767
8987987654321989754015679499798767898983210128789989498999210398765656789654319895323567998789349878
9098999876510199863126789987652358997654321247899796987578931239976768899965498789212348987679257989
2129998995431239765257891099543768998765432656789545995467892946988979987896796568901236896578998996
3679976789542398954348999198959879999876558767893239876567899897899989896997987699322345985456789125
4599865679954987895459998987899989987998769879992123987678976789976799765989698789533959876348993234
5989234567899876799567897956789890196549878989789544998989765678965698943476569897659896987257899345
9976123456798765678989986844456789987698989395678959899895454567894567892565456998998765432125678976
8765277567987654569998765432349999898987693234569998789654323456923478921012345679539854321034567897
7654356679599987678999654321267898789965432145698987678943210147898569543235789794323969834245688998
8765467989459899789998874210158999679876541016987894599964521234997679659347898989019879765766899899
9876579894398789899987654332345696567987662929876793488895435349898989798959987879123989989877899787
3987698789298678967998965453456789349898799899985932376789655456789599987899976768934599999999989656
2498987679976543459019879867899892198789987789984321245698767567893498876789895457995678999878767938
1349876567895432398999989988957989987679876698765540356789878678932987545456789346889789998767656899
0198765456789321256789099899545678976598765549854321456899989799891095432347993275678992987654545789
1987674345699592345679129654334688965499994321965545567998999896789194321236892164568921298543237898
9876543234598989456989298743212567894389986539876667679567954965678987210235791013569432987654568956
5987621023987679867895497652101345789579876547988798789678942134567976521256789123456943498765678945
4997732654976598978996598764212345678989989656799899899989653655679876432345898235597894569876789534
9876543459865467899987987654325766789799999789899965957899879769989997543456797546678965778989899325
4988654598764348998998999766434589994598999898998654345678998998799987676567897667799796989999998934
3298797679974245697999429887847893212987898987987434234789897567688999789878998778945689998999987896
2129898989765134976878999998956789109876987656896510123498765434567899899989469899434567897898756789
3298959398754299865757889979767899912985436445689323234569854323776789998794357986523479956989434599
4987643239965987654345679869878999894976721336578935446798765612845899987654219875612396549876526989
9876543129876799765234889754999998789987530123489976557999976701234599998764329874323489932987789878
1995432035987999832123598932101989699998432236599987698999964312345678989876598765435567893499898767
3989654124599898641034567893219876568999543445679998899989899423456989879987699897576789965689989656
9878965435698765432155678965898765432987657656789999999875678954567999765698989998798999986789878543
8767896776789986749878789896799987621298767767899899989654579765678989854799878999939998997898765432
5456789987899987856989896789986798532349898898965778976543459879889875423999967894321987898969987643
2345678999998998767998945999965987643656929969434569198754678992994989567898656976439876749456898765
1234589923457899879987899889894398784789019654323691019867899921012398978997645897899985432345679876
2365699894578923989875987676789249895899998766513589129878987892124987899876536789999876521236789987
5456789689989012399954298545892134976999899895405678934989656789439876987654324679998787210123996798
7687893578899234569895987699976549899999789984312389545694545696598765699765312567899654323245695459
9798932345688965998769898987898799768987698765423578999943234789987674198653202469998765534367789568
9899321234567899899654679876789988657899579877567678987890125679876583019765313578929878946978897679
3976532345678945798753498765698976545986467988678789876789236798989432123984323489212989897889998989
2987645456789659987654579874987654324985358999799899765698945987998943439876534794343598789999999990
1298756569898798798765989989876543219876469999899988954567899876587894646987645679654987678999889891
2349869698999997659877996593987655424976567895999876543456998765456789656798789789769887567998768789
3567998987898989541988987432998966565987678934598765432567899854345699787899899899898765456789657679
7678987676487678932399998949899879677898989323459876554678998743234678998976989944999854346996545568
9899878554345568943567899898799998788969793212589988965689987664145699999765678932499710157895433477
1998765443123459956789998765678969899654579323679999878799876543234589987654567943987621238997312556
0197654321012367897991019834789456998765678954568954989899987654545678998765678954598534567898101245

51
2021/d10/ex1/ex1.py Executable file
View file

@ -0,0 +1,51 @@
#!/usr/bin/env python
import itertools
import sys
from typing import List, Optional, cast
def solve(input: List[str]) -> int:
def find_illegal_char(input: str) -> Optional[int]:
chunks: List[str] = []
ends_to_start = {
")": "(",
"]": "[",
"}": "{",
">": "<",
}
for i, c in enumerate(input):
# Is it a chunk beginning
if c not in ends_to_start:
chunks.append(c)
continue
# Is it matching the last element on the chunk stack
current = chunks.pop()
# If not, corruption has been found
if ends_to_start[c] != current:
return i
return None
score = {
")": 3,
"]": 57,
"}": 1197,
">": 25137,
}
return sum(
score[line[cast(int, find_illegal_char(line))]]
for line in input
if find_illegal_char(line) is not None
)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

110
2021/d10/ex1/input Normal file
View file

@ -0,0 +1,110 @@
<{[[({([{<[[([()<>]<<><>>)<[[]()]{<>()}>][<(()[])<()<>>>]]<(<(<><>)(())>)[<([]()){<><>}>{{()<>}<{}>}]>>[({
<<({<{{{[(<{([{}{}]{{}[]}){({}())}}<<[[][]](()<>)>[{<><>}<{}<>>]>>(<[[()]([]{})]((()<>)[()()
({<<({[<[[<[([()])]<<<{}<>><()[]>>[[()<>]<[]()>]>>([((()[])[()])]{[[()()]<<><>>][{<>[]}(()<>)]})
(([[<<[{(({{{(()<>)[()()]}[({}<>)[[][]]]}([{{}<>}<[]()}]{[[][]][()<>]})})(([({[]{}}{[][]})
({{<[[<<{{[{[<{}{}>{[]{}}]<([]{}){()[]}>}]<{([<><>]([]<>)){({}())({}<>)}}[[<{}<>>([]{})][[()[]]]]>}}>>]]
[(<<<{([[[<{(<()<>>{<><>})(<[]<>>([][]))}[<[{}[]]>{<[]>(<><>)}]>([{{[]()}{[]()}}([<>[]][{}<>
<[{[{{([<(<{{[{}<>]({}{})}{{<>[]}(()())}}(<<<>{}>(<><>)>(<<>{}>[[]<>]))>)>[{[<[[()<>]{{}<>
{(<<[([<{<<<({()()}(<><>)}{((){})([]())}>>[<{<[]<>>{[]{}}}([()()])>]>}>]{{([{<{{[]<>}(<>)}<({}(
((<[{(({([<{(<[][]>)[<<>{}><()<>>]}<{<{}{}>}[{()[]}<{}[]>]>>])})<[{[{([{[]()}((){})][<<>()><()()>])>]}][[({{[
[[{([[<<[({[<<[]{}>{[][]}>(<()<>>[{}()])][([{}[]}[<><>]){[{}()]}]}[<<<{}<>>{()<>}>(<{}()>([]()))>{[[<><>][
{{{<[([[{<[<{([]{}>{<>()}}{<(){}><()()>}>[{[[][]](<>())}[{[]<>}[<>[]]]]]{<{{{}[]}{<><>}}[[()()](<
(([{(({[[[[{(<<>[]>((){})){<()<>><[]{}>}}{[{{}()}(()[])]((()){()<>})}]]]<[<<{(()[])<{}[]>}{
{<<([(<([{{(<(()<>)[[][]]>{[[]{}]{(){}}})[{<(){}>[[]<>]}]}}])[[[([[{()()}{<>[]}]{[()]{[][]}}]<[((
[{<<{<([(<[<{<{}[]>{{}[]}}(([]()))>{([(){}]<()[]>)(<[][]>{[]<>})}]>[{<<{<>()}[{}[]]>{(<><>)}>{[<[]<>>{()[]}]<
<{{<{[[{{([({<()>{(){}}}((<>[])(<><>)))[(<[]<>>[()[]])<<()()>{<>{}}>]])<[{[{[]<>}{()()}][[{}{}]<[]>]}<{{{
[(<[{([{({<<[<<>()>[[]()]]{<<>{}><{}<>>}><{<()()>{{}{}}}(({}<>)<()<>>)>>})({(([<{}<>>{[][]
{(([{[<{{[({{[{}<>][()]}<{{}<>}>}(({<>[]})))]{((<[<>{}][<>[])>[[<>()]{[][]}]))<<<({}<>){{}<>}>(
{[<{({((([[{[(<>{}){<>()}][([]{}}]}]{[<(()()){[]()}>((<>[])<(){}>)]}]){<(<<{{}<>}>>)([[{<><>}<
{<{[(<{<[({<<[<><>]{()()}>[{()<>}<{}>]>[[<[]>][{()[]}{()}]]}(<(({}()){<><>})<(()())[<>()]>>{
<[({[[{<([[{<{{}}>(<<><>>{[]()}}}[{{<>{}}<()<>>}{[[][]]{[]<>}}]]<((([][])<<>{}>)<((){})[{}()
{{[{<[{<<{<<(<{}[]><(){}>)<{<>[]}[{}()]>>[{{[]<>}<()<>>}[{{}<>}(<>())]]><[(({}<>)({}<>))<<{}<>><<
{<<({(<[(<[[{{[][]}{{}}}<[[]()]<<>())>]](<{{{}<>}{()<>}}>)>)]<(({{[[[][]]]}<[[(){}]<[]{}>]>}))>
({{([<<{[(<<{{<>()}<[]()>}<{(){}}{{}{}}>><(<{}[]>{<>{}})<([]<>)([]())>>>)]}>>[[{<{[<<(<>{})
<{({{({<<[{<<{<>}{{}[]}>>[[{[]<>}{()()}]<<[][]>(()())>]}]{<<{[(){}><<><>>}{{<>[]}(()<>)}>[<<(){}>[[]
<({<([[[([({<{(){}}([][])>}<[{[][]}{<>()}]<[(){}][{}<>]>>){<({{}()}{[]{}})>{[{[]{})[()[]]]<[
{([({[(([<{((({}()){{}<>})(<()[]>{()()})){[({}{})<<>[]>]([{}][()])}}>]{<<[<[[][]]{[][]}>({[]{}}<{}()>)
<<{[<({[(<[({<{}{}><()()>})]{<<[<><>]>>}>){{[((<<><>><{}[]>)){<{[]()}(<>())>}]{{{({}<>){[]<>}}{{
<<({<[{(([{({({}())(<>{})}{((){})})[<[[][]]([][])>]}<<[[<>{}][[]<>]]<([]<>)[()()]>><<<{}[]>(()())>>]])[[[
[{<({<(({([([{[]}]<{[]()}[()<>]>]{{(()())<{}()>}{{[]()}<()<>>}}][{(<[]<>>[<>{}])[<[]<>><<><>>]}<(<<
<{{((<<(<{({<{<>{}}[[]{}]>})[[{{[][]}{{}<>}}]]}[(([(()[])<()<>>]((<>[])<<>{}>))<[[<>()][<>
{[([<<<({[{<<[<>{}]<[]<>>>>({<<>{}>((){})}<{[][]}{[]{}}>)}<{(([]{})<<>>><{{}()}(<><>)>}<{<[]
<({[[{{{([(({[{}<>]{[]<>}}[{()<>}<(){}>])<[<{}()>[[]<>]]>)]{{[<{<>()}[{}()]><<<><>>{[][]}>]([[[][]](<>())
[<<{{<[[{[<(<{()[]}[(){}]><[()[]]<<>{}>>)>[[[[[]{}]{[][]}]{<[]{}><(){}>}]({{<>()}<<>{}>}({()<>
[<[[(<[[[[<<<{()<>}[[]()]>>{[[<>[]]{[]{}}]{[<>[]][[]<>]}}>]]({[({[<><>]<{}<>>}<<(){}>[{}()]>)[[<<>
<{{({<([{({({{{}()}((){})})([([][])[[]<>]](([])[{}<>]))}<(([()<>][()[]])[<[]><{}{}>])<{({}<>)<<>()>}
{<[[[([{[<<(({{}<>})(<{}()](<>{}))){[[<>[]]({}<>)]}><[((<>()){[][]})[({}())<{}<>>]]<({<>[]}{{}<>
{<[<[({<({(<(({}[])<{}>)((<>[])({}<>))><(([][])(()()))>)})({{({{<>())}<{<>()}([]{})>)[[(()<>)<<>[]>](<<
{(<(<<{{({{<{(<>[])[[]]}{(<>{})>>{<(()())[{}{}]>{{[]{}}{{}<>}}}}(<{[{}[]]<(){}>}[[{}{}]{()<>}]>([{<>{}}[<>]]
[{[{{[[[{([<(({}{})<[][]>)<([]{})<<>{}>>>[{[[]{}]([]<>)}{(()())[<>[]]}}]((<([]<>)[()<>]>(<{}()>)){<[<>[]](
{<<<<{(({<<({(()())(<>{})}<([][])[[]()]>)[{{[]()}(<>)}]><{[<<>()>]{<[]()>[[]<>]}}>>}>[(<<<[{()<>}{{}<>
[{[(<[<{<[({[{[]<>}<[][]>]{{<>()}}}[({<>[]}{{}{}}){[[]<>](<><>)}])({({{}<>}<{}<>>)}<[((){})[{
{<[[{[(((<({([[]<>](()<>))}[{{()<>}({}())}(<[]{}>{[]{}})])<<(({})[{}{}])[(<>{})<()>]>>>))<[{{<[{{}<>}[[][]
[[<<([{<{((([<{}{}>(<><>)][({}[])])[[(<>{}){{}<>}](<()[]>[[]()])])[{({()}{{}{}})(({}())([]{}))}({[{}[]]{
<<((<[<(([<{[<{}<>>][<{}{}>{{}[]}]}[{<<>()>[[]{}]}{[()[]]([]{})}]><{({()()}{[]})<{[]()}[{}{}]>}{((<>(
({<<{[(({{<<{[[]()]{{}()}}{[<>{}]<[][]>}>{(([]())[<>[]]){<[]{}>{<><>}}}>{{{{<>[]}{{}()]}{{<>{}}[{}{}
{((<<{<<<((<(({}{})(()))[[()()]{{}<>}]>{(<()[]][{}[]])[[<>[]]<<>>]}))>([[{({()[]}{()[]})(<[][]><{}{}>)}[[[[][
[(<<{<[(<([<(<[]()><()()>)>]{({[<><>]{[][]}})}}<[([(()())[{}]]<<(){}>[<><>]>)][<<((){})>>[[{<><>}[{}()]][([]
[<{[<[[(({{{[<{}[]><<>{}}][[{}<>]<<>()>]}{[{[]}[{}[]]]{[()[]]<{}<>>}}}[({<(){}>{{}{}}}<(()[])(
<{([{(<[<<<<[[{}{}]([])](<{}{}>[[][]])>{{(()[]){<>[]}}<[<><>][(){}]>}>(([[{}{}]{{}<>}](<()()>))<<(<>{})<<
[(<[[[[(({[<<<<>{}><<><>>>>]<{[{{}<>}<()>]{{[]{}}[<>[]]}}<<<()[]>([]<>)><(<>{}){[]()}>>>}))[[([{<
[([(({<{([<[({{}{}}<{}{}>)[[[]{}]({}<>)]]<[([]<>)({}[])]{(()[]){[]<>}}>>{[<{[]<>}>{[()[]]<()<>>}][<([
{<[<[[<[{({{<[[]()][{}()]>({()[]}(()[]))}([[<>()]])})}{{<{{[<>]<[]()>}{<{}[]]}}<{{()[]}<[]<>>}([()()]<{}()>)>
[{{(<(([([((<{{}{}}({}())>{{<>[]}<[]{}>})([{<>()}{{}<>}]{<()<>>[{}{}]})>]{{[<(<>){[]{}}>{<<>[]>[()[]]}]
[<<(({[[({({[{[]{}}<<>[]]]{[()()]<<>()>}}{[{<>[]}<()>]}){{<<()[]>{[]{}}><<<>>[[]()]>}}})<[{{[<
<([{{{(<{{{<<({}()){(){})>{<{}{}>{<>()}}>{(<()[]>({}()))<({}())<()()>>}}[(({[]{}}[<>{}])([{}<>])){{{{}}(<
<<{[{[[<([[<(<{}<>><()[]>)<{<>()>[<>()]>>]][{{{{()<>}<{}[]>}[<[]()>[()[]]]}<{[{}](<>{})}>}[<[[<><>]<
[{[([{<<[([[{<<>[]>(<>())}((()())({}{}))>{[({}())<<>()>]<(<><>){()<>}>}]({([(){}]<[]<>>){<[
[{<<[[<((({[<[[]()]>(<<>>(<>{}))]<<([]<>)[<>{}]>{<[][]>(<>())}>}(([{{}<>}{{}[]}]{[()[]]{{}{}}})<
([<<{[([[<{<{({}())}{(()<>)}>[<[{}<>][[]()]><[<>{}]({}[])>]}<[([<>()]){[()<>]([]{})}][[[{}[]]][
<{[[{({<{(<[[(()[])<<>[]>]<<<>()>{[]<>}>]>{[<(<>()){<>[]}>(([][]))]([<[]<>><{}{}>]([<>{}](()()
{[([{{(([[<{<[{}<>]([]{})>[((){})<()[]>]}[[{{}<>}<[][]>]<{[]<>}([]())>]>{<{{{}<>}{(){}}]{{[]
[{{<[([<{[[({[{}[]][(){}]}<{<>()}<[]{}>>)]<<({()<>}[[]()]){[<>[]]<()()>}>>]{[<<(<>[])<[]<>>
<[{<[<((<({[{[{}[])(<>{})}(({}[])[<>])]})>[[([{((){})<<>>}])[<((()<>)[{}{}])[[(){}]]>{<<{}()>{
({[[[[[[[<((<[<><>][()[]]><(<><>)>))>{{<(<<>[]>{{}})[[[]<>]({}())]><{{()<>>[[]<>]}[<{}{}>{<>{}}]>}[{<(<
([([(<[{<[[{<{<>[]}>{{{}()]<{}()>}}<([{}()]{{}<>})[([][]){{}<>}]>]{<{{[]{}}{{}<>}}<[[]()]>>[[{()[]}{(){}}]{[
{<{([([{<<<<{[<>()]<[]>}[[()<>]<()>]>>(([[<>[]][{}{}]])((([][]){<><>})([[]{}]<()<>})))><{<<[()()]<[]()>
[{({[{[(([([{{{}<>}{()<>}}(([]<>){{}{}})][<{()}{{}{}}>(<<>()>[<>()])])]))<<<({((<>()){[]{}})[{
([([(<(({<(<<<{}[]>){{<><>}[[]{}]}>[{{[][]}<<><>>}])<{<<[]>(()[])>[[{}()]<[]<>>]}{({[]()}[<><>]){{{}[]}[{
{{[{<{((<((<<<(){}>>{([]{})[[]()]}>([({}{})]{<()>{[]{}}}))[[(<<><>>)]<<<{}{}><(){}>>[({}[])({}
<{[({{<<((([[[(){}][{}<>]]<(<>[])<[]()>>]<[[{}()]({}())]>)({<[(){}]>{(<><>){[]{}}}})){{{<<[]()>[()<>]>[<
(<[[(<((<[[<{[[]()][()<>]}<[[][]]<[][]>>>((({}())((){}))(<{}<>]{()()}))]]>[[{<{[{}<>]}>([[{}<>](()
<<<{<(<[<{[[([<>()][()<>])(<<>()>[{}()])][<{<>()}>]]}<<<({<>[]}([]{}))<<(){}>>>[[({}())(())]]>{({({}[
([<<<{<{({<[{(()<>)<()()>}]<[({}<>)]>>{[[{<>{}}(()[])](<<>>[[]<>])]}}{<<{<<>()>}><{{<>()}<<><>>}{<(){}>[(){
{{(<<[{{({({({[]()}<{}()>)(([][])[()<>])}<{(<>())[()<>]}>)[[(([]<>)[[]<>])]]}[({[({}()){{}>]})
<((((<{{{{{[{[(){}]([][])}[{<>{}}([]<>)]]([((){})]<[[][]][<><>]>)}}<(<{(()<>)([]{})}><([()<>]{<>()
<{<{<(<(<<<(<[[][]]<<>()>><[<>[]][[]]>)[<[<>{}][<>()]>]>{<{(()){{}()}}[<{}<>><<><>>]>[{<{}[]><<><>>}]}>{(
{[{<<(<([({{<<[]<>>[<>{}]>{<{}<>>([]<>)}}([<<>{}>((){})}<{<>[]}>)}<<({{}()})<(<>[])[[]]>>[[<<>[]>{()[]}][[[]
<<<<[<{<[{{(<[<>]<[]{}>><(<>{}){{}()}>)[[{<>{}}{{}[]}](<[]>[<>{}])]}{{[{{}[]}(()<>)]<([]())(
[([({[[{<{{<<{()()}<{}{}>>({{}{}}[{}{}]>>[{(<>{})<()()>}[[<>[]]<{}()>]]}[<<{<><>}(()[])>(<{}[]>
[<<[[(({<[[([(<>[]){()[]}][(<><>)[<>[]]])]<({<{}{}>(()[])}[<{}<>>({}())])((({}{})<{}[]>)[{{}<>}{{}{}}])
(({({[<<[<[([<<><>){[]{}}][<()<>><[]<>>])({[{}<>]<[]<>>}<({}{})[{}[]]>)][{((<><>)(()[]))}[<<{}()>{<>
<((<[(<(<[[([{(){}}<<>()>]({(){}}{(){}}))]][{{[{[][]]{<>{}}]{[{}<>]([]{})}}}[<{[[]{}][<>()]}[(()[])<
{((<<{({<[([(([]())([]()))(<[][]>{()})])<<[<<>[]>[<>())](<()<>><()()>)>>]>{{[{<[<>{}]<[]<>>>}<{{<><>}<
{<<<<[((<{({<{<>[]}{<>()}><{<><>){<>()}>}([[[]()][(){}]]<<()<>>{<><>}>)){[{{<>[]}{[]()}}{(<>())
({<<<<[{<{<{(<{}{}><()[]>)[{<>{}}]}>(<[([]{}){<><>}]{<<><>>[()[]]}}<[{()[]}{()[]}][{{}[]}{[][]}]>)}>}]
({({{<{{{[<{{(<><>)}({[]()}{<><>})}{<[{}[]]<[]<>>>[(<>[]){()()}]}><[[<<>()><[][]>]]>]<<[{[[]()](
<{[<([<[({([{(<>{}){()}}<([]<>)[{}{}]>)[[<()[]><{}<>>][(<>{})[()()]]]){<{({}){{}{}}}<{<>()}[[
{((({[{<{[<({[{}()]({}{})}<(<>)(()())>)({[[]{}](<><>)}[<<>[]>[<>[]]])><<[{[]<>}][([][])<[]()>]>(
({[((([<[(<{[(<>[])(()[])]}<[([][])([]<>)]<(()())>>>((([(){}])<(<>())<<><>>>)<[[()<>>{{}{}
[{{((<{[(<{[{{[][]}{<><>}}[[()[]]<[][]>]]<<({}[])((){})>>}>[(<([[]()]<{}[]>)([{}<>])>({<<>[]>(<>[])}[(
((<((<[<(<[{(<[]()>[()[]])((())(()[]))}{[[{}<>]([]{})]<[[]()]>}]>)><([<[<{[]<>}<{}[]>>[[(){}]]]({[[][]]{
[(([([(([<<(<{<>}{[][]}>)[{[()<>]{{}{}}}({{}{}}<[]()>)]>>{([({<><>}<[]>){<{}{}>[()<>]}]<<[[]{}]{{
[(<{<[[{[<<{<[[]<>]<[]()>>{{[]<>}}}><[[{{}()}([]<>)]][({{}<>}{{}{}})<[[][]](()())>]>><<({([]{})<<>{}>}){
<{[<[(<<[[[[{<{}[]><[]<>>](<{}[]>{()[]})](<[<>()][[][]]>([()()]<<>()>))]]({([<()[]>[{}()]])<{[[]()]}{<[][
<{{{([[([{{{<({}[])>}}<<({<>{}}{{}()))<({}{}){[]{}}>>[[(<>())[{}[]]]<<[]{}>{<>[]}>]>}([{[<<
[{<(({{<[{[[([<>{}]{<>{}})(((){})[{}[]])][{<{}<>>}[([]{})<{}>]]]}{<<([<>{}][()[]})[{{}}<{}()>]>(
[((<[{<<[<[{[{{}()}[{}[]]]<[<>[]][{}<>]>}]>]{[([([[]<>]{{}[]})[<(){}>[()[]]]]<<({}[])<[]()>>[(
[{([<{([(({(<[[]<>]>{({}()){<><>}})([([]())[[]<>]])}(<[({}())(<>())]<{()<>}[[]<>]>>)){{([[<><>][()[]>](<[][]>
<<(<<<{{{<<(<<[]<>>{<>()}>[<[]>])<{<{}<>>}{[{}{}]<<>[]>}>><{<<<>{}>({}<>)>}{([()[]]<<>()>)(<<><>><[]{}>)}>><
{[<<[{{(<<[((({}{})[{}{}])(({}<>)[{}()]))]>{([[[(){}]<{}<>>][[{}[]]{()[]}]](<{[]{}}(<>[]>>))}>)}[(<
<{{[[<[[[(((({<><>}{{}{}})<[<>()]([]())>)[<{{}<>}>{((){})[[][]]}]))<{<{[[]()]<[][]>}{<[]<>>(<>
{<[<<([[[[{{<({}<>)<{}()>>[(<>())({}[])]}<{([][])({}<>)}>}(({[<>{}]{[]{}}}<(<>{})>)[<{[][]}{[][]}>{({}()){
[[({{[(((({<{{<><>]}{(<><>){<><>}}>(<[()<>]<{}{}>>((<><>){<>[]}))}{<[[{}[]]([][])][{{}[]}[
[{{<([((<<{(((<><>)<(){}>))((({}[]){<>()})(<{}()}({}{})))}><[<<{<>[]}[()<>]>{[<>{}]{[]()}}>{{
[<{<{{({{<<<{[<><>]<[][]>}<<(){}>(<>)>>>([<{[]()}{{}[]}>{<{}()><[]<>>}]([([]())[[]]]([[]()]<<>{}>)))>
<[{<[(<{{([[<[{}{}]><([]<>)(()())>][<((){})[[]()]>[([]<>){<><>}]]]{<{<[]<>><<>>}{<{}<>><[]{}
{[((((((({([([(){}]([]<>)){[{}](<>[])}]{[<<>()>([]<>)]<<[][]>{[]<>}>})<<[{{}[]}(<><>)]<[()<>]
([({(([[[([[((<>{}){{}{}}]{[()<>]((){})}]((({})(<>[])){[{}{}]<<>()>})]{({(())[<>{}]}((<>{})
{<[<{(<[{<[{(<{}()>)[<<>[]>(()<>)]}([{()[]}<{}<>>])}(({<[]{}><{}{}>}([[]<>]{{}()})){[[{}<>]
<<({[[<<[([(<[<><>]{<>{}}>{<(){}>(()())})<{[<>{}]<()[]>}>]<{[{<>{}}<<>()>]<{{}()}[()<>]>}<[[{}<>][{

82
2021/d10/ex2/ex2.py Executable file
View file

@ -0,0 +1,82 @@
#!/usr/bin/env python
import itertools
import sys
from typing import Iterator, List, Optional
def solve(input: List[str]) -> int:
def find_illegal_char(input: str) -> Optional[int]:
chunks: List[str] = []
ends_to_start = {
")": "(",
"]": "[",
"}": "{",
">": "<",
}
for i, c in enumerate(input):
# Is it a chunk beginning
if c not in ends_to_start:
chunks.append(c)
continue
# Is it matching the last element on the chunk stack
current = chunks.pop()
# If not, corruption has been found
if ends_to_start[c] != current:
return i
return None
def complete_line(input: str) -> str:
chunks: List[str] = []
start_to_end = {
"(": ")",
"[": "]",
"{": "}",
"<": ">",
}
for c in input:
# Is it a chunk beginning
if c in start_to_end:
chunks.append(c)
continue
# Otherwise we must match the last open chunk
assert start_to_end[chunks.pop()] == c # Sanity check
return "".join(reversed(list(start_to_end[c] for c in chunks)))
def score_completion(completion: str) -> int:
char_score = {
")": 1,
"]": 2,
"}": 3,
">": 4,
}
score = 0
for c in completion:
score *= 5
score += char_score[c]
return score
def score_completions(completions: Iterator[str]) -> int:
scores = sorted(map(score_completion, completions))
return scores[len(scores) // 2]
incomplete_lines = filter(lambda line: find_illegal_char(line) is None, input)
completions = map(complete_line, incomplete_lines)
return score_completions(completions)
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

110
2021/d10/ex2/input Normal file
View file

@ -0,0 +1,110 @@
<{[[({([{<[[([()<>]<<><>>)<[[]()]{<>()}>][<(()[])<()<>>>]]<(<(<><>)(())>)[<([]()){<><>}>{{()<>}<{}>}]>>[({
<<({<{{{[(<{([{}{}]{{}[]}){({}())}}<<[[][]](()<>)>[{<><>}<{}<>>]>>(<[[()]([]{})]((()<>)[()()
({<<({[<[[<[([()])]<<<{}<>><()[]>>[[()<>]<[]()>]>>([((()[])[()])]{[[()()]<<><>>][{<>[]}(()<>)]})
(([[<<[{(({{{(()<>)[()()]}[({}<>)[[][]]]}([{{}<>}<[]()}]{[[][]][()<>]})})(([({[]{}}{[][]})
({{<[[<<{{[{[<{}{}>{[]{}}]<([]{}){()[]}>}]<{([<><>]([]<>)){({}())({}<>)}}[[<{}<>>([]{})][[()[]]]]>}}>>]]
[(<<<{([[[<{(<()<>>{<><>})(<[]<>>([][]))}[<[{}[]]>{<[]>(<><>)}]>([{{[]()}{[]()}}([<>[]][{}<>
<[{[{{([<(<{{[{}<>]({}{})}{{<>[]}(()())}}(<<<>{}>(<><>)>(<<>{}>[[]<>]))>)>[{[<[[()<>]{{}<>
{(<<[([<{<<<({()()}(<><>)}{((){})([]())}>>[<{<[]<>>{[]{}}}([()()])>]>}>]{{([{<{{[]<>}(<>)}<({}(
((<[{(({([<{(<[][]>)[<<>{}><()<>>]}<{<{}{}>}[{()[]}<{}[]>]>>])})<[{[{([{[]()}((){})][<<>()><()()>])>]}][[({{[
[[{([[<<[({[<<[]{}>{[][]}>(<()<>>[{}()])][([{}[]}[<><>]){[{}()]}]}[<<<{}<>>{()<>}>(<{}()>([]()))>{[[<><>][
{{{<[([[{<[<{([]{}>{<>()}}{<(){}><()()>}>[{[[][]](<>())}[{[]<>}[<>[]]]]]{<{{{}[]}{<><>}}[[()()](<
(([{(({[[[[{(<<>[]>((){})){<()<>><[]{}>}}{[{{}()}(()[])]((()){()<>})}]]]<[<<{(()[])<{}[]>}{
{<<([(<([{{(<(()<>)[[][]]>{[[]{}]{(){}}})[{<(){}>[[]<>]}]}}])[[[([[{()()}{<>[]}]{[()]{[][]}}]<[((
[{<<{<([(<[<{<{}[]>{{}[]}}(([]()))>{([(){}]<()[]>)(<[][]>{[]<>})}]>[{<<{<>()}[{}[]]>{(<><>)}>{[<[]<>>{()[]}]<
<{{<{[[{{([({<()>{(){}}}((<>[])(<><>)))[(<[]<>>[()[]])<<()()>{<>{}}>]])<[{[{[]<>}{()()}][[{}{}]<[]>]}<{{{
[(<[{([{({<<[<<>()>[[]()]]{<<>{}><{}<>>}><{<()()>{{}{}}}(({}<>)<()<>>)>>})({(([<{}<>>{[][]
{(([{[<{{[({{[{}<>][()]}<{{}<>}>}(({<>[]})))]{((<[<>{}][<>[])>[[<>()]{[][]}]))<<<({}<>){{}<>}>(
{[<{({((([[{[(<>{}){<>()}][([]{}}]}]{[<(()()){[]()}>((<>[])<(){}>)]}]){<(<<{{}<>}>>)([[{<><>}<
{<{[(<{<[({<<[<><>]{()()}>[{()<>}<{}>]>[[<[]>][{()[]}{()}]]}(<(({}()){<><>})<(()())[<>()]>>{
<[({[[{<([[{<{{}}>(<<><>>{[]()}}}[{{<>{}}<()<>>}{[[][]]{[]<>}}]]<((([][])<<>{}>)<((){})[{}()
{{[{<[{<<{<<(<{}[]><(){}>)<{<>[]}[{}()]>>[{{[]<>}<()<>>}[{{}<>}(<>())]]><[(({}<>)({}<>))<<{}<>><<
{<<({(<[(<[[{{[][]}{{}}}<[[]()]<<>())>]](<{{{}<>}{()<>}}>)>)]<(({{[[[][]]]}<[[(){}]<[]{}>]>}))>
({{([<<{[(<<{{<>()}<[]()>}<{(){}}{{}{}}>><(<{}[]>{<>{}})<([]<>)([]())>>>)]}>>[[{<{[<<(<>{})
<{({{({<<[{<<{<>}{{}[]}>>[[{[]<>}{()()}]<<[][]>(()())>]}]{<<{[(){}><<><>>}{{<>[]}(()<>)}>[<<(){}>[[]
<({<([[[([({<{(){}}([][])>}<[{[][]}{<>()}]<[(){}][{}<>]>>){<({{}()}{[]{}})>{[{[]{})[()[]]]<[
{([({[(([<{((({}()){{}<>})(<()[]>{()()})){[({}{})<<>[]>]([{}][()])}}>]{<<[<[[][]]{[][]}>({[]{}}<{}()>)
<<{[<({[(<[({<{}{}><()()>})]{<<[<><>]>>}>){{[((<<><>><{}[]>)){<{[]()}(<>())>}]{{{({}<>){[]<>}}{{
<<({<[{(([{({({}())(<>{})}{((){})})[<[[][]]([][])>]}<<[[<>{}][[]<>]]<([]<>)[()()]>><<<{}[]>(()())>>]])[[[
[{<({<(({([([{[]}]<{[]()}[()<>]>]{{(()())<{}()>}{{[]()}<()<>>}}][{(<[]<>>[<>{}])[<[]<>><<><>>]}<(<<
<{{((<<(<{({<{<>{}}[[]{}]>})[[{{[][]}{{}<>}}]]}[(([(()[])<()<>>]((<>[])<<>{}>))<[[<>()][<>
{[([<<<({[{<<[<>{}]<[]<>>>>({<<>{}>((){})}<{[][]}{[]{}}>)}<{(([]{})<<>>><{{}()}(<><>)>}<{<[]
<({[[{{{([(({[{}<>]{[]<>}}[{()<>}<(){}>])<[<{}()>[[]<>]]>)]{{[<{<>()}[{}()]><<<><>>{[][]}>]([[[][]](<>())
[<<{{<[[{[<(<{()[]}[(){}]><[()[]]<<>{}>>)>[[[[[]{}]{[][]}]{<[]{}><(){}>}]({{<>()}<<>{}>}({()<>
[<[[(<[[[[<<<{()<>}[[]()]>>{[[<>[]]{[]{}}]{[<>[]][[]<>]}}>]]({[({[<><>]<{}<>>}<<(){}>[{}()]>)[[<<>
<{{({<([{({({{{}()}((){})})([([][])[[]<>]](([])[{}<>]))}<(([()<>][()[]])[<[]><{}{}>])<{({}<>)<<>()>}
{<[[[([{[<<(({{}<>})(<{}()](<>{}))){[[<>[]]({}<>)]}><[((<>()){[][]})[({}())<{}<>>]]<({<>[]}{{}<>
{<[<[({<({(<(({}[])<{}>)((<>[])({}<>))><(([][])(()()))>)})({{({{<>())}<{<>()}([]{})>)[[(()<>)<<>[]>](<<
{(<(<<{{({{<{(<>[])[[]]}{(<>{})>>{<(()())[{}{}]>{{[]{}}{{}<>}}}}(<{[{}[]]<(){}>}[[{}{}]{()<>}]>([{<>{}}[<>]]
[{[{{[[[{([<(({}{})<[][]>)<([]{})<<>{}>>>[{[[]{}]([]<>)}{(()())[<>[]]}}]((<([]<>)[()<>]>(<{}()>)){<[<>[]](
{<<<<{(({<<({(()())(<>{})}<([][])[[]()]>)[{{[]()}(<>)}]><{[<<>()>]{<[]()>[[]<>]}}>>}>[(<<<[{()<>}{{}<>
[{[(<[<{<[({[{[]<>}<[][]>]{{<>()}}}[({<>[]}{{}{}}){[[]<>](<><>)}])({({{}<>}<{}<>>)}<[((){})[{
{<[[{[(((<({([[]<>](()<>))}[{{()<>}({}())}(<[]{}>{[]{}})])<<(({})[{}{}])[(<>{})<()>]>>>))<[{{<[{{}<>}[[][]
[[<<([{<{((([<{}{}>(<><>)][({}[])])[[(<>{}){{}<>}](<()[]>[[]()])])[{({()}{{}{}})(({}())([]{}))}({[{}[]]{
<<((<[<(([<{[<{}<>>][<{}{}>{{}[]}]}[{<<>()>[[]{}]}{[()[]]([]{})}]><{({()()}{[]})<{[]()}[{}{}]>}{((<>(
({<<{[(({{<<{[[]()]{{}()}}{[<>{}]<[][]>}>{(([]())[<>[]]){<[]{}>{<><>}}}>{{{{<>[]}{{}()]}{{<>{}}[{}{}
{((<<{<<<((<(({}{})(()))[[()()]{{}<>}]>{(<()[]][{}[]])[[<>[]]<<>>]}))>([[{({()[]}{()[]})(<[][]><{}{}>)}[[[[][
[(<<{<[(<([<(<[]()><()()>)>]{({[<><>]{[][]}})}}<[([(()())[{}]]<<(){}>[<><>]>)][<<((){})>>[[{<><>}[{}()]][([]
[<{[<[[(({{{[<{}[]><<>{}}][[{}<>]<<>()>]}{[{[]}[{}[]]]{[()[]]<{}<>>}}}[({<(){}>{{}{}}}<(()[])(
<{([{(<[<<<<[[{}{}]([])](<{}{}>[[][]])>{{(()[]){<>[]}}<[<><>][(){}]>}>(([[{}{}]{{}<>}](<()()>))<<(<>{})<<
[(<[[[[(({[<<<<>{}><<><>>>>]<{[{{}<>}<()>]{{[]{}}[<>[]]}}<<<()[]>([]<>)><(<>{}){[]()}>>>}))[[([{<
[([(({<{([<[({{}{}}<{}{}>)[[[]{}]({}<>)]]<[([]<>)({}[])]{(()[]){[]<>}}>>{[<{[]<>}>{[()[]]<()<>>}][<([
{<[<[[<[{({{<[[]()][{}()]>({()[]}(()[]))}([[<>()]])})}{{<{{[<>]<[]()>}{<{}[]]}}<{{()[]}<[]<>>}([()()]<{}()>)>
[{{(<(([([((<{{}{}}({}())>{{<>[]}<[]{}>})([{<>()}{{}<>}]{<()<>>[{}{}]})>]{{[<(<>){[]{}}>{<<>[]>[()[]]}]
[<<(({[[({({[{[]{}}<<>[]]]{[()()]<<>()>}}{[{<>[]}<()>]}){{<<()[]>{[]{}}><<<>>[[]()]>}}})<[{{[<
<([{{{(<{{{<<({}()){(){})>{<{}{}>{<>()}}>{(<()[]>({}()))<({}())<()()>>}}[(({[]{}}[<>{}])([{}<>])){{{{}}(<
<<{[{[[<([[<(<{}<>><()[]>)<{<>()>[<>()]>>]][{{{{()<>}<{}[]>}[<[]()>[()[]]]}<{[{}](<>{})}>}[<[[<><>]<
[{[([{<<[([[{<<>[]>(<>())}((()())({}{}))>{[({}())<<>()>]<(<><>){()<>}>}]({([(){}]<[]<>>){<[
[{<<[[<((({[<[[]()]>(<<>>(<>{}))]<<([]<>)[<>{}]>{<[][]>(<>())}>}(([{{}<>}{{}[]}]{[()[]]{{}{}}})<
([<<{[([[<{<{({}())}{(()<>)}>[<[{}<>][[]()]><[<>{}]({}[])>]}<[([<>()]){[()<>]([]{})}][[[{}[]]][
<{[[{({<{(<[[(()[])<<>[]>]<<<>()>{[]<>}>]>{[<(<>()){<>[]}>(([][]))]([<[]<>><{}{}>]([<>{}](()()
{[([{{(([[<{<[{}<>]([]{})>[((){})<()[]>]}[[{{}<>}<[][]>]<{[]<>}([]())>]>{<{{{}<>}{(){}}]{{[]
[{{<[([<{[[({[{}[]][(){}]}<{<>()}<[]{}>>)]<<({()<>}[[]()]){[<>[]]<()()>}>>]{[<<(<>[])<[]<>>
<[{<[<((<({[{[{}[])(<>{})}(({}[])[<>])]})>[[([{((){})<<>>}])[<((()<>)[{}{}])[[(){}]]>{<<{}()>{
({[[[[[[[<((<[<><>][()[]]><(<><>)>))>{{<(<<>[]>{{}})[[[]<>]({}())]><{{()<>>[[]<>]}[<{}{}>{<>{}}]>}[{<(<
([([(<[{<[[{<{<>[]}>{{{}()]<{}()>}}<([{}()]{{}<>})[([][]){{}<>}]>]{<{{[]{}}{{}<>}}<[[]()]>>[[{()[]}{(){}}]{[
{<{([([{<<<<{[<>()]<[]>}[[()<>]<()>]>>(([[<>[]][{}{}]])((([][]){<><>})([[]{}]<()<>})))><{<<[()()]<[]()>
[{({[{[(([([{{{}<>}{()<>}}(([]<>){{}{}})][<{()}{{}{}}>(<<>()>[<>()])])]))<<<({((<>()){[]{}})[{
([([(<(({<(<<<{}[]>){{<><>}[[]{}]}>[{{[][]}<<><>>}])<{<<[]>(()[])>[[{}()]<[]<>>]}{({[]()}[<><>]){{{}[]}[{
{{[{<{((<((<<<(){}>>{([]{})[[]()]}>([({}{})]{<()>{[]{}}}))[[(<<><>>)]<<<{}{}><(){}>>[({}[])({}
<{[({{<<((([[[(){}][{}<>]]<(<>[])<[]()>>]<[[{}()]({}())]>)({<[(){}]>{(<><>){[]{}}}})){{{<<[]()>[()<>]>[<
(<[[(<((<[[<{[[]()][()<>]}<[[][]]<[][]>>>((({}())((){}))(<{}<>]{()()}))]]>[[{<{[{}<>]}>([[{}<>](()
<<<{<(<[<{[[([<>()][()<>])(<<>()>[{}()])][<{<>()}>]]}<<<({<>[]}([]{}))<<(){}>>>[[({}())(())]]>{({({}[
([<<<{<{({<[{(()<>)<()()>}]<[({}<>)]>>{[[{<>{}}(()[])](<<>>[[]<>])]}}{<<{<<>()>}><{{<>()}<<><>>}{<(){}>[(){
{{(<<[{{({({({[]()}<{}()>)(([][])[()<>])}<{(<>())[()<>]}>)[[(([]<>)[[]<>])]]}[({[({}()){{}>]})
<((((<{{{{{[{[(){}]([][])}[{<>{}}([]<>)]]([((){})]<[[][]][<><>]>)}}<(<{(()<>)([]{})}><([()<>]{<>()
<{<{<(<(<<<(<[[][]]<<>()>><[<>[]][[]]>)[<[<>{}][<>()]>]>{<{(()){{}()}}[<{}<>><<><>>]>[{<{}[]><<><>>}]}>{(
{[{<<(<([({{<<[]<>>[<>{}]>{<{}<>>([]<>)}}([<<>{}>((){})}<{<>[]}>)}<<({{}()})<(<>[])[[]]>>[[<<>[]>{()[]}][[[]
<<<<[<{<[{{(<[<>]<[]{}>><(<>{}){{}()}>)[[{<>{}}{{}[]}](<[]>[<>{}])]}{{[{{}[]}(()<>)]<([]())(
[([({[[{<{{<<{()()}<{}{}>>({{}{}}[{}{}]>>[{(<>{})<()()>}[[<>[]]<{}()>]]}[<<{<><>}(()[])>(<{}[]>
[<<[[(({<[[([(<>[]){()[]}][(<><>)[<>[]]])]<({<{}{}>(()[])}[<{}<>>({}())])((({}{})<{}[]>)[{{}<>}{{}{}}])
(({({[<<[<[([<<><>){[]{}}][<()<>><[]<>>])({[{}<>]<[]<>>}<({}{})[{}[]]>)][{((<><>)(()[]))}[<<{}()>{<>
<((<[(<(<[[([{(){}}<<>()>]({(){}}{(){}}))]][{{[{[][]]{<>{}}]{[{}<>]([]{})}}}[<{[[]{}][<>()]}[(()[])<
{((<<{({<[([(([]())([]()))(<[][]>{()})])<<[<<>[]>[<>())](<()<>><()()>)>>]>{{[{<[<>{}]<[]<>>>}<{{<><>}<
{<<<<[((<{({<{<>[]}{<>()}><{<><>){<>()}>}([[[]()][(){}]]<<()<>>{<><>}>)){[{{<>[]}{[]()}}{(<>())
({<<<<[{<{<{(<{}{}><()[]>)[{<>{}}]}>(<[([]{}){<><>}]{<<><>>[()[]]}}<[{()[]}{()[]}][{{}[]}{[][]}]>)}>}]
({({{<{{{[<{{(<><>)}({[]()}{<><>})}{<[{}[]]<[]<>>>[(<>[]){()()}]}><[[<<>()><[][]>]]>]<<[{[[]()](
<{[<([<[({([{(<>{}){()}}<([]<>)[{}{}]>)[[<()[]><{}<>>][(<>{})[()()]]]){<{({}){{}{}}}<{<>()}[[
{((({[{<{[<({[{}()]({}{})}<(<>)(()())>)({[[]{}](<><>)}[<<>[]>[<>[]]])><<[{[]<>}][([][])<[]()>]>(
({[((([<[(<{[(<>[])(()[])]}<[([][])([]<>)]<(()())>>>((([(){}])<(<>())<<><>>>)<[[()<>>{{}{}
[{{((<{[(<{[{{[][]}{<><>}}[[()[]]<[][]>]]<<({}[])((){})>>}>[(<([[]()]<{}[]>)([{}<>])>({<<>[]>(<>[])}[(
((<((<[<(<[{(<[]()>[()[]])((())(()[]))}{[[{}<>]([]{})]<[[]()]>}]>)><([<[<{[]<>}<{}[]>>[[(){}]]]({[[][]]{
[(([([(([<<(<{<>}{[][]}>)[{[()<>]{{}{}}}({{}{}}<[]()>)]>>{([({<><>}<[]>){<{}{}>[()<>]}]<<[[]{}]{{
[(<{<[[{[<<{<[[]<>]<[]()>>{{[]<>}}}><[[{{}()}([]<>)]][({{}<>}{{}{}})<[[][]](()())>]>><<({([]{})<<>{}>}){
<{[<[(<<[[[[{<{}[]><[]<>>](<{}[]>{()[]})](<[<>()][[][]]>([()()]<<>()>))]]({([<()[]>[{}()]])<{[[]()]}{<[][
<{{{([[([{{{<({}[])>}}<<({<>{}}{{}()))<({}{}){[]{}}>>[[(<>())[{}[]]]<<[]{}>{<>[]}>]>}([{[<<
[{<(({{<[{[[([<>{}]{<>{}})(((){})[{}[]])][{<{}<>>}[([]{})<{}>]]]}{<<([<>{}][()[]})[{{}}<{}()>]>(
[((<[{<<[<[{[{{}()}[{}[]]]<[<>[]][{}<>]>}]>]{[([([[]<>]{{}[]})[<(){}>[()[]]]]<<({}[])<[]()>>[(
[{([<{([(({(<[[]<>]>{({}()){<><>}})([([]())[[]<>]])}(<[({}())(<>())]<{()<>}[[]<>]>>)){{([[<><>][()[]>](<[][]>
<<(<<<{{{<<(<<[]<>>{<>()}>[<[]>])<{<{}<>>}{[{}{}]<<>[]>}>><{<<<>{}>({}<>)>}{([()[]]<<>()>)(<<><>><[]{}>)}>><
{[<<[{{(<<[((({}{})[{}{}])(({}<>)[{}()]))]>{([[[(){}]<{}<>>][[{}[]]{()[]}]](<{[]{}}(<>[]>>))}>)}[(<
<{{[[<[[[(((({<><>}{{}{}})<[<>()]([]())>)[<{{}<>}>{((){})[[][]]}]))<{<{[[]()]<[][]>}{<[]<>>(<>
{<[<<([[[[{{<({}<>)<{}()>>[(<>())({}[])]}<{([][])({}<>)}>}(({[<>{}]{[]{}}}<(<>{})>)[<{[][]}{[][]}>{({}()){
[[({{[(((({<{{<><>]}{(<><>){<><>}}>(<[()<>]<{}{}>>((<><>){<>[]}))}{<[[{}[]]([][])][{{}[]}[
[{{<([((<<{(((<><>)<(){}>))((({}[]){<>()})(<{}()}({}{})))}><[<<{<>[]}[()<>]>{[<>{}]{[]()}}>{{
[<{<{{({{<<<{[<><>]<[][]>}<<(){}>(<>)>>>([<{[]()}{{}[]}>{<{}()><[]<>>}]([([]())[[]]]([[]()]<<>{}>)))>
<[{<[(<{{([[<[{}{}]><([]<>)(()())>][<((){})[[]()]>[([]<>){<><>}]]]{<{<[]<>><<>>}{<{}<>><[]{}
{[((((((({([([(){}]([]<>)){[{}](<>[])}]{[<<>()>([]<>)]<<[][]>{[]<>}>})<<[{{}[]}(<><>)]<[()<>]
([({(([[[([[((<>{}){{}{}}]{[()<>]((){})}]((({})(<>[])){[{}{}]<<>()>})]{({(())[<>{}]}((<>{})
{<[<{(<[{<[{(<{}()>)[<<>[]>(()<>)]}([{()[]}<{}<>>])}(({<[]{}><{}{}>}([[]<>]{{}()})){[[{}<>]
<<({[[<<[([(<[<><>]{<>{}}>{<(){}>(()())})<{[<>{}]<()[]>}>]<{[{<>{}}<<>()>]<{{}()}[()<>]>}<[[{}<>][{

66
2021/d11/ex1/ex1.py Executable file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env python
import itertools
import sys
from copy import deepcopy
from typing import Iterator, List, Set, Tuple
Grid = List[List[int]]
Point = Tuple[int, int]
def solve(input: List[str]) -> int:
levels = [[int(c) for c in line] for line in input]
def step(levels: Grid) -> Tuple[Grid, int]:
# First step, increase levels
levels = [[l + 1 for l in line] for line in levels]
def excited(levels: Grid) -> Set[Point]:
return set(
(i, j)
for i in range(len(levels))
for j in range(len(levels[i]))
if levels[i][j] > 9
)
def neighbours_of(point: Point) -> Iterator[Point]:
for dx, dy in itertools.product((-1, 0, 1), repeat=2):
if dx == 0 and dy == 0:
continue
x = point[0] + dx
y = point[1] + dy
if x < 0 or x >= len(levels):
continue
if y < 0 or y >= len(levels[x]):
continue
yield x, y
# Second step, do flashes
has_flashed: Set[Point] = set()
while len(flashes := (excited(levels) - has_flashed)) > 0:
for (i, j) in flashes:
has_flashed.add((i, j))
for x, y in neighbours_of((i, j)):
levels[x][y] += 1
# Finally, bring back energy levels to 0
for i, j in has_flashed:
levels[i][j] = 0
return levels, len(has_flashed)
res = 0
for __ in range(100):
levels, flashes = step(levels)
res += flashes
return res
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

10
2021/d11/ex1/input Normal file
View file

@ -0,0 +1,10 @@
4341347643
5477728451
2322733878
5453762556
2718123421
4237886115
5631617114
2217667227
4236581255
4482627641

67
2021/d11/ex2/ex2.py Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python
import itertools
import sys
from copy import deepcopy
from typing import Iterator, List, Set, Tuple
Grid = List[List[int]]
Point = Tuple[int, int]
def solve(input: List[str]) -> int:
levels = [[int(c) for c in line] for line in input]
def step(levels: Grid) -> Tuple[Grid, int]:
# First step, increase levels
levels = [[l + 1 for l in line] for line in levels]
def excited(levels: Grid) -> Set[Point]:
return set(
(i, j)
for i in range(len(levels))
for j in range(len(levels[i]))
if levels[i][j] > 9
)
def neighbours_of(point: Point) -> Iterator[Point]:
for dx, dy in itertools.product((-1, 0, 1), repeat=2):
if dx == 0 and dy == 0:
continue
x = point[0] + dx
y = point[1] + dy
if x < 0 or x >= len(levels):
continue
if y < 0 or y >= len(levels[x]):
continue
yield x, y
# Second step, do flashes
has_flashed: Set[Point] = set()
while len(flashes := (excited(levels) - has_flashed)) > 0:
for (i, j) in flashes:
has_flashed.add((i, j))
for x, y in neighbours_of((i, j)):
levels[x][y] += 1
# Finally, bring back energy levels to 0
for i, j in has_flashed:
levels[i][j] = 0
return levels, len(has_flashed)
for i in itertools.count(1):
levels, flashes = step(levels)
if flashes == len(list(itertools.chain.from_iterable(levels))):
return i
assert False # Sanity check
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

10
2021/d11/ex2/input Normal file
View file

@ -0,0 +1,10 @@
4341347643
5477728451
2322733878
5453762556
2718123421
4237886115
5631617114
2217667227
4236581255
4482627641

46
2021/d12/ex1/ex1.py Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env python
import itertools
import sys
from collections import defaultdict
from typing import Dict, List, Set
Map = Dict[str, Set[str]]
def solve(input: List[str]) -> int:
def parse() -> Map:
res: Map = defaultdict(set)
for (start, to) in map(lambda s: s.split("-"), input):
res[start].add(to)
res[to].add(start)
return res
caves = parse()
def dfs(start: str, seen: Set[str] = set()) -> int:
if start == "end":
return 1
seen = seen | {start}
res = 0
for dest in caves[start]:
if dest in seen and dest.islower():
continue
res += dfs(dest, seen)
return res
return dfs("start")
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

22
2021/d12/ex1/input Normal file
View file

@ -0,0 +1,22 @@
ax-end
xq-GF
end-xq
im-wg
ax-ie
start-ws
ie-ws
CV-start
ng-wg
ng-ie
GF-ng
ng-av
CV-end
ie-GF
CV-ie
im-xq
start-GF
GF-ws
wg-LY
CV-ws
im-CV
CV-wg

49
2021/d12/ex2/ex2.py Executable file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env python
import itertools
import sys
from collections import defaultdict
from typing import Dict, List, Set
Map = Dict[str, Set[str]]
def solve(input: List[str]) -> int:
def parse() -> Map:
res: Map = defaultdict(set)
for (start, to) in map(lambda s: s.split("-"), input):
res[start].add(to)
res[to].add(start)
return res
caves = parse()
def dfs(start: str, seen: Set[str] = set(), has_doubled_back: bool = False) -> int:
if start == "end":
return 1
seen = seen | {start}
res = 0
for dest in caves[start]:
doubles_back = False
if dest in seen and dest.islower():
if has_doubled_back or dest == "start":
continue
doubles_back = True
res += dfs(dest, seen, has_doubled_back or doubles_back)
return res
return dfs("start")
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

22
2021/d12/ex2/input Normal file
View file

@ -0,0 +1,22 @@
ax-end
xq-GF
end-xq
im-wg
ax-ie
start-ws
ie-ws
CV-start
ng-wg
ng-ie
GF-ng
ng-av
CV-end
ie-GF
CV-ie
im-xq
start-GF
GF-ws
wg-LY
CV-ws
im-CV
CV-wg

71
2021/d13/ex1/ex1.py Executable file
View file

@ -0,0 +1,71 @@
#!/usr/bin/env python
import itertools
import sys
from copy import deepcopy
from typing import Iterable, List, Tuple, cast
Paper = List[List[bool]]
Instruction = Tuple[str, int]
def solve(input: List[str]) -> int:
def transpose(paper: Paper) -> Paper:
return cast(Paper, list(zip(*paper)))
def fold_along_y(paper: Paper, y: int) -> Paper:
assert not any(paper[y]) # Sanity check
assert y >= len(paper) // 2 # Ensure that we can actually fold the paper
paper = deepcopy(paper)
for i, j in zip(itertools.count(y, -1), range(y, len(paper))):
paper[i] = [(dot_i or dot_j) for dot_i, dot_j in zip(paper[i], paper[j])]
paper = paper[:y]
return paper
def fold_along_x(paper: Paper, x: int) -> Paper:
return transpose(fold_along_y(transpose(paper), x))
def do_folds(paper: Paper, instructions: Iterable[Instruction]) -> Paper:
for axis, n in instructions:
if axis == "x":
paper = fold_along_x(paper, n)
elif axis == "y":
paper = fold_along_y(paper, n)
else:
assert False # Sanity check
return paper
def parse() -> Tuple[Paper, List[Instruction]]:
paper_part = itertools.takewhile(lambda s: s != "", input)
fold_part = itertools.islice(
itertools.dropwhile(lambda s: s != "", input), 1, None
)
points = {(int(x), int(y)) for x, y in map(lambda s: s.split(","), paper_part)}
folds = [
(axis[-1], int(n)) for axis, n in map(lambda s: s.split("="), fold_part)
]
width, height = max(p[0] for p in points), max(p[1] for p in points)
paper = [
[(x, y) in points for x in range(width + 1)] for y in range(height + 1)
]
return paper, folds
paper, instructions = parse()
return sum(itertools.chain.from_iterable(do_folds(paper, instructions[:1])))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
print(solve(input))
if __name__ == "__main__":
main()

890
2021/d13/ex1/input Normal file
View file

@ -0,0 +1,890 @@
1159,502
291,184
850,631
460,631
301,707
768,56
654,829
582,466
490,276
261,3
793,35
559,453
497,288
594,275
458,264
1044,387
654,842
925,22
688,72
224,833
1228,2
1034,487
420,830
172,717
1049,198
669,305
1168,49
157,304
90,669
1036,1
118,549
1084,593
268,61
909,294
467,74
1114,632
1066,609
1058,602
271,35
474,299
1173,35
75,864
470,543
176,100
485,736
199,332
23,634
654,329
748,526
274,395
378,474
910,673
1012,810
383,859
1163,5
353,291
741,383
631,241
179,436
793,75
495,324
321,169
1087,278
1302,361
1232,572
417,317
960,161
1088,642
1161,613
631,653
94,327
244,733
857,549
572,220
75,337
259,5
1235,192
805,393
126,68
1250,723
580,68
397,441
1297,165
700,82
1200,275
214,56
932,711
676,126
676,320
169,388
1049,3
25,319
1218,215
179,217
681,635
512,574
966,371
1124,282
387,444
930,644
179,781
192,247
1061,290
500,557
378,54
855,159
585,891
358,295
545,234
1287,429
425,648
416,579
1255,99
59,290
768,585
1126,733
253,600
1235,332
852,184
223,357
1143,585
1096,36
1125,194
95,677
885,768
870,820
165,527
1201,660
465,291
387,702
1041,872
502,316
358,814
435,747
1217,439
177,113
505,781
850,746
343,38
499,530
85,789
1285,563
321,541
45,15
1295,669
1285,393
363,262
271,347
646,809
1143,794
505,753
224,201
50,420
2,297
507,312
823,100
222,252
1007,241
1116,157
274,499
505,617
1036,137
987,623
22,105
30,239
244,609
835,333
1019,184
572,443
134,613
348,263
927,35
500,625
421,807
763,595
277,838
856,54
1310,52
855,287
166,628
1155,684
609,327
445,892
1116,640
159,346
1116,478
1116,849
982,565
1232,754
1163,337
0,136
422,455
1049,451
857,345
59,640
688,822
507,582
351,485
294,744
1218,383
751,665
969,761
50,165
1253,358
393,59
629,331
550,842
30,80
505,113
731,30
641,761
513,780
221,543
1131,561
1072,315
252,602
87,556
1148,282
902,61
276,308
35,250
1057,600
403,785
113,502
805,501
0,86
1028,364
428,301
1305,353
616,645
199,607
744,68
350,385
412,530
574,554
713,806
1193,327
923,514
952,743
542,586
890,830
378,26
0,394
214,523
335,210
75,30
845,282
395,357
1285,187
597,806
160,154
589,760
52,843
654,117
711,170
137,78
1130,595
959,37
1220,690
797,252
923,444
1285,259
55,99
980,638
641,305
781,549
1126,768
184,161
915,477
731,702
888,868
537,194
738,219
1288,777
982,285
1087,357
595,254
1288,161
266,21
1310,136
1144,745
1111,562
671,893
1198,756
1176,389
1123,372
748,831
1168,400
810,886
805,141
705,614
269,760
417,126
1044,313
1228,226
1158,204
288,645
870,504
266,313
1253,470
954,84
134,806
420,694
348,711
957,623
1285,271
1007,485
428,593
494,493
1051,5
274,698
852,150
1216,327
559,477
522,625
691,644
261,787
823,346
773,775
907,254
1288,789
728,466
559,665
664,809
833,656
258,679
422,474
877,95
1253,246
822,709
224,542
154,456
502,302
171,231
1253,872
1260,824
256,176
372,364
358,743
1056,225
1049,787
1168,285
679,465
157,590
214,820
1051,89
652,358
192,80
888,474
679,241
430,201
681,53
572,668
626,501
724,663
78,98
639,229
591,495
798,721
494,857
437,10
1131,312
1028,194
72,428
1014,151
229,117
1086,835
923,892
515,485
485,288
366,249
1088,252
745,390
266,425
880,649
952,814
656,329
865,78
440,504
397,889
706,294
1096,56
177,672
900,285
154,785
803,582
1126,126
174,466
438,171
658,760
57,694
962,183
440,820
57,134
1138,774
430,733
107,756
1149,600
1242,889
997,57
1156,886
331,497
947,856
671,445
658,358
1118,169
1156,456
276,488
873,884
267,10
880,61
805,589
944,249
412,812
326,394
1265,816
460,74
562,526
377,56
552,61
291,698
822,194
959,241
656,758
1071,332
22,341
565,504
1052,679
1091,299
1217,639
852,296
1268,394
738,883
1238,225
938,194
811,124
1039,347
507,648
460,737
1019,849
177,841
798,270
319,684
915,865
117,226
1006,509
274,163
561,535
497,606
585,3
185,52
30,814
348,183
221,799
395,477
749,887
957,603
1014,631
1310,842
723,473
214,717
1290,768
50,40
1087,2
797,556
425,768
1163,785
387,514
1302,809
813,606
649,786
189,719
343,843
947,262
992,439
93,639
261,451
13,133
932,837
1153,590
582,109
1053,565
922,581
634,320
311,856
1176,281
1265,368
460,605
1084,301
1144,73
341,837
706,729
303,485
445,450
960,385
353,603
1151,122
160,37
564,690
798,574
1049,891
885,563
363,710
879,537
840,49
751,229
1285,779
868,562
353,623
1059,109
269,134
257,329
788,711
462,590
74,490
706,600
602,753
229,246
602,312
1144,628
646,231
1288,105
566,826
619,625
440,717
536,549
1275,250
477,501
1168,721
907,108
708,507
1039,547
13,729
93,455
1118,647
1161,165
74,378
417,518
142,721
967,843
1307,436
947,710
699,773
1280,814
425,200
1260,138
110,395
356,84
1310,381
1146,86
522,631
110,499
5,353
629,563
1242,679
840,319
276,756
134,358
1287,689
152,690
1260,698
217,682
1034,57
440,522
201,773
1193,567
870,729
1253,424
301,719
8,533
738,443
706,165
475,458
256,718
298,875
999,856
894,740
105,246
420,679
668,810
1073,765
301,603
853,756
763,138
991,796
954,19
400,428
1144,266
323,497
938,364
708,312
462,395
870,74
52,644
982,665
1066,40
433,95
421,535
1230,788
997,110
1019,698
161,600
1094,420
559,417
923,702
1022,645
166,821
505,841
162,282
773,119
90,690
977,786
586,231
223,892
457,756
1232,322
512,721
137,443
1201,234
979,49
1258,283
97,246
422,868
1009,187
889,87
251,416
1202,492
1302,533
763,756
597,424
311,221
313,589
1253,134
810,557
1235,30
356,19
982,829
1145,527
132,30
885,648
654,381
654,65
234,816
458,710
1111,332
1019,710
331,49
561,7
440,281
360,150
261,198
440,165
412,59
1131,582
78,754
1009,164
587,501
1029,773
746,690
50,600
1260,756
117,327
257,441
582,557
89,481
400,669
216,249
701,190
50,196
1302,737
933,838
421,87
82,674
80,554
951,30
1089,799
1163,458
562,816
1141,836
65,507
110,691
162,612
189,368
490,170
634,520
179,582
656,198
160,857
1041,760
549,205
994,712
1236,516
1280,239
201,121
706,586
1131,436
629,501
237,70
1285,331
898,82
1230,554
350,161
59,254
945,194
1071,780
1034,308
878,833
1220,669
1049,835
1043,10
142,46
403,109
599,547
403,640
872,723
351,37
562,511
1131,113
462,695
1252,315
179,333
842,282
460,254
887,665
184,768
529,549
269,872
1096,717
117,345
1089,95
159,122
475,436
803,246
597,312
605,614
751,417
542,56
751,453
1015,702
713,760
822,681
595,640
895,108
1056,18
1220,231
1150,717
1173,443
269,470
147,5
214,726
667,0
1243,537
159,621
349,170
1305,338
388,581
611,773
160,149
1228,450
550,700
875,747
305,668
1173,3
418,679
102,833
500,886
1148,612
923,436
894,154
813,288
996,660
922,469
55,302
902,891
1168,718
403,108
490,808
458,184
857,247
678,553
662,887
149,165
1238,785
805,672
520,389
1202,402
1101,700
870,372
1193,119
788,631
734,572
793,371
661,556
1168,848
224,352
505,277
562,368
11,0
888,455
1131,781
865,892
1006,52
298,19
714,385
209,700
803,436
7,679
579,702
522,263
1150,269
1252,586
160,73
1238,428
103,380
1278,374
808,578
169,288
495,845
668,84
137,451
338,182
850,737
587,393
721,555
1275,698
741,105
774,866
803,648
1039,35
1253,200
1303,679
831,278
843,633
421,439
78,572
576,572
852,710
259,889
416,740
294,150
184,817
678,777
994,182
442,562
393,3
885,200
669,761
1150,149
1034,474
542,585
189,67
820,276
65,339
31,416
209,567
502,578
1081,648
445,78
1118,247
217,458
336,455
1126,817
805,753
331,677
25,331
336,439
92,158
960,733
137,603
174,428
734,140
460,157
43,569
58,586
196,632
313,784
199,287
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

75
2021/d13/ex2/ex2.py Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env python
import itertools
import sys
from copy import deepcopy
from typing import Iterable, List, Tuple, cast
Paper = List[List[bool]]
Instruction = Tuple[str, int]
def solve(input: List[str]) -> None:
def transpose(paper: Paper) -> Paper:
return cast(Paper, list(zip(*paper)))
def fold_along_y(paper: Paper, y: int) -> Paper:
assert not any(paper[y]) # Sanity check
assert y >= len(paper) // 2 # Ensure that we can actually fold the paper
paper = deepcopy(paper)
for i, j in zip(itertools.count(y, -1), range(y, len(paper))):
paper[i] = [(dot_i or dot_j) for dot_i, dot_j in zip(paper[i], paper[j])]
paper = paper[:y]
return paper
def fold_along_x(paper: Paper, x: int) -> Paper:
return transpose(fold_along_y(transpose(paper), x))
def do_folds(paper: Paper, instructions: Iterable[Instruction]) -> Paper:
for axis, n in instructions:
if axis == "x":
paper = fold_along_x(paper, n)
elif axis == "y":
paper = fold_along_y(paper, n)
else:
assert False # Sanity check
return paper
def parse() -> Tuple[Paper, List[Instruction]]:
paper_part = itertools.takewhile(lambda s: s != "", input)
fold_part = itertools.islice(
itertools.dropwhile(lambda s: s != "", input), 1, None
)
points = {(int(x), int(y)) for x, y in map(lambda s: s.split(","), paper_part)}
folds = [
(axis[-1], int(n)) for axis, n in map(lambda s: s.split("="), fold_part)
]
width, height = max(p[0] for p in points), max(p[1] for p in points)
paper = [
[(x, y) in points for x in range(width + 1)] for y in range(height + 1)
]
return paper, folds
def dump(paper: Paper) -> None:
for line in paper:
print("".join("#" if dot else "." for dot in line))
paper, instructions = parse()
dump(do_folds(paper, instructions))
def main() -> None:
input = [line.strip() for line in sys.stdin.readlines()]
solve(input)
if __name__ == "__main__":
main()

890
2021/d13/ex2/input Normal file
View file

@ -0,0 +1,890 @@
1159,502
291,184
850,631
460,631
301,707
768,56
654,829
582,466
490,276
261,3
793,35
559,453
497,288
594,275
458,264
1044,387
654,842
925,22
688,72
224,833
1228,2
1034,487
420,830
172,717
1049,198
669,305
1168,49
157,304
90,669
1036,1
118,549
1084,593
268,61
909,294
467,74
1114,632
1066,609
1058,602
271,35
474,299
1173,35
75,864
470,543
176,100
485,736
199,332
23,634
654,329
748,526
274,395
378,474
910,673
1012,810
383,859
1163,5
353,291
741,383
631,241
179,436
793,75
495,324
321,169
1087,278
1302,361
1232,572
417,317
960,161
1088,642
1161,613
631,653
94,327
244,733
857,549
572,220
75,337
259,5
1235,192
805,393
126,68
1250,723
580,68
397,441
1297,165
700,82
1200,275
214,56
932,711
676,126
676,320
169,388
1049,3
25,319
1218,215
179,217
681,635
512,574
966,371
1124,282
387,444
930,644
179,781
192,247
1061,290
500,557
378,54
855,159
585,891
358,295
545,234
1287,429
425,648
416,579
1255,99
59,290
768,585
1126,733
253,600
1235,332
852,184
223,357
1143,585
1096,36
1125,194
95,677
885,768
870,820
165,527
1201,660
465,291
387,702
1041,872
502,316
358,814
435,747
1217,439
177,113
505,781
850,746
343,38
499,530
85,789
1285,563
321,541
45,15
1295,669
1285,393
363,262
271,347
646,809
1143,794
505,753
224,201
50,420
2,297
507,312
823,100
222,252
1007,241
1116,157
274,499
505,617
1036,137
987,623
22,105
30,239
244,609
835,333
1019,184
572,443
134,613
348,263
927,35
500,625
421,807
763,595
277,838
856,54
1310,52
855,287
166,628
1155,684
609,327
445,892
1116,640
159,346
1116,478
1116,849
982,565
1232,754
1163,337
0,136
422,455
1049,451
857,345
59,640
688,822
507,582
351,485
294,744
1218,383
751,665
969,761
50,165
1253,358
393,59
629,331
550,842
30,80
505,113
731,30
641,761
513,780
221,543
1131,561
1072,315
252,602
87,556
1148,282
902,61
276,308
35,250
1057,600
403,785
113,502
805,501
0,86
1028,364
428,301
1305,353
616,645
199,607
744,68
350,385
412,530
574,554
713,806
1193,327
923,514
952,743
542,586
890,830
378,26
0,394
214,523
335,210
75,30
845,282
395,357
1285,187
597,806
160,154
589,760
52,843
654,117
711,170
137,78
1130,595
959,37
1220,690
797,252
923,444
1285,259
55,99
980,638
641,305
781,549
1126,768
184,161
915,477
731,702
888,868
537,194
738,219
1288,777
982,285
1087,357
595,254
1288,161
266,21
1310,136
1144,745
1111,562
671,893
1198,756
1176,389
1123,372
748,831
1168,400
810,886
805,141
705,614
269,760
417,126
1044,313
1228,226
1158,204
288,645
870,504
266,313
1253,470
954,84
134,806
420,694
348,711
957,623
1285,271
1007,485
428,593
494,493
1051,5
274,698
852,150
1216,327
559,477
522,625
691,644
261,787
823,346
773,775
907,254
1288,789
728,466
559,665
664,809
833,656
258,679
422,474
877,95
1253,246
822,709
224,542
154,456
502,302
171,231
1253,872
1260,824
256,176
372,364
358,743
1056,225
1049,787
1168,285
679,465
157,590
214,820
1051,89
652,358
192,80
888,474
679,241
430,201
681,53
572,668
626,501
724,663
78,98
639,229
591,495
798,721
494,857
437,10
1131,312
1028,194
72,428
1014,151
229,117
1086,835
923,892
515,485
485,288
366,249
1088,252
745,390
266,425
880,649
952,814
656,329
865,78
440,504
397,889
706,294
1096,56
177,672
900,285
154,785
803,582
1126,126
174,466
438,171
658,760
57,694
962,183
440,820
57,134
1138,774
430,733
107,756
1149,600
1242,889
997,57
1156,886
331,497
947,856
671,445
658,358
1118,169
1156,456
276,488
873,884
267,10
880,61
805,589
944,249
412,812
326,394
1265,816
460,74
562,526
377,56
552,61
291,698
822,194
959,241
656,758
1071,332
22,341
565,504
1052,679
1091,299
1217,639
852,296
1268,394
738,883
1238,225
938,194
811,124
1039,347
507,648
460,737
1019,849
177,841
798,270
319,684
915,865
117,226
1006,509
274,163
561,535
497,606
585,3
185,52
30,814
348,183
221,799
395,477
749,887
957,603
1014,631
1310,842
723,473
214,717
1290,768
50,40
1087,2
797,556
425,768
1163,785
387,514
1302,809
813,606
649,786
189,719
343,843
947,262
992,439
93,639
261,451
13,133
932,837
1153,590
582,109
1053,565
922,581
634,320
311,856
1176,281
1265,368
460,605
1084,301
1144,73
341,837
706,729
303,485
445,450
960,385
353,603
1151,122
160,37
564,690
798,574
1049,891
885,563
363,710
879,537
840,49
751,229
1285,779
868,562
353,623
1059,109
269,134
257,329
788,711
462,590
74,490
706,600
602,753
229,246
602,312
1144,628
646,231
1288,105
566,826
619,625
440,717
536,549
1275,250
477,501
1168,721
907,108
708,507
1039,547
13,729
93,455
1118,647
1161,165
74,378
417,518
142,721
967,843
1307,436
947,710
699,773
1280,814
425,200
1260,138
110,395
356,84
1310,381
1146,86
522,631
110,499
5,353
629,563
1242,679
840,319
276,756
134,358
1287,689
152,690
1260,698
217,682
1034,57
440,522
201,773
1193,567
870,729
1253,424
301,719
8,533
738,443
706,165
475,458
256,718
298,875
999,856
894,740
105,246
420,679
668,810
1073,765
301,603
853,756
763,138
991,796
954,19
400,428
1144,266
323,497
938,364
708,312
462,395
870,74
52,644
982,665
1066,40
433,95
421,535
1230,788
997,110
1019,698
161,600
1094,420
559,417
923,702
1022,645
166,821
505,841
162,282
773,119
90,690
977,786
586,231
223,892
457,756
1232,322
512,721
137,443
1201,234
979,49
1258,283
97,246
422,868
1009,187
889,87
251,416
1202,492
1302,533
763,756
597,424
311,221
313,589
1253,134
810,557
1235,30
356,19
982,829
1145,527
132,30
885,648
654,381
654,65
234,816
458,710
1111,332
1019,710
331,49
561,7
440,281
360,150
261,198
440,165
412,59
1131,582
78,754
1009,164
587,501
1029,773
746,690
50,600
1260,756
117,327
257,441
582,557
89,481
400,669
216,249
701,190
50,196
1302,737
933,838
421,87
82,674
80,554
951,30
1089,799
1163,458
562,816
1141,836
65,507
110,691
162,612
189,368
490,170
634,520
179,582
656,198
160,857
1041,760
549,205
994,712
1236,516
1280,239
201,121
706,586
1131,436
629,501
237,70
1285,331
898,82
1230,554
350,161
59,254
945,194
1071,780
1034,308
878,833
1220,669
1049,835
1043,10
142,46
403,109
599,547
403,640
872,723
351,37
562,511
1131,113
462,695
1252,315
179,333
842,282
460,254
887,665
184,768
529,549
269,872
1096,717
117,345
1089,95
159,122
475,436
803,246
597,312
605,614
751,417
542,56
751,453
1015,702
713,760
822,681
595,640
895,108
1056,18
1220,231
1150,717
1173,443
269,470
147,5
214,726
667,0
1243,537
159,621
349,170
1305,338
388,581
611,773
160,149
1228,450
550,700
875,747
305,668
1173,3
418,679
102,833
500,886
1148,612
923,436
894,154
813,288
996,660
922,469
55,302
902,891
1168,718
403,108
490,808
458,184
857,247
678,553
662,887
149,165
1238,785
805,672
520,389
1202,402
1101,700
870,372
1193,119
788,631
734,572
793,371
661,556
1168,848
224,352
505,277
562,368
11,0
888,455
1131,781
865,892
1006,52
298,19
714,385
209,700
803,436
7,679
579,702
522,263
1150,269
1252,586
160,73
1238,428
103,380
1278,374
808,578
169,288
495,845
668,84
137,451
338,182
850,737
587,393
721,555
1275,698
741,105
774,866
803,648
1039,35
1253,200
1303,679
831,278
843,633
421,439
78,572
576,572
852,710
259,889
416,740
294,150
184,817
678,777
994,182
442,562
393,3
885,200
669,761
1150,149
1034,474
542,585
189,67
820,276
65,339
31,416
209,567
502,578
1081,648
445,78
1118,247
217,458
336,455
1126,817
805,753
331,677
25,331
336,439
92,158
960,733
137,603
174,428
734,140
460,157
43,569
58,586
196,632
313,784
199,287
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