Compare commits
No commits in common. "33da7d80939ba1f44c3851bbb08c28d216f49f6b" and "b28322a448adedcdde2b2487633c7d0d74f51a03" have entirely different histories.
33da7d8093
...
b28322a448
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1 @@
|
|||||||
*/inputs
|
*/inputs
|
||||||
**/__pycache__
|
|
@ -1,63 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
"""
|
|
||||||
Jour 01 du défi Advent Of Code pour l'année 2023
|
|
||||||
"""
|
|
||||||
|
|
||||||
def read_sample():
|
|
||||||
"""récupère les entrées depuis le fichier texte correspondant"""
|
|
||||||
with open('inputs/day01.txt', 'r') as f:
|
|
||||||
sample = f.read().split('\n')
|
|
||||||
sample = [ i for i in sample if i != '' ]
|
|
||||||
return sample
|
|
||||||
|
|
||||||
def number(word):
|
|
||||||
nbs = (0, 0)
|
|
||||||
seen = 0
|
|
||||||
for i in word:
|
|
||||||
if i >= '0' and i <= '9':
|
|
||||||
if seen == 0:
|
|
||||||
nbs = (int(i), int(i))
|
|
||||||
seen += 1
|
|
||||||
else:
|
|
||||||
nbs = (nbs[0], int(i))
|
|
||||||
return 10*nbs[0]+nbs[1]
|
|
||||||
|
|
||||||
def part1(sample):
|
|
||||||
"""Partie 1 du défi"""
|
|
||||||
return sum([
|
|
||||||
number(word) for word in sample
|
|
||||||
])
|
|
||||||
|
|
||||||
def treat_input(sample):
|
|
||||||
pairs = [
|
|
||||||
("one", "o1e"),
|
|
||||||
("two", "t2o"),
|
|
||||||
("three", "th3ee"),
|
|
||||||
("four", "fo4r"),
|
|
||||||
("five", "f5ve"),
|
|
||||||
("six", "s6x"),
|
|
||||||
("seven", "se7en"),
|
|
||||||
("eight", "eig8ht"),
|
|
||||||
("nine", "ni9ne"),
|
|
||||||
("zero", "ze0ro")
|
|
||||||
]
|
|
||||||
for base, rep in pairs:
|
|
||||||
sample = [i.replace(base, rep) for i in sample]
|
|
||||||
|
|
||||||
return sample
|
|
||||||
|
|
||||||
def part2(sample):
|
|
||||||
"""Partie 2 du défi"""
|
|
||||||
return sum([
|
|
||||||
number(word) for word in treat_input(sample)
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
"""Fonction principale"""
|
|
||||||
sample = read_sample()
|
|
||||||
print(f"part1: {part1(sample)}")
|
|
||||||
print(f"part2: {part2(sample)}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
20
utils/cli.py
20
utils/cli.py
@ -7,7 +7,6 @@ Suggéré: alias aoc="$CHEMIN_VERS_CE_REPO/utils/cli.py"
|
|||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import datetime
|
import datetime
|
||||||
import colors
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@ -31,23 +30,10 @@ def download_input(year, day):
|
|||||||
print("ok")
|
print("ok")
|
||||||
|
|
||||||
|
|
||||||
def show_puzzle(year, day, part=None, colored=False):
|
def show_puzzle(year, day, part=None):
|
||||||
"""Affiche l'énoncé du jour demandé"""
|
"""Affiche l'énoncé du jour demandé"""
|
||||||
request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}")
|
request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}")
|
||||||
|
soup = BeautifulSoup(request.content, "html.parser")
|
||||||
content = request.content.decode("utf-8")
|
|
||||||
if colored:
|
|
||||||
content = content.replace("<h2", f"{colors.bold}<h2")
|
|
||||||
content = content.replace('<em class="star">', f'<em class="star">{colors.bold}{colors.yellow}')
|
|
||||||
content = content.replace('<em>', f'<em>{colors.bold}')
|
|
||||||
content = content.replace('<a href', f'{colors.green}<a href')
|
|
||||||
content = content.replace("</h2>", f"</h2>{colors.reset}\n")
|
|
||||||
content = content.replace("</em>", f"</em>{colors.reset}")
|
|
||||||
content = content.replace("</a>", f"</a>{colors.reset}")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
soup = BeautifulSoup(content, "html.parser")
|
|
||||||
day_desc = soup.find_all("article", {"class": "day-desc"})
|
day_desc = soup.find_all("article", {"class": "day-desc"})
|
||||||
if part is None:
|
if part is None:
|
||||||
for i in range(len(day_desc)):
|
for i in range(len(day_desc)):
|
||||||
@ -81,7 +67,7 @@ def __main__(options, args):
|
|||||||
options.level = None
|
options.level = None
|
||||||
else:
|
else:
|
||||||
options.level = int(options.level)
|
options.level = int(options.level)
|
||||||
show_puzzle(options.year, options.day, options.level, colored=True)
|
show_puzzle(options.year, options.day, options.level)
|
||||||
elif args[0] == "submit" or args[0] == "sb":
|
elif args[0] == "submit" or args[0] == "sb":
|
||||||
if len(args) < 2:
|
if len(args) < 2:
|
||||||
answer = input("Answer ?\n>> ")
|
answer = input("Answer ?\n>> ")
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
reset = "\033[0m"
|
|
||||||
bold = "\033[1m"
|
|
||||||
|
|
||||||
magenta = "\033[35m"
|
|
||||||
yellow = "\033[33m"
|
|
||||||
white = "\033[37m"
|
|
||||||
green = "\033[32m"
|
|
||||||
black = "\033[30m"
|
|
||||||
cyan = "\033[36m"
|
|
||||||
blue = "\033[34m"
|
|
||||||
red = "\033[31m"
|
|
Loading…
x
Reference in New Issue
Block a user