Compare commits
16 commits
68bb1132a9
...
a53cf34520
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | a53cf34520 | ||
Bruno BELANYI | e8d61b3cf8 | ||
Bruno BELANYI | 48e597d188 | ||
Bruno BELANYI | 65943d6c4a | ||
Bruno BELANYI | f42799b6c8 | ||
Bruno BELANYI | f8a4ffa90d | ||
Bruno BELANYI | 5766f226a1 | ||
Bruno BELANYI | 7ba2723a40 | ||
Bruno BELANYI | b7362b4889 | ||
Bruno BELANYI | 78ae52c9c5 | ||
Bruno BELANYI | 571136b6df | ||
Bruno BELANYI | b87458688a | ||
Bruno BELANYI | 57a2d60dc0 | ||
Bruno BELANYI | a968844df2 | ||
Bruno BELANYI | 4195a9e321 | ||
Bruno BELANYI | 9d9dec0715 |
32
2021/d02/ex1/ex1.py
Executable file
32
2021/d02/ex1/ex1.py
Executable file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
|
||||
def solve(input: List[str]) -> int:
|
||||
x, y = 0, 0
|
||||
|
||||
for instruction in input:
|
||||
dir, length = instruction.split(" ")
|
||||
length = int(length)
|
||||
if dir == "forward":
|
||||
x += length
|
||||
elif dir == "down":
|
||||
y += length
|
||||
elif dir == "up":
|
||||
y -= length
|
||||
else:
|
||||
assert False
|
||||
|
||||
return x * y
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [line.strip() for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
2021/d02/ex1/input
Normal file
1000
2021/d02/ex1/input
Normal file
File diff suppressed because it is too large
Load diff
33
2021/d02/ex2/ex2.py
Executable file
33
2021/d02/ex2/ex2.py
Executable file
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
|
||||
def solve(input: List[str]) -> int:
|
||||
x, y, aim = 0, 0, 0
|
||||
|
||||
for instruction in input:
|
||||
dir, length = instruction.split(" ")
|
||||
length = int(length)
|
||||
if dir == "forward":
|
||||
x += length
|
||||
y += length * aim
|
||||
elif dir == "down":
|
||||
aim += length
|
||||
elif dir == "up":
|
||||
aim -= length
|
||||
else:
|
||||
assert False
|
||||
|
||||
return x * y
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [line.strip() for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
2021/d02/ex2/input
Normal file
1000
2021/d02/ex2/input
Normal file
File diff suppressed because it is too large
Load diff
30
2021/d03/ex1/ex1.py
Executable file
30
2021/d03/ex1/ex1.py
Executable file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
|
||||
def solve(input: List[int]) -> int:
|
||||
gamma, epsilon = 0, 0
|
||||
bit = 1
|
||||
|
||||
while any(map(bool, input)):
|
||||
num_bits = sum(n % 2 for n in input)
|
||||
if num_bits >= len(input) / 2:
|
||||
gamma += bit
|
||||
else:
|
||||
epsilon += bit
|
||||
input = [n // 2 for n in input]
|
||||
bit *= 2
|
||||
|
||||
return gamma * epsilon
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [int(line, 2) for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
2021/d03/ex1/input
Normal file
1000
2021/d03/ex1/input
Normal file
File diff suppressed because it is too large
Load diff
68
2021/d03/ex2/ex2.py
Executable file
68
2021/d03/ex2/ex2.py
Executable file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from typing import List
|
||||
|
||||
|
||||
def solve(input: List[int]) -> int:
|
||||
def highest_pow2(n: int) -> int:
|
||||
return len(bin(n)) - 2 - 1 # '0b' prefix, and off-by-one
|
||||
|
||||
def count_bits_at(input: List[int], bit: int) -> int:
|
||||
return sum((n & pow(2, bit)) != 0 for n in input)
|
||||
|
||||
def filter_by_bit_value(input: List[int], bit: int, value: int) -> List[int]:
|
||||
# Simplify the filter by mapping the value to the actual power of 2
|
||||
if value != 0:
|
||||
value = pow(2, bit)
|
||||
return list(filter(lambda n: (n & pow(2, bit)) == value, input))
|
||||
|
||||
def filter_oxygen(input: List[int], bit: int) -> List[int]:
|
||||
# No further processing needed
|
||||
if len(input) <= 1:
|
||||
return input
|
||||
|
||||
num_bits = count_bits_at(input, bit)
|
||||
# Keep 1s on equality
|
||||
if num_bits >= len(input) / 2:
|
||||
return filter_by_bit_value(input, bit, 1)
|
||||
else:
|
||||
return filter_by_bit_value(input, bit, 0)
|
||||
|
||||
def filter_co2(input: List[int], bit: int) -> List[int]:
|
||||
# No further processing needed
|
||||
if len(input) <= 1:
|
||||
return input
|
||||
|
||||
num_bits = count_bits_at(input, bit)
|
||||
# Keep 0s on equality
|
||||
if num_bits < len(input) / 2:
|
||||
return filter_by_bit_value(input, bit, 1)
|
||||
else:
|
||||
return filter_by_bit_value(input, bit, 0)
|
||||
|
||||
oxygen, co2 = 0, 0
|
||||
max_bit = max(highest_pow2(n) for n in input)
|
||||
oxygen_input, co2_input = deepcopy(input), deepcopy(input)
|
||||
|
||||
for bit in range(max_bit, -1, -1):
|
||||
oxygen_input = filter_oxygen(oxygen_input, bit)
|
||||
co2_input = filter_co2(co2_input, bit)
|
||||
|
||||
if len(oxygen_input) == 1:
|
||||
oxygen = oxygen_input[0]
|
||||
if len(co2_input) == 1:
|
||||
co2 = co2_input[0]
|
||||
|
||||
return oxygen * co2
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [int(line, 2) for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
2021/d03/ex2/input
Normal file
1000
2021/d03/ex2/input
Normal file
File diff suppressed because it is too large
Load diff
59
2021/d04/ex1/ex1.py
Executable file
59
2021/d04/ex1/ex1.py
Executable file
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List, Tuple
|
||||
|
||||
Board = List[List[int]]
|
||||
|
||||
|
||||
def solve(input: List[str]) -> int:
|
||||
def parse_input() -> Tuple[List[int], List[Board]]:
|
||||
def listify(line: str, delim: str = " ") -> List[int]:
|
||||
return [int(n) for n in line.split(delim) if n != ""]
|
||||
|
||||
draw_order = listify(input[0], delim=",")
|
||||
boards: List[Board] = []
|
||||
for l in input[1:]:
|
||||
if l == "":
|
||||
boards.append([])
|
||||
continue
|
||||
boards[-1].append(listify(l))
|
||||
|
||||
return draw_order, boards
|
||||
|
||||
def bingo(board: Board, draw: List[int]) -> bool:
|
||||
assert len(board) == len(board[0])
|
||||
|
||||
def line(l: int) -> bool:
|
||||
return all(n in draw for n in board[l])
|
||||
|
||||
def row(r: int) -> bool:
|
||||
return all(board[i][r] in draw for i in range(len(board)))
|
||||
|
||||
lines = [line(i) for i in range(len(board))]
|
||||
rows = [row(i) for i in range(len(board[0]))]
|
||||
|
||||
return any(itertools.chain(lines, rows))
|
||||
|
||||
draw_order, boards = parse_input()
|
||||
|
||||
draw = []
|
||||
for d in draw_order:
|
||||
draw.append(d)
|
||||
for b in boards:
|
||||
if not bingo(b, draw):
|
||||
continue
|
||||
return d * sum(n for n in itertools.chain.from_iterable(b) if n not in draw)
|
||||
|
||||
# Sanity check
|
||||
assert False
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [line.strip() for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
601
2021/d04/ex1/input
Normal file
601
2021/d04/ex1/input
Normal file
|
@ -0,0 +1,601 @@
|
|||
85,84,30,15,46,71,64,45,13,90,63,89,62,25,87,68,73,47,65,78,2,27,67,95,88,99,96,17,42,31,91,98,57,28,38,93,43,0,55,49,22,24,82,54,59,52,3,26,9,32,4,48,39,50,80,21,5,1,23,10,58,34,12,35,74,8,6,79,40,76,86,69,81,61,14,92,97,19,7,51,33,11,77,75,20,70,29,36,60,18,56,37,72,41,94,44,83,66,16,53
|
||||
|
||||
78 13 8 62 67
|
||||
42 89 97 16 65
|
||||
5 12 73 50 56
|
||||
45 10 63 41 64
|
||||
49 1 95 71 17
|
||||
|
||||
60 25 66 82 22
|
||||
94 45 68 5 12
|
||||
46 44 48 31 34
|
||||
10 56 37 96 81
|
||||
99 39 84 32 6
|
||||
|
||||
11 86 77 36 2
|
||||
57 68 27 74 4
|
||||
81 92 49 37 51
|
||||
78 43 94 46 63
|
||||
13 52 72 17 44
|
||||
|
||||
88 13 81 21 20
|
||||
80 99 23 37 53
|
||||
44 68 15 38 55
|
||||
84 48 82 97 6
|
||||
4 43 52 72 31
|
||||
|
||||
39 62 45 86 44
|
||||
12 17 16 7 6
|
||||
84 42 82 34 85
|
||||
19 77 9 48 98
|
||||
21 99 67 26 69
|
||||
|
||||
1 75 50 5 44
|
||||
3 28 62 17 43
|
||||
14 52 64 77 81
|
||||
32 89 7 11 70
|
||||
38 36 71 45 58
|
||||
|
||||
53 32 35 69 63
|
||||
6 21 75 64 96
|
||||
10 89 15 48 26
|
||||
23 20 43 57 33
|
||||
18 49 51 47 74
|
||||
|
||||
20 79 9 74 13
|
||||
52 28 77 26 43
|
||||
57 83 4 25 70
|
||||
90 1 30 53 38
|
||||
56 66 82 35 51
|
||||
|
||||
12 3 31 93 8
|
||||
20 27 51 78 9
|
||||
29 46 82 85 75
|
||||
15 76 91 70 63
|
||||
59 39 13 43 79
|
||||
|
||||
46 35 15 13 2
|
||||
65 69 97 77 87
|
||||
64 59 94 88 40
|
||||
34 79 92 93 58
|
||||
47 28 74 82 29
|
||||
|
||||
32 38 24 68 12
|
||||
8 78 79 89 43
|
||||
67 54 6 98 48
|
||||
1 14 83 15 37
|
||||
44 10 97 74 33
|
||||
|
||||
9 95 2 99 1
|
||||
8 42 60 56 40
|
||||
32 11 71 14 80
|
||||
77 6 68 46 48
|
||||
98 70 39 44 62
|
||||
|
||||
43 94 41 13 15
|
||||
96 99 35 27 8
|
||||
22 75 73 17 90
|
||||
62 23 5 88 3
|
||||
10 52 61 60 57
|
||||
|
||||
31 62 74 3 79
|
||||
15 49 60 28 71
|
||||
66 2 11 36 41
|
||||
34 80 33 94 75
|
||||
64 56 84 70 16
|
||||
|
||||
98 94 68 32 26
|
||||
61 7 52 66 18
|
||||
40 20 82 81 74
|
||||
28 36 89 14 35
|
||||
71 11 44 13 72
|
||||
|
||||
81 30 6 86 37
|
||||
46 45 64 83 62
|
||||
7 70 38 51 15
|
||||
91 41 26 40 4
|
||||
87 0 82 74 60
|
||||
|
||||
83 99 26 69 1
|
||||
6 98 53 31 43
|
||||
82 64 42 90 34
|
||||
87 62 11 40 39
|
||||
77 51 2 30 97
|
||||
|
||||
96 5 24 44 32
|
||||
48 92 78 74 76
|
||||
99 33 93 97 49
|
||||
45 8 88 66 59
|
||||
52 64 29 60 82
|
||||
|
||||
69 23 59 96 71
|
||||
14 93 21 44 62
|
||||
65 84 2 39 1
|
||||
0 68 38 81 4
|
||||
48 31 26 60 34
|
||||
|
||||
24 46 44 52 98
|
||||
65 23 31 89 5
|
||||
34 79 75 96 41
|
||||
76 28 90 12 11
|
||||
68 29 38 70 50
|
||||
|
||||
51 0 45 23 20
|
||||
44 49 12 31 7
|
||||
41 26 46 75 92
|
||||
90 30 72 95 55
|
||||
87 57 10 99 40
|
||||
|
||||
25 67 80 74 44
|
||||
3 82 27 81 11
|
||||
33 42 97 57 70
|
||||
19 94 0 2 49
|
||||
6 90 60 29 58
|
||||
|
||||
79 59 96 68 14
|
||||
38 70 65 66 69
|
||||
36 75 20 18 29
|
||||
64 88 35 61 43
|
||||
57 76 62 23 25
|
||||
|
||||
60 9 81 94 62
|
||||
73 20 87 72 14
|
||||
95 63 42 51 13
|
||||
75 83 32 30 66
|
||||
97 6 80 82 17
|
||||
|
||||
3 88 31 43 68
|
||||
20 78 47 10 91
|
||||
14 42 40 74 39
|
||||
5 32 16 97 1
|
||||
9 33 49 70 36
|
||||
|
||||
77 31 65 27 52
|
||||
49 74 57 25 66
|
||||
24 4 39 33 1
|
||||
23 14 19 2 21
|
||||
80 71 29 81 91
|
||||
|
||||
32 68 47 3 88
|
||||
1 97 99 28 80
|
||||
2 25 18 31 51
|
||||
26 10 73 34 40
|
||||
8 55 45 36 37
|
||||
|
||||
79 81 33 94 51
|
||||
84 4 91 0 69
|
||||
49 80 35 67 20
|
||||
98 48 64 38 30
|
||||
25 83 45 97 42
|
||||
|
||||
18 5 84 94 50
|
||||
36 47 2 52 65
|
||||
39 77 83 37 80
|
||||
51 88 15 12 31
|
||||
87 17 68 48 67
|
||||
|
||||
39 95 30 8 86
|
||||
45 57 40 51 60
|
||||
85 88 33 93 25
|
||||
76 52 37 68 6
|
||||
11 80 69 19 71
|
||||
|
||||
6 71 25 66 54
|
||||
33 17 98 63 20
|
||||
27 14 44 43 18
|
||||
68 10 50 35 65
|
||||
61 3 83 12 13
|
||||
|
||||
46 21 43 15 19
|
||||
99 82 8 95 80
|
||||
1 10 45 58 53
|
||||
23 94 50 66 52
|
||||
57 98 26 77 90
|
||||
|
||||
11 50 55 28 79
|
||||
4 3 26 57 56
|
||||
68 86 10 87 69
|
||||
32 35 89 63 29
|
||||
66 27 33 8 30
|
||||
|
||||
23 34 94 93 47
|
||||
7 71 9 52 50
|
||||
45 79 13 43 86
|
||||
0 51 17 6 26
|
||||
4 82 44 38 37
|
||||
|
||||
49 24 16 64 32
|
||||
46 84 3 29 51
|
||||
71 82 33 61 26
|
||||
15 5 94 86 41
|
||||
63 36 10 67 43
|
||||
|
||||
94 17 3 71 91
|
||||
93 50 88 36 27
|
||||
54 68 7 8 34
|
||||
9 92 37 45 52
|
||||
47 29 70 10 69
|
||||
|
||||
79 27 30 0 12
|
||||
51 70 19 89 20
|
||||
2 42 64 21 49
|
||||
48 39 1 3 56
|
||||
98 35 95 82 72
|
||||
|
||||
91 71 65 95 44
|
||||
26 72 92 59 43
|
||||
61 93 6 4 90
|
||||
76 31 8 1 29
|
||||
82 64 89 22 45
|
||||
|
||||
55 4 1 42 87
|
||||
88 34 67 83 45
|
||||
22 23 98 24 12
|
||||
74 72 49 32 25
|
||||
73 7 19 26 3
|
||||
|
||||
0 43 50 57 80
|
||||
68 21 87 1 91
|
||||
60 6 81 78 99
|
||||
35 98 72 49 16
|
||||
36 25 13 48 22
|
||||
|
||||
59 1 26 3 71
|
||||
43 55 50 7 16
|
||||
5 64 29 38 84
|
||||
41 23 60 19 24
|
||||
85 58 49 98 33
|
||||
|
||||
80 48 3 65 38
|
||||
30 97 96 45 7
|
||||
6 85 8 90 40
|
||||
37 78 84 16 24
|
||||
69 11 43 64 63
|
||||
|
||||
28 14 19 1 97
|
||||
37 39 86 23 64
|
||||
20 67 85 65 90
|
||||
54 51 59 91 43
|
||||
17 30 11 24 7
|
||||
|
||||
22 88 27 43 10
|
||||
35 3 72 52 57
|
||||
61 54 28 69 37
|
||||
71 78 96 82 81
|
||||
33 39 32 40 7
|
||||
|
||||
50 60 69 33 57
|
||||
84 22 95 74 6
|
||||
90 94 71 45 68
|
||||
72 86 77 9 24
|
||||
73 12 89 13 1
|
||||
|
||||
66 35 36 87 73
|
||||
77 96 52 47 68
|
||||
63 4 83 20 95
|
||||
17 70 9 18 50
|
||||
98 40 25 60 26
|
||||
|
||||
31 37 81 34 56
|
||||
3 15 43 51 35
|
||||
67 70 1 20 12
|
||||
80 54 69 17 88
|
||||
65 91 60 8 53
|
||||
|
||||
76 23 87 41 18
|
||||
49 58 92 98 25
|
||||
77 53 44 17 27
|
||||
67 28 37 66 95
|
||||
59 39 33 4 34
|
||||
|
||||
0 25 2 5 22
|
||||
26 85 90 51 21
|
||||
31 79 10 41 45
|
||||
69 56 1 67 40
|
||||
59 98 99 89 6
|
||||
|
||||
95 67 72 52 78
|
||||
88 61 96 11 43
|
||||
34 73 53 54 8
|
||||
71 3 70 42 58
|
||||
12 82 97 68 98
|
||||
|
||||
20 10 13 74 89
|
||||
82 25 45 92 61
|
||||
58 62 0 22 57
|
||||
68 90 36 18 75
|
||||
48 39 69 4 52
|
||||
|
||||
40 3 86 33 98
|
||||
30 67 39 7 69
|
||||
80 64 77 54 51
|
||||
24 49 6 68 61
|
||||
62 94 1 26 50
|
||||
|
||||
89 88 7 21 87
|
||||
83 10 78 27 97
|
||||
35 62 86 13 38
|
||||
28 80 19 36 75
|
||||
98 93 47 33 57
|
||||
|
||||
22 88 35 79 85
|
||||
98 96 89 69 17
|
||||
37 62 57 39 1
|
||||
99 10 55 50 71
|
||||
65 94 67 4 63
|
||||
|
||||
7 83 51 95 98
|
||||
56 93 62 85 9
|
||||
72 14 44 70 67
|
||||
42 4 65 37 54
|
||||
47 82 1 60 55
|
||||
|
||||
0 73 60 25 64
|
||||
90 11 93 85 89
|
||||
80 97 86 76 96
|
||||
43 92 88 72 44
|
||||
62 87 81 34 49
|
||||
|
||||
47 27 89 98 68
|
||||
86 76 14 96 17
|
||||
21 4 41 74 29
|
||||
18 82 33 34 20
|
||||
30 62 95 42 51
|
||||
|
||||
45 4 70 20 53
|
||||
66 39 43 82 1
|
||||
54 30 68 77 42
|
||||
61 41 65 94 35
|
||||
25 78 22 26 46
|
||||
|
||||
70 73 44 48 61
|
||||
69 7 85 47 89
|
||||
91 22 12 98 11
|
||||
25 60 58 46 54
|
||||
5 37 83 62 65
|
||||
|
||||
47 62 30 70 40
|
||||
86 9 64 61 0
|
||||
27 63 90 88 17
|
||||
18 71 42 33 93
|
||||
91 14 81 4 31
|
||||
|
||||
81 7 22 94 55
|
||||
99 90 60 9 46
|
||||
65 2 47 1 73
|
||||
78 76 75 19 88
|
||||
63 51 86 56 49
|
||||
|
||||
25 27 12 22 30
|
||||
87 75 16 4 32
|
||||
19 73 5 20 52
|
||||
18 6 34 94 31
|
||||
23 96 84 26 66
|
||||
|
||||
23 69 51 35 5
|
||||
13 76 99 89 82
|
||||
88 3 50 54 33
|
||||
19 59 92 84 34
|
||||
64 80 42 40 60
|
||||
|
||||
15 91 92 60 36
|
||||
46 40 53 34 27
|
||||
13 35 96 16 42
|
||||
4 61 81 56 24
|
||||
85 21 7 99 20
|
||||
|
||||
32 37 19 21 28
|
||||
66 7 96 46 88
|
||||
23 52 25 50 22
|
||||
53 62 34 81 27
|
||||
98 31 14 40 49
|
||||
|
||||
23 43 71 61 12
|
||||
8 94 91 74 7
|
||||
67 2 59 77 4
|
||||
39 18 97 41 21
|
||||
55 15 31 9 38
|
||||
|
||||
29 69 52 16 75
|
||||
71 15 34 79 86
|
||||
62 57 48 44 54
|
||||
11 32 96 13 60
|
||||
56 77 26 68 82
|
||||
|
||||
93 57 21 94 31
|
||||
29 4 59 24 40
|
||||
13 99 34 96 91
|
||||
70 55 47 62 51
|
||||
33 32 19 69 71
|
||||
|
||||
76 80 1 57 20
|
||||
13 28 72 27 79
|
||||
40 21 71 37 85
|
||||
26 12 67 33 99
|
||||
11 41 62 18 64
|
||||
|
||||
23 22 92 69 86
|
||||
38 79 47 56 83
|
||||
74 46 1 95 24
|
||||
93 71 28 54 52
|
||||
94 51 33 57 73
|
||||
|
||||
17 96 4 81 76
|
||||
67 20 24 21 70
|
||||
28 77 3 74 10
|
||||
45 78 18 7 15
|
||||
8 48 27 58 13
|
||||
|
||||
51 58 59 73 35
|
||||
13 7 92 15 98
|
||||
75 26 1 49 24
|
||||
91 85 44 34 74
|
||||
64 2 20 72 90
|
||||
|
||||
46 89 50 54 79
|
||||
9 60 98 36 78
|
||||
91 16 80 92 20
|
||||
77 69 13 76 75
|
||||
95 41 45 3 40
|
||||
|
||||
86 7 67 20 99
|
||||
14 18 97 70 0
|
||||
81 27 89 30 3
|
||||
39 37 56 42 32
|
||||
35 71 49 8 73
|
||||
|
||||
60 67 61 6 86
|
||||
25 41 24 29 88
|
||||
98 3 90 56 87
|
||||
45 22 84 70 99
|
||||
53 59 27 26 57
|
||||
|
||||
17 4 11 41 66
|
||||
28 39 27 54 89
|
||||
3 78 37 93 29
|
||||
95 23 86 51 40
|
||||
75 67 71 57 92
|
||||
|
||||
60 41 91 89 52
|
||||
68 46 83 62 1
|
||||
18 21 72 19 35
|
||||
55 34 11 16 75
|
||||
32 71 61 78 50
|
||||
|
||||
27 38 70 48 93
|
||||
16 2 80 17 63
|
||||
97 89 55 86 85
|
||||
54 5 41 33 60
|
||||
51 95 12 67 37
|
||||
|
||||
72 17 74 6 41
|
||||
53 19 8 12 92
|
||||
39 84 82 63 48
|
||||
22 21 87 13 32
|
||||
40 34 64 15 31
|
||||
|
||||
75 2 46 64 99
|
||||
26 72 79 90 76
|
||||
85 68 10 28 67
|
||||
20 34 81 12 83
|
||||
92 1 65 43 71
|
||||
|
||||
49 80 85 54 9
|
||||
31 40 22 94 51
|
||||
12 73 43 68 98
|
||||
78 91 70 3 28
|
||||
47 59 69 99 62
|
||||
|
||||
46 56 28 73 20
|
||||
5 29 69 68 22
|
||||
64 12 8 52 92
|
||||
36 44 90 72 0
|
||||
76 48 33 86 66
|
||||
|
||||
99 61 97 17 74
|
||||
32 52 44 42 9
|
||||
57 67 36 41 31
|
||||
68 1 50 22 11
|
||||
73 12 21 48 62
|
||||
|
||||
44 53 77 88 87
|
||||
27 99 59 98 74
|
||||
33 66 51 14 34
|
||||
29 30 60 49 80
|
||||
47 84 36 12 71
|
||||
|
||||
29 89 54 59 70
|
||||
87 65 77 38 25
|
||||
40 17 41 9 30
|
||||
45 27 0 5 24
|
||||
52 8 35 68 10
|
||||
|
||||
16 41 66 87 76
|
||||
94 70 51 48 96
|
||||
90 73 98 89 91
|
||||
4 46 30 28 63
|
||||
68 45 37 80 57
|
||||
|
||||
19 11 46 41 14
|
||||
94 48 66 86 9
|
||||
42 90 56 70 21
|
||||
95 54 74 30 87
|
||||
81 89 49 60 34
|
||||
|
||||
18 90 79 64 98
|
||||
27 74 59 53 11
|
||||
96 45 17 14 23
|
||||
9 60 30 42 12
|
||||
97 21 31 5 41
|
||||
|
||||
98 63 51 92 64
|
||||
55 30 46 22 91
|
||||
8 73 61 57 67
|
||||
37 60 49 31 10
|
||||
80 99 77 11 82
|
||||
|
||||
52 69 77 41 8
|
||||
94 11 78 62 28
|
||||
91 39 96 79 3
|
||||
44 88 37 0 47
|
||||
6 80 49 98 48
|
||||
|
||||
93 2 70 26 4
|
||||
47 8 94 12 3
|
||||
10 7 24 40 23
|
||||
49 84 50 56 44
|
||||
41 53 96 1 85
|
||||
|
||||
76 78 70 24 75
|
||||
71 19 85 77 25
|
||||
21 44 58 45 64
|
||||
40 38 9 50 61
|
||||
79 42 86 37 6
|
||||
|
||||
34 39 94 84 0
|
||||
90 80 78 54 49
|
||||
13 81 87 60 56
|
||||
74 59 75 41 28
|
||||
29 67 66 44 20
|
||||
|
||||
50 66 43 39 16
|
||||
88 94 60 70 64
|
||||
63 80 56 69 36
|
||||
53 48 32 22 79
|
||||
59 77 20 30 67
|
||||
|
||||
70 56 80 12 11
|
||||
35 55 40 71 87
|
||||
84 27 96 46 85
|
||||
20 23 26 29 14
|
||||
58 37 21 75 68
|
||||
|
||||
78 23 13 37 94
|
||||
65 44 54 43 38
|
||||
29 60 83 1 57
|
||||
98 2 75 12 14
|
||||
92 25 48 9 52
|
||||
|
||||
64 37 93 48 34
|
||||
22 81 58 5 13
|
||||
63 80 2 67 53
|
||||
62 52 79 41 44
|
||||
83 75 96 91 88
|
||||
|
||||
1 54 88 45 90
|
||||
81 78 19 8 40
|
||||
17 74 69 87 33
|
||||
9 64 85 50 71
|
||||
92 38 65 82 41
|
||||
|
||||
2 62 96 60 81
|
||||
51 1 34 48 25
|
||||
78 13 74 65 42
|
||||
46 64 57 19 72
|
||||
85 88 53 68 76
|
||||
|
||||
57 95 40 92 27
|
||||
65 37 42 90 9
|
||||
17 72 78 43 45
|
||||
87 28 48 81 79
|
||||
7 4 24 67 70
|
69
2021/d04/ex2/ex2.py
Executable file
69
2021/d04/ex2/ex2.py
Executable file
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import List, Tuple
|
||||
|
||||
Board = List[List[int]]
|
||||
|
||||
|
||||
def solve(input: List[str]) -> int:
|
||||
def parse_input() -> Tuple[List[int], List[Board]]:
|
||||
def listify(line: str, delim: str = " ") -> List[int]:
|
||||
return [int(n) for n in line.split(delim) if n != ""]
|
||||
|
||||
draw_order = listify(input[0], delim=",")
|
||||
boards: List[Board] = []
|
||||
for l in input[1:]:
|
||||
if l == "":
|
||||
boards.append([])
|
||||
continue
|
||||
boards[-1].append(listify(l))
|
||||
|
||||
return draw_order, boards
|
||||
|
||||
def bingo(board: Board, draw: List[int]) -> bool:
|
||||
assert len(board) == len(board[0])
|
||||
|
||||
def line(l: int) -> bool:
|
||||
return all(n in draw for n in board[l])
|
||||
|
||||
def row(r: int) -> bool:
|
||||
return all(board[i][r] in draw for i in range(len(board)))
|
||||
|
||||
lines = [line(i) for i in range(len(board))]
|
||||
rows = [row(i) for i in range(len(board[0]))]
|
||||
|
||||
return any(itertools.chain(lines, rows))
|
||||
|
||||
draw_order, boards = parse_input()
|
||||
|
||||
draw = []
|
||||
losers = set(range(len(boards)))
|
||||
|
||||
for d in draw_order:
|
||||
draw.append(d)
|
||||
for i, b in enumerate(boards):
|
||||
# Stop early if we already know this board wins
|
||||
if i not in losers:
|
||||
continue
|
||||
if not bingo(b, draw):
|
||||
continue
|
||||
# Discard winners until the last one
|
||||
if len(losers) != 1:
|
||||
losers.discard(i)
|
||||
continue
|
||||
# At thid point, we must be looking at the last winning board
|
||||
return d * sum(n for n in itertools.chain.from_iterable(b) if n not in draw)
|
||||
|
||||
# Sanity check
|
||||
assert False
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = [line.strip() for line in sys.stdin.readlines()]
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
601
2021/d04/ex2/input
Normal file
601
2021/d04/ex2/input
Normal file
|
@ -0,0 +1,601 @@
|
|||
85,84,30,15,46,71,64,45,13,90,63,89,62,25,87,68,73,47,65,78,2,27,67,95,88,99,96,17,42,31,91,98,57,28,38,93,43,0,55,49,22,24,82,54,59,52,3,26,9,32,4,48,39,50,80,21,5,1,23,10,58,34,12,35,74,8,6,79,40,76,86,69,81,61,14,92,97,19,7,51,33,11,77,75,20,70,29,36,60,18,56,37,72,41,94,44,83,66,16,53
|
||||
|
||||
78 13 8 62 67
|
||||
42 89 97 16 65
|
||||
5 12 73 50 56
|
||||
45 10 63 41 64
|
||||
49 1 95 71 17
|
||||
|
||||
60 25 66 82 22
|
||||
94 45 68 5 12
|
||||
46 44 48 31 34
|
||||
10 56 37 96 81
|
||||
99 39 84 32 6
|
||||
|
||||
11 86 77 36 2
|
||||
57 68 27 74 4
|
||||
81 92 49 37 51
|
||||
78 43 94 46 63
|
||||
13 52 72 17 44
|
||||
|
||||
88 13 81 21 20
|
||||
80 99 23 37 53
|
||||
44 68 15 38 55
|
||||
84 48 82 97 6
|
||||
4 43 52 72 31
|
||||
|
||||
39 62 45 86 44
|
||||
12 17 16 7 6
|
||||
84 42 82 34 85
|
||||
19 77 9 48 98
|
||||
21 99 67 26 69
|
||||
|
||||
1 75 50 5 44
|
||||
3 28 62 17 43
|
||||
14 52 64 77 81
|
||||
32 89 7 11 70
|
||||
38 36 71 45 58
|
||||
|
||||
53 32 35 69 63
|
||||
6 21 75 64 96
|
||||
10 89 15 48 26
|
||||
23 20 43 57 33
|
||||
18 49 51 47 74
|
||||
|
||||
20 79 9 74 13
|
||||
52 28 77 26 43
|
||||
57 83 4 25 70
|
||||
90 1 30 53 38
|
||||
56 66 82 35 51
|
||||
|
||||
12 3 31 93 8
|
||||
20 27 51 78 9
|
||||
29 46 82 85 75
|
||||
15 76 91 70 63
|
||||
59 39 13 43 79
|
||||
|
||||
46 35 15 13 2
|
||||
65 69 97 77 87
|
||||
64 59 94 88 40
|
||||
34 79 92 93 58
|
||||
47 28 74 82 29
|
||||
|
||||
32 38 24 68 12
|
||||
8 78 79 89 43
|
||||
67 54 6 98 48
|
||||
1 14 83 15 37
|
||||
44 10 97 74 33
|
||||
|
||||
9 95 2 99 1
|
||||
8 42 60 56 40
|
||||
32 11 71 14 80
|
||||
77 6 68 46 48
|
||||
98 70 39 44 62
|
||||
|
||||
43 94 41 13 15
|
||||
96 99 35 27 8
|
||||
22 75 73 17 90
|
||||
62 23 5 88 3
|
||||
10 52 61 60 57
|
||||
|
||||
31 62 74 3 79
|
||||
15 49 60 28 71
|
||||
66 2 11 36 41
|
||||
34 80 33 94 75
|
||||
64 56 84 70 16
|
||||
|
||||
98 94 68 32 26
|
||||
61 7 52 66 18
|
||||
40 20 82 81 74
|
||||
28 36 89 14 35
|
||||
71 11 44 13 72
|
||||
|
||||
81 30 6 86 37
|
||||
46 45 64 83 62
|
||||
7 70 38 51 15
|
||||
91 41 26 40 4
|
||||
87 0 82 74 60
|
||||
|
||||
83 99 26 69 1
|
||||
6 98 53 31 43
|
||||
82 64 42 90 34
|
||||
87 62 11 40 39
|
||||
77 51 2 30 97
|
||||
|
||||
96 5 24 44 32
|
||||
48 92 78 74 76
|
||||
99 33 93 97 49
|
||||
45 8 88 66 59
|
||||
52 64 29 60 82
|
||||
|
||||
69 23 59 96 71
|
||||
14 93 21 44 62
|
||||
65 84 2 39 1
|
||||
0 68 38 81 4
|
||||
48 31 26 60 34
|
||||
|
||||
24 46 44 52 98
|
||||
65 23 31 89 5
|
||||
34 79 75 96 41
|
||||
76 28 90 12 11
|
||||
68 29 38 70 50
|
||||
|
||||
51 0 45 23 20
|
||||
44 49 12 31 7
|
||||
41 26 46 75 92
|
||||
90 30 72 95 55
|
||||
87 57 10 99 40
|
||||
|
||||
25 67 80 74 44
|
||||
3 82 27 81 11
|
||||
33 42 97 57 70
|
||||
19 94 0 2 49
|
||||
6 90 60 29 58
|
||||
|
||||
79 59 96 68 14
|
||||
38 70 65 66 69
|
||||
36 75 20 18 29
|
||||
64 88 35 61 43
|
||||
57 76 62 23 25
|
||||
|
||||
60 9 81 94 62
|
||||
73 20 87 72 14
|
||||
95 63 42 51 13
|
||||
75 83 32 30 66
|
||||
97 6 80 82 17
|
||||
|
||||
3 88 31 43 68
|
||||
20 78 47 10 91
|
||||
14 42 40 74 39
|
||||
5 32 16 97 1
|
||||
9 33 49 70 36
|
||||
|
||||
77 31 65 27 52
|
||||
49 74 57 25 66
|
||||
24 4 39 33 1
|
||||
23 14 19 2 21
|
||||
80 71 29 81 91
|
||||
|
||||
32 68 47 3 88
|
||||
1 97 99 28 80
|
||||
2 25 18 31 51
|
||||
26 10 73 34 40
|
||||
8 55 45 36 37
|
||||
|
||||
79 81 33 94 51
|
||||
84 4 91 0 69
|
||||
49 80 35 67 20
|
||||
98 48 64 38 30
|
||||
25 83 45 97 42
|
||||
|
||||
18 5 84 94 50
|
||||
36 47 2 52 65
|
||||
39 77 83 37 80
|
||||
51 88 15 12 31
|
||||
87 17 68 48 67
|
||||
|
||||
39 95 30 8 86
|
||||
45 57 40 51 60
|
||||
85 88 33 93 25
|
||||
76 52 37 68 6
|
||||
11 80 69 19 71
|
||||
|
||||
6 71 25 66 54
|
||||
33 17 98 63 20
|
||||
27 14 44 43 18
|
||||
68 10 50 35 65
|
||||
61 3 83 12 13
|
||||
|
||||
46 21 43 15 19
|
||||
99 82 8 95 80
|
||||
1 10 45 58 53
|
||||
23 94 50 66 52
|
||||
57 98 26 77 90
|
||||
|
||||
11 50 55 28 79
|
||||
4 3 26 57 56
|
||||
68 86 10 87 69
|
||||
32 35 89 63 29
|
||||
66 27 33 8 30
|
||||
|
||||
23 34 94 93 47
|
||||
7 71 9 52 50
|
||||
45 79 13 43 86
|
||||
0 51 17 6 26
|
||||
4 82 44 38 37
|
||||
|
||||
49 24 16 64 32
|
||||
46 84 3 29 51
|
||||
71 82 33 61 26
|
||||
15 5 94 86 41
|
||||
63 36 10 67 43
|
||||
|
||||
94 17 3 71 91
|
||||
93 50 88 36 27
|
||||
54 68 7 8 34
|
||||
9 92 37 45 52
|
||||
47 29 70 10 69
|
||||
|
||||
79 27 30 0 12
|
||||
51 70 19 89 20
|
||||
2 42 64 21 49
|
||||
48 39 1 3 56
|
||||
98 35 95 82 72
|
||||
|
||||
91 71 65 95 44
|
||||
26 72 92 59 43
|
||||
61 93 6 4 90
|
||||
76 31 8 1 29
|
||||
82 64 89 22 45
|
||||
|
||||
55 4 1 42 87
|
||||
88 34 67 83 45
|
||||
22 23 98 24 12
|
||||
74 72 49 32 25
|
||||
73 7 19 26 3
|
||||
|
||||
0 43 50 57 80
|
||||
68 21 87 1 91
|
||||
60 6 81 78 99
|
||||
35 98 72 49 16
|
||||
36 25 13 48 22
|
||||
|
||||
59 1 26 3 71
|
||||
43 55 50 7 16
|
||||
5 64 29 38 84
|
||||
41 23 60 19 24
|
||||
85 58 49 98 33
|
||||
|
||||
80 48 3 65 38
|
||||
30 97 96 45 7
|
||||
6 85 8 90 40
|
||||
37 78 84 16 24
|
||||
69 11 43 64 63
|
||||
|
||||
28 14 19 1 97
|
||||
37 39 86 23 64
|
||||
20 67 85 65 90
|
||||
54 51 59 91 43
|
||||
17 30 11 24 7
|
||||
|
||||
22 88 27 43 10
|
||||
35 3 72 52 57
|
||||
61 54 28 69 37
|
||||
71 78 96 82 81
|
||||
33 39 32 40 7
|
||||
|
||||
50 60 69 33 57
|
||||
84 22 95 74 6
|
||||
90 94 71 45 68
|
||||
72 86 77 9 24
|
||||
73 12 89 13 1
|
||||
|
||||
66 35 36 87 73
|
||||
77 96 52 47 68
|
||||
63 4 83 20 95
|
||||
17 70 9 18 50
|
||||
98 40 25 60 26
|
||||
|
||||
31 37 81 34 56
|
||||
3 15 43 51 35
|
||||
67 70 1 20 12
|
||||
80 54 69 17 88
|
||||
65 91 60 8 53
|
||||
|
||||
76 23 87 41 18
|
||||
49 58 92 98 25
|
||||
77 53 44 17 27
|
||||
67 28 37 66 95
|
||||
59 39 33 4 34
|
||||
|
||||
0 25 2 5 22
|
||||
26 85 90 51 21
|
||||
31 79 10 41 45
|
||||
69 56 1 67 40
|
||||
59 98 99 89 6
|
||||
|
||||
95 67 72 52 78
|
||||
88 61 96 11 43
|
||||
34 73 53 54 8
|
||||
71 3 70 42 58
|
||||
12 82 97 68 98
|
||||
|
||||
20 10 13 74 89
|
||||
82 25 45 92 61
|
||||
58 62 0 22 57
|
||||
68 90 36 18 75
|
||||
48 39 69 4 52
|
||||
|
||||
40 3 86 33 98
|
||||
30 67 39 7 69
|
||||
80 64 77 54 51
|
||||
24 49 6 68 61
|
||||
62 94 1 26 50
|
||||
|
||||
89 88 7 21 87
|
||||
83 10 78 27 97
|
||||
35 62 86 13 38
|
||||
28 80 19 36 75
|
||||
98 93 47 33 57
|
||||
|
||||
22 88 35 79 85
|
||||
98 96 89 69 17
|
||||
37 62 57 39 1
|
||||
99 10 55 50 71
|
||||
65 94 67 4 63
|
||||
|
||||
7 83 51 95 98
|
||||
56 93 62 85 9
|
||||
72 14 44 70 67
|
||||
42 4 65 37 54
|
||||
47 82 1 60 55
|
||||
|
||||
0 73 60 25 64
|
||||
90 11 93 85 89
|
||||
80 97 86 76 96
|
||||
43 92 88 72 44
|
||||
62 87 81 34 49
|
||||
|
||||
47 27 89 98 68
|
||||
86 76 14 96 17
|
||||
21 4 41 74 29
|
||||
18 82 33 34 20
|
||||
30 62 95 42 51
|
||||
|
||||
45 4 70 20 53
|
||||
66 39 43 82 1
|
||||
54 30 68 77 42
|
||||
61 41 65 94 35
|
||||
25 78 22 26 46
|
||||
|
||||
70 73 44 48 61
|
||||
69 7 85 47 89
|
||||
91 22 12 98 11
|
||||
25 60 58 46 54
|
||||
5 37 83 62 65
|
||||
|
||||
47 62 30 70 40
|
||||
86 9 64 61 0
|
||||
27 63 90 88 17
|
||||
18 71 42 33 93
|
||||
91 14 81 4 31
|
||||
|
||||
81 7 22 94 55
|
||||
99 90 60 9 46
|
||||
65 2 47 1 73
|
||||
78 76 75 19 88
|
||||
63 51 86 56 49
|
||||
|
||||
25 27 12 22 30
|
||||
87 75 16 4 32
|
||||
19 73 5 20 52
|
||||
18 6 34 94 31
|
||||
23 96 84 26 66
|
||||
|
||||
23 69 51 35 5
|
||||
13 76 99 89 82
|
||||
88 3 50 54 33
|
||||
19 59 92 84 34
|
||||
64 80 42 40 60
|
||||
|
||||
15 91 92 60 36
|
||||
46 40 53 34 27
|
||||
13 35 96 16 42
|
||||
4 61 81 56 24
|
||||
85 21 7 99 20
|
||||
|
||||
32 37 19 21 28
|
||||
66 7 96 46 88
|
||||
23 52 25 50 22
|
||||
53 62 34 81 27
|
||||
98 31 14 40 49
|
||||
|
||||
23 43 71 61 12
|
||||
8 94 91 74 7
|
||||
67 2 59 77 4
|
||||
39 18 97 41 21
|
||||
55 15 31 9 38
|
||||
|
||||
29 69 52 16 75
|
||||
71 15 34 79 86
|
||||
62 57 48 44 54
|
||||
11 32 96 13 60
|
||||
56 77 26 68 82
|
||||
|
||||
93 57 21 94 31
|
||||
29 4 59 24 40
|
||||
13 99 34 96 91
|
||||
70 55 47 62 51
|
||||
33 32 19 69 71
|
||||
|
||||
76 80 1 57 20
|
||||
13 28 72 27 79
|
||||
40 21 71 37 85
|
||||
26 12 67 33 99
|
||||
11 41 62 18 64
|
||||
|
||||
23 22 92 69 86
|
||||
38 79 47 56 83
|
||||
74 46 1 95 24
|
||||
93 71 28 54 52
|
||||
94 51 33 57 73
|
||||
|
||||
17 96 4 81 76
|
||||
67 20 24 21 70
|
||||
28 77 3 74 10
|
||||
45 78 18 7 15
|
||||
8 48 27 58 13
|
||||
|
||||
51 58 59 73 35
|
||||
13 7 92 15 98
|
||||
75 26 1 49 24
|
||||
91 85 44 34 74
|
||||
64 2 20 72 90
|
||||
|
||||
46 89 50 54 79
|
||||
9 60 98 36 78
|
||||
91 16 80 92 20
|
||||
77 69 13 76 75
|
||||
95 41 45 3 40
|
||||
|
||||
86 7 67 20 99
|
||||
14 18 97 70 0
|
||||
81 27 89 30 3
|
||||
39 37 56 42 32
|
||||
35 71 49 8 73
|
||||
|
||||
60 67 61 6 86
|
||||
25 41 24 29 88
|
||||
98 3 90 56 87
|
||||
45 22 84 70 99
|
||||
53 59 27 26 57
|
||||
|
||||
17 4 11 41 66
|
||||
28 39 27 54 89
|
||||
3 78 37 93 29
|
||||
95 23 86 51 40
|
||||
75 67 71 57 92
|
||||
|
||||
60 41 91 89 52
|
||||
68 46 83 62 1
|
||||
18 21 72 19 35
|
||||
55 34 11 16 75
|
||||
32 71 61 78 50
|
||||
|
||||
27 38 70 48 93
|
||||
16 2 80 17 63
|
||||
97 89 55 86 85
|
||||
54 5 41 33 60
|
||||
51 95 12 67 37
|
||||
|
||||
72 17 74 6 41
|
||||
53 19 8 12 92
|
||||
39 84 82 63 48
|
||||
22 21 87 13 32
|
||||
40 34 64 15 31
|
||||
|
||||
75 2 46 64 99
|
||||
26 72 79 90 76
|
||||
85 68 10 28 67
|
||||
20 34 81 12 83
|
||||
92 1 65 43 71
|
||||
|
||||
49 80 85 54 9
|
||||
31 40 22 94 51
|
||||
12 73 43 68 98
|
||||
78 91 70 3 28
|
||||
47 59 69 99 62
|
||||
|
||||
46 56 28 73 20
|
||||
5 29 69 68 22
|
||||
64 12 8 52 92
|
||||
36 44 90 72 0
|
||||
76 48 33 86 66
|
||||
|
||||
99 61 97 17 74
|
||||
32 52 44 42 9
|
||||
57 67 36 41 31
|
||||
68 1 50 22 11
|
||||
73 12 21 48 62
|
||||
|
||||
44 53 77 88 87
|
||||
27 99 59 98 74
|
||||
33 66 51 14 34
|
||||
29 30 60 49 80
|
||||
47 84 36 12 71
|
||||
|
||||
29 89 54 59 70
|
||||
87 65 77 38 25
|
||||
40 17 41 9 30
|
||||
45 27 0 5 24
|
||||
52 8 35 68 10
|
||||
|
||||
16 41 66 87 76
|
||||
94 70 51 48 96
|
||||
90 73 98 89 91
|
||||
4 46 30 28 63
|
||||
68 45 37 80 57
|
||||
|
||||
19 11 46 41 14
|
||||
94 48 66 86 9
|
||||
42 90 56 70 21
|
||||
95 54 74 30 87
|
||||
81 89 49 60 34
|
||||
|
||||
18 90 79 64 98
|
||||
27 74 59 53 11
|
||||
96 45 17 14 23
|
||||
9 60 30 42 12
|
||||
97 21 31 5 41
|
||||
|
||||
98 63 51 92 64
|
||||
55 30 46 22 91
|
||||
8 73 61 57 67
|
||||
37 60 49 31 10
|
||||
80 99 77 11 82
|
||||
|
||||
52 69 77 41 8
|
||||
94 11 78 62 28
|
||||
91 39 96 79 3
|
||||
44 88 37 0 47
|
||||
6 80 49 98 48
|
||||
|
||||
93 2 70 26 4
|
||||
47 8 94 12 3
|
||||
10 7 24 40 23
|
||||
49 84 50 56 44
|
||||
41 53 96 1 85
|
||||
|
||||
76 78 70 24 75
|
||||
71 19 85 77 25
|
||||
21 44 58 45 64
|
||||
40 38 9 50 61
|
||||
79 42 86 37 6
|
||||
|
||||
34 39 94 84 0
|
||||
90 80 78 54 49
|
||||
13 81 87 60 56
|
||||
74 59 75 41 28
|
||||
29 67 66 44 20
|
||||
|
||||
50 66 43 39 16
|
||||
88 94 60 70 64
|
||||
63 80 56 69 36
|
||||
53 48 32 22 79
|
||||
59 77 20 30 67
|
||||
|
||||
70 56 80 12 11
|
||||
35 55 40 71 87
|
||||
84 27 96 46 85
|
||||
20 23 26 29 14
|
||||
58 37 21 75 68
|
||||
|
||||
78 23 13 37 94
|
||||
65 44 54 43 38
|
||||
29 60 83 1 57
|
||||
98 2 75 12 14
|
||||
92 25 48 9 52
|
||||
|
||||
64 37 93 48 34
|
||||
22 81 58 5 13
|
||||
63 80 2 67 53
|
||||
62 52 79 41 44
|
||||
83 75 96 91 88
|
||||
|
||||
1 54 88 45 90
|
||||
81 78 19 8 40
|
||||
17 74 69 87 33
|
||||
9 64 85 50 71
|
||||
92 38 65 82 41
|
||||
|
||||
2 62 96 60 81
|
||||
51 1 34 48 25
|
||||
78 13 74 65 42
|
||||
46 64 57 19 72
|
||||
85 88 53 68 76
|
||||
|
||||
57 95 40 92 27
|
||||
65 37 42 90 9
|
||||
17 72 78 43 45
|
||||
87 28 48 81 79
|
||||
7 4 24 67 70
|
65
2021/d05/ex1/ex1.py
Executable file
65
2021/d05/ex1/ex1.py
Executable file
|
@ -0,0 +1,65 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterable, List, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class Point:
|
||||
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 (p1.x, y)
|
||||
return
|
||||
|
||||
if p1.y == p2.y:
|
||||
for x in inclusive_range_any_order(p1.x, p2.x):
|
||||
yield (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()
|
500
2021/d05/ex1/input
Normal file
500
2021/d05/ex1/input
Normal 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
|
60
2021/d05/ex2/ex2.py
Executable file
60
2021/d05/ex2/ex2.py
Executable file
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterable, List, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class Point:
|
||||
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 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
500
2021/d05/ex2/input
Normal 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
|
Loading…
Reference in a new issue