Nouveau Repository

Je perd l'historique, mais ne stocke plus les fichiers input.txt
entre autre choses
This commit is contained in:
2023-12-02 12:45:07 +01:00
parent 3e06d85867
commit fbf1c66a39
6 changed files with 273 additions and 0 deletions

16
day1/part1.py Normal file
View File

@@ -0,0 +1,16 @@
import re
accumulator = 0
with open("input.txt") as input:
while line := input.readline():
if match := re.match(".*?([0-9]).*([0-9]).*?", line):
# print(match.groups(1))
number = int(match.groups(1)[0] + match.groups(1)[1])
# print(number)
accumulator += number
elif match := re.match(".*([0-9]).*", line):
# print(match.groups(1))
number = int(match.groups(1)[0] + match.groups(1)[0])
# print(number)
accumulator += number
print(accumulator)

44
day1/part2.py Normal file
View File

@@ -0,0 +1,44 @@
import re
digits = {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
accumulator = 0
with open("input.txt") as input:
while line := input.readline():
if match := re.match(
".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?",
line,
):
#print(match.groups(1))
number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[1]])
#print(number)
accumulator += number
elif match := re.match(".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?", line):
#print(match.groups(1))
number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[0]])
#print(number)
accumulator += number
else:
print(line)
print(accumulator)