Compare commits
51 Commits
75bae4dd22
...
main
Author | SHA1 | Date | |
---|---|---|---|
0885af7f06 | |||
5289db169b | |||
3aec98af5b | |||
dd7eae1ba6 | |||
345a5bfccb | |||
2d7ab5e287 | |||
3c535c8036 | |||
200a9c18a9 | |||
b638d89a0a | |||
7be7d0654b | |||
90feb14cd5 | |||
7accf6c2e8 | |||
91f1535c73 | |||
cc708b5b97 | |||
5377ef9abe | |||
df47044ce4 | |||
5ab804addc | |||
9459f5cc97 | |||
dd0989dafd | |||
64624865d6 | |||
eb95c2147e | |||
222eff07d2 | |||
ca39de9040 | |||
0d50624697 | |||
a8ff3fc882 | |||
dfa1a49c7b | |||
5adb259376 | |||
e13186dce0 | |||
f8a88e4220 | |||
c9a3d978d8 | |||
5ad716e9a0 | |||
b6f3782b51 | |||
677f8350a0 | |||
7a06186409 | |||
8ced78338c | |||
23a9d663e4 | |||
d7ef53e747 | |||
34b9e54c0f | |||
bf896841d2 | |||
4d2161f5b1 | |||
618324bb17 | |||
9c79eaa6cf | |||
26bf8f179a | |||
1d1b9630c8 | |||
299a70970e | |||
5db1f6a717 | |||
9057c1603a | |||
dfa92b39fb | |||
95320ae0b4 | |||
feb5cdd844 | |||
8eded1bfe8 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
**/input.txt
|
**/input.txt
|
||||||
|
**/test.txt
|
24
2022/day01/alex.py
Normal file
24
2022/day01/alex.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
f = open("file", "r")
|
||||||
|
Calories = f.read()
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
somme = []
|
||||||
|
sum = 0
|
||||||
|
|
||||||
|
l_calories = Calories.split("\n\n")
|
||||||
|
for i in l_calories:
|
||||||
|
liste = i.split("\n")
|
||||||
|
for j in liste:
|
||||||
|
sum += int(j)
|
||||||
|
somme.append(sum)
|
||||||
|
sum = 0
|
||||||
|
|
||||||
|
def sauver_noel(liste):
|
||||||
|
for i in range(1, len(liste)-1):
|
||||||
|
if liste[i-1] >= liste[i]:
|
||||||
|
maxi = liste[i-1]
|
||||||
|
else:
|
||||||
|
maxi = liste[i]
|
||||||
|
return maxi
|
||||||
|
|
||||||
|
print(sauver_noel(somme))
|
11
2022/day01/part1.py
Normal file
11
2022/day01/part1.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
f = open("file", "r")
|
||||||
|
str = f.read()
|
||||||
|
f.close()
|
||||||
|
lst = []
|
||||||
|
for paquet in str.split("\n\n"):
|
||||||
|
Sum = 0;
|
||||||
|
for sub_paquet in paquet.split("\n"):
|
||||||
|
if (sub_paquet != ""):
|
||||||
|
Sum += int(sub_paquet)
|
||||||
|
lst.append(Sum);
|
||||||
|
print(max(lst))
|
12
2022/day01/part2.py
Normal file
12
2022/day01/part2.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
f = open("file", "r")
|
||||||
|
str = f.read()
|
||||||
|
f.close()
|
||||||
|
lst = []
|
||||||
|
for paquet in str.split("\n\n"):
|
||||||
|
Sum = 0;
|
||||||
|
for sub_paquet in paquet.split("\n"):
|
||||||
|
if (sub_paquet != ""):
|
||||||
|
Sum += int(sub_paquet)
|
||||||
|
lst.append(Sum);
|
||||||
|
lst.sort();
|
||||||
|
print(sum(lst[-3:]))
|
2520
2022/day02/main.py
Normal file
2520
2022/day02/main.py
Normal file
File diff suppressed because it is too large
Load Diff
14
2022/day03/part1.py
Normal file
14
2022/day03/part1.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
f = open("file")
|
||||||
|
str = f.read();
|
||||||
|
f.close();
|
||||||
|
sum = 0;
|
||||||
|
priority = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
||||||
|
for sac in str.split("\n"):
|
||||||
|
compartiment1 = sac[:len(sac)//2]
|
||||||
|
compartiment2 = sac[len(sac)//2:]
|
||||||
|
for element1 in compartiment1:
|
||||||
|
if (element1 in compartiment2):
|
||||||
|
print(element1, priority.index(element1) + 1)
|
||||||
|
sum += priority.index(element1) + 1;
|
||||||
|
break;
|
||||||
|
print (sum);
|
22
2022/day03/part2.py
Normal file
22
2022/day03/part2.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
f = open("file");
|
||||||
|
str = f.read();
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
priority = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
||||||
|
|
||||||
|
|
||||||
|
splitted = []
|
||||||
|
str = str.split("\n")
|
||||||
|
while len(str) != 0:
|
||||||
|
splitted.append("\n".join(str[:3]));
|
||||||
|
str = str[3:];
|
||||||
|
|
||||||
|
sum = 0;
|
||||||
|
for group in splitted:
|
||||||
|
resplitted = group.split("\n");
|
||||||
|
print(resplitted)
|
||||||
|
for i in resplitted[0]:
|
||||||
|
if (i in resplitted[1] and i in resplitted[2]):
|
||||||
|
sum += priority.index(i) + 1;
|
||||||
|
break;
|
||||||
|
print (sum);
|
19
2022/day04/part1.py
Normal file
19
2022/day04/part1.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
f = open("file");
|
||||||
|
s = f.read();
|
||||||
|
f.close();
|
||||||
|
menfou = 0;
|
||||||
|
for pair in s.splitlines():
|
||||||
|
elfs = pair.split(",");
|
||||||
|
values = elfs[0].split("-")
|
||||||
|
start1 = int(values[0]);
|
||||||
|
end1 = int(values[1]);
|
||||||
|
values = elfs[1].split("-")
|
||||||
|
start2 = int(values[0]);
|
||||||
|
end2 = int(values[1]);
|
||||||
|
if (start2 <= start1 <= end2 and end1 <= end2):
|
||||||
|
print (start2, start1, end1, end2)
|
||||||
|
menfou += 1;
|
||||||
|
elif (start1 <= start2 <= end1 and end2 <= end1):
|
||||||
|
print (start1, start2, end2, end1)
|
||||||
|
menfou += 1;
|
||||||
|
print(menfou);
|
19
2022/day04/part2.py
Normal file
19
2022/day04/part2.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
f = open("file");
|
||||||
|
s = f.read();
|
||||||
|
f.close();
|
||||||
|
menfou = 0;
|
||||||
|
for pair in s.splitlines():
|
||||||
|
elfs = pair.split(",");
|
||||||
|
values = elfs[0].split("-")
|
||||||
|
start1 = int(values[0]);
|
||||||
|
end1 = int(values[1]);
|
||||||
|
values = elfs[1].split("-")
|
||||||
|
start2 = int(values[0]);
|
||||||
|
end2 = int(values[1]);
|
||||||
|
verif = 0;
|
||||||
|
for i in range(start1, end1 + 1):
|
||||||
|
for y in range(start2, end2 + 1):
|
||||||
|
if (y == i):
|
||||||
|
verif = 1;
|
||||||
|
menfou += verif;
|
||||||
|
print(menfou);
|
17
2022/day05/part1.py
Normal file
17
2022/day05/part1.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
f = open("file");
|
||||||
|
s = f.read();
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
splitted = s.split("\n\n");
|
||||||
|
default = splitted[0];
|
||||||
|
instructions = splitted[1];
|
||||||
|
resplitted = default.split("\n")
|
||||||
|
|
||||||
|
for i in range (len(resplitted) - 1):
|
||||||
|
letter = False;
|
||||||
|
for y in range (len(resplitted[i] - 1)):
|
||||||
|
if (resplitted[i][y] == '['):
|
||||||
|
letter = True;
|
||||||
|
elif (letter):
|
||||||
|
letter = False;
|
||||||
|
tab[i ]
|
10
2023/day03/example1.txt
Normal file
10
2023/day03/example1.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
10
2023/day03/example2.txt
Normal file
10
2023/day03/example2.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
29
2023/day03/src/part1.py
Normal file
29
2023/day03/src/part1.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
value: int = 0;
|
||||||
|
|
||||||
|
def is_partnumber(lst:[str], x, y, length: int):
|
||||||
|
start_x = x - (x > 0)
|
||||||
|
start_y = y - (y > 0)
|
||||||
|
stop_x = x + length + (x < len(lst[y]) - 1)
|
||||||
|
stop_y = y + (y < len(lst) - 1)
|
||||||
|
for y, line in enumerate(lst[start_y:stop_y + 1]):
|
||||||
|
line = line[start_x:stop_x]
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (not char.isdigit() and not char == "."):
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
lst = text.split("\n")
|
||||||
|
length: int = 0
|
||||||
|
for y in range(len(lst)):
|
||||||
|
lst[y] += "."
|
||||||
|
for x in range(len(lst[y])):
|
||||||
|
if (lst[y][x].isdigit()):
|
||||||
|
length += 1
|
||||||
|
elif (length != 0):
|
||||||
|
if (is_partnumber(lst, x - length, y, length)):
|
||||||
|
value += int(lst[y][x - length:x])
|
||||||
|
length = 0
|
||||||
|
|
||||||
|
print(value)
|
51
2023/day03/src/part2.py
Normal file
51
2023/day03/src/part2.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
value: int = 0;
|
||||||
|
|
||||||
|
def get_pos_of_digits_around(lst: list[str], x:int, y:int):
|
||||||
|
start_x = x - (x > 0)
|
||||||
|
start_y = y - (y > 0)
|
||||||
|
stop_x = x + (x < len(lst[y]) - 1)
|
||||||
|
stop_y = y + (y < len(lst) - 1)
|
||||||
|
|
||||||
|
lst_pos = []
|
||||||
|
|
||||||
|
for y, line in enumerate(lst[start_y:stop_y + 1]):
|
||||||
|
line = line[start_x:stop_x + 1]
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (char.isdigit() and not (x > 0 and line[x - 1].isdigit())):
|
||||||
|
lst_pos.append((start_x + x, start_y + y))
|
||||||
|
|
||||||
|
return (lst_pos)
|
||||||
|
|
||||||
|
def get_numbers_by_pos(lst: [str], lst_pos: [(int, int)]):
|
||||||
|
|
||||||
|
lst_number = []
|
||||||
|
|
||||||
|
for x, y in lst_pos:
|
||||||
|
start: int = x
|
||||||
|
while (start > 0 and lst[y][start - 1].isdigit()):
|
||||||
|
start -= 1
|
||||||
|
stop: int = x
|
||||||
|
while (stop < len(lst[y]) and lst[y][stop].isdigit()):
|
||||||
|
stop += 1
|
||||||
|
print(lst[y][start:stop])
|
||||||
|
lst_number.append(int(lst[y][start:stop]))
|
||||||
|
print(f"example2.txt:{y + 1}:{x + 1}")
|
||||||
|
return lst_number
|
||||||
|
|
||||||
|
lst_text: [str] = text.split("\n")
|
||||||
|
|
||||||
|
for y, line in enumerate(lst_text):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (char == "*"):
|
||||||
|
lst = get_pos_of_digits_around(lst_text, x, y)
|
||||||
|
if (len(lst) != 2):
|
||||||
|
continue
|
||||||
|
lst = get_numbers_by_pos(lst_text, lst)
|
||||||
|
print(lst)
|
||||||
|
value += math.prod(lst)
|
||||||
|
|
||||||
|
print(value)
|
6
2023/day04/example1.txt
Normal file
6
2023/day04/example1.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||||
|
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||||
|
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||||
|
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||||
|
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||||
|
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
6
2023/day04/example2.txt
Normal file
6
2023/day04/example2.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||||
|
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||||
|
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||||
|
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||||
|
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||||
|
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
18
2023/day04/src/part1.py
Normal file
18
2023/day04/src/part1.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
bozo_value: int = 0
|
||||||
|
bozo = line.split(": ")
|
||||||
|
bozo2 = bozo[1].split(" | ")
|
||||||
|
expected_value = bozo2[0].split(" ")
|
||||||
|
values = bozo2[1].split(" ")
|
||||||
|
for current_value in values:
|
||||||
|
if (current_value != "" and current_value in expected_value):
|
||||||
|
bozo_value: int = bozo_value * 2 + (bozo_value == 0)
|
||||||
|
value += bozo_value
|
||||||
|
|
||||||
|
print(value)
|
28
2023/day04/src/part2.py
Normal file
28
2023/day04/src/part2.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
cards_coef = {}
|
||||||
|
|
||||||
|
for card_id, line in enumerate(lines):
|
||||||
|
card_id += 1
|
||||||
|
bozo_value: int = 0
|
||||||
|
bozo = line.split(": ")
|
||||||
|
bozo2 = bozo[1].split(" | ")
|
||||||
|
expected_value = bozo2[0].split(" ")
|
||||||
|
values = bozo2[1].split(" ")
|
||||||
|
for current_value in values:
|
||||||
|
if (current_value != "" and current_value in expected_value):
|
||||||
|
bozo_value += 1
|
||||||
|
cards_coef.update({card_id: cards_coef.get(card_id, 0) + 1})
|
||||||
|
current_card_coef = cards_coef.get(card_id)
|
||||||
|
for next_card_id in range(card_id + 1, card_id + bozo_value + 1):
|
||||||
|
next_cards_coef = cards_coef.get(next_card_id, 0)
|
||||||
|
cards_coef.update({next_card_id: next_cards_coef + current_card_coef})
|
||||||
|
|
||||||
|
for card_id, coef in cards_coef.items():
|
||||||
|
value += coef
|
||||||
|
|
||||||
|
print(value)
|
33
2023/day05/example.txt
Normal file
33
2023/day05/example.txt
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
seeds: 79 14 55 13
|
||||||
|
|
||||||
|
seed-to-soil map:
|
||||||
|
50 98 2
|
||||||
|
52 50 48
|
||||||
|
|
||||||
|
soil-to-fertilizer map:
|
||||||
|
0 15 37
|
||||||
|
37 52 2
|
||||||
|
39 0 15
|
||||||
|
|
||||||
|
fertilizer-to-water map:
|
||||||
|
49 53 8
|
||||||
|
0 11 42
|
||||||
|
42 0 7
|
||||||
|
57 7 4
|
||||||
|
|
||||||
|
water-to-light map:
|
||||||
|
88 18 7
|
||||||
|
18 25 70
|
||||||
|
|
||||||
|
light-to-temperature map:
|
||||||
|
45 77 23
|
||||||
|
81 45 19
|
||||||
|
68 64 13
|
||||||
|
|
||||||
|
temperature-to-humidity map:
|
||||||
|
0 69 1
|
||||||
|
1 0 69
|
||||||
|
|
||||||
|
humidity-to-location map:
|
||||||
|
60 56 37
|
||||||
|
56 93 4
|
23
2023/day05/src/part1.py
Normal file
23
2023/day05/src/part1.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
values = list(map(int, lines[0][7:].split()))
|
||||||
|
|
||||||
|
for conversions in text.split("\n\n")[1:]:
|
||||||
|
|
||||||
|
_conversion = []
|
||||||
|
for conversion in conversions.splitlines()[1:]:
|
||||||
|
_conversion.append(list(map(int, conversion.split())))
|
||||||
|
|
||||||
|
for i, value in enumerate(values):
|
||||||
|
for dst, src, length in _conversion:
|
||||||
|
if (src <= value <= src + length):
|
||||||
|
values[i] = value + dst - src
|
||||||
|
break;
|
||||||
|
|
||||||
|
_value = min(values)
|
||||||
|
|
||||||
|
print(_value)
|
99
2023/day05/src/part2.py
Normal file
99
2023/day05/src/part2.py
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
_lines = text.splitlines()
|
||||||
|
CHUNK_SIZE = 100000000
|
||||||
|
|
||||||
|
current_ranges = []
|
||||||
|
convertions = []
|
||||||
|
|
||||||
|
bozo = _lines[0][7:].split(" ")
|
||||||
|
for i, value in enumerate(bozo):
|
||||||
|
if (i % 2):
|
||||||
|
continue
|
||||||
|
current_ranges.append((int(bozo[i]), int(bozo[i]) + int(bozo[i + 1])))
|
||||||
|
|
||||||
|
def extract_map_data(lines: [str]):
|
||||||
|
lst = []
|
||||||
|
for line in lines:
|
||||||
|
if (line == ""):
|
||||||
|
break
|
||||||
|
bozo = line.split()
|
||||||
|
destination = int(bozo[0])
|
||||||
|
source_start = int(bozo[1])
|
||||||
|
length = int(bozo[2])
|
||||||
|
lst.append([destination - source_start, source_start, source_start + length])
|
||||||
|
return lst
|
||||||
|
|
||||||
|
def translation(lst: list[[]], value: int):
|
||||||
|
for dst, src_start, src_stop in lst:
|
||||||
|
if src_start <= value <= src_stop:
|
||||||
|
return(value + dst)
|
||||||
|
return value
|
||||||
|
|
||||||
|
for i, line in enumerate(_lines):
|
||||||
|
if (line.endswith(" map:")):
|
||||||
|
convertions.append(extract_map_data(_lines[i + 1:]))
|
||||||
|
|
||||||
|
current_time = time.time()
|
||||||
|
def display_duration(interval_between_call, nb_total_values):
|
||||||
|
global current_time
|
||||||
|
old_time = current_time
|
||||||
|
current_time = time.time()
|
||||||
|
diff = int(((current_time - old_time) / interval_between_call) * nb_total_values)
|
||||||
|
print(f"{int(diff / 3600)}h {int(diff % 3600 / 60)}min {int(diff % 60)}s")
|
||||||
|
|
||||||
|
def do_all_convertions(lst_convertions: [[[]]], value):
|
||||||
|
for i, tab_convertion in enumerate(convertions):
|
||||||
|
value = translation(tab_convertion, value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def get_min_in_range(lst_conversions, start, stop):
|
||||||
|
min_value = do_all_convertions(lst_conversions, start)
|
||||||
|
i = start + 1;
|
||||||
|
while (i != stop):
|
||||||
|
if ((i - start) % 1000000 == 0):
|
||||||
|
print(f"{int((i - start) * 100/(stop - start))}% ({i - start}/{stop - start})")
|
||||||
|
value = do_all_convertions(lst_conversions, i)
|
||||||
|
min_value = min(value, min_value)
|
||||||
|
i += 1
|
||||||
|
return (min_value)
|
||||||
|
|
||||||
|
values = []
|
||||||
|
|
||||||
|
def thread_process(convertions, start_value, chunk):
|
||||||
|
global values
|
||||||
|
values.append(get_min_in_range(convertions, start_value, chunk))
|
||||||
|
|
||||||
|
for current_range in current_ranges:
|
||||||
|
print("new range")
|
||||||
|
start, stop = current_range
|
||||||
|
i = start
|
||||||
|
chunks = []
|
||||||
|
while(i < stop):
|
||||||
|
i += CHUNK_SIZE
|
||||||
|
if (i >= stop):
|
||||||
|
chunks.append(stop)
|
||||||
|
else:
|
||||||
|
chunks.append(i)
|
||||||
|
start_value = start
|
||||||
|
i = 0
|
||||||
|
while (i < len(chunks)):
|
||||||
|
t = threading.Thread(target=thread_process, args=(convertions, start_value, chunks[i]))
|
||||||
|
t.start()
|
||||||
|
print(f"start thread{i + 1}/{len(chunks)}")
|
||||||
|
i += 1
|
||||||
|
start_value = chunks[i - 1]
|
||||||
|
|
||||||
|
while (threading.active_count() != 1):
|
||||||
|
print(values, threading.active_count() - 1)
|
||||||
|
time.sleep(10)
|
||||||
|
|
||||||
|
print(values)
|
||||||
|
_value = min(values)
|
||||||
|
|
||||||
|
print(_value)
|
2
2023/day06/example.txt
Normal file
2
2023/day06/example.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
19
2023/day06/src/part1.py
Normal file
19
2023/day06/src/part1.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 1
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
times = [46857582]
|
||||||
|
distances = [208141212571410]
|
||||||
|
|
||||||
|
for start in range(0, len(times)):
|
||||||
|
distance = distances[start]
|
||||||
|
time = times[start]
|
||||||
|
value = 0
|
||||||
|
for i in range(0, time):
|
||||||
|
if ((time - i) * i > distance):
|
||||||
|
value += 1
|
||||||
|
_value *= value
|
||||||
|
|
||||||
|
print(_value)
|
19
2023/day06/src/part2.py
Normal file
19
2023/day06/src/part2.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 1
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
times = [46857582]
|
||||||
|
distances = [208141212571410]
|
||||||
|
|
||||||
|
for start in range(0, len(times)):
|
||||||
|
distance = distances[start]
|
||||||
|
time = times[start]
|
||||||
|
value = 0
|
||||||
|
for i in range(0, time):
|
||||||
|
if ((time - i) * i > distance):
|
||||||
|
value += 1
|
||||||
|
_value *= value
|
||||||
|
|
||||||
|
print(_value)
|
7
2023/day07/example.txt
Normal file
7
2023/day07/example.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483
|
||||||
|
77777 1
|
||||||
|
KK677 28
|
||||||
|
12345 2
|
70
2023/day07/src/part1.py
Normal file
70
2023/day07/src/part1.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
CARDS = "AKQJT98765432"
|
||||||
|
|
||||||
|
def parse_line(line: str):
|
||||||
|
bozo = line.split()
|
||||||
|
card = bozo[0]
|
||||||
|
bid = int(bozo[1])
|
||||||
|
same = {"card": card, "bid": bid}
|
||||||
|
bozo = set(card)
|
||||||
|
if (len(bozo) == 5):
|
||||||
|
_type = 0
|
||||||
|
elif (len(bozo) == 4):
|
||||||
|
_type = 1
|
||||||
|
elif (len(bozo) == 3 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
||||||
|
_type = 2
|
||||||
|
elif (len(bozo) == 3):
|
||||||
|
_type = 3
|
||||||
|
elif (len(bozo) == 2 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
||||||
|
_type = 4
|
||||||
|
elif (len(bozo) == 2):
|
||||||
|
_type = 5
|
||||||
|
elif (len(bozo) == 1):
|
||||||
|
_type = 6
|
||||||
|
|
||||||
|
same.update({"type": int(_type)})
|
||||||
|
return same
|
||||||
|
|
||||||
|
def parse(lines):
|
||||||
|
hands = []
|
||||||
|
for line in lines:
|
||||||
|
hands.append(parse_line(line))
|
||||||
|
return hands
|
||||||
|
|
||||||
|
def hand_cmp(hand1: dict, hand2: dict):
|
||||||
|
for card1, card2 in zip(hand1.get("card"), hand2.get("card")):
|
||||||
|
if card1 == card2:
|
||||||
|
continue
|
||||||
|
return (CARDS.index(card1) - CARDS.index(card2))
|
||||||
|
|
||||||
|
def get_insert_index(lst: [dict], hand: dict):
|
||||||
|
index: int = None
|
||||||
|
for i, hand2 in enumerate(lst):
|
||||||
|
if (hand.get("type") == hand2.get("type")):
|
||||||
|
if (hand_cmp(hand, hand2) < 0):
|
||||||
|
return i
|
||||||
|
elif (hand.get("type") > hand2.get("type")):
|
||||||
|
return i
|
||||||
|
return len(lst)
|
||||||
|
|
||||||
|
def sort_hands(hands: [dict]):
|
||||||
|
sort = [hands[0]]
|
||||||
|
for hand in hands[1:]:
|
||||||
|
index = get_insert_index(sort, hand)
|
||||||
|
sort.insert(index, hand)
|
||||||
|
return sort
|
||||||
|
|
||||||
|
hands = parse(lines)
|
||||||
|
hands = sort_hands(hands)
|
||||||
|
|
||||||
|
for i, hand in enumerate(hands):
|
||||||
|
_value += (len(hands) - i) * hand.get("bid")
|
||||||
|
#print(f'{hand.get("card")} {hand.get("bid")}')
|
||||||
|
print(f'{len(hands) - i} {hand.get("card")} {hand.get("bid")} type={hand.get("type")}')
|
||||||
|
|
||||||
|
print(_value)
|
96
2023/day07/src/part2.py
Normal file
96
2023/day07/src/part2.py
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
CARDS = "AKQT98765432J"
|
||||||
|
|
||||||
|
def get_best_replace(string: str):
|
||||||
|
if ("J" not in string):
|
||||||
|
return string
|
||||||
|
if (string == "JJJJJ"):
|
||||||
|
return "AAAAA"
|
||||||
|
tmp = string
|
||||||
|
tmp = tmp.replace("J", "")
|
||||||
|
bozo: list = list(set(tmp))
|
||||||
|
bozo2: list = [(tmp.count(x), x) for x in tmp]
|
||||||
|
max_occurence = [bozo2[0]]
|
||||||
|
for occurence, char in bozo2[1:]:
|
||||||
|
if (max_occurence[0] == occurence):
|
||||||
|
max_occurence.append((occurence, char))
|
||||||
|
elif (max_occurence[0][0] < occurence):
|
||||||
|
max_occurence = [(occurence, char)]
|
||||||
|
_max = max_occurence[0][1]
|
||||||
|
for occurence, char in max_occurence[1:]:
|
||||||
|
if (CARDS.index(_max) > CARDS.index(char)):
|
||||||
|
_max = char
|
||||||
|
|
||||||
|
string = string.replace("J", _max)
|
||||||
|
return string
|
||||||
|
|
||||||
|
def parse_line(line: str):
|
||||||
|
bozo = line.split()
|
||||||
|
raw_card = bozo[0]
|
||||||
|
bid = int(bozo[1])
|
||||||
|
card = get_best_replace(raw_card)
|
||||||
|
same = {"card": card, "raw_card": raw_card, "bid": bid}
|
||||||
|
bozo = set(card)
|
||||||
|
if (len(bozo) == 5):
|
||||||
|
_type = 0
|
||||||
|
elif (len(bozo) == 4):
|
||||||
|
_type = 1
|
||||||
|
elif (len(bozo) == 3 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
||||||
|
_type = 2
|
||||||
|
elif (len(bozo) == 3):
|
||||||
|
_type = 3
|
||||||
|
elif (len(bozo) == 2 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
|
||||||
|
_type = 4
|
||||||
|
elif (len(bozo) == 2):
|
||||||
|
_type = 5
|
||||||
|
elif (len(bozo) == 1):
|
||||||
|
_type = 6
|
||||||
|
|
||||||
|
same.update({"type": int(_type)})
|
||||||
|
return same
|
||||||
|
|
||||||
|
def parse(lines):
|
||||||
|
hands = []
|
||||||
|
for line in lines:
|
||||||
|
hands.append(parse_line(line))
|
||||||
|
return hands
|
||||||
|
|
||||||
|
def hand_cmp(hand1: dict, hand2: dict):
|
||||||
|
#print(hand1, hand2)
|
||||||
|
for card1, card2 in zip(hand1.get("raw_card"), hand2.get("raw_card")):
|
||||||
|
if card1 == card2:
|
||||||
|
continue
|
||||||
|
return (CARDS.index(card1) - CARDS.index(card2))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def get_insert_index(lst: [dict], hand: dict):
|
||||||
|
index: int = None
|
||||||
|
for i, hand2 in enumerate(lst):
|
||||||
|
if (hand.get("type") == hand2.get("type")):
|
||||||
|
if (hand_cmp(hand, hand2) < 0):
|
||||||
|
return i
|
||||||
|
elif (hand.get("type") > hand2.get("type")):
|
||||||
|
return i
|
||||||
|
return len(lst)
|
||||||
|
|
||||||
|
def sort_hands(hands: [dict]):
|
||||||
|
sort = [hands[0]]
|
||||||
|
for hand in hands[1:]:
|
||||||
|
index = get_insert_index(sort, hand)
|
||||||
|
sort.insert(index, hand)
|
||||||
|
return sort
|
||||||
|
|
||||||
|
hands = parse(lines)
|
||||||
|
hands = sort_hands(hands)
|
||||||
|
|
||||||
|
for i, hand in enumerate(hands):
|
||||||
|
_value += (len(hands) - i) * hand.get("bid")
|
||||||
|
#print(f'{hand.get("card")} {hand.get("raw_card")} {hand.get("bid")}')
|
||||||
|
#print(f'{len(hands) - i} {hand.get("card")} {hand.get("bid")} type={hand.get("type")}')
|
||||||
|
|
||||||
|
print(_value)
|
9
2023/day08/example1.txt
Normal file
9
2023/day08/example1.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
RL
|
||||||
|
|
||||||
|
AAA = (BBB, CCC)
|
||||||
|
BBB = (DDD, EEE)
|
||||||
|
CCC = (ZZZ, GGG)
|
||||||
|
DDD = (DDD, DDD)
|
||||||
|
EEE = (EEE, EEE)
|
||||||
|
GGG = (GGG, GGG)
|
||||||
|
ZZZ = (ZZZ, ZZZ)
|
5
2023/day08/example2.txt
Normal file
5
2023/day08/example2.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
LLR
|
||||||
|
|
||||||
|
AAA = (BBB, BBB)
|
||||||
|
BBB = (AAA, ZZZ)
|
||||||
|
ZZZ = (ZZZ, ZZZ)
|
10
2023/day08/example3.txt
Normal file
10
2023/day08/example3.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
LR
|
||||||
|
|
||||||
|
11A = (11B, XXX)
|
||||||
|
11B = (XXX, 11Z)
|
||||||
|
11Z = (11B, XXX)
|
||||||
|
22A = (22B, XXX)
|
||||||
|
22B = (22C, 22C)
|
||||||
|
22C = (22Z, 22Z)
|
||||||
|
22Z = (22B, 22B)
|
||||||
|
XXX = (XXX, XXX)
|
27
2023/day08/src/part1.py
Normal file
27
2023/day08/src/part1.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
deplacements = lines[0]
|
||||||
|
ground = {}
|
||||||
|
|
||||||
|
for line in lines[2:]:
|
||||||
|
data = line.split(" = ")
|
||||||
|
data[1] = data[1][1:9]
|
||||||
|
ground.update({data[0]: data[1].split(", ")})
|
||||||
|
|
||||||
|
def resolve(ground: dict, deplacements: str, start: str):
|
||||||
|
nb_deplacement = 0
|
||||||
|
pos = start
|
||||||
|
while True:
|
||||||
|
for deplacement in deplacements:
|
||||||
|
if (pos == "ZZZ"):
|
||||||
|
return (nb_deplacement)
|
||||||
|
nb_deplacement += 1
|
||||||
|
pos = ground.get(pos)[deplacement == "R"]
|
||||||
|
|
||||||
|
_value = resolve(ground, deplacements, "AAA")
|
||||||
|
|
||||||
|
print(_value)
|
36
2023/day08/src/part2.py
Normal file
36
2023/day08/src/part2.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
deplacements = lines[0]
|
||||||
|
ground = {}
|
||||||
|
|
||||||
|
starts = []
|
||||||
|
for line in lines[2:]:
|
||||||
|
data = line.split(" = ")
|
||||||
|
if (data[0][2] == 'A'):
|
||||||
|
starts.append(data[0])
|
||||||
|
data[1] = data[1][1:9]
|
||||||
|
ground.update({data[0]: data[1].split(", ")})
|
||||||
|
|
||||||
|
def resolve(ground: dict, deplacements: str, start: str):
|
||||||
|
nb_deplacement = 0
|
||||||
|
pos = start
|
||||||
|
while True:
|
||||||
|
for deplacement in deplacements:
|
||||||
|
if (pos[2] == "Z"):
|
||||||
|
return (nb_deplacement)
|
||||||
|
nb_deplacement += 1
|
||||||
|
pos = ground.get(pos)[deplacement == "R"]
|
||||||
|
|
||||||
|
lst = []
|
||||||
|
for start in starts:
|
||||||
|
lst.append(resolve(ground, deplacements, start))
|
||||||
|
|
||||||
|
_value = math.lcm(*lst)
|
||||||
|
|
||||||
|
print(_value)
|
3
2023/day09/example.txt
Normal file
3
2023/day09/example.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
0 3 6 9 12 15
|
||||||
|
1 3 6 10 15 21
|
||||||
|
10 13 16 21 30 45
|
32
2023/day09/src/part1.py
Normal file
32
2023/day09/src/part1.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
def get_difference(values: [int]):
|
||||||
|
lst = []
|
||||||
|
for i in range(0, len(values) - 1):
|
||||||
|
lst.append(values[i + 1] - values[i])
|
||||||
|
return lst
|
||||||
|
|
||||||
|
def get_next(derives: [[int]]):
|
||||||
|
value = 0
|
||||||
|
for values in derives[::-1]:
|
||||||
|
if (len(values) == 0):
|
||||||
|
continue
|
||||||
|
value = values[-1] + value
|
||||||
|
return value
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
values = list(map(int, line.split()))
|
||||||
|
derives: [[int]] = [values]
|
||||||
|
lst = values
|
||||||
|
while True:
|
||||||
|
lst = get_difference(lst)
|
||||||
|
derives.append(lst)
|
||||||
|
if (lst.count(0) == len(lst)):
|
||||||
|
break
|
||||||
|
_value += get_next(derives)
|
||||||
|
|
||||||
|
print(_value)
|
32
2023/day09/src/part2.py
Normal file
32
2023/day09/src/part2.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
text: str = open("example.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
def get_difference(values: [int]):
|
||||||
|
lst = []
|
||||||
|
for i in range(0, len(values) - 1):
|
||||||
|
lst.append(values[i + 1] - values[i])
|
||||||
|
return lst
|
||||||
|
|
||||||
|
def get_next(derives: [[int]]):
|
||||||
|
value = 0
|
||||||
|
for values in derives[::-1]:
|
||||||
|
if (len(values) == 0):
|
||||||
|
continue
|
||||||
|
value = values[0] - value
|
||||||
|
return value
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
values = list(map(int, line.split()))
|
||||||
|
derives: [[int]] = [values]
|
||||||
|
lst = values
|
||||||
|
while True:
|
||||||
|
lst = get_difference(lst)
|
||||||
|
derives.append(lst)
|
||||||
|
if (lst.count(0) == len(lst)):
|
||||||
|
break
|
||||||
|
_value += get_next(derives)
|
||||||
|
|
||||||
|
print(_value)
|
6
2023/day10/example1.txt
Normal file
6
2023/day10/example1.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
......,
|
||||||
|
.S-7...
|
||||||
|
.|.|..,
|
||||||
|
.L7L-7.
|
||||||
|
..L--J.
|
||||||
|
.......
|
57
2023/day10/src/part1.py
Normal file
57
2023/day10/src/part1.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
def get_symbol_vector(symbol: str):
|
||||||
|
match symbol:
|
||||||
|
case "|":
|
||||||
|
return ((0, -1), (0, +1))
|
||||||
|
case "L":
|
||||||
|
return ((0, -1), (+1, 0))
|
||||||
|
case "J":
|
||||||
|
return ((-1, 0), (0, -1))
|
||||||
|
case "7":
|
||||||
|
return ((-1, 0), (0, +1))
|
||||||
|
case "F":
|
||||||
|
return ((+1, 0), (0, +1))
|
||||||
|
case "-":
|
||||||
|
return ((+1, 0), (-1, 0))
|
||||||
|
|
||||||
|
def get_start(lines: [str]):
|
||||||
|
for y, line in enumerate(lines):
|
||||||
|
for x, char in enumerate(line):
|
||||||
|
if (char == "S"):
|
||||||
|
return (x, y)
|
||||||
|
|
||||||
|
def get_around(lines, x, y):
|
||||||
|
lst = []
|
||||||
|
if (len(lines[y]) != x + 1 and lines[y][x + 1] != '.'):
|
||||||
|
lst.append((x + 1, y))
|
||||||
|
if (x > 0 and lines[y][x - 1] != '.'):
|
||||||
|
lst.append((x - 1, y))
|
||||||
|
if (len(lines) != y + 1 and lines[y + 1][x] != '.'):
|
||||||
|
lst.append((x, y + 1))
|
||||||
|
if (y > 0 and lines[y - 1][x] != '.'):
|
||||||
|
lst.append((x, y - 1))
|
||||||
|
return lst
|
||||||
|
|
||||||
|
old_x, old_y = get_start(lines)
|
||||||
|
x, y = get_around(lines, old_x, old_y)[0]
|
||||||
|
nb_moves = 1
|
||||||
|
while True:
|
||||||
|
backward, forward = get_symbol_vector(lines[y][x])
|
||||||
|
if (backward == (old_x - x, old_y - y)):
|
||||||
|
old_x, old_y = x, y
|
||||||
|
x, y = map(sum, zip((x, y), forward))
|
||||||
|
else:
|
||||||
|
old_x, old_y = x, y
|
||||||
|
x,y = map(sum, zip((x, y), backward))
|
||||||
|
print(lines[y][x], x, y)
|
||||||
|
nb_moves += 1
|
||||||
|
if (lines[y][x] == "S"):
|
||||||
|
_value = nb_moves // 2
|
||||||
|
break
|
||||||
|
|
||||||
|
print(_value)
|
10
2023/day11/example1.txt
Normal file
10
2023/day11/example1.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
...#......
|
||||||
|
.......#..
|
||||||
|
#.........
|
||||||
|
..........
|
||||||
|
......#...
|
||||||
|
.#........
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
.......#..
|
||||||
|
#...#.....
|
2
2023/day11/example2.txt
Normal file
2
2023/day11/example2.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#.
|
||||||
|
.#
|
34
2023/day11/src/part1.py
Normal file
34
2023/day11/src/part1.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
empty_lines: int = 0
|
||||||
|
cords: [(int, int)] = []
|
||||||
|
for y, line in enumerate(lines):
|
||||||
|
if "#" in line:
|
||||||
|
for _ in range(line.count("#")):
|
||||||
|
cords.append(y + empty_lines)
|
||||||
|
else:
|
||||||
|
empty_lines += 1
|
||||||
|
|
||||||
|
bozo: int = 0
|
||||||
|
empty_colums: int = 0
|
||||||
|
for x in range(len(lines[0])):
|
||||||
|
apagnan = 1
|
||||||
|
for y in range(len(lines)):
|
||||||
|
if lines[y][x] == "#":
|
||||||
|
apagnan = 0
|
||||||
|
cords[bozo] = (x + empty_colums, cords[bozo])
|
||||||
|
bozo += 1
|
||||||
|
if apagnan:
|
||||||
|
empty_colums += 1
|
||||||
|
|
||||||
|
while len(cords):
|
||||||
|
xa, ya = cords.pop()
|
||||||
|
print(xa, ya)
|
||||||
|
for xb, yb in cords:
|
||||||
|
_value += abs(xb - xa) + abs(yb - ya)
|
||||||
|
|
||||||
|
print(_value)
|
34
2023/day11/src/part2.py
Normal file
34
2023/day11/src/part2.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
text: str = open("input.txt", "r").read()
|
||||||
|
|
||||||
|
_value: int = 0
|
||||||
|
|
||||||
|
lines = text.splitlines()
|
||||||
|
|
||||||
|
empty_lines: int = 0
|
||||||
|
cords: [(int, int)] = []
|
||||||
|
for y, line in enumerate(lines):
|
||||||
|
if "#" in line:
|
||||||
|
for _ in range(line.count("#")):
|
||||||
|
cords.append(y + empty_lines)
|
||||||
|
else:
|
||||||
|
empty_lines += 1000000 -1
|
||||||
|
|
||||||
|
bozo: int = 0
|
||||||
|
empty_colums: int = 0
|
||||||
|
for x in range(len(lines[0])):
|
||||||
|
apagnan = 1
|
||||||
|
for y in range(len(lines)):
|
||||||
|
if lines[y][x] == "#":
|
||||||
|
apagnan = 0
|
||||||
|
cords[bozo] = (x + empty_colums, cords[bozo])
|
||||||
|
bozo += 1
|
||||||
|
if apagnan:
|
||||||
|
empty_colums += 1000000 -1
|
||||||
|
|
||||||
|
while len(cords):
|
||||||
|
xa, ya = cords.pop()
|
||||||
|
print(xa, ya)
|
||||||
|
for xb, yb in cords:
|
||||||
|
_value += abs(xb - xa) + abs(yb - ya)
|
||||||
|
|
||||||
|
print(_value)
|
18
2024/day01/part1.py
Normal file
18
2024/day01/part1.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
data: list[list[str]] = [line.split(" ") for line in lines]
|
||||||
|
|
||||||
|
right_data: list[str] = [tmp[1] for tmp in data]
|
||||||
|
left_data: list[str] = [tmp[0] for tmp in data]
|
||||||
|
|
||||||
|
right: list[int] = list(map(int, right_data))
|
||||||
|
left: list[int] = list(map(int, left_data))
|
||||||
|
|
||||||
|
right.sort()
|
||||||
|
left.sort()
|
||||||
|
|
||||||
|
print(sum(map(abs, [a_i - b_i for a_i, b_i in zip(left, right)])))
|
18
2024/day01/part2.py
Normal file
18
2024/day01/part2.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
text: str
|
||||||
|
|
||||||
|
with open("test.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
data: list[list[str]] = [line.split(" ") for line in lines]
|
||||||
|
|
||||||
|
right_data: list[str] = [tmp[1] for tmp in data]
|
||||||
|
left_data: list[str] = [tmp[0] for tmp in data]
|
||||||
|
|
||||||
|
right: list[int] = list(map(int, right_data))
|
||||||
|
left: list[int] = list(map(int, left_data))
|
||||||
|
|
||||||
|
right.sort()
|
||||||
|
left.sort()
|
||||||
|
|
||||||
|
print(sum(map(abs, [x * right.count(x) for x in left])))
|
21
2024/day02/part1.py
Normal file
21
2024/day02/part1.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
numbers: list[int] = list(map(int, line.split(" ")))
|
||||||
|
direction: int = (numbers[1] - numbers[0])
|
||||||
|
if (direction == 0):
|
||||||
|
continue
|
||||||
|
direction = direction / abs(direction)
|
||||||
|
for x1, x2 in zip(numbers[:-1], numbers[1:]):
|
||||||
|
if ((x2 - x1) * direction not in range(1, 4)):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
total += 1
|
||||||
|
print(total)
|
14
2024/day03/part1.py
Normal file
14
2024/day03/part1.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
for x, y in re.findall("mul\(([0-9]{1,3})\,([0-9]{1,3})\)", text):
|
||||||
|
x, y = int(x), int(y)
|
||||||
|
total += x * y
|
||||||
|
|
||||||
|
print(total)
|
19
2024/day03/part2.py
Normal file
19
2024/day03/part2.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
do_or_not: bool = True
|
||||||
|
|
||||||
|
for x, y, don_t, do in re.findall("mul\(([0-9]{1,3})\,([0-9]{1,3})\)|(don\'t\(\))|(do\(\))", text):
|
||||||
|
if (do != ""):
|
||||||
|
do_or_not = True
|
||||||
|
elif (don_t != ""):
|
||||||
|
do_or_not = False
|
||||||
|
elif (do_or_not):
|
||||||
|
total += int(x) * int(y)
|
||||||
|
print(total)
|
32
2024/day04/part1.py
Normal file
32
2024/day04/part1.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
|
||||||
|
def search(values: list[list[str]], word: str, coord_x: int, coord_y: int, inc_x: int, inc_y: int):
|
||||||
|
|
||||||
|
array_length = len(values)
|
||||||
|
line_length = len(values[0])
|
||||||
|
|
||||||
|
for i in range(len(word)):
|
||||||
|
x: int = coord_x + i * inc_x
|
||||||
|
y: int = coord_y + i * inc_y
|
||||||
|
if (x >= line_length or x < 0) or (y >= array_length or y < 0) or (values[y][x] != word[i]):
|
||||||
|
return 0
|
||||||
|
return 1
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
directions = [(-1, -1), (0, -1), (+1, -1), (+1, 0), (+1, +1), (0, +1), (-1, +1), (-1, 0)]
|
||||||
|
|
||||||
|
for y, line in enumerate(lines):
|
||||||
|
for x, c in enumerate(line):
|
||||||
|
if (c == 'X'):
|
||||||
|
for inc_x, inc_y in directions:
|
||||||
|
total += search(lines, "XMAS", x, y, inc_x, inc_y)
|
||||||
|
|
||||||
|
print(total)
|
21
2024/day04/part2.py
Normal file
21
2024/day04/part2.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
for line1, line2, line3 in zip(lines[0:-2], lines[1:-1], lines[2:]):
|
||||||
|
for i in range(0, len(line1) - 2):
|
||||||
|
if (line2[i + 1] == "A"):
|
||||||
|
total += (line1[i] == "M" and line3[i] == "M" and line1[i + 2] == "S" and line3[i + 2] == "S")
|
||||||
|
total += (line1[i] == "M" and line3[i] == "S" and line1[i + 2] == "M" and line3[i + 2] == "S")
|
||||||
|
total += (line1[i] == "S" and line3[i] == "S" and line1[i + 2] == "M" and line3[i + 2] == "M")
|
||||||
|
total += (line1[i] == "S" and line3[i] == "M" and line1[i + 2] == "S" and line3[i + 2] == "M")
|
||||||
|
|
||||||
|
|
||||||
|
print(total)
|
42
2024/day06/part1.py
Normal file
42
2024/day06/part1.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
lines = text.split("\n")
|
||||||
|
lines = [list(line) for line in lines]
|
||||||
|
|
||||||
|
x_max: int = len(lines[0])
|
||||||
|
y_max: int = len(lines)
|
||||||
|
|
||||||
|
spawn_index: int = text.index('^')
|
||||||
|
|
||||||
|
x: int = spawn_index % (x_max + 1)
|
||||||
|
y: int = spawn_index // (x_max + 1)
|
||||||
|
|
||||||
|
angle: int = math.pi * 3 / 2
|
||||||
|
|
||||||
|
while True:
|
||||||
|
new_x: int = round(x + math.cos(angle))
|
||||||
|
new_y: int = round(y + math.sin(angle))
|
||||||
|
|
||||||
|
if (not (x_max > new_x >= 0 and y_max > new_y >= 0)):
|
||||||
|
break
|
||||||
|
|
||||||
|
if (lines[new_y][new_x] == "#"):
|
||||||
|
angle += math.pi / 2
|
||||||
|
continue
|
||||||
|
|
||||||
|
x = new_x
|
||||||
|
y = new_y
|
||||||
|
if (lines[y][x] == '.'):
|
||||||
|
lines[y][x] = "X"
|
||||||
|
total += 1
|
||||||
|
|
||||||
|
print("\n".join(["".join(line) for line in lines]))
|
||||||
|
|
||||||
|
print(total + 1)
|
39
2024/day10/part1.py
Normal file
39
2024/day10/part1.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("test.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
lines: list[str] = text.splitlines()
|
||||||
|
lines: list[int] = [[int(value) if value != "." else 11 for value in line] for line in lines]
|
||||||
|
|
||||||
|
print(lines)
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
def trailhead_tester(ground: list[int], pos_y: int, pos_x: int, ground_y: int, ground_x: int):
|
||||||
|
|
||||||
|
if ground[pos_y][pos_x] == 9:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
min_x: int = max(pos_x - 1, 0)
|
||||||
|
max_x: int = min(pos_x + 1, ground_x)
|
||||||
|
min_y: int = max(pos_y - 1, 0)
|
||||||
|
max_y: int = min(pos_y + 1, ground_y)
|
||||||
|
|
||||||
|
for y, line in enumerate(ground[min_y:max_y]):
|
||||||
|
for x, value in enumerate(line[min_x:max_x]):
|
||||||
|
if ground[pos_y][pos_x] + 1 == value:
|
||||||
|
return trailhead_tester(ground, y + (max_y - min_y) - 2, x + (max_x - min_x) - 2, ground_y, ground_x)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
y_max = len(lines)
|
||||||
|
x_max = len(lines[0])
|
||||||
|
|
||||||
|
for y, line in enumerate(lines):
|
||||||
|
for x, value in enumerate(line):
|
||||||
|
if (value == 0):
|
||||||
|
total += trailhead_tester(lines, y, x, y_max, x_max)
|
||||||
|
|
||||||
|
print(total)
|
28
2024/day11/part1.py
Normal file
28
2024/day11/part1.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
def blink(stones: list[int]):
|
||||||
|
new_stones: list[int] = []
|
||||||
|
for stone in stones:
|
||||||
|
tmp: str = str(stone)
|
||||||
|
length: int = len(tmp)
|
||||||
|
if (stone == 0):
|
||||||
|
new_stones.append(1)
|
||||||
|
elif (length % 2 == 0):
|
||||||
|
new_stones.append(int(tmp[0:length // 2]))
|
||||||
|
new_stones.append(int(tmp[length // 2:]))
|
||||||
|
else:
|
||||||
|
new_stones.append(stone * 2024)
|
||||||
|
return new_stones
|
||||||
|
|
||||||
|
stones = list(map(int, text.split(" ")))
|
||||||
|
|
||||||
|
for i in range(25):
|
||||||
|
stones = blink(stones)
|
||||||
|
print(i)
|
||||||
|
|
||||||
|
print(len(stones))
|
44
2024/day13/part1.py
Normal file
44
2024/day13/part1.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import math
|
||||||
|
import re
|
||||||
|
|
||||||
|
text: str
|
||||||
|
|
||||||
|
with open("input.txt") as f:
|
||||||
|
text = f.read()
|
||||||
|
|
||||||
|
total: int = 0
|
||||||
|
|
||||||
|
exp: str = r"Button A: X([+-]\d+), Y([+-]\d+)\nButton B: X([+-]\d+), Y([+-]\d+)\nPrize: X=(\d+). Y=(\d+)"
|
||||||
|
|
||||||
|
def resolv(Ax, Ay, Bx, By, goal_x, goal_y):
|
||||||
|
for press_b in range(101):
|
||||||
|
press_a = (goal_x - (press_b * Bx)) / Ax
|
||||||
|
if press_a * Ay + press_b * By != goal_y:
|
||||||
|
continue
|
||||||
|
if (press_a // 1 != press_a):
|
||||||
|
continue
|
||||||
|
if (press_a > 100):
|
||||||
|
continue
|
||||||
|
return press_a, press_b
|
||||||
|
|
||||||
|
def resolv_min(Ax, Ay, Bx, By, goal_x, goal_y):
|
||||||
|
tmp1 = resolv(Ax, Ay, Bx, By, goal_x, goal_y)
|
||||||
|
tmp2 = resolv(Bx, By, Ax, Ay, goal_x, goal_y)
|
||||||
|
if (tmp1 is None):
|
||||||
|
return tmp2
|
||||||
|
if (tmp2 is None):
|
||||||
|
return tmp1
|
||||||
|
press_a1, press_b1 = tmp1
|
||||||
|
press_b2, press_a2 = tmp2
|
||||||
|
if (press_a1 > press_a2):
|
||||||
|
return press_a2, press_b2
|
||||||
|
return press_a1, press_b1
|
||||||
|
|
||||||
|
for Ax, Ay, Bx, By, goal_x, goal_y in re.findall(exp, text):
|
||||||
|
Ax, Ay, Bx, By, goal_x, goal_y = int(Ax), int(Ay), int(Bx), int(By), int(goal_x), int(goal_y)
|
||||||
|
tmp = resolv_min(Ax, Ay, Bx, By, goal_x, goal_y)
|
||||||
|
if (tmp is None):
|
||||||
|
continue
|
||||||
|
press_a, press_b = tmp
|
||||||
|
total += press_a * 3 + press_b
|
||||||
|
print(total)
|
Reference in New Issue
Block a user