import 2021

This commit is contained in:
Thibaud Gasser 2024-12-03 15:28:23 +01:00
parent 6b6136a8d0
commit a454b7e15a
17 changed files with 5534 additions and 0 deletions

44
2021/day1/day1.py Normal file
View File

@ -0,0 +1,44 @@
from itertools import islice
def window(seq, n=3):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def part1(infile):
with open(infile) as f:
lines = f.readlines()
previous = int(lines[0])
i = 0
for line in lines[1:]:
if int(line) > previous:
i += 1
previous = int(line)
print("Part 1 ", i)
def part2(infile):
with open(infile) as f:
lines = f.readlines()
previous = None
i = 0
for w in window(lines):
measure = sum(int(x) for x in w)
if previous is not None and measure > previous:
i += 1
previous = measure
print("Part 2 ", i)
if __name__ == "__main__":
part1("./input.txt")
part2("./input.txt")

2000
2021/day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

42
2021/day2/day2.py Normal file
View File

@ -0,0 +1,42 @@
def part1(inp):
horizontal_pos = 0
depth = 0
for line in inp:
command, amount = line.split(" ")
if command == "forward":
horizontal_pos += int(amount)
elif command == "up":
depth -= int(amount)
elif command == "down":
depth += int(amount)
#print(f"horizontal {horizontal_pos}, depth {depth}")
print("Part 1", horizontal_pos * depth)
def part2(inp):
horizontal_pos = 0
depth = 0
aim = 0
for line in inp:
command, amount = line.split(" ")
if command == "forward":
horizontal_pos += int(amount)
# It increases your depth by your aim multiplied by X.
depth += aim * int(amount)
elif command == "up":
aim -= int(amount)
elif command == "down":
aim += int(amount)
#print(f"horizontal {horizontal_pos}, depth {depth}")
print("Part 2", horizontal_pos * depth)
def main(input_file):
with open(input_file) as f:
entries = f.readlines()
part1(entries)
part2(entries)
if __name__ == "__main__":
main("input.txt")

1000
2021/day2/input.txt Normal file

File diff suppressed because it is too large Load Diff

67
2021/day3/day3.py Normal file
View File

