Compare commits

..

18 Commits

Author SHA1 Message Date
0885af7f06 wip: 2024/day10/part1 2024-12-13 12:22:28 +01:00
5289db169b wip: 2024/day10/part1 2024-12-13 12:19:28 +01:00
3aec98af5b add: 2024/day11/part1 2024-12-13 11:39:41 +01:00
dd7eae1ba6 fix: 2024/day13/part1 2024-12-13 11:12:23 +01:00
345a5bfccb wip 2024/day13/part1 2024-12-13 11:09:33 +01:00
2d7ab5e287 add: 2024/day06/part1 2024-12-06 10:57:24 +01:00
3c535c8036 add: 2024/day04/part2 2024-12-05 14:51:02 +01:00
200a9c18a9 add: 2024/day02/part1 2024-12-05 14:24:55 +01:00
b638d89a0a add: 2024/day04/part1 2024-12-04 20:13:10 +01:00
7be7d0654b fix: 2024/day03 regex expression 2024-12-03 10:15:53 +01:00
90feb14cd5 add: 2024/day03/part2 2024-12-03 09:10:08 +01:00
7accf6c2e8 add: 2024/day03/part1 2024-12-03 08:52:13 +01:00
91f1535c73 add: 2024/day01/part2 2024-12-01 21:04:06 +01:00
cc708b5b97 add: 2024/day01/part1 2024-12-01 21:00:23 +01:00
5377ef9abe update: gitgnore 2024-12-01 21:00:06 +01:00
df47044ce4 add: 2022 2024-08-29 17:12:29 +02:00
5ab804addc add: days11 2023-12-11 08:49:13 +01:00
9459f5cc97 add: part 1 2023-12-10 17:04:57 +01:00
27 changed files with 3099 additions and 1 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
**/input.txt
**/test.txt

24
2022/day01/alex.py Normal file
View 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
View 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
View 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

File diff suppressed because it is too large Load Diff

14
2022/day03/part1.py Normal file
View 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
View 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
View 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
View 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
View 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 ]

6
2023/day10/example1.txt Normal file
View File

@ -0,0 +1,6 @@
......,
.S-7...
.|.|..,
.L7L-7.
..L--J.
.......

57
2023/day10/src/part1.py Normal file
View 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
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

2
2023/day11/example2.txt Normal file
View File

@ -0,0 +1,2 @@
#.
.#

34
2023/day11/src/part1.py Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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)