Added unfinished 2022 challenges

This commit is contained in:
augustin64 2023-12-10 15:33:05 +01:00
parent aaf069f1a9
commit d819cd6aa7
3 changed files with 261 additions and 0 deletions

147
2022/day17.py Executable file
View File

@ -0,0 +1,147 @@
#!/usr/bin/python3
"""
Jour 17 du défi Advent Of Code pour l'année 2022
<!-- NE FONCTIONNE PAS --!>
"""
PRINT = True
ROCKS = [
[
["@", "@", "@", "@"]
],
[
[".", "@", "."],
["@", "@", "@"],
[".", "@", "."]
],
[
[".", ".", "@"],
[".", ".", "@"],
["@", "@", "@"]
],
[
["@"],
["@"],
["@"],
["@"]
],
[
["@", "@"],
["@", "@"],
]
]
def read_sample():
"""récupère les entrées depuis le fichier texte correspondant"""
with open('inputs/day17.txt', 'r') as f:
return f.read()
def print_puzzle(heights, rock, left_down_corner):
if not PRINT:
return
for i in range(len(rock)):
print("|"+"."*left_down_corner[1]+"".join(rock[i])+"."*(7-len(rock[i])-left_down_corner[1])+"|")
for i in range(left_down_corner[0]-max(heights)):
print("|"+"."*7+"|")
for i in range(max(heights)):
print("|", end="")
for j in range(len(heights)):
if heights[j] >= (max(heights)-i):
print("#", end="")
else:
print(".", end="")
print("|")
print("+"+"-"*7+"+")
print()
def gas_jet(direction, pos, rock, largeur):
if direction == '>':
if pos[1]+max([len(j) for j in rock]) < largeur: #Move
pos = (pos[0], pos[1]+1)
print("Move right:")
else:
print("Failed to move right:")
else:
if pos[1] > 0: #Move
pos = (pos[0], pos[1]-1)
print("Move left:")
else:
print("Failed to move left:")
return pos
def part1(sample):
"""Partie 1 du défi"""
largeur = 7
heights = [0 for i in range(largeur)]
stream_pos = 0
for i in range(2022):
print(f"rock {i}")
rock = ROCKS[(i)%len(ROCKS)]
left_down_corner = (max(heights)+3, 2)
falling = True
while falling:
left_down_corner = gas_jet(sample[stream_pos%len(sample)], left_down_corner, rock, largeur)
stream_pos += 1
print_puzzle(heights, rock, left_down_corner)
print("falls 1 unit:")
left_down_corner = (left_down_corner[0]-1, left_down_corner[1])
print_puzzle(heights, rock, left_down_corner)
# Check si en bas
if left_down_corner[0] <= 0:
falling = False
elif left_down_corner[0] <= max(heights)+1:
print("Potentially blocked")
for j in range(len(rock[0])):
print(f"trying on rock[*][{j}]")
if (len(rock)-1) - max([k for k in range(len(rock)) if rock[k][j] != '.']) + left_down_corner[0] == heights[j+left_down_corner[1]]:
falling = False
print("blocked")
break
if falling:
print_puzzle(heights, rock, left_down_corner)
left_down_corner = gas_jet(sample[stream_pos%len(sample)], left_down_corner, rock, largeur)
stream_pos += 1
print_puzzle(heights, rock, left_down_corner)
for j in range(len(rock[0])):
(len(rock)-1) - max([k for k in range(len(rock)) if rock[k][j] != '.']) + left_down_corner[0]
print(j+left_down_corner[1], left_down_corner[0], 1+min([k for k in range(len(rock)) if rock[k][j] != '.']))
if heights[j+left_down_corner[1]] > left_down_corner[0] + len(rock)-(min([k for k in range(len(rock)) if rock[k][j] != '.'])):
print(heights[j+left_down_corner[1]], left_down_corner[0] + len(rock)-(min([k for k in range(len(rock)) if rock[k][j] != '.'])), "Error!")
print_puzzle(heights, rock, left_down_corner)
print(heights)
print(left_down_corner)
print(len(rock))
print(min([k for k in range(len(rock)) if rock[k][j] != '.']))
raise Exception
heights[j+left_down_corner[1]] = left_down_corner[0] + len(rock)-(min([k for k in range(len(rock)) if rock[k][j] != '.']))
print("## ignore rock:")
print_puzzle(heights, rock, left_down_corner)
return max(heights)
def part2(sample):
"""Partie 2 du défi"""
return NotImplementedError
def main():
"""Fonction principale"""
sample = read_sample()
print(f"part1: {part1(sample)}")
print(f"part2: {part2(sample)}")
if __name__ == "__main__":
main()

51
2022/day20.py Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/python3
"""
Jour 20 du défi Advent Of Code pour l'année 2022
<!-- NE FONCTIONNE PAS --!>
"""
def read_sample():
"""récupère les entrées depuis le fichier texte correspondant"""
with open('inputs/day20.txt', 'r') as f:
sample = f.read().split('\n')
sample = [ int(i) for i in sample if i != '' ]
return sample
def grove_coordinates(decrypted_file):
n = len(decrypted_file)
index_z = decrypted_file.index(0)
return (
decrypted_file[(1000+index_z)%n],
decrypted_file[(2000+index_z)%n],
decrypted_file[(3000+index_z)%n]
)
def move(sample, item):
index = sample.index(item)
new_index = (index+item)%(len(sample)-1)
sample.remove(item)
sample.insert(new_index, item)
def part1(sample):
"""Partie 1 du défi"""
encrypted_file = sample.copy()
for i in sample:
move(encrypted_file, i)
coords = grove_coordinates(encrypted_file)
print(coords)
return coords[0]+coords[1]+coords[2]
def part2(sample):
"""Partie 2 du défi"""
return NotImplementedError
def main():
"""Fonction principale"""
sample = read_sample()
print(f"part1: {part1(sample)}")
print(f"part2: {part2(sample)}")
if __name__ == "__main__":
main()

63
2022/day22.py Executable file
View File

@ -0,0 +1,63 @@
#!/usr/bin/python3
"""
Jour 22 du défi Advent Of Code pour l'année 2022
<!-- NE FONCTIONNE PAS --!>
"""
def read_sample():
"""récupère les entrées depuis le fichier texte correspondant"""
with open('inputs/day22.txt', 'r') as f:
sample = f.read().split('\n\n')
map = [ i for i in sample[0] if i != '' ]
moves = []
cur = 0
for i in sample[1]:
if i in ['R', 'L']:
moves.append(({'R': 1, 'L': -1}[i], cur))
else:
cur = cur*10+int(i)
return map, moves
def has_wall(map, pos):
if pos[2] == 0:
if map[pos[0]][pos[1]+1] == '#':
return True
def move(map, pos, distance):
for i in range(distance):
if has_wall(map, pos):
break
return pos
def part1(sample):
"""Partie 1 du défi"""
map, moves = sample
pos = (0, min([i for i in range(len(sample[0])) if sample[0][i] == '.']), 0)
for i in moves:
pos = move(map, pos, i[1])
pos = (pos[0], pos[1], (pos[2]+i[0])%4)
return NotImplementedError
def part2(sample):
"""Partie 2 du défi"""
return NotImplementedError
def main():
"""Fonction principale"""
sample = read_sample()
print(f"part1: {part1(sample)}")
print(f"part2: {part2(sample)}")
if __name__ == "__main__":
main()