Compare commits
No commits in common. "d9a5376e0fc2692939def393e30dc541271ead49" and "dc7f9bb764914f9a7cec043735d1585f93838688" have entirely different histories.
d9a5376e0f
...
dc7f9bb764
@ -1,88 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
"""
|
||||
Jour 14 du défi Advent Of Code pour l'année 2023
|
||||
"""
|
||||
import os
|
||||
from functools import cache
|
||||
|
||||
def read_sample():
|
||||
"""récupère les entrées depuis le fichier texte correspondant"""
|
||||
filename = os.path.join(os.path.dirname(__file__ ), "inputs", "day14.txt")
|
||||
with open(filename, 'r') as f:
|
||||
sample = f.read().split('\n')
|
||||
sample = [ [j for j in i] for i in sample if i != '' ]
|
||||
return sample
|
||||
|
||||
@cache
|
||||
def in_bounds(i_, j_, i, j):
|
||||
return i >=0 and j >= 0 and i < i_ and j < j_
|
||||
|
||||
|
||||
def tilt(sample, direction=(1, 0)):
|
||||
i_, j_ = len(sample), len(sample[0])
|
||||
change = True
|
||||
while change:
|
||||
change = False
|
||||
for i in range(len(sample)):
|
||||
for j in range(len(sample[0])):
|
||||
if sample[i][j] == 'O' and in_bounds(i_, j_, i+direction[0], j+direction[1]) and sample[i+direction[0]][j+direction[1]] == '.':
|
||||
sample[i][j] = '.'
|
||||
sample[i+direction[0]][j+direction[1]] = 'O'
|
||||
change = True
|
||||
#print("\n".join(["".join(i) for i in sample]))
|
||||
#print()
|
||||
|
||||
|
||||
def load(sample):
|
||||
load = 0
|
||||
for i in range(len(sample)):
|
||||
for j in range(len(sample)):
|
||||
if sample[i][j] == 'O':
|
||||
load += len(sample)-i
|
||||
return load
|
||||
|
||||
def s_to_str(sample):
|
||||
return "\n".join(["".join(i) for i in sample])
|
||||
|
||||
def cycle(sample, cycles):
|
||||
views = {}
|
||||
for i in range(cycles):
|
||||
s = s_to_str(sample)
|
||||
if s in views.keys():
|
||||
cycle_length = i-views[s]
|
||||
break
|
||||
else:
|
||||
views[s] = i
|
||||
|
||||
tilt(sample, direction=(-1, 0))
|
||||
tilt(sample, direction=(0, -1))
|
||||
tilt(sample, direction=(1, 0))
|
||||
tilt(sample, direction=(0, 1))
|
||||
|
||||
for i in range((cycles-i)%cycle_length):
|
||||
tilt(sample, direction=(-1, 0))
|
||||
tilt(sample, direction=(0, -1))
|
||||
tilt(sample, direction=(1, 0))
|
||||
tilt(sample, direction=(0, 1))
|
||||
|
||||
return load(sample)
|
||||
|
||||
def part1():
|
||||
"""Partie 1 du défi"""
|
||||
sample = read_sample()
|
||||
tilt(sample, direction=(-1, 0))
|
||||
return load(sample)
|
||||
|
||||
def part2():
|
||||
"""Partie 2 du défi"""
|
||||
sample = read_sample()
|
||||
return cycle(sample, 1000000000)
|
||||
|
||||
|
||||
def main():
|
||||
"""Fonction principale"""
|
||||
print(f"part1: {part1()}")
|
||||
print(f"part2: {part2()}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -1,68 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
"""
|
||||
Jour 15 du défi Advent Of Code pour l'année 2023
|
||||
"""
|
||||
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", "day15.txt")
|
||||
with open(filename, 'r') as f:
|
||||
sample = f.read().split('\n')
|
||||
sample = [ [j for j in i.split(",")] for i in sample if i != '' ][0]
|
||||
return sample
|
||||
|
||||
def hash(s):
|
||||
cur_val = 0
|
||||
for i in s:
|
||||
cur_val += ord(i)
|
||||
cur_val *= 17
|
||||
cur_val = cur_val % 256
|
||||
return cur_val
|
||||
|
||||
def print_boxes(boxes):
|
||||
print("===")
|
||||
for i in boxes.keys():
|
||||
if boxes[i] != []:
|
||||
print(f"Box {i}: ", end="")
|
||||
for a, b in boxes[i]:
|
||||
print(f"[{a}={b}] ", end="")
|
||||
print()
|
||||
|
||||
def part1(sample):
|
||||
"""Partie 1 du défi"""
|
||||
return sum(hash(i) for i in sample)
|
||||
|
||||
def part2(sample):
|
||||
"""Partie 2 du défi"""
|
||||
boxes = {i: [] for i in range(256)}
|
||||
for i in sample:
|
||||
if '=' in i:
|
||||
label = i.split("=")[0]
|
||||
box = hash(label)
|
||||
lens = int(i.split("=")[1])
|
||||
found = False
|
||||
for i in range(len(boxes[box])):
|
||||
if boxes[box][i][0] == label:
|
||||
boxes[box][i] = (label, lens)
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
boxes[box].append((label, lens))
|
||||
else:
|
||||
label = i.split("-")[0]
|
||||
box = hash(label)
|
||||
boxes[box] = [(a, b) for (a, b) in boxes[box] if a != label]
|
||||
|
||||
print_boxes(boxes)
|
||||
return sum([sum([(1+i)*(1+j)*boxes[i][j][1] for j in range(len(boxes[i]))]) for i in boxes.keys()])
|
||||
|
||||
|
||||
def main():
|
||||
"""Fonction principale"""
|
||||
sample = read_sample()
|
||||
print(f"part1: {part1(sample)}")
|
||||
print(f"part2: {part2(sample)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user