summaryrefslogtreecommitdiffstats
path: root/day5
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-05 23:05:13 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-05 23:05:13 +0100
commit372f7ec0a7e8c3358129f200f5b5b74e79ae83ff (patch)
tree15b662aefd79ed589bde8cf56018b6ee801327e0 /day5
parent5ba784ae1220ec930ffce9ea1ccbb1d6411f841e (diff)
download2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.tar.gz
2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.tar.bz2
2021-372f7ec0a7e8c3358129f200f5b5b74e79ae83ff.zip
Day 5 part 1
Diffstat (limited to 'day5')
-rw-r--r--day5/__init__.py74
-rw-r--r--day5/example.txt10
-rw-r--r--day5/input.txt500
-rw-r--r--day5/test_init.py15
4 files changed, 599 insertions, 0 deletions
diff --git a/day5/__init__.py b/day5/__init__.py
new file mode 100644
index 0000000..4d5260d
--- /dev/null
+++ b/day5/__init__.py
@@ -0,0 +1,74 @@
1from collections import Counter
2from copy import copy
3from dataclasses import dataclass
4from typing import Iterator, List, Optional, Tuple, Callable
5
6from aoc import BaseAssignment
7
8Vector = Tuple[Tuple[int, int], Tuple[int, int]]
9
10class Assignment(BaseAssignment):
11 def parse_item(self, item: str) -> Vector:
12 start, end = item.split('->')
13 return (
14 tuple(int(_) for _ in start.strip().split(',')),
15 tuple(int(_) for _ in end.strip().split(',')),
16 )
17
18 def read_input(self, example = False) -> List[Vector]:
19 return list(super().read_input(example))
20
21
22def is_horizontal(v: Vector):
23 return v[0][0] == v[1][0]
24
25def is_vertical(v: Vector):
26 return v[0][1] == v[1][1]
27
28def points_in_vector(vector: Vector) -> List[Tuple[int, int]]:
29 first_x = min(vector[0][0], vector[1][0])
30 last_x = max(vector[0][0], vector[1][0])
31 first_y = min(vector[0][1], vector[1][1])
32 last_y = max(vector[0][1], vector[1][1])
33
34 return [
35 (vector[0][0], y)
36 for y
37 in range(first_y, last_y + 1)
38 ] if is_horizontal(vector) else [
39 (x, vector[0][1])
40 for x
41 in range(first_x, last_x + 1)
42 ] if is_vertical(vector) else []
43
44
45class AssignmentOne(Assignment):
46 example_result = 5
47
48 def run(self, input: List[Vector]) -> int:
49 coordinates = []
50
51 for vector in input:
52 coordinates += points_in_vector(vector)
53
54 return len([c for c in Counter(coordinates).values() if c >= 2])
55
56
57
58
59# class AssignmentTwo(Assignment):
60# example_result = 1924
61#
62# def run(self, input: Tuple[List[int], List[BingoCard]]) -> int:
63# nrs, cards = input
64#
65# in_game = copy(cards)
66# for nr in nrs:
67# for card in copy(in_game):
68# card.mark(nr)
69#
70# if card.bingo:
71# in_game.remove(card)
72#
73# if len(in_game) == 0:
74# return nr * sum(card.unmarked) \ No newline at end of file
diff --git a/day5/example.txt b/day5/example.txt
new file mode 100644
index 0000000..1d4e36d
--- /dev/null
+++ b/day5/example.txt
@@ -0,0 +1,10 @@
10,9 -> 5,9
28,0 -> 0,8
39,4 -> 3,4
42,2 -> 2,1
57,0 -> 7,4
66,4 -> 2,0
70,9 -> 2,9
83,4 -> 1,4
90,0 -> 8,8
105,5 -> 8,2 \ No newline at end of file
diff --git a/day5/input.txt b/day5/input.txt
new file mode 100644
index 0000000..fc4f01d
--- /dev/null
+++ b/day5/input.txt
@@ -0,0 +1,500 @@
1498,436 -> 498,932
2173,176 -> 845,848
3927,799 -> 927,418
4576,67 -> 801,67
5908,147 -> 743,147
6300,478 -> 300,224
7286,979 -> 286,310
8230,435 -> 729,934
9260,602 -> 260,56
1082,686 -> 655,113
11460,918 -> 460,224
12191,820 -> 454,820
13964,483 -> 964,772
14395,705 -> 142,705
15281,563 -> 134,563
16155,509 -> 152,509
17754,586 -> 742,586
18620,114 -> 620,775
19450,476 -> 450,212
20135,845 -> 591,845
21922,902 -> 110,90
22562,71 -> 895,71
23843,13 -> 843,620
24413,398 -> 486,471
25206,601 -> 409,398
26854,813 -> 65,813
27625,17 -> 293,349
28778,807 -> 778,145
29372,655 -> 566,655
30520,490 -> 520,289
31450,787 -> 450,717
32416,649 -> 826,649
33479,601 -> 313,601
34817,560 -> 817,862
35135,272 -> 287,272
36705,250 -> 796,159
37457,656 -> 445,656
38667,829 -> 659,829
39804,450 -> 804,410
40565,322 -> 565,113
41645,614 -> 972,614
4294,634 -> 421,634
43739,903 -> 122,903
44730,549 -> 963,782
45798,563 -> 969,563
46784,245 -> 176,245
47439,872 -> 448,872
48107,586 -> 906,586
4933,162 -> 573,702
5048,426 -> 362,112
51927,662 -> 927,939
52723,363 -> 723,389
53321,237 -> 982,237
54554,541 -> 554,726
55779,292 -> 779,61
56864,491 -> 864,970
57497,482 -> 497,801
58919,767 -> 919,965
5957,611 -> 57,221
60200,481 -> 200,229
61164,292 -> 164,608
62103,664 -> 694,73
63950,162 -> 181,931
64977,336 -> 293,336
65946,912 -> 80,912
66805,142 -> 89,858
67976,85 -> 245,85
68987,961 -> 987,530
69783,683 -> 443,683
70276,746 -> 490,532
71424,352 -> 424,725
72103,31 -> 559,31
73697,284 -> 697,305
74292,789 -> 627,454
7561,83 -> 151,83
76686,199 -> 652,233
77780,883 -> 780,512
78901,609 -> 346,54
79136,939 -> 136,612
80678,594 -> 408,594
81987,693 -> 987,178
82783,517 -> 828,517
83718,846 -> 23,151
84416,286 -> 518,286
85480,612 -> 130,612
86801,805 -> 801,259
87676,749 -> 119,192
88330,954 -> 936,348
89258,407 -> 258,791
90497,804 -> 668,804
91330,723 -> 497,556
92691,253 -> 691,408
93141,68 -> 365,68
94643,497 -> 305,159
95498,938 -> 765,671
96982,825 -> 580,825
97863,300 -> 959,396
98291,64 -> 291,745
99601,609 -> 601,182
100564,428 -> 564,66
101792,365 -> 792,161
102718,123 -> 718,813
103545,493 -> 903,851
104176,13 -> 988,825
105514,205 -> 514,415
10686,825 -> 446,825
107684,867 -> 684,951
108265,917 -> 936,246
109310,655 -> 310,920
110794,370 -> 809,370
111170,475 -> 75,570
112128,644 -> 200,572
113293,105 -> 600,105
114846,984 -> 976,984
115531,311 -> 977,311
116749,565 -> 315,565
117892,311 -> 452,751
11829,980 -> 974,35
119433,355 -> 433,307
120718,528 -> 261,985
121649,488 -> 156,488
122571,265 -> 119,717
123801,31 -> 66,766
124984,521 -> 213,521
125553,973 -> 20,973
126981,984 -> 15,18
127174,117 -> 174,880
128308,161 -> 308,443
129342,633 -> 342,507
130871,822 -> 258,822
131899,835 -> 83,19
132557,242 -> 557,211
133163,57 -> 895,789
134252,84 -> 342,174
135863,512 -> 863,66
136351,458 -> 351,304
137548,427 -> 266,145
138794,624 -> 794,303
139408,889 -> 408,457
14010,782 -> 10,216
141837,230 -> 837,213
142973,809 -> 109,809
143913,53 -> 41,925
144637,324 -> 921,40
145806,603 -> 806,800
146814,181 -> 41,954
147320,790 -> 574,790
148281,683 -> 646,683
149145,344 -> 246,344
150806,128 -> 806,164
151677,855 -> 650,882
152177,841 -> 177,25
153775,254 -> 41,254
154337,685 -> 563,459
15569,271 -> 69,463
156748,390 -> 504,634
157370,203 -> 370,338
158401,914 -> 436,879
159562,139 -> 266,435
160253,99 -> 232,99
161238,270 -> 142,270
162532,555 -> 433,555
163219,23 -> 919,723
164988,783 -> 988,795
16560,663 -> 12,663
166392,961 -> 962,961
167116,805 -> 23,712
168739,824 -> 68,824
169968,461 -> 245,461
170511,926 -> 918,519
17176,729 -> 367,438
172335,138 -> 335,849
173888,53 -> 86,855
174660,111 -> 660,534
175938,981 -> 63,106
176190,487 -> 145,487
17713,738 -> 13,18
178964,959 -> 16,11
179940,508 -> 456,508
180311,516 -> 311,391
181249,430 -> 597,778
182944,60 -> 944,337
183696,228 -> 490,228
184189,458 -> 341,610
185414,970 -> 570,970
18651,912 -> 960,912
187736,923 -> 96,923
188884,482 -> 552,150
189819,41 -> 168,41
190762,806 -> 169,806
191285,59 -> 890,59
192891,751 -> 469,751
193147,466 -> 147,868
194151,626 -> 151,45
195986,986 -> 10,10
196539,681 -> 539,979
197530,273 -> 530,181
198613,140 -> 599,154
199426,473 -> 426,467
200338,795 -> 857,795
201548,711 -> 715,711
202479,30 -> 479,177
20316,871 -> 75,812
204410,621 -> 552,621
205730,814 -> 58,814
206990,728 -> 209,728
207164,783 -> 164,820
208523,689 -> 189,355
20986,531 -> 875,531
210470,585 -> 152,267
211704,484 -> 551,484
212829,197 -> 776,197
21339,10 -> 979,950
214653,144 -> 844,144
21580,462 -> 104,486
216249,703 -> 160,703
217817,725 -> 817,15
218972,895 -> 173,96
219610,28 -> 610,664
220452,581 -> 452,848
221243,623 -> 144,623
222837,175 -> 757,95
223592,843 -> 592,210
224233,505 -> 848,505
225591,710 -> 820,481
226945,46 -> 247,744
227357,883 -> 961,279
228615,900 -> 185,900
22993,673 -> 976,673
230629,692 -> 298,361
231888,111 -> 888,652
232873,876 -> 366,369
23314,373 -> 14,370
234690,786 -> 73,169
235305,888 -> 305,811
236429,490 -> 429,442
237177,92 -> 177,836
238892,733 -> 754,733
239442,211 -> 442,896
240686,21 -> 686,809
241413,689 -> 275,689
242522,601 -> 522,525
24341,952 -> 925,68
244635,97 -> 538,194
245849,826 -> 351,826
246915,254 -> 877,292
247118,64 -> 928,64
24836,21 -> 985,970
249334,374 -> 589,374
250152,742 -> 286,742
251714,441 -> 459,696
252871,370 -> 871,191
253410,527 -> 410,62
254713,114 -> 804,205
255577,271 -> 577,369
256144,72 -> 939,867
257424,139 -> 424,491
258458,585 -> 228,815
259550,151 -> 932,533
260179,200 -> 179,981
261423,524 -> 701,246
262755,552 -> 755,689
263728,250 -> 840,362
264892,583 -> 892,785
265328,241 -> 506,241
266837,441 -> 837,414
267101,44 -> 942,885
268917,833 -> 451,833
26952,216 -> 35,199
270964,375 -> 451,888
271572,509 -> 572,680
272602,558 -> 602,396
273555,652 -> 763,652
274198,617 -> 523,617
275585,964 -> 585,721
276376,534 -> 556,534
277360,466 -> 360,757
278875,571 -> 875,68
279964,757 -> 595,757
280787,785 -> 804,785
281670,863 -> 343,863
282449,453 -> 449,681
28316,313 -> 657,954
284958,68 -> 706,320
285726,635 -> 726,715
286790,865 -> 790,459
287926,946 -> 13,33
28861,761 -> 247,761
289386,939 -> 386,426
290909,192 -> 909,216
291387,161 -> 28,520
292182,740 -> 182,178
293443,975 -> 715,703
294194,714 -> 870,38
295636,522 -> 636,308
296153,220 -> 248,315
297532,427 -> 72,887
298673,521 -> 464,521
299191,362 -> 749,920
300718,747 -> 306,335
30185,733 -> 561,733
302710,714 -> 243,714
303191,132 -> 704,645
304983,60 -> 76,60
305740,708 -> 460,988
306105,987 -> 908,184
307629,943 -> 629,136
308386,515 -> 236,365
309988,10 -> 12,986
310989,479 -> 989,855
311232,860 -> 232,612
312719,526 -> 406,213
313548,242 -> 175,242
314172,21 -> 172,295
315158,935 -> 158,267
316743,331 -> 109,965
317177,465 -> 177,883
318694,46 -> 694,714
319589,328 -> 915,328
320721,865 -> 170,314
32156,373 -> 934,373
322558,635 -> 558,199
323187,116 -> 323,252
324743,875 -> 827,791
325313,219 -> 718,624
326418,175 -> 830,175
327620,244 -> 557,244
32833,199 -> 33,768
329595,562 -> 595,75
330725,294 -> 725,97
331645,554 -> 552,554
332909,828 -> 849,828
333820,25 -> 110,25
334591,803 -> 961,803
335101,109 -> 760,109
336473,593 -> 473,609
337990,714 -> 822,714
338263,183 -> 263,301
339919,409 -> 414,914
340554,36 -> 554,165
341223,168 -> 989,934
342764,30 -> 725,30
343518,730 -> 185,397
344482,548 -> 482,472
345933,34 -> 484,34
346210,494 -> 210,769
347354,659 -> 278,659
34879,172 -> 603,696
349703,120 -> 300,523
350466,228 -> 315,77
351977,43 -> 515,505
352474,95 -> 163,95
353213,275 -> 213,39
354166,12 -> 332,12
355931,606 -> 122,606
356117,424 -> 616,424
357493,980 -> 21,508
358172,492 -> 766,492
359630,59 -> 301,59
360377,10 -> 970,603
361119,287 -> 119,332
362635,829 -> 635,313
363626,132 -> 550,208
364520,570 -> 27,77
365817,974 -> 347,974
366943,99 -> 943,734
367470,895 -> 686,679
36892,303 -> 286,497
369869,714 -> 869,879
37063,234 -> 777,948
371426,461 -> 622,461
372832,275 -> 832,410
373898,937 -> 248,287
374797,891 -> 70,164
375642,974 -> 642,143
376954,654 -> 868,654
377603,34 -> 871,302
37842,849 -> 65,849
379683,377 -> 534,526
380390,227 -> 490,227
381422,980 -> 422,223
382761,640 -> 612,640
383238,170 -> 959,891
384663,674 -> 904,433
385675,833 -> 165,833
386535,128 -> 535,14
387509,444 -> 509,519
388137,668 -> 442,668
38974,427 -> 74,685
390291,827 -> 291,486
391557,302 -> 196,302
392960,164 -> 210,914
393599,418 -> 392,418
394655,339 -> 13,981
395202,243 -> 847,888
39613,329 -> 359,675
397814,191 -> 814,802
398555,942 -> 555,377
399879,359 -> 879,935
400622,841 -> 622,821
401639,592 -> 823,592
402699,187 -> 46,840
403967,927 -> 286,927
404484,185 -> 484,441
405103,442 -> 232,442
406176,19 -> 459,302
407696,871 -> 109,871
408424,475 -> 424,722
40990,28 -> 837,28
410177,114 -> 737,674
411626,753 -> 736,753
412133,616 -> 133,180
413934,62 -> 934,442
414100,883 -> 100,497
415549,483 -> 729,483
416976,39 -> 36,979
417386,140 -> 114,412
418363,459 -> 219,459
419563,77 -> 563,598
420142,60 -> 827,745
421460,519 -> 610,519
42256,249 -> 325,249
423924,492 -> 768,336
424165,726 -> 475,416
42557,418 -> 580,418
426756,554 -> 163,554
42782,237 -> 846,237
428123,99 -> 383,99
429816,527 -> 816,65
430224,171 -> 866,813
431833,92 -> 362,92
432671,808 -> 671,984
433522,25 -> 522,43
434248,424 -> 16,192
435297,225 -> 805,225
436454,534 -> 79,534
437380,936 -> 184,936
438286,866 -> 286,97
439543,807 -> 224,807
440518,700 -> 600,782
441961,809 -> 566,414
442140,298 -> 140,695
443328,51 -> 328,534
444927,942 -> 28,43
445166,654 -> 745,75
44625,976 -> 988,13
447563,99 -> 721,99
448605,808 -> 605,648
449483,603 -> 836,956
45039,127 -> 39,815
451942,157 -> 409,690
452358,593 -> 733,218
453655,297 -> 892,297
454477,276 -> 919,276
455355,583 -> 825,113
456186,329 -> 522,665
457497,838 -> 725,838
458868,54 -> 200,54
459270,14 -> 270,307
46047,33 -> 47,218
461238,190 -> 280,232
462857,700 -> 56,700
463593,164 -> 38,719
464765,237 -> 25,977
465517,48 -> 517,570
466584,587 -> 311,587
467781,268 -> 641,128
468564,656 -> 564,154
46931,530 -> 31,496
47050,36 -> 954,940
47117,989 -> 987,19
472280,88 -> 762,88
473104,698 -> 261,541
47468,303 -> 388,303
475959,966 -> 356,966
476794,91 -> 35,850
477783,197 -> 608,197
478413,506 -> 218,311
479642,10 -> 118,534
480812,963 -> 657,963
481877,828 -> 850,828
482214,546 -> 214,116
483559,685 -> 700,826
484494,66 -> 774,66
485886,730 -> 886,660
486811,67 -> 820,67
487375,510 -> 678,510
488711,868 -> 490,647
489420,945 -> 420,222
490453,948 -> 279,948
491956,749 -> 956,311
49255,846 -> 55,260
493402,94 -> 799,94
494683,206 -> 683,324
495710,269 -> 187,269
496120,114 -> 120,784
497401,616 -> 401,989
498641,650 -> 850,650
499424,208 -> 351,135
50031,985 -> 988,28 \ No newline at end of file
diff --git a/day5/test_init.py b/day5/test_init.py
new file mode 100644
index 0000000..5096a60
--- /dev/null
+++ b/day5/test_init.py
@@ -0,0 +1,15 @@
1from day5 import points_in_vector
2
3
4def test_points_in_vector():
5 assert points_in_vector(((1,1), (1,3))) == [
6 (1, 1), (1, 2), (1, 3)
7 ]
8
9 assert points_in_vector(((9,7), (7,7))) == [
10 (7, 7), (8, 7), (9, 7)
11 ]
12
13
14
15