jour 9 qui marche pas

This commit is contained in:
Marguerite Miallier
2024-12-09 21:14:32 +01:00
parent 3fdb680747
commit 52064badaa
2 changed files with 104 additions and 40 deletions

90
day6.py
View File

@ -1,3 +1,5 @@
import time
global l
l = ["....#.....", l = ["....#.....",
".........#", ".........#",
"..........", "..........",
@ -8,91 +10,119 @@ l = ["....#.....",
"........#.", "........#.",
"#.........", "#.........",
"......#..."] "......#..."]
t0 = time.time()
# with open('day6.txt') as f:
# l = f.read().splitlines()
global up, down, left, right global up, down, left, right
up,down,left,right = "^","v","<",">" up,down,left,right = "^","v","<",">"
dir = [up,down,left,right] dir = [up,down,left,right]
act_dir = None global act_dir
act_dir = (0,0)
global obstacles global obstacles
obstacles = [] obstacles = []
pose = None pose = None
# global path global path
path = [] path = []
global h,w global h,w
h,w = len(l),len(l[0]) h,w = len(l),len(l[0])
global init_pose
global init_dir
global loops
loops = 0
def move_up(pose, path):
print(path) def move_up(pose):
# print(path)
out_of_map = False out_of_map = False
while pose[0] - 1 not in obstacles: #or pose[0] != 0: while (pose[0] - 1,pose[1]) not in obstacles: #or pose[0] != 0:
print(pose, path,pose in path) # print(pose, path)
pose = (pose[0] - 1,pose[1])
if pose not in path: if pose not in path:
path.append(pose) path.append(pose)
pose[0] = pose[0] - 1
print(pose)
if pose[0] == 0: if pose[0] == 0:
out_of_map = True out_of_map = True
break break
print(path) if act_dir == init_dir and pose == init_pose:
loop = True
# print(pose)
# print(path)
act_dir = right act_dir = right
return pose, act_dir, out_of_map, path return pose, out_of_map, loop
def move_down(pose): def move_down(pose):
out_of_map = False out_of_map = False
while pose[0] + 1 not in obstacles or pose[0] == h-1: loop = False
pose[0] = pose[0] + 1 while (pose[0] + 1,pose[1]) not in obstacles: #or pose[0] == h-1:
print(pose in path) pose = (pose[0] + 1,pose[1])
# print(pose in path)
if pose not in path: if pose not in path:
path.append(pose) path.append(pose)
if pose[0] == h-1: if pose[0] == h-1:
out_of_map = True out_of_map = True
break
if act_dir == init_dir and pose == init_pose:
loop = True
act_dir = left act_dir = left
return pose, act_dir, out_of_map return pose, out_of_map, loop
def move_left(pose): def move_left(pose):
out_of_map = False out_of_map = False
while pose[1] - 1 not in obstacles or pose[1] == 0: while (pose[0],pose[1] - 1) not in obstacles or pose[1] == 0:
pose[1] = pose[1] - 1 pose = (pose[0],pose[1] - 1)
if pose not in path: if pose not in path:
path.append(pose) path.append(pose)
if pose[1] == 0: if pose[1] == 0:
out_of_map = True out_of_map = True
break
if act_dir == init_dir and pose == init_pose:
loop = True
act_dir = up act_dir = up
return pose, act_dir, out_of_map return pose, out_of_map, loop
def move_right(pose): def move_right(pose):
out_of_map = False out_of_map = False
while pose[1] + 1 not in obstacles or pose[1] == w - 1: while (pose[0],pose[1] + 1) not in obstacles: #or pose[1] == w - 1:
pose[1] = pose[1] + 1 pose = (pose[0],pose[1] + 1)
if pose not in path: if pose not in path:
path.append(pose) path.append(pose)
if pose[1] == w - 1: if pose[1] == w - 1:
out_of_map = True out_of_map = True
break
if act_dir == init_dir and pose == init_pose:
loop = True
act_dir = down act_dir = down
return pose, act_dir, out_of_map return pose, out_of_map, loop
for i in range(len(l)): for i in range(len(l)):
for j in range(len(l[0])): for j in range(len(l[0])):
if l[i][j] in dir: if l[i][j] in dir:
act_dir = l[i][j] act_dir = l[i][j]
pose = [i,j] init_pose, init_dir = (i,j), l[i][j]
pose = (i,j)
path.append(pose) path.append(pose)
elif l[i][j] == "#": elif l[i][j] == "#":
obstacles.append([i,j]) obstacles.append((i,j))
print(obstacles) # print(obstacles)
print(path) # print(path)
out_of_map = False out_of_map = False
while not out_of_map: for i in range(h):
for j in range(w):
if (i,j) not in obstacles:
obstacles.append((i,j))
while not out_of_map:
if act_dir == up: if act_dir == up:
pose, act_dir, out_of_map, path = move_up(pose,path) pose, out_of_map, loop = move_up(pose)
elif act_dir == down: elif act_dir == down:
pose, act_dir, out_of_map = move_down(pose) pose, out_of_map, loop = move_down(pose)
elif act_dir == left: elif act_dir == left:
pose, act_dir, out_of_map = move_left(pose) pose, out_of_map, loop = move_left(pose)
elif act_dir == right: elif act_dir == right:
pose, act_dir, out_of_map = move_right(pose) pose, out_of_map, loop = move_right(pose)
if loop :
loops +=1
print(len(path)) print(len(path))
print(time.time()- t0)

34
day9.py Normal file
View File

@ -0,0 +1,34 @@
map = "2333133121414131402"
# with open('day9.txt') as f:
# map = f.readline()[:-2]
print(len(map))
l = []
for i in range(len(map)):
if i%2 == 0:
for j in range(int(map[i])):
l.append(i//2)
else :
for j in range(int(map[i])):
l.append(None)
# print(l)
for el in l:
# print(el)
if el == None:
last = l.pop()
# print(last)
while last == None:
last = l.pop()
l[l.index(el)] = last
checksum = 0
for i in range(len(l)):
checksum += i*l[i]
# print(l)
print(checksum)