Compare commits

..

27 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
dd0989dafd rename example file 2023-12-09 10:22:09 +01:00
64624865d6 add: part2 2023-12-09 10:22:00 +01:00
eb95c2147e add: part1 2023-12-09 10:19:10 +01:00
222eff07d2 add: part 2 2023-12-08 10:21:20 +01:00
ca39de9040 add: part1 2023-12-08 10:21:09 +01:00
0d50624697 add: part2 2023-12-07 18:08:26 +01:00
a8ff3fc882 recreation of part1 2023-12-07 17:07:46 +01:00
dfa1a49c7b fix 2023-12-07 13:21:49 +01:00
5adb259376 opti 2023-12-07 12:57:22 +01:00
37 changed files with 3402 additions and 62 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
**/input.txt
**/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 ]

View File

@ -1,4 +1,4 @@
text: str = open("example.txt", "r").read()
text: str = open("input.txt", "r").read()
_value: int = 0
@ -6,73 +6,65 @@ lines = text.splitlines()
CARDS = "AKQJT98765432"
def get_same(string: str):
sames = {"key": string}
for card in CARDS:
if string.count(card) != 0:
bozo: list = sames.get(string.count(card), "")
bozo += (card)
sames.update({string.count(card): bozo})
return sames
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
sames = []
for line in lines:
bozo: [str] = line.split()
same = get_same(bozo[0])
same.update({"value": int(bozo[1])})
sames.append(same)
same.update({"type": int(_type)})
return same
def parse(lines):
hands = []
for line in lines:
hands.append(parse_line(line))
return hands
def get_highest(cards1, cards2, i):
if (cards1 == cards2):
return 0
if (i != 1):
l1 = len(cards1)
l2 = len(cards2)
if (l1 > l2):
return 1
if (l2 > l1):
return 2
for char1, char2 in zip(cards1, cards2):
if (char2 == char1):
def hand_cmp(hand1: dict, hand2: dict):
for card1, card2 in zip(hand1.get("card"), hand2.get("card")):
if card1 == card2:
continue
return 1 + (CARDS.index(char1) > CARDS.index(char2))
return 0
return (CARDS.index(card1) - CARDS.index(card2))
def get_index(lst: [dict], key: str):
for i, hand in enumerate(lst):
if (hand.get("key") == key):
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)
sort = [sames[0]]
for same in sames[1:]:
places = []
opponents: [dict] = sort.copy()
for card_index in range(5, 0, -1):
new = []
for opponent in opponents:
my_cards = same.get(card_index, "")
opponent_cards = opponent.get(card_index, "")
highest = get_highest(my_cards, opponent_cards, card_index)
if (highest == 0):
new.append(opponent)
elif (highest == 1):
places.append(get_index(sort, opponent.get("key")))
opponents = new
if (places != []):
break
if (places == []):
places = [len(sort)]
place = min(places)
sort.insert(place, same)
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
for i, line in enumerate(lines):
key, value = line.split()
for j, bozo in enumerate(sort):
if (bozo.get("key", "") == key):
rank: int = (len(lines) - j)
_value += rank * bozo.get("value")
print(f'{rank}, {bozo.get("key")} {bozo.get("value")}')
break
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
View 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
View 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
View File

@ -0,0 +1,5 @@
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)

10
2023/day08/example3.txt Normal file
View 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
View 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
View 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
View 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
View 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
View 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
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)