@ -0,0 +1,67 @@
def calculate_gamma(inp):
gamma_rate = [0] * len(inp[0])
for line in inp:
for index, char in enumerate(line):
gamma_rate[index] += int(char)
gamma_rate = [0 if x < len(inp) // 2 else 1 for x in gamma_rate]
return gamma_rate
def part1(inp):
gamma = calculate_gamma(inp)
epsilon = [0 if x == 1 else 1 for x in gamma]
# power consumption = dec(gamma_rate) * dec(epsilon_rate)
power = int("".join(str(x) for x in gamma), 2) * int("".join(str(x) for x in epsilon), 2)
print("Part 1, power consumption : ", power)
def calculate_most_common(inp, pos):
sum = 0
for line in inp:
sum += int(line[pos])
return 0 if sum < len(inp) // 2 else 1
def filter_oxygen(inp, pos, most_common):
result = []
for line in inp:
if int(line[pos]) == most_common:
result.append(line)
return result
def oxygen_rating(inp):
result = inp[:]
for pos in range(len(inp[0])):
most_common = calculate_most_common(result, pos)
result = filter_oxygen(result, pos, most_common)
if len(result) == 1:
return result
def co2_rating(inp):
result = inp[:]
for pos in range(len(inp[0])):
least_common = 1 - calculate_most_common(result, pos)
result = filter_oxygen(result, pos, least_common)
if len(result) == 1:
return result
def part2(inp):
oxygen = oxygen_rating(inp)
co2 = co2_rating(inp)
res = int("".join(str(x) for x in oxygen), 2) * int("".join(str(x) for x in co2), 2)
print(f"Part 2 : {res}")
def main(input_file):
with open(input_file) as f:
entries = [x.rstrip() for x in f.readlines()]
part1(entries)
part2(entries)
if __name__ == "__main__":
main("input.txt")

1000
2021/day3/input.txt Normal file

File diff suppressed because it is too large Load Diff

97
2021/day4/day4.py Normal file
View File

@ -0,0 +1,97 @@
from dataclasses import dataclass
from collections import deque
@dataclass
class BingoItem:
value: int
marked: bool = False
def parse_grid(inp):
raw_grid = [inp.popleft() for _ in range(5)]
grid = [[BingoItem(int(y)) for y in x.rstrip().split(" ") if y != ''] for x in raw_grid]
return grid
def parse_grids(inp):
grids = []
while len(inp) >= 5:
grid = parse_grid(inp)
grids.append(grid)
try:
inp.popleft()
except IndexError:
break
return grids
def check_line_win(grid):
for line in grid:
if all(n.marked for n in line):
return True
return False
def check_column_win(grid):
for col_number in range(len(grid[0])):
column = [line[col_number] for line in grid]
if all(x.marked for x in column):
return True
return False
def calculate_score(grid, final_num):
unmarked = sum([sum([n.value for n in line if not n.marked]) for line in grid])
return final_num * unmarked
def print_green(text, end):
print(f"\033[1;32;40m{text}\033[0;37;40m", end=end)
def print_grid(grid):
for line in grid:
for col in line:
if col.marked:
print_green(f"{str(col.value).ljust(2)}", " ")
else:
print(f"{str(col.value).ljust(2)}", end=" ")
print()
print()
def play_bingo(numbers, grids):
winning_grids = []
for number in numbers:
print(number)
for grid in grids:
for line in grid:
for grid_number in line:
if grid_number.value == number:
grid_number.marked = True
for grid in grids:
win = [check_line_win(grid), check_column_win(grid)]
if any(win):
winning_grids.append((grid, number))
# the grid won, remove it from the game
grids.remove(grid)
first_winning_grid, number = winning_grids[0]
first_score = calculate_score(first_winning_grid, number)
print(f"Part 1, score = {first_score}")
last_winning_grid, number = winning_grids[-1]
last_score = calculate_score(last_winning_grid, number)
print(f"Part 2, score {last_score}")
def main(input_file):
with open(input_file) as f:
inp = deque(f.readlines())
numbers = [int(x) for x in inp.popleft().split(",")]
inp.popleft()
grids = parse_grids(inp)
play_bingo(numbers, grids)
if __name__ == "__main__":
main("input.txt")

601
2021/day4/input.txt Normal file
View File

@ -0,0 +1,601 @@
15,62,2,39,49,25,65,28,84,59,75,24,20,76,60,55,17,7,93,69,32,23,44,81,8,67,41,56,43,89,95,97,61,77,64,37,29,10,79,26,51,48,5,86,71,58,78,90,57,82,45,70,11,14,13,50,68,94,99,22,47,12,1,74,18,46,4,6,88,54,83,96,63,66,35,27,36,72,42,98,0,52,40,91,33,21,34,85,3,38,31,92,9,87,19,73,30,16,53,80
92 3 88 13 50
90 70 24 28 52
15 98 10 26 5
84 34 37 73 87
25 36 74 33 63
66 64 50 75 53
73 24 80 84 5
72 20 68 1 99
83 57 44 60 52
32 15 59 48 98
33 51 85 92 89
38 22 93 62 75
24 76 50 90 25
69 6 52 77 3
47 9 88 53 63
78 75 29 32 73
22 85 42 1 23
80 98 81 58 9
61 76 69 83 53
71 7 15 11 95
33 57 76 73 26
6 71 35 39 85
54 77 36 14 87
66 79 8 64 32
2 84 98 34 13
43 51 16 95 59
22 53 6 49 94
32 72 46 23 37
40 85 39 45 74
87 62 69 98 0
84 5 73 32 23
40 64 98 27 8
80 71 1 31 69
46 42 7 4 70
88 90 48 33 29
3 91 8 98 50
54 70 29 94 25
17 77 33 46 12
28 36 39 40 5
22 38 51 69 45
90 35 94 31 44
11 13 74 38 49
60 96 91 63 16
23 26 84 41 7
73 65 32 18 81
62 42 35 21 87
57 27 26 71 94
73 92 77 53 86
1 60 38 75 43
10 70 55 84 5
3 58 57 66 51
67 94 37 86 25
33 11 4 36 83
64 2 0 13 59
77 19 80 93 97
99 13 24 49 90
96 15 10 67 2
9 78 5 42 80
28 75 51 58 82
31 83 20 60 48
91 38 65 34 58
71 28 66 64 72
63 10 83 37 56
84 39 19 51 74
23 90 81 85 13
12 42 10 11 29
99 60 24 94 25
9 40 76 33 97
32 75 16 37 27
15 69 54 52 22
31 40 33 45 89
61 82 9 32 75
60 88 91 27 62
79 94 36 83 25
56 39 8 13 55
39 95 92 2 56
88 70 63 62 13
49 43 46 0 47
83 42 44 7 26
60 27 69 73 29
54 67 26 19 45
8 50 86 51 92
60 98 31 95 53
24 71 55 22 63
4 38 21 35 32
4 34 26 32 58
16 67 76 78 46
73 95 68 56 60
35 40 42 6 87
7 97 54 92 24
98 80 66 95 14
73 19 94 63 60
52 18 28 72 26
33 93 56 4 21
59 68 74 48 3
7 27 84 80 79
1 21 11 37 47
88 38 30 8 72
4 52 13 19 26
57 6 58 0 98
62 50 0 37 77
32 31 2 53 4
74 56 41 23 59
60 89 94 54 39
76 98 20 61 82
35 90 5 80 18
45 20 60 8 77
26 17 61 55 29
24 76 3 41 64
4 74 85 10 82
62 23 27 89 61
45 65 30 14 66
52 72 48 99 0
5 40 42 81 37
93 4 67 2 9
27 87 68 50 41
18 60 12 45 48
93 38 8 6 13
99 37 59 94 64
40 55 63 67 31
70 4 34 49 71
36 81 52 62 55
18 64 63 85 5
72 99 77 76 54
22 23 0 1 37
34 88 69 20 30
73 11 93 68 56
78 35 80 22 24
15 95 32 51 25
67 91 52 5 14
8 54 26 34 71
16 47 39 96 58
4 95 38 6 45
94 63 18 99 72
19 91 80 73 30
77 9 78 76 60
8 31 73 74 17
22 25 7 64 47
75 32 89 87 40
13 44 10 95 49
78 3 90 99 6
22 52 25 53 72
55 98 77 56 32
85 86 0 7 12
74 84 33 45 1
57 53 26 54 69
56 8 58 91 40
65 97 44 51 2
85 60 72 22 89
66 16 67 90 93
9 93 65 94 29
2 80 7 16 79
11 5 21 73 50
20 70 37 48 85
99 3 55 58 8
26 37 60 63 47
21 39 69 68 22
83 94 55 91 80
35 89 6 45 17
23 85 84 73 7
74 36 81 41 8
14 22 30 86 90
84 97 11 67 77
42 47 55 76 64
95 92 59 93 53
64 16 19 68 50
90 12 47 40 62
86 1 48 2 58
96 79 92 46 91
14 85 59 45 30
3 1 55 13 5
59 85 50 42 20
67 99 17 29 39
30 35 23 49 25
89 53 21 9 6
90 91 47 99 37
82 24 56 27 2
95 57 33 4 97
51 26 29 67 98
21 62 42 43 9
92 16 89 24 96
31 18 2 64 20
6 34 99 50 85
13 32 19 43 37
48 47 23 78 77
95 16 87 61 6
46 15 24 72 60
43 56 80 35 53
97 25 98 42 14
51 11 10 3 45
96 42 4 45 40
65 8 17 58 23
53 38 14 12 84
68 92 11 6 51
87 22 5 99 0
45 51 26 18 91
7 31 95 37 74
66 41 48 20 87
99 96 64 53 0
3 28 15 46 79
66 34 23 78 12
65 72 33 14 5
4 59 3 62 64
7 60 31 52 87
80 39 27 58 74
91 94 64 46 28
99 29 79 58 0
18 19 24 59 16
3 73 52 9 86
37 61 1 93 68
37 98 80 41 53
85 18 55 31 17
39 61 63 97 52
47 22 99 50 88
48 14 9 93 96
11 66 89 91 34
98 25 53 7 65
42 32 9 14 77
85 87 26 12 64
45 99 29 88 4
63 3 16 13 33
28 32 37 90 11
94 44 18 38 68
30 87 95 52 58
79 43 53 70 19
94 67 56 43 47
77 37 93 90 92
66 48 98 20 61
51 2 85 57 11
22 84 79 17 72
86 59 15 85 5
93 41 23 53 62
46 48 70 57 49
17 45 32 79 12
64 73 26 6 9
12 88 27 43 21
66 42 84 82 62
94 46 96 63 86
69 79 40 39 92
22 87 71 44 53
89 26 45 78 25
21 40 70 66 33
97 80 94 18 1
12 55 20 24 39
7 32 31 37 72
15 56 39 57 40
67 59 26 30 90
84 2 41 25 7
96 23 79 99 85
13 10 86 51 53
73 8 79 19 48
29 36 89 62 22
13 96 59 91 10
90 9 1 78 65
83 50 24 88 60
20 61 63 82 53
86 11 55 10 85
5 37 65 21 54
89 75 59 73 48
41 50 29 71 93
81 13 46 17 47
95 19 33 91 55
5 73 54 50 98
63 77 30 40 58
9 57 94 92 20
54 99 94 23 81
32 86 50 28 8
69 18 11 39 67
10 79 91 15 43
13 98 55 16 22
83 99 54 12 80
94 61 49 33 62
16 23 68 87 10
1 76 25 89 71
8 45 74 28 27
66 28 72 76 33
9 99 27 96 60
84 67 35 50 79
55 44 18 98 13
94 70 42 21 65
96 97 79 75 46
11 65 41 72 92
87 59 26 70 10
37 8 68 73 63
55 95 84 49 50
51 27 63 31 24
82 11 87 6 2
75 57 85 1 46
91 71 72 13 56
10 64 65 49 69
36 26 67 61 84
99 10 2 24 47
35 28 65 57 91
30 27 1 78 14
96 50 70 38 37
62 33 41 98 35
80 92 4 48 70
2 11 23 15 52
83 39 79 81 1
54 93 27 18 24
12 75 20 81 23
77 99 47 24 82
92 29 85 30 21
49 45 98 4 91
9 53 28 1 54
72 46 53 3 19
83 49 39 12 22
47 62 58 14 79
82 69 84 75 1
67 7 21 45 65
43 21 47 84 94
93 53 37 44 15
48 10 59 35 41
91 78 98 34 66
85 75 95 92 39
94 6 17 16 12
39 41 11 65 78
97 85 49 64 72
59 84 83 42 28
32 96 46 89 44
54 29 71 64 78
32 13 52 58 28
84 85 95 26 86
23 41 70 53 87
27 15 57 16 2
92 99 45 81 32
86 25 56 76 52
95 3 6 88 1
71 70 24 19 62
59 16 11 2 34
43 56 11 7 49
1 50 84 89 0
97 18 60 95 25
42 33 75 31 29
35 62 78 99 76
98 84 53 3 22
54 87 41 76 83
39 27 36 79 78
55 1 89 48 81
49 26 77 96 67
99 79 98 84 47
72 14 49 3 10
30 9 12 61 1
21 50 75 82 8
86 44 13 83 88
82 94 33 70 17
97 22 45 53 55
19 71 35 54 52
41 42 63 65 3
88 10 67 81 69
50 90 18 2 22
51 85 67 40 61
3 71 99 93 46
65 29 45 60 75
5 74 6 66 98
68 80 59 29 5
6 16 45 44 92
74 13 64 30 25
69 94 54 97 3
42 47 26 19 17
38 79 36 61 90
19 59 18 3 71
70 99 16 93 22
68 34 88 76 17
75 54 49 85 86
8 96 80 15 28
23 98 58 84 69
21 3 60 38 97
43 56 34 25 64
24 1 39 44 78
21 60 14 55 29
34 61 63 18 5
19 28 54 72 7
32 46 92 80 73
40 68 75 67 98
57 21 88 90 33
63 5 25 24 49
29 7 23 19 13
85 93 75 41 68
98 69 12 76 31
74 88 75 81 51
46 77 66 60 20
47 0 23 64 43
68 41 38 65 48
53 26 54 17 83
39 21 78 15 99
25 97 24 70 56
57 66 31 75 71
47 91 30 4 65
94 11 77 76 44
35 42 72 38 51
96 32 3 64 48
81 50 37 55 79
90 67 54 6 12
31 45 71 25 76
60 58 90 3 74
48 16 49 30 46
68 51 0 80 96
26 71 36 27 28
57 94 79 42 50
96 27 94 74 89
57 19 51 5 78
20 59 14 73 69
8 41 79 76 32
24 98 63 46 13
20 53 42 70 86
12 49 96 0 77
31 26 38 22 87
51 78 60 36 13
57 8 73 94 7
75 39 93 85 99
78 50 3 96 68
62 10 28 80 41
89 40 46 69 19
37 13 16 2 67
85 24 99 70 20
31 60 41 63 81
34 87 93 39 37
55 43 44 25 78
97 21 3 28 40
44 14 92 89 62
90 76 84 52 33
78 54 26 32 9
85 99 25 10 55
28 23 22 97 94
13 38 37 98 15
78 62 9 50 2
77 68 65 18 74
90 21 95 53 33
60 25 17 64 1
45 87 64 33 66
31 85 19 90 48
74 3 70 77 9
44 46 61 91 32
0 15 94 65 22
12 1 66 47 3
63 7 2 42 21
6 75 44 26 82
52 45 48 89 68
96 92 25 15 76
64 16 49 71 28
7 45 63 74 21
87 25 46 23 9
0 31 92 24 77
65 78 22 60 75
4 54 58 83 60
25 12 82 0 73
32 62 2 31 49
64 18 35 19 10
61 46 43 34 38
84 48 30 77 79
15 42 4 25 72
28 78 22 7 70
46 6 31 24 41
98 93 34 37 71
72 9 71 1 54
97 98 91 90 92
23 88 13 87 68
45 36 86 41 56
69 16 24 20 93
35 34 60 67 52
12 73 99 89 61
33 94 27 16 15
4 64 47 22 74
24 53 71 66 76
32 62 51 58 1
11 76 75 33 60
55 54 39 52 48
6 87 3 8 99
40 65 24 66 70
61 23 22 12 74
73 25 85 11 0
20 38 26 88 33
63 39 50 49 83
71 18 56 37 7
46 12 90 52 48
73 24 78 34 94
15 19 47 72 89
60 35 74 67 30
13 18 17 93 0
37 87 0 94 27
18 56 54 4 33
84 20 12 86 6
5 40 52 97 74
63 59 69 19 77
15 53 20 35 14
24 25 63 85 79
65 96 2 60 50
72 10 77 12 92
32 94 95 16 71
78 52 55 20 40
33 66 81 48 18
32 69 13 5 84
23 67 68 61 34
11 63 4 93 65
51 89 37 46 29
69 56 71 9 91
28 54 7 16 31
67 5 97 42 43
98 32 65 34 30

41
2021/day5/day5.py Normal file
View File

@ -0,0 +1,41 @@
from collections import defaultdict
def main(infile):
points = defaultdict(int)
with open(infile) as f:
for line in f:
start, end = line.split(" -> ")
start_x, start_y = [int(x) for x in start.split(",")]
end_x, end_y = [int(x) for x in end.split(",")]
# column |
if start_x == end_x:
step = 1 if start_y < end_y else -1
for y in range(start_y, end_y + step, step):
points[(start_x, y)] = points[(start_x, y)] + 1
# line -
elif start_y == end_y:
step = 1 if start_x < end_x else -1
for x in range(start_x, end_x + step, step):
points[(x, start_y)] = points[(x, start_y)] + 1
# diagonal \
elif ((start_x < end_x and start_y > end_y)
or (start_x > end_x and start_y < end_y)):
step = 1 if start_y > end_y else -1
for dx in range(0, end_x - start_x + step, step):
points[(start_x + dx, start_y - dx)] = points[(start_x + dx, start_y + dx)] + 1
# diagonal /
elif ((start_x < end_x and start_y < end_y)
or (start_x > end_x and start_y > end_y)):
step = 1 if start_y < end_y else -1
for dx in range(0, end_x - start_x + step, step):
points[(start_x + dx, start_y + dx)] = points[(start_x + dx, start_y + dx)] + 1
res = len([x for x in points.values() if x >= 2])
print(res)
if __name__ == "__main__":
main("input.txt")

500
2021/day5/input.txt Normal file
View File

@ -0,0 +1,500 @@
299,462 -> 299,747
855,314 -> 855,140
981,328 -> 798,328
610,444 -> 680,374
797,242 -> 606,242
217,42 -> 147,42
735,378 -> 735,188
247,192 -> 912,192
377,341 -> 768,341
472,701 -> 66,701
48,970 -> 885,133
893,35 -> 664,35
617,237 -> 951,237
540,643 -> 190,293
575,815 -> 302,815
146,380 -> 146,562
568,481 -> 568,161
38,101 -> 921,984
613,12 -> 185,12
967,30 -> 17,980
823,620 -> 584,859
672,822 -> 413,822
259,626 -> 385,752
752,415 -> 857,310
758,659 -> 758,76
909,893 -> 35,19
964,913 -> 105,54
697,196 -> 697,913
389,821 -> 163,821
783,65 -> 281,65
775,732 -> 558,732
818,817 -> 42,817
499,537 -> 896,140
81,957 -> 81,844
851,256 -> 559,548
268,970 -> 268,170
106,216 -> 68,178
107,371 -> 850,371
160,107 -> 748,107
300,619 -> 524,395
940,196 -> 780,356
752,498 -> 752,94
807,619 -> 728,619
831,89 -> 313,89
56,389 -> 191,524
206,75 -> 206,816
486,924 -> 486,389
280,708 -> 542,446
562,917 -> 190,545
40,231 -> 40,404
804,327 -> 726,249
538,670 -> 170,302
473,229 -> 912,668
645,195 -> 645,916
502,13 -> 502,266
639,955 -> 639,434
87,56 -> 943,912
143,798 -> 699,798
469,261 -> 79,651
715,98 -> 104,709
914,339 -> 463,790
456,263 -> 456,101
656,105 -> 109,105
28,944 -> 123,944
981,652 -> 270,652
953,681 -> 605,333
474,858 -> 310,858
542,736 -> 807,736
234,412 -> 620,26
615,786 -> 36,207
169,56 -> 169,132
133,930 -> 989,74
342,34 -> 516,34
210,97 -> 947,834
43,857 -> 824,76
673,840 -> 673,156
718,123 -> 896,123
673,311 -> 673,564
639,352 -> 72,919
552,571 -> 661,462
819,335 -> 953,335
756,84 -> 823,84
250,969 -> 287,969
551,260 -> 378,433
417,412 -> 465,412
621,260 -> 249,632
226,633 -> 394,465
475,179 -> 602,306
272,571 -> 272,839
820,666 -> 820,829
988,608 -> 974,608
124,318 -> 124,589
303,516 -> 839,516
983,477 -> 983,786
299,870 -> 927,242
284,875 -> 213,875
427,493 -> 545,493
74,755 -> 17,698
294,326 -> 294,23
331,193 -> 391,193
381,671 -> 408,644
805,537 -> 805,155
721,956 -> 10,956
918,36 -> 918,915
981,891 -> 445,355
591,288 -> 933,288
199,256 -> 250,307
318,810 -> 789,339
250,245 -> 522,517
592,248 -> 958,614
722,215 -> 235,215
654,496 -> 654,905
76,860 -> 678,258
29,20 -> 954,945
379,851 -> 567,851
722,161 -> 13,870
965,302 -> 390,877
114,892 -> 114,348
265,681 -> 26,920
94,463 -> 94,160
340,150 -> 340,759
727,612 -> 175,60
457,951 -> 154,648
602,200 -> 602,841
487,194 -> 27,654
356,699 -> 887,168
915,237 -> 262,890
81,225 -> 815,959
227,877 -> 694,877
441,674 -> 441,968
865,201 -> 865,528
969,214 -> 511,214
802,748 -> 802,86
662,313 -> 636,313
308,447 -> 308,545
245,236 -> 532,236
621,195 -> 621,140
889,159 -> 197,851
68,683 -> 179,572
859,261 -> 56,261
982,539 -> 982,619
144,362 -> 851,362
304,905 -> 304,553
551,905 -> 637,905
432,316 -> 142,606
104,588 -> 104,862
316,392 -> 680,392
372,413 -> 866,413
874,53 -> 697,53
499,668 -> 499,329
32,207 -> 802,977
403,108 -> 100,411
578,442 -> 578,489
134,161 -> 848,875
851,935 -> 95,179
485,190 -> 485,57
411,47 -> 680,47
378,878 -> 378,127
908,717 -> 516,717
432,863 -> 328,863
212,278 -> 212,326
552,426 -> 933,807
419,329 -> 492,402
975,750 -> 424,750
40,54 -> 915,929
570,349 -> 576,349
32,784 -> 32,473
854,407 -> 343,407
18,932 -> 425,932
223,571 -> 468,816
939,330 -> 939,870
126,637 -> 105,616
84,310 -> 84,788
491,890 -> 229,890
737,831 -> 737,726
137,471 -> 137,957
642,429 -> 253,429
319,103 -> 903,103
38,872 -> 38,110
809,183 -> 809,653
877,87 -> 56,908
455,136 -> 693,374
218,647 -> 727,647
626,544 -> 797,544
147,46 -> 122,46
316,430 -> 495,430
608,469 -> 331,192
353,769 -> 714,769
649,90 -> 410,90
105,311 -> 105,674
594,600 -> 484,600
822,933 -> 279,933
478,267 -> 478,341
114,912 -> 114,387
843,480 -> 754,480
747,701 -> 747,143
646,88 -> 646,375
195,129 -> 757,691
470,895 -> 470,673
450,595 -> 334,711
165,872 -> 165,155
744,947 -> 987,947
153,15 -> 357,15
272,222 -> 272,201
535,551 -> 120,966
102,748 -> 102,281
348,482 -> 129,482
499,679 -> 499,821
399,875 -> 399,285
695,585 -> 733,547
40,509 -> 95,564
199,857 -> 228,886
491,885 -> 978,885
926,887 -> 104,65
492,128 -> 957,593
302,904 -> 302,906
706,782 -> 706,217
721,506 -> 721,675
847,725 -> 583,989
225,734 -> 942,17
141,161 -> 161,161
858,736 -> 858,433
183,724 -> 13,554
299,647 -> 299,420
623,39 -> 358,304
657,373 -> 657,976
452,714 -> 452,735
857,537 -> 392,72
758,979 -> 758,457
141,609 -> 141,100
266,76 -> 974,784
527,66 -> 236,357
971,176 -> 865,282
961,935 -> 74,48
328,434 -> 328,663
384,670 -> 12,670
534,508 -> 334,508
336,603 -> 202,469
690,140 -> 807,140
491,511 -> 491,166
265,493 -> 236,493
552,113 -> 552,329
370,542 -> 370,688
919,556 -> 488,125
142,949 -> 107,949
917,824 -> 360,267
866,109 -> 624,109
714,657 -> 714,155
727,567 -> 727,570
235,920 -> 235,683
329,261 -> 55,261
672,718 -> 113,159
469,380 -> 66,783
884,289 -> 884,15
412,197 -> 496,197
971,875 -> 20,875
831,245 -> 831,946
22,985 -> 391,985
984,136 -> 187,933
845,334 -> 660,519
367,299 -> 367,912
25,985 -> 946,64
487,416 -> 487,453
89,223 -> 723,857
890,953 -> 19,82
199,256 -> 199,521
785,981 -> 710,906
673,160 -> 673,682
65,730 -> 421,730
957,89 -> 832,89
647,361 -> 33,361
243,347 -> 784,888
50,760 -> 50,372
564,278 -> 564,846
344,652 -> 832,652
219,586 -> 219,502
976,500 -> 976,650
515,265 -> 744,36
930,730 -> 930,332
479,48 -> 592,48
979,326 -> 374,326
790,520 -> 375,520
129,919 -> 511,537
203,558 -> 212,558
688,842 -> 502,842
979,976 -> 90,87
78,929 -> 78,478
578,203 -> 802,427
788,360 -> 260,888
847,342 -> 212,977
256,578 -> 821,13
493,561 -> 712,342
90,116 -> 181,116
317,736 -> 963,90
453,548 -> 37,548
15,472 -> 514,971
972,579 -> 956,579
456,66 -> 349,66
102,771 -> 769,771
320,741 -> 327,748
981,150 -> 592,150
739,978 -> 739,804
861,10 -> 142,729
457,596 -> 580,596
358,287 -> 237,408
705,719 -> 59,73
27,770 -> 27,133
846,690 -> 846,464
138,351 -> 174,315
380,942 -> 380,985
490,421 -> 803,734
559,449 -> 761,449
709,218 -> 718,218
271,877 -> 271,814
89,845 -> 918,16
613,436 -> 976,73
27,88 -> 867,88
541,965 -> 739,767
187,746 -> 916,17
294,13 -> 333,13
600,647 -> 925,647
915,942 -> 41,68
38,625 -> 176,763
468,905 -> 468,727
337,89 -> 337,581
48,969 -> 732,285
555,301 -> 555,610
155,525 -> 985,525
235,167 -> 700,167
728,134 -> 728,289
696,595 -> 892,595
983,696 -> 401,114
581,40 -> 515,40
171,837 -> 975,33
588,683 -> 734,683
880,132 -> 231,132
847,145 -> 332,660
657,179 -> 657,18
49,957 -> 496,510
791,497 -> 552,736
988,989 -> 10,11
937,659 -> 937,461
403,458 -> 403,637
237,765 -> 237,813
35,504 -> 35,663
556,897 -> 802,897
382,491 -> 786,895
103,566 -> 528,566
598,570 -> 623,570
345,343 -> 345,985
537,59 -> 537,386
207,811 -> 974,44
463,623 -> 463,21
966,915 -> 966,965
569,281 -> 569,183
470,648 -> 470,666
441,420 -> 817,796
451,723 -> 908,266
300,297 -> 840,297
902,201 -> 902,912
598,930 -> 654,930
874,433 -> 874,176
551,967 -> 795,967
892,23 -> 137,778
306,463 -> 679,90
16,78 -> 16,980
782,749 -> 782,117
235,240 -> 244,231
461,183 -> 981,183
608,170 -> 608,640
75,711 -> 645,141
49,238 -> 488,238
14,963 -> 947,30
120,56 -> 120,73
630,978 -> 316,664
219,389 -> 803,973
106,918 -> 978,46
941,439 -> 788,592
313,943 -> 325,943
325,683 -> 325,67
774,816 -> 774,908
608,833 -> 608,679
447,289 -> 447,135
405,650 -> 405,406
622,467 -> 636,467
855,606 -> 855,663
918,640 -> 918,831
640,869 -> 640,904
481,405 -> 481,791
185,582 -> 21,746
556,772 -> 114,330
490,144 -> 490,591
60,74 -> 974,988
967,978 -> 10,21
159,669 -> 486,342
302,636 -> 302,771
841,427 -> 793,427
670,743 -> 234,743
47,676 -> 233,490
877,768 -> 123,14
139,462 -> 139,541
204,59 -> 204,300
720,702 -> 720,525
171,341 -> 787,341
699,899 -> 293,899
431,513 -> 431,849
904,278 -> 124,278
919,67 -> 815,67
401,519 -> 688,232
675,279 -> 675,137
376,786 -> 362,786
57,817 -> 801,73
809,468 -> 410,867
669,171 -> 669,65
520,36 -> 218,36
702,159 -> 709,159
399,646 -> 399,828
853,759 -> 853,91
58,143 -> 867,952
896,939 -> 42,85
677,120 -> 646,120
947,714 -> 737,714
515,107 -> 752,344
793,142 -> 793,789
86,896 -> 967,15
663,493 -> 833,493
986,766 -> 293,766
71,874 -> 71,417
471,426 -> 148,426
444,982 -> 142,982
124,582 -> 846,582
336,436 -> 257,436
877,750 -> 177,50
69,73 -> 911,915
315,363 -> 315,45
620,272 -> 556,272
616,186 -> 331,186
766,756 -> 59,49
555,271 -> 555,183
437,246 -> 454,246
169,57 -> 169,688
448,605 -> 420,605
194,149 -> 960,915
597,308 -> 597,512
328,337 -> 328,349
506,331 -> 506,144
608,633 -> 838,863
37,99 -> 767,829
128,487 -> 128,246
473,303 -> 473,529
754,890 -> 754,269
854,958 -> 957,958
704,360 -> 526,360
613,752 -> 260,752
179,302 -> 805,302
916,176 -> 519,176
318,622 -> 161,622
783,785 -> 322,785
148,289 -> 802,943
944,280 -> 671,280
758,402 -> 442,86
988,959 -> 56,27
716,642 -> 429,642
899,48 -> 899,111
981,325 -> 168,325
603,77 -> 474,77
103,387 -> 112,387
100,40 -> 160,40
969,317 -> 109,317
938,424 -> 938,179
834,980 -> 527,980
875,83 -> 22,83
89,572 -> 582,79
642,862 -> 642,247
499,26 -> 499,242
873,173 -> 206,840
314,112 -> 314,388
778,25 -> 778,944
902,457 -> 964,519
822,891 -> 623,891
538,76 -> 618,156
418,179 -> 204,179
138,131 -> 300,131
51,386 -> 51,764
756,728 -> 330,302
801,374 -> 801,506
239,416 -> 781,958
311,651 -> 848,651
67,586 -> 67,250
794,244 -> 794,515
228,810 -> 368,670
399,561 -> 682,561
919,61 -> 868,61
204,253 -> 224,253
74,657 -> 74,235
208,422 -> 722,422
246,353 -> 441,548
362,175 -> 362,688
403,681 -> 403,821
146,183 -> 23,183

26
2021/day6/day6.py Normal file
View File

@ -0,0 +1,26 @@
from collections import defaultdict, Counter
def calculate_fishes(inp, days):
fishes = Counter(inp)
for day in range(days):
fishes_new = defaultdict(int)
for fish, cnt in fishes.items():
if fish == 0:
fishes_new[8] += cnt
fishes_new[6] += cnt
else:
fishes_new[fish - 1] += cnt
fishes = fishes_new
return sum(fishes.values())
def main(infile):
with open(infile) as f:
inp = [int(x) for x in f.readline().split(",")]
res = calculate_fishes(inp, 80)
print(f"Part 1, {res}")
res = calculate_fishes(inp, 256)
print(f"Part 2, {res}")
if __name__ == "__main__":
main("input.txt")

34
2021/day6/day6_2.py Normal file
View File

@ -0,0 +1,34 @@
total = 0
numbers = [0, 0, 0, 0, 0, 0, 0, 0, 0]
with open("input.txt") as f:
data = [int(x) for x in f.readline().split(',')]
for i in data:
if i == 0:
numbers[0] += 1
if i == 1:
numbers[1] += 1
if i == 2:
numbers[2] += 1
if i == 3:
numbers[3] += 1
if i == 4:
numbers[4] += 1
if i == 5:
numbers[5] += 1
if i == 6:
numbers[6] += 1
if i == 7:
numbers[7] += 1
if i == 8:
numbers[8] += 1
def rotate(l):
return l[1:] + l[:1]
for j in range(256):
numbers = rotate(numbers)
numbers[6] += numbers[8]
print(f'DAY {j+1} AMOUNT OF FISH: {sum(numbers)}')

1
2021/day6/input.txt Normal file
View File

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

33
2021/day7/day7.py Normal file
View File

@ -0,0 +1,33 @@
from collections import OrderedDict
def part1(crabs):
moves = OrderedDict()
for pos in range(min(crabs), max(crabs) + 1):
# calculate total fuel required to move to pos:
for crab in crabs:
fuel_cost = abs(pos - crab)
moves[pos] = moves.get(pos, 0) + fuel_cost
min_move = min(moves, key=moves.get)
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
def part2(crabs):
moves = OrderedDict()
for pos in range(min(crabs), max(crabs) + 1):
# calculate total fuel required to move to pos:
for crab in crabs:
dx = abs(pos - crab)
# S = (n+1)(u0 + un)/2
fuel_cost = dx * (dx+1)//2
moves[pos] = moves.get(pos, 0) + fuel_cost
min_move = min(moves, key=moves.get)
print(f"Part 1, min move {min_move}, cost {moves[min_move]}")
if __name__ == "__main__":
with open("input.txt") as infile:
crabs = [int(x) for x in infile.readline().split(",")]
part1(crabs)
part2(crabs)

1
2021/day7/input.txt Normal file
View File

@ -0,0 +1 @@
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,76,50,481,307,723,1100,235,147,851,504,1068,780,490,126,771,831,358,711,68,281,645,644,862,705,372,217,679,1097,1005,728,1739,571,40,1293,782,378,802,245,1370,1512,9,372,448,64,36,1212,141,585,1468,278,286,476,239,285,664,277,43,665,1037,654,205,1238,36,41,276,945,624,47,91,1569,284,107,845,60,961,30,21,269,1091,304,364,339,169,430,176,156,1483,1510,111,146,447,590,1227,611,483,428,396,839,307,901,380,128,80,535,461,482,379,59,281,977,44,966,545,37,163,845,845,151,936,269,938,612,1284,863,516,927,511,825,125,599,101,394,1062,140,483,218,83,443,404,492,78,507,860,1741,43,128,60,566,181,5,554,937,163,280,655,184,480,408,127,935,613,766,46,312,770,697,275,13,84,600,168,83,281,599,607,1441,197,344,0,302,414,147,370,748,421,844,871,319,666,117,640,247,167,529,324,252,235,303,443,1305,796,109,20,364,532,1388,708,769,916,340,405,90,47,504,516,97,535,28,69,960,590,254,106,188,190,1388,698,246,264,98,229,1648,292,710,14,421,31,147,1493,552,1371,454,4,146,674,452,1267,1027,170,141,936,1341,884,558,276,631,68,39,2,464,1,839,318,881,413,2,452,352,34,89,323,884,1439,243,79,56,128,1273,1134,606,11,682,747,415,599,782,179,269,320,682,177,336,466,10,370,159,1636,367,888,573,171,682,60,9,59,332,10,1496,637,1029,413,186,1183,77,309,461,883,1079,699,233,69,259,108,1160,435,480,495,13,858,718,126,115,728,1008,133,442,7,598,1475,1156,226,162,415,3,151,72,527,792,494,763,144,64,490,273,1245,300,465,744,36,1465,251,8,494,1126,362,180,1263,175,141,1041,103,163,205,568,93,699,103,437,204,931,563,550,88,415,146,265,31,221,1123,835,375,1101,578,388,92,1417,845,308,343,499,158,293,242,4,509,574,254,1556,69,668,691,0,558,16,687,1210,166,748,400,863,66,600,771,1073,561,738,398,384,232,350,393,1113,1222,153,462,907,797,712,18,1463,1185,1055,994,57,130,265,131,52,463,902,453,38,132,783,1560,232,169,1162,173,311,5,1477,397,336,480,540,491,67,340,27,291,341,35,275,78,1525,387,218,63,79,533,4,569,1643,595,1508,851,39,1200,912,10,53,42,60,154,1174,155,275,137,677,367,1373,4,708,441,756,647,1054,872,1039,109,530,1179,939,429,567,866,1411,436,23,212,184,66,79,831,538,90,827,678,549,313,434,60,907,284,171,570,1091,603,447,122,1092,29,789,563,462,15,310,340,16,365,393,614,48,368,42,457,736,737,1008,513,61,764,366,400,525,1683,1177,909,908,112,734,16,79,917,541,127,107,79,1208,32,258,596,166,376,1313,735,1457,864,563,55,439,54,694,81,93,48,470,1028,689,1177,1331,155,412,847,250,405,387,8,456,18,619,533,729,1475,1182,935,210,55,355,958,15,32,598,85,175,471,1087,280,652,53,13,225,12,488,717,353,2,134,351,698,276,456,209,535,604,19,12,785,3,63,879,437,216,1,1275,811,786,417,33,51,733,1074,143,309,65,555,557,78,611,909,260,973,701,998,490,213,9,233,760,933,916,437,1369,1952,372,324,859,670,73,296,1391,127,407,230,52,16,547,803,883,258,308,710,343,1290,184,8,41,9,68,104,175,1034,1544,219,752,327,690,134,601,1574,214,385,1233,231,267,944,1533,349,431,97,632,278,1505,162,888,62,90,489,351,990,846,14,159,134,14,314,148,214,1153,513,114,6,49,10,14,957,219,16,204,954,863,50,482,90,696,99,253,252,433,57,385,54,343,106,154,78,1595,590,380,102,825,1933,191,1328,374,263,355,137,494,60,781,1113,391,274,325,326,14,965,269,15,32,742,81,393,730,892,982,103,890,499,58,816,292,29,480,173,831,132,1033,1511,1137,1511,22,1105,146,344,308,915,540,1371,1238,414,352,304,841,749,6,491,30,1322,415,293,1207,31,90,636,303,1551,354,23,275,18,32,623,1483,49,12,311,407,1551,296,252,647,778,1499,98,1220,264,1020,1440,377,1125,8,72,270,162,348,3,1023,965,719,62,1467,1176,663,439,557,654,85,1493,70,349,10,727,15,1173,387,529,608,1398,905,619,173,849,1493,49,88,4,708,1084,370,1007,285,4,530,770,561,26,669,1100,30,876,649,178,32,354,621,911,334,514,9,449,1019,107

46
2021/day8/day8.py Normal file
View File

@ -0,0 +1,46 @@
def part1(inp):
total = 0
for display in inp:
_, output_values = display
for value in output_values:
if len(value) in [2, 4, 3, 7]:
total += 1
print(f"Part 1 : {total}")
def part2(inp):
for display in inp:
patterns, values = display
patterns = sorted(patterns, key=lambda x: len(x))
print(patterns)
# easy
d1 = [x for x in patterns if len(x) == 2][0]
print("1", d1)
d4 = [x for x in patterns if len(x) == 4][0]
print("4", d4)
d7 = [x for x in patterns if len(x) == 3][0]
print("7", d7)
d8 = [x for x in patterns if len(x) == 7][0]
print("8", d8)
# 3 is the only digit that has all common segments with 1
breakpoint()
d3 = [x for x in patterns if set(d1).issubset(set(x)) and len(x) == 5][0]
print("3", d3)
break
def main(infile):
inp = []
with open(infile) as f:
for display in f:
display = display.rstrip().split(" | ")
signal_patterns = display[0].split(" ")
output_values = display[1].split(" ")
inp.append([signal_patterns, output_values])
part1(inp)
part2(inp)
if __name__ == "__main__":
main("input.txt")

1
2021/day8/input.txt Normal file
View File

@ -0,0 +1 @@
acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf