Compare commits
3 Commits
909b0d3b40
...
cf78ef9f04
Author | SHA1 | Date | |
---|---|---|---|
cf78ef9f04 | |||
b9b6f72244 | |||
aa71587aea |
44
2024/day01.py
Executable file
44
2024/day01.py
Executable file
@ -0,0 +1,44 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
Jour 01 du défi Advent Of Code pour l'année 2024
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
|
||||||
|
def read_sample():
|
||||||
|
"""récupère les entrées depuis le fichier texte correspondant"""
|
||||||
|
filename = os.path.join(os.path.dirname(__file__ ), "inputs", "day01.txt")
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
sample = f.read().split('\n')
|
||||||
|
sample = [ list(map(int, i.split())) for i in sample if i != '' ]
|
||||||
|
return sample
|
||||||
|
|
||||||
|
def part1(sample):
|
||||||
|
"""Partie 1 du défi"""
|
||||||
|
l1, l2 = sorted([l[0] for l in sample]), sorted([l[1] for l in sample])
|
||||||
|
|
||||||
|
return sum(abs(l1[i]-l2[i]) for i in range(len(l1)))
|
||||||
|
|
||||||
|
def part2(sample):
|
||||||
|
"""Partie 2 du défi"""
|
||||||
|
l1, l2 = sorted([l[0] for l in sample]), sorted([l[1] for l in sample])
|
||||||
|
pres = {}
|
||||||
|
for i in l2:
|
||||||
|
if i not in pres:
|
||||||
|
pres[i] = 0
|
||||||
|
pres[i] += 1
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
for i in l1:
|
||||||
|
if i in pres:
|
||||||
|
score += pres[i]*i
|
||||||
|
return score
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Fonction principale"""
|
||||||
|
sample = read_sample()
|
||||||
|
print(f"part1: {part1(sample)}")
|
||||||
|
print(f"part2: {part2(sample)}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
60
FICHE.md
Normal file
60
FICHE.md
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
# Fiche de solutions/ astuces fréquentes pour AoC
|
||||||
|
|
||||||
|
Si on cherche à savoir quand qqchose va arriver et que ça arrive à la conjonction de plusieurs évènements
|
||||||
|
```python
|
||||||
|
math.lcm(*[_, _, ...])
|
||||||
|
```
|
||||||
|
|
||||||
|
quand on calcule les mêmes choses plein de fois (mais VRAIMENT les mêmes choses, pas un chouïa différent)
|
||||||
|
```python
|
||||||
|
from functools import cache
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def ma_fonction(*args)
|
||||||
|
```
|
||||||
|
|
||||||
|
quand on doit calculer un truc très grand et que c'est un nb d'états finis (qui se répètent potentiellement)
|
||||||
|
détection de cycle
|
||||||
|
|
||||||
|
quand on doit calculer un truc très grand et que c'est un nb d'états infinis:
|
||||||
|
```python
|
||||||
|
snippets.lagrange_interpolation([(x1, y1), (x2, y2), ...], x0)
|
||||||
|
```
|
||||||
|
|
||||||
|
quand on doit calculer l'aire d'un polygone
|
||||||
|
```python
|
||||||
|
snippets.area([(x1, y1), (x2, y2), ...], count_border=True)
|
||||||
|
```
|
||||||
|
|
||||||
|
trop d'éléments en entrée: les considérer par ensembles si possible (genre séquences d'entiers)
|
||||||
|
```python
|
||||||
|
a = intervals.Interval(0, 5)
|
||||||
|
b = intervals.Interval(10, 15)
|
||||||
|
|
||||||
|
a.intersection(b) # None
|
||||||
|
a.intersect(b) # False
|
||||||
|
|
||||||
|
a = intervals.Interval(0, 5)
|
||||||
|
b = intervals.Interval(4, 15)
|
||||||
|
|
||||||
|
a.intersection(b) # [4, 5]
|
||||||
|
a.intersect(b) # True
|
||||||
|
```
|
||||||
|
|
||||||
|
quand on a un parcours de graphe, utiliser Graph, et le compresser si nécessaire (attention, que non-orienté !)
|
||||||
|
|
||||||
|
Trouver une instance qui satisfait plein de trucs et ça paraît impossible ? z3 ! `pip install z3-solver`
|
||||||
|
```python
|
||||||
|
px, py, pz, dx, dy, dz = z3.Ints("px py pz dx dy dz")
|
||||||
|
collision = [z3.Int("t"+str(i)) for i in range(len(hailstones))]
|
||||||
|
solver = z3.Solver()
|
||||||
|
for i in range(len(hailstones)):
|
||||||
|
h = hailstones[i]
|
||||||
|
solver.add(px + dx*collision[i] == h.px + h.dx*collision[i])
|
||||||
|
solver.add(py + dy*collision[i] == h.py + h.dy*collision[i])
|
||||||
|
solver.add(pz + dz*collision[i] == h.pz + h.dz*collision[i])
|
||||||
|
|
||||||
|
solver.check()
|
||||||
|
|
||||||
|
return solver.model().evaluate(px + py + pz)
|
||||||
|
```
|
@ -7,7 +7,7 @@
|
|||||||
2020: ***~.....................
|
2020: ***~.....................
|
||||||
2021: *******~~~...............
|
2021: *******~~~...............
|
||||||
2022: ***************~.*..*....
|
2022: ***************~.*..*....
|
||||||
2023: **********************~..
|
2023: *************************
|
||||||
|
|
||||||
.: nothing
|
.: nothing
|
||||||
~: 1 star
|
~: 1 star
|
||||||
|
@ -3,4 +3,4 @@ z3-solver
|
|||||||
matplotlib
|
matplotlib
|
||||||
networkx
|
networkx
|
||||||
requests
|
requests
|
||||||
beautifulsoup
|
beautifulsoup4
|
@ -17,7 +17,7 @@ from bs4 import BeautifulSoup
|
|||||||
|
|
||||||
home = str(Path.home())
|
home = str(Path.home())
|
||||||
with open(os.path.join(home, ".aoc-cookie"), "r", encoding='utf8') as file:
|
with open(os.path.join(home, ".aoc-cookie"), "r", encoding='utf8') as file:
|
||||||
cookie = file.read()
|
cookie = file.read().replace("\n", " ").strip()
|
||||||
|
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
session.cookies["session"] = cookie
|
session.cookies["session"] = cookie
|
||||||
|
Loading…
Reference in New Issue
Block a user