trying p2
This commit is contained in:
parent
311ab759a7
commit
2fe6b8bb7d
@ -29,20 +29,19 @@ class Guard:
|
|||||||
self.pos = p
|
self.pos = p
|
||||||
self.dir = d
|
self.dir = d
|
||||||
|
|
||||||
def move(self, ob) -> bool:
|
def move(self, ob) -> Vector | None | int:
|
||||||
new_pos = self.pos + self.dir
|
new_pos = self.pos + self.dir
|
||||||
|
|
||||||
# Check if need to turn
|
# Check if need to turn
|
||||||
if new_pos in ob:
|
if new_pos in ob:
|
||||||
self.turn()
|
return new_pos
|
||||||
return self.move(ob)
|
|
||||||
|
|
||||||
# Check if move out of bounds
|
# Check if move out of bounds
|
||||||
if new_pos.y < 0 or new_pos.y >= y_bound or new_pos.x < 0 or new_pos.x >= x_bound:
|
if new_pos.y < 0 or new_pos.y >= y_bound or new_pos.x < 0 or new_pos.x >= x_bound:
|
||||||
return False
|
return -1
|
||||||
|
|
||||||
self.pos = new_pos
|
self.pos = new_pos
|
||||||
return True
|
return None
|
||||||
|
|
||||||
|
|
||||||
def turn(self):
|
def turn(self):
|
||||||
@ -67,7 +66,14 @@ starting_pos = guard.pos
|
|||||||
# Part 1
|
# Part 1
|
||||||
been = [starting_pos]
|
been = [starting_pos]
|
||||||
|
|
||||||
while guard.move(obstacles):
|
while True:
|
||||||
|
ob = guard.move(obstacles)
|
||||||
|
|
||||||
|
if type(ob) == Vector:
|
||||||
|
guard.turn()
|
||||||
|
elif type(ob) == int:
|
||||||
|
break
|
||||||
|
|
||||||
if guard.pos not in been:
|
if guard.pos not in been:
|
||||||
been.append(guard.pos)
|
been.append(guard.pos)
|
||||||
|
|
||||||
@ -77,19 +83,44 @@ print(f'Part 1: {len(been)}')
|
|||||||
total = 0
|
total = 0
|
||||||
|
|
||||||
been.remove(starting_pos)
|
been.remove(starting_pos)
|
||||||
for place in been:
|
for i, place in enumerate(been):
|
||||||
|
print(i)
|
||||||
temp_obstacles = obstacles.copy()
|
temp_obstacles = obstacles.copy()
|
||||||
temp_obstacles.append(place)
|
temp_obstacles.append(place)
|
||||||
|
|
||||||
guard.pos = starting_pos
|
guard.pos = starting_pos
|
||||||
guard.dir = Vector(0, -1)
|
guard.dir = Vector(0, -1)
|
||||||
|
|
||||||
|
# hit_new_ob_pos = None
|
||||||
|
# hit_new_ob_dir = None
|
||||||
|
|
||||||
steps = 0
|
steps = 0
|
||||||
while guard.move(temp_obstacles):
|
while True:
|
||||||
steps += 1
|
ob = guard.move(temp_obstacles)
|
||||||
|
|
||||||
|
if type(ob) == Vector:
|
||||||
|
# if ob == place and not hit_new_ob_pos:
|
||||||
|
# hit_new_ob_pos = guard.pos
|
||||||
|
# hit_new_ob_dir = guard.dir
|
||||||
|
|
||||||
|
# elif hit_new_ob_dir and hit_new_ob_pos:
|
||||||
|
# if guard.pos == hit_new_ob_pos and guard.dir == hit_new_ob_dir:
|
||||||
|
# total += 1
|
||||||
|
# break
|
||||||
|
|
||||||
|
guard.turn()
|
||||||
|
|
||||||
|
|
||||||
|
# guard.turn()
|
||||||
|
|
||||||
|
elif type(ob) == int:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
steps += 1
|
||||||
|
|
||||||
|
|
||||||
# Yup, this is what I count as in a loop...
|
# Yup, this is what I count as in a loop...
|
||||||
if steps >= 10 ** 5:
|
if steps >= 6500:
|
||||||
total += 1
|
total += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user