summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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