summaryrefslogtreecommitdiffstats
path: root/day13
diff options
context:
space:
mode:
Diffstat (limited to 'day13')
-rw-r--r--day13/__init__.py109
-rw-r--r--day13/example.txt21
-rw-r--r--day13/input.txt996
3 files changed, 1126 insertions, 0 deletions
diff --git a/day13/__init__.py b/day13/__init__.py
new file mode 100644
index 0000000..41b81dc
--- /dev/null
+++ b/day13/__init__.py
@@ -0,0 +1,109 @@
1import re
2from abc import ABC
3from copy import copy
4from dataclasses import dataclass
5from typing import List, Tuple, Union, TypedDict
6
7from aoc import BaseAssignment
8
9Coordinate = Tuple[int, int]
10
11def mirrored_index(index: int, fold: int) -> int:
12 return fold + (fold - index)
13
14@dataclass
15class Sheet:
16 dots: List[Coordinate]
17 width: int = 0
18 height: int = 0
19
20 def __post_init__(self):
21 x_coordinates, y_coordinates = zip(*self.dots)
22
23 self.width = max(x_coordinates) + 1
24 self.height = max(y_coordinates) + 1
25
26
27 def fold_x(self, position: int):
28 for x, y in copy(self.dots):
29 mirrored_x = mirrored_index(x, position)
30 if mirrored_x < position:
31 self.dots.append((mirrored_x, y))
32 self.dots.remove((x, y))
33
34 self.width = self.width // 2
35 self.dots = list(set(self.dots))
36
37 def fold_y(self, position: int):
38 for x, y in copy(self.dots):
39 mirrored_y = mirrored_index(y, position)
40 if mirrored_y < position:
41 self.dots.append((x, mirrored_y))
42 self.dots.remove((x, y))
43
44 self.height = self.height // 2
45 self.dots = list(set(self.dots))
46
47 def __repr__(self):
48 dots = set(self.dots)
49 return '\n'.join([
50 '',
51 *[
52 ''.join([
53 '█' if (x, y) in dots else ' '
54 for x in range(self.width)
55 ])
56 for y in range(self.height)
57 ],
58 '',
59 ])
60
61class Instruction(TypedDict):
62 direction: str
63 position: int
64
65coordinate_regex = re.compile('(\d+),(\d+)')
66instruction_regex = re.compile('fold along (?P<direction>[xy])=(?P<position>\d+)')
67
68class Assignment(BaseAssignment, ABC):
69 def parse_item(self, item: str) -> Union[Coordinate,Instruction]:
70 if coordinate := coordinate_regex.match(item):
71 return tuple(int(i) for i in coordinate.groups())
72
73 if instruction := instruction_regex.match(item):
74 return {
75 **instruction.groupdict(),
76 'position': int(instruction.groupdict()['position'])
77 }
78
79 def read_input(self, example = False) -> Tuple[Sheet, List[Instruction]]:
80 input = super().read_input(example)
81
82 coordinates: List[Coordinate] = []
83
84 while True:
85 coordinate = next(input)
86 if coordinate is None:
87 break
88
89 coordinates.append(coordinate)
90
91 sheet = Sheet(dots=coordinates)
92
93 return sheet, list(input)
94
95
96class AssignmentOne(Assignment):
97 example_result = 17
98
99 def run(self, input: Tuple[Sheet, List[Instruction]]) -> int:
100 sheet, instructions = input
101 getattr(sheet, f'fold_{instructions[0]["direction"]}')(instructions[0]["position"])
102 return len(sheet.dots)
103
104class AssignmentTwo(Assignment):
105 def run(self, input: Tuple[Sheet, List[Instruction]]) -> Sheet:
106 sheet, instructions = input
107 for instruction in instructions:
108 getattr(sheet, f'fold_{instruction["direction"]}')(instruction["position"])
109 return sheet
diff --git a/day13/example.txt b/day13/example.txt
new file mode 100644
index 0000000..282114c
--- /dev/null
+++ b/day13/example.txt
@@ -0,0 +1,21 @@
16,10
20,14
39,10
40,3
510,4
64,11
76,0
86,12
94,1
100,13
1110,12
123,4
133,0
148,4
151,10
162,14
178,10
189,0
19
20fold along y=7
21fold along x=5
diff --git a/day13/input.txt b/day13/input.txt
new file mode 100644
index 0000000..0c5500b
--- /dev/null
+++ b/day13/input.txt
@@ -0,0 +1,996 @@
1381,143
2954,841
31061,183
4654,247
530,192
672,215
71223,680
8512,367
9150,747
1085,890
11495,127
12714,710
1331,238
14242,878
151218,772
16862,654
171190,710
18112,229
19276,679
201225,240
211104,654
221120,175
23266,690
24142,79
25977,639
26626,203
271134,147
28898,206
29438,731
3075,800
31731,576
32932,287
33283,236
34659,222
35241,751
36682,182
37305,612
38651,218
39152,491
40890,100
41788,823
421198,229
43842,189
441283,789
45887,113
4617,679
4792,122
48831,21
49894,637
50803,52
511235,36
52373,669
53512,444
54864,376
55184,316
56104,822
57303,340
5882,289
59465,878
60890,794
611041,29
62925,116
63847,553
64905,330
65296,382
66708,387
671255,718
68530,669
69977,460
701153,568
7130,30
72350,740
73872,781
74944,893
751069,143
76438,427
771222,425
78925,555
791034,215
80547,379
811233,567
82982,373
831054,754
84110,508
85214,550
86291,505
871036,163
881089,834
891223,214
90569,415
911179,534
92529,578
93206,654
941082,551
95495,655
96353,871
97528,838
98224,431
99380,283
100455,505
1011168,130
102626,483
103687,758
1043,672
10555,340
106907,599
107124,369
108328,73
1091123,396
110194,171
111903,892
11280,660
113277,184
1141233,631
115736,821
11613,750
117562,502
1181307,373
119447,65
120654,763
12143,130
122781,578
123711,610
124159,319
125323,890
1261164,679
127535,829
128888,178
1291190,240
130110,59
131370,800
132135,789
1331111,389
13427,789
1351283,229
136828,315
1371175,789
138736,597
139857,578
1401151,486
1411236,693
1421218,271
1431087,444
144741,206
145947,420
146803,207
1471178,7
148756,808
149507,394
1501178,439
151550,126
1521233,782
1531218,392
15413,669
155805,893
1561153,12
15758,236
1581094,337
159689,684
160793,464
161176,747
162338,102
163402,21
1641004,652
1651044,876
1661200,59
167900,458
168238,469
1691242,579
1701163,388
171142,415
172574,682
173684,203
174518,271
175359,126
176522,103
17720,541
1781168,344
179756,86
180117,673
181454,838
182763,379
183982,719
184132,119
185202,416
186623,880
1871297,750
1881297,669
1891101,730
190723,561
1911210,346
192579,576
193888,40
194135,658
195589,599
196944,445
197201,354
1981240,779
199175,626
2001005,612
201462,365
2021168,792
203897,143
204395,687
205311,239
2061233,711
207364,488
208266,876
2091225,767
210924,408
211602,785
212492,323
213618,591
214997,673
215728,763
2161168,450
21777,711
218782,838
219820,312
220780,221
221835,282
222682,600
223733,858
2241252,184
225326,424
226403,599
227320,425
228885,798
229693,876
230954,467
231586,315
232127,540
233282,334
234162,246
23558,122
236862,206
237946,854
238582,315
239579,688
240159,224
2411297,228
242691,551
243348,176
24449,184
245904,430
246940,352
24755,718
248525,98
249268,408
2501168,367
251512,311
2521178,190
253704,54
254982,315
2551052,635
256432,574
257940,542
258937,893
259792,175
260405,241
261577,782
262512,226
263313,673
264266,602
265474,231
266885,730
267554,86
268105,50
269594,689
270628,600
271522,375
272140,408
273576,766
2741134,747
275366,893
2761020,488
2771307,72
278878,320
279855,729
280773,92
281298,871
28285,767
28325,603
284862,10
2851282,19
286753,388
287311,500
288185,108
289277,374
290967,207
291699,289
292969,225
293328,358
29480,297
295733,782
296669,666
297815,127
2981049,599
2991206,822
3001280,330
3011285,695
302231,409
303925,778
3043,879
305726,660
306146,679
307651,73
308387,551
30952,11
310120,436
3111141,528
3121046,429
31395,561
31428,691
315549,60
31621,612
317744,201
318408,361
319304,826
32075,878
3211039,4
322897,222
3231235,654
324915,207
3251260,663
326387,343
32777,40
328542,229
3291119,117
330684,427
331924,684
332788,375
333651,676
334572,798
335741,448
336616,100
337464,679
338686,416
339132,551
3401094,557
341750,707
342579,206
34397,718
344748,271
345276,635
346651,374
3471022,557
348124,669
349229,164
350758,207
351693,764
352142,444
35392,772
3541210,824
355416,649
356266,428
357956,712
3581124,714
359224,483
360209,164
3611225,127
3621109,204
363420,516
3641086,154
365507,52
366999,394
367969,669
3681168,479
36988,245
3701141,814
371736,350
3721282,203
373792,600
374863,513
3751258,355
37694,187
377880,411
3781062,16
379768,229
380686,390
381894,245
382914,623
383157,12
38452,387
385378,623
386894,693
3871203,442
38894,35
3891218,563
390453,578
391551,86
392659,521
3931072,705
394254,91
3951186,525
396659,218
397627,789
398728,131
399888,707
400656,22
401224,17
4021109,652
403820,548
404932,831
405679,219
406309,455
407944,449
4081216,854
4091059,815
410962,176
4111068,16
4121069,751
4131260,792
414226,536
4151044,154
41660,226
417946,406
418393,14
4191086,226
420378,831
421890,516
422431,444
423853,868
424401,887
425631,675
426617,802
427194,723
428542,217
429813,882
4301275,119
4311232,318
4321034,565
433753,718
434932,259
4351242,271
436268,184
437460,275
438167,94
4391178,847
440599,340
441623,758
4421151,821
443377,798
4441068,878
445258,635
4461034,550
447462,81
448604,439
4491251,343
450731,688
451376,632
4521181,882
4531125,786
454999,500
455364,504
45650,834
457621,684
458396,44
4591245,151
4601161,414
4611021,466
4621088,822
463184,652
464334,425
465266,204
466758,707
467535,381
468464,623
46943,289
470224,226
471175,873
472416,245
473628,712
4741237,455
4751272,150
4761126,653
4771012,759
47868,271
479715,343
480350,154
4811235,800
4821250,226
483244,10
484944,1
485629,654
486157,326
4871001,7
488388,674
489589,606
490922,292
491517,430
492733,205
493410,605
4941280,504
495184,653
4961015,598
497238,189
498659,676
4991193,673
500748,772
501905,564
502907,634
503377,350
504572,884
505582,131
506626,243
5071034,679
508798,31
509120,240
51028,243
511478,147
512284,38
513654,647
514792,719
515492,312
516189,654
51774,873
518708,539
519249,183
520602,539
521537,506
522628,47
523651,521
524296,512
525624,116
526604,565
527890,563
5281260,455
529785,658
530756,346
531574,43
532249,653
5331238,763
534522,280
535569,240
5361213,611
537487,710
538200,751
5391280,414
540729,215
541616,673
542403,260
543228,551
544820,582
545584,115
546432,434
547900,10
5481160,747
549888,803
550815,575
551624,390
5521026,542
5531260,887
5541205,834
555410,458
5561020,87
557780,413
558850,275
5591009,240
560721,295
5611272,744
562569,448
563846,719
564706,792
5651007,554
566676,886
567586,719
5681190,688
569452,520
570200,56
5711190,206
572738,884
573267,103
57428,701
575626,53
576629,603
57788,425
578842,856
5791294,455
580602,508
5811059,257
582460,495
583492,771
5841183,354
5851255,340
586142,527
5871210,808
588716,689
589301,240
590944,72
591264,518
592422,406
593910,803
594979,442
595544,387
596400,215
597616,744
598222,822
599805,1
60013,144
601897,560
602309,7
603753,631
604604,117
605872,427
606681,647
6071235,240
6081061,856
6091190,654
610766,387
61135,801
612378,287
613879,198
614569,206
615490,312
6161168,764
617730,21
618551,24
619599,610
620512,653
621366,449
62288,201
62392,271
624865,730
625632,740
626631,219
6271240,115
6281123,274
629328,373
630574,821
631678,740
6321242,626
6331235,438
634612,352
635679,610
636959,554
637813,80
638721,158
639448,10
6401252,658
641433,589
642185,786
6431245,599
644880,292
64528,483
646301,871
647167,224
648903,450
649651,452
650416,201
6511022,374
652562,54
6531170,408
654281,225
655410,10
6561141,12
657884,551
6581043,343
659510,603
660519,236
661887,857
6621143,74
663132,774
664550,296
665724,315
6661143,261
667378,719
6681175,236
669788,519
6701124,705
671139,172
672413,591
673242,16
674341,669
675706,264
676594,241
6773,224
678773,221
679879,444
680291,474
681271,4
682378,679
6831089,813
684522,775
6851179,360
686965,215
687855,876
688855,781
689624,778
690847,341
6911185,802
692328,821
693877,81
694186,180
6951284,623
696676,606
697562,772
6981022,337
69930,116
700216,337
701749,362
702847,777
703363,57
704900,829
705984,521
706949,658
70774,469
708463,553
709791,105
710125,802
711331,390
7121232,128
713848,365
714907,260
715478,595
716321,367
7171109,578
718999,655
719201,242
7201072,649
721865,388
7221094,163
7231241,828
724187,274
725366,520
726569,446
72794,488
728652,184
729425,730
73026,600
731828,579
732502,403
733463,117
734447,513
735633,658
736932,271
737425,544
738298,471
739634,456
740788,614
741700,560
7421213,487
743741,672
744200,838
745509,455
746782,751
747264,205
74833,348
7491307,879
750525,546
751348,718
752845,878
7531268,682
754313,221
7551042,408
756479,21
757687,14
758587,561
759984,175
7601238,679
761623,14
762937,449
763760,126
764979,890
76528,53
766321,460
767862,884
7681026,38
769441,856
77072,679
771606,840
772592,429
773986,262
774223,696
775472,155
776577,406
777422,854
778882,360
779750,488
780572,515
7811135,268
782604,663
783888,406
784773,506
785567,886
7861184,591
787480,43
7881131,164
789724,402
790512,578
791201,204
7921289,684
793455,876
7941136,851
79552,81
7961044,204
797589,295
798741,890
7991072,469
800341,225
8011089,60
802572,96
8031260,24
8041307,676
8051034,774
806721,151
807681,240
8081004,204
809880,877
8101076,383
811363,420
812396,623
813706,117
814378,215
8151215,857
8161236,469
8171007,667
818232,707
8191289,282
820167,74
821651,222
822831,873
823497,814
824773,388
82585,127
826629,647
8271006,68
828736,544
8291109,354
8301063,444
831522,614
832512,792
833818,373
834487,185
835589,151
836415,662
837514,714
8381078,707
8391242,763
840806,299
8411079,409
842954,759
8431205,844
84452,59
845569,672
846741,479
847939,829
848455,389
8491051,207
850182,603
8511273,130
852813,814
853858,30
854234,63
8551218,740
856110,835
8571274,303
858915,687
859679,284
860729,7
8611210,70
8621151,408
863580,21
864856,751
865933,350
866711,564
867580,873
868748,47
8691044,690
870587,225
871602,109
872490,582
873356,759
874328,536
875649,316
876977,367
877745,663
878987,890
879517,16
8801252,236
881446,376
88273,679
883708,508
8841148,694
8851307,749
8861053,66
887288,333
888197,236
889984,73
890221,50
891258,259
89262,824
8931061,815
8941171,620
895801,455
896364,303
8971210,86
898431,198
899561,532
900358,512
9011166,739
9021216,488
903793,16
904706,663
905447,829
906922,220
907422,259
9081217,532
9091228,379
910400,803
911708,507
91280,682
913962,877
914787,744
915162,694
916733,112
917628,182
9181233,183
9191158,491
92016,455
9211233,260
922135,236
923385,865
924705,262
925863,65
926783,540
9271228,96
928303,284
929581,887
93077,634
931962,718
932552,207
93315,599
9341066,458
9351222,469
936594,242
93797,487
938100,86
939798,241
940879,450
941515,274
942780,669
94380,73
9441178,551
9451022,333
9461206,434
947159,486
948251,260
949316,102
950624,480
9511121,430
952445,730
953562,346
9541061,711
955271,130
956452,374
9571170,486
958199,876
9591261,632
960552,687
961888,259
9621001,679
96385,240
964745,567
96574,201
966537,316
967224,463
968261,295
969487,261
9701268,352
971582,763
972946,591
973232,187
974706,439
9751135,873
976144,739
977522,71
978597,477
979693,802
980798,792
9811225,336
982326,623
9831126,316
984
985fold along x=655
986fold along y=447
987fold along x=327
988fold along y=223
989fold along x=163
990fold along y=111
991fold along x=81
992fold along y=55
993fold along x=40
994fold along y=27
995fold along y=13
996fold along y=